view data fixes
This commit is contained in:
@@ -10,6 +10,8 @@ import { Result } from '@/lib/contracts/Result';
|
||||
import { DomainError } from '@/lib/contracts/services/Service';
|
||||
import { MediaBinaryDTO } from '@/lib/types/MediaBinaryDTO';
|
||||
|
||||
// TODO why is this an adapter?
|
||||
|
||||
/**
|
||||
* MediaAdapter
|
||||
*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getWebsiteApiBaseUrl } from '../config/apiBaseUrl';
|
||||
import { ApiClient } from './api/ApiClient';
|
||||
import { getWebsiteApiBaseUrl } from './config/apiBaseUrl';
|
||||
|
||||
export const apiClient = new ApiClient(getWebsiteApiBaseUrl());
|
||||
91
apps/website/lib/page-queries/OnboardingPageQuery.test.ts
Normal file
91
apps/website/lib/page-queries/OnboardingPageQuery.test.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user