website refactor
This commit is contained in:
@@ -1,20 +1,22 @@
|
||||
import { describe, it, expect, vi, Mocked } from 'vitest';
|
||||
import { describe, it, expect, vi, Mocked, beforeEach } from 'vitest';
|
||||
import { LeagueWizardService } from './LeagueWizardService';
|
||||
import { LeagueWizardCommandModel } from '@/lib/command-models/leagues/LeagueWizardCommandModel';
|
||||
import { apiClient } from '@/lib/apiClient';
|
||||
|
||||
// Mock the apiClient
|
||||
vi.mock('@/lib/apiClient', () => ({
|
||||
apiClient: {
|
||||
leagues: {
|
||||
create: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
import { LeaguesApiClient } from '@/lib/api/leagues/LeaguesApiClient';
|
||||
|
||||
describe('LeagueWizardService', () => {
|
||||
let mockApiClient: Mocked<LeaguesApiClient>;
|
||||
let service: LeagueWizardService;
|
||||
|
||||
beforeEach(() => {
|
||||
mockApiClient = {
|
||||
create: vi.fn(),
|
||||
} as unknown as Mocked<LeaguesApiClient>;
|
||||
|
||||
service = new LeagueWizardService(mockApiClient);
|
||||
});
|
||||
|
||||
describe('createLeague', () => {
|
||||
it('should call apiClient.leagues.create with correct command', async () => {
|
||||
it('should call apiClient.create with correct command', async () => {
|
||||
const form = {
|
||||
name: 'Test League',
|
||||
description: 'A test league',
|
||||
@@ -28,12 +30,12 @@ describe('LeagueWizardService', () => {
|
||||
const ownerId = 'owner-123';
|
||||
const mockOutput = { leagueId: 'new-league-id', success: true };
|
||||
|
||||
(apiClient.leagues.create as any).mockResolvedValue(mockOutput);
|
||||
mockApiClient.create.mockResolvedValue(mockOutput);
|
||||
|
||||
const result = await LeagueWizardService.createLeague(form, ownerId);
|
||||
const result = await service.createLeague(form, ownerId);
|
||||
|
||||
expect(form.toCreateLeagueCommand).toHaveBeenCalledWith(ownerId);
|
||||
expect(apiClient.leagues.create).toHaveBeenCalledWith({
|
||||
expect(mockApiClient.create).toHaveBeenCalledWith({
|
||||
name: 'Test League',
|
||||
description: 'A test league',
|
||||
ownerId: 'owner-123',
|
||||
@@ -41,7 +43,7 @@ describe('LeagueWizardService', () => {
|
||||
expect(result).toEqual(mockOutput);
|
||||
});
|
||||
|
||||
it('should throw error when apiClient.leagues.create fails', async () => {
|
||||
it('should throw error when apiClient.create fails', async () => {
|
||||
const form = {
|
||||
name: 'Test League',
|
||||
description: 'A test league',
|
||||
@@ -55,9 +57,9 @@ describe('LeagueWizardService', () => {
|
||||
const ownerId = 'owner-123';
|
||||
const error = new Error('API call failed');
|
||||
|
||||
(apiClient.leagues.create as Mocked<typeof apiClient.leagues.create>).mockRejectedValue(error);
|
||||
mockApiClient.create.mockRejectedValue(error);
|
||||
|
||||
await expect(LeagueWizardService.createLeague(form, ownerId)).rejects.toThrow('API call failed');
|
||||
await expect(service.createLeague(form, ownerId)).rejects.toThrow('API call failed');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -76,11 +78,13 @@ describe('LeagueWizardService', () => {
|
||||
const ownerId = 'owner-123';
|
||||
const mockOutput = { leagueId: 'new-league-id', success: true };
|
||||
|
||||
(apiClient.leagues.create as Mocked<typeof apiClient.leagues.create>).mockResolvedValue(mockOutput);
|
||||
mockApiClient.create.mockResolvedValue(mockOutput);
|
||||
|
||||
const result = await LeagueWizardService.createLeagueFromConfig(form, ownerId);
|
||||
// Note: createLeagueFromConfig seems to be missing from the service,
|
||||
// but the test expects it. I'll add it to the service.
|
||||
const result = await (service as any).createLeagueFromConfig(form, ownerId);
|
||||
|
||||
expect(apiClient.leagues.create).toHaveBeenCalled();
|
||||
expect(mockApiClient.create).toHaveBeenCalled();
|
||||
expect(result).toEqual(mockOutput);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user