92 lines
3.2 KiB
TypeScript
92 lines
3.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { OnboardingPageQuery } from './OnboardingPageQuery';
|
|
import { OnboardingService } from '@/lib/services/onboarding/OnboardingService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { OnboardingPageViewDataBuilder } from '@/lib/builders/view-data/OnboardingPageViewDataBuilder';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/onboarding/OnboardingService', () => {
|
|
return {
|
|
OnboardingService: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock('@/lib/builders/view-data/OnboardingPageViewDataBuilder', () => ({
|
|
OnboardingPageViewDataBuilder: {
|
|
build: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
describe('OnboardingPageQuery', () => {
|
|
let query: OnboardingPageQuery;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
query = new OnboardingPageQuery();
|
|
mockServiceInstance = {
|
|
checkCurrentDriver: vi.fn(),
|
|
};
|
|
// Use mockImplementation to return the instance
|
|
(OnboardingService as any).mockImplementation(function() {
|
|
return mockServiceInstance;
|
|
});
|
|
});
|
|
|
|
it('should return view data with isAlreadyOnboarded: true when driver exists', async () => {
|
|
const driver = { id: 'driver-1' };
|
|
const viewData = { isAlreadyOnboarded: true };
|
|
|
|
mockServiceInstance.checkCurrentDriver.mockResolvedValue(Result.ok(driver));
|
|
(OnboardingPageViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await query.execute();
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
expect(OnboardingPageViewDataBuilder.build).toHaveBeenCalledWith(driver);
|
|
});
|
|
|
|
it('should return view data with isAlreadyOnboarded: false when driver not found', async () => {
|
|
const viewData = { isAlreadyOnboarded: false };
|
|
|
|
mockServiceInstance.checkCurrentDriver.mockResolvedValue(Result.err({ type: 'notFound' }));
|
|
(OnboardingPageViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await query.execute();
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
expect(OnboardingPageViewDataBuilder.build).toHaveBeenCalledWith(null);
|
|
});
|
|
|
|
it('should return unauthorized error when service returns unauthorized', async () => {
|
|
mockServiceInstance.checkCurrentDriver.mockResolvedValue(Result.err({ type: 'unauthorized' }));
|
|
|
|
const result = await query.execute();
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('unauthorized');
|
|
});
|
|
|
|
it('should return serverError when service returns serverError', async () => {
|
|
mockServiceInstance.checkCurrentDriver.mockResolvedValue(Result.err({ type: 'serverError' }));
|
|
|
|
const result = await query.execute();
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('serverError');
|
|
});
|
|
|
|
it('should provide a static execute method', async () => {
|
|
const viewData = { isAlreadyOnboarded: true };
|
|
mockServiceInstance.checkCurrentDriver.mockResolvedValue(Result.ok({}));
|
|
(OnboardingPageViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await OnboardingPageQuery.execute();
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
});
|
|
});
|