website refactor
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
|
||||
import { GetAllTeamsUseCase, type GetAllTeamsInput, type GetAllTeamsResult } from './GetAllTeamsUseCase';
|
||||
import type { TeamRepository } from '../../domain/repositories/TeamRepository';
|
||||
import type { Logger } from '@core/shared/domain/Logger';
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import type { TeamMembershipRepository } from '../../domain/repositories/TeamMembershipRepository';
|
||||
import type { TeamRepository } from '../../domain/repositories/TeamRepository';
|
||||
import type { TeamStatsRepository } from '../../domain/repositories/TeamStatsRepository';
|
||||
import type { ResultRepository } from '../../domain/repositories/ResultRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { GetAllTeamsUseCase, type GetAllTeamsInput } from './GetAllTeamsUseCase';
|
||||
|
||||
describe('GetAllTeamsUseCase', () => {
|
||||
const mockTeamFindAll = vi.fn();
|
||||
const mockTeamRepo: ITeamRepository = {
|
||||
const mockTeamRepo: TeamRepository = {
|
||||
findById: vi.fn(),
|
||||
findAll: mockTeamFindAll,
|
||||
findByLeagueId: vi.fn(),
|
||||
@@ -18,7 +18,7 @@ describe('GetAllTeamsUseCase', () => {
|
||||
};
|
||||
|
||||
const mockTeamMembershipCountByTeamId = vi.fn();
|
||||
const mockTeamMembershipRepo: ITeamMembershipRepository = {
|
||||
const mockTeamMembershipRepo: TeamMembershipRepository = {
|
||||
getMembership: vi.fn(),
|
||||
getActiveMembershipForDriver: vi.fn(),
|
||||
getTeamMembers: vi.fn(),
|
||||
@@ -30,28 +30,13 @@ describe('GetAllTeamsUseCase', () => {
|
||||
removeJoinRequest: vi.fn(),
|
||||
};
|
||||
|
||||
const mockTeamStatsRepo: ITeamStatsRepository = {
|
||||
const mockTeamStatsRepo: TeamStatsRepository = {
|
||||
getTeamStats: vi.fn(),
|
||||
saveTeamStats: vi.fn(),
|
||||
getAllStats: vi.fn(),
|
||||
clear: vi.fn(),
|
||||
};
|
||||
|
||||
const mockResultRepo: IResultRepository = {
|
||||
findAll: vi.fn(),
|
||||
findById: vi.fn(),
|
||||
findByRaceId: vi.fn(),
|
||||
findByDriverId: vi.fn(),
|
||||
findByDriverIdAndLeagueId: vi.fn(),
|
||||
create: vi.fn(),
|
||||
createMany: vi.fn(),
|
||||
update: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
deleteByRaceId: vi.fn(),
|
||||
exists: vi.fn(),
|
||||
existsByRaceId: vi.fn(),
|
||||
};
|
||||
|
||||
const mockLogger: Logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
@@ -61,20 +46,19 @@ describe('GetAllTeamsUseCase', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return teams data', async () => {
|
||||
const useCase = new GetAllTeamsUseCase(mockTeamRepo,
|
||||
mockTeamMembershipRepo,
|
||||
mockTeamStatsRepo,
|
||||
mockResultRepo,
|
||||
mockLogger);
|
||||
|
||||
const team1 = {
|
||||
id: 'team1',
|
||||
name: { props: 'Team One' },
|
||||
tag: { props: 'TO' },
|
||||
description: { props: 'Description One' },
|
||||
id: { toString: () => 'team1' },
|
||||
name: { toString: () => 'Team One' },
|
||||
tag: { toString: () => 'TO' },
|
||||
description: { toString: () => 'Description One' },
|
||||
ownerId: { toString: () => 'owner1' },
|
||||
leagues: [{ toString: () => 'league1' }],
|
||||
createdAt: { toDate: () => new Date('2023-01-01T00:00:00Z') },
|
||||
@@ -83,10 +67,10 @@ describe('GetAllTeamsUseCase', () => {
|
||||
isRecruiting: false,
|
||||
};
|
||||
const team2 = {
|
||||
id: 'team2',
|
||||
name: { props: 'Team Two' },
|
||||
tag: { props: 'TT' },
|
||||
description: { props: 'Description Two' },
|
||||
id: { toString: () => 'team2' },
|
||||
name: { toString: () => 'Team Two' },
|
||||
tag: { toString: () => 'TT' },
|
||||
description: { toString: () => 'Description Two' },
|
||||
ownerId: { toString: () => 'owner2' },
|
||||
leagues: [{ toString: () => 'league2' }],
|
||||
createdAt: { toDate: () => new Date('2023-01-02T00:00:00Z') },
|
||||
@@ -98,7 +82,6 @@ describe('GetAllTeamsUseCase', () => {
|
||||
mockTeamFindAll.mockResolvedValue([team1, team2]);
|
||||
mockTeamMembershipCountByTeamId.mockImplementation((id: string) => Promise.resolve(id === 'team1' ? 5 : 3));
|
||||
|
||||
// Provide precomputed stats so the use case doesn't compute from results.
|
||||
(mockTeamStatsRepo.getTeamStats as unknown as Mock).mockImplementation((teamId: string) =>
|
||||
Promise.resolve(
|
||||
teamId === 'team1'
|
||||
@@ -126,62 +109,21 @@ describe('GetAllTeamsUseCase', () => {
|
||||
const result = await useCase.execute({} as GetAllTeamsInput);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
const presented = result.unwrap();
|
||||
|
||||
const presented = expect(presented).toEqual({
|
||||
teams: [
|
||||
{
|
||||
id: 'team1',
|
||||
name: 'Team One',
|
||||
tag: 'TO',
|
||||
description: 'Description One',
|
||||
ownerId: 'owner1',
|
||||
leagues: ['league1'],
|
||||
createdAt: new Date('2023-01-01T00:00:00Z'),
|
||||
memberCount: 5,
|
||||
totalWins: 2,
|
||||
totalRaces: 10,
|
||||
performanceLevel: 'intermediate',
|
||||
specialization: 'mixed',
|
||||
region: 'EU',
|
||||
languages: ['en'],
|
||||
logoRef: team1.logoRef,
|
||||
logoUrl: null,
|
||||
rating: 1200,
|
||||
category: undefined,
|
||||
isRecruiting: false,
|
||||
},
|
||||
{
|
||||
id: 'team2',
|
||||
name: 'Team Two',
|
||||
tag: 'TT',
|
||||
description: 'Description Two',
|
||||
ownerId: 'owner2',
|
||||
leagues: ['league2'],
|
||||
createdAt: new Date('2023-01-02T00:00:00Z'),
|
||||
memberCount: 3,
|
||||
totalWins: 5,
|
||||
totalRaces: 20,
|
||||
performanceLevel: 'advanced',
|
||||
specialization: 'mixed',
|
||||
region: 'US',
|
||||
languages: ['en', 'de'],
|
||||
logoRef: team2.logoRef,
|
||||
logoUrl: null,
|
||||
rating: 1400,
|
||||
category: undefined,
|
||||
isRecruiting: true,
|
||||
},
|
||||
],
|
||||
totalCount: 2,
|
||||
});
|
||||
expect(presented.totalCount).toBe(2);
|
||||
expect(presented.teams[0].team).toBe(team1);
|
||||
expect(presented.teams[0].memberCount).toBe(5);
|
||||
expect(presented.teams[0].rating).toBe(1200);
|
||||
expect(presented.teams[1].team).toBe(team2);
|
||||
expect(presented.teams[1].memberCount).toBe(3);
|
||||
expect(presented.teams[1].rating).toBe(1400);
|
||||
});
|
||||
|
||||
it('should return empty result when no teams', async () => {
|
||||
const useCase = new GetAllTeamsUseCase(mockTeamRepo,
|
||||
mockTeamMembershipRepo,
|
||||
mockTeamStatsRepo,
|
||||
mockResultRepo,
|
||||
mockLogger);
|
||||
|
||||
mockTeamFindAll.mockResolvedValue([]);
|
||||
@@ -189,19 +131,16 @@ describe('GetAllTeamsUseCase', () => {
|
||||
const result = await useCase.execute({} as GetAllTeamsInput);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
const presented = result.unwrap();
|
||||
|
||||
const presented = expect(presented).toEqual({
|
||||
teams: [],
|
||||
totalCount: 0,
|
||||
});
|
||||
expect(presented.teams).toEqual([]);
|
||||
expect(presented.totalCount).toBe(0);
|
||||
});
|
||||
|
||||
it('should return error when repository throws', async () => {
|
||||
const useCase = new GetAllTeamsUseCase(mockTeamRepo,
|
||||
mockTeamMembershipRepo,
|
||||
mockTeamStatsRepo,
|
||||
mockResultRepo,
|
||||
mockLogger);
|
||||
|
||||
const error = new Error('Repository error');
|
||||
@@ -214,6 +153,5 @@ describe('GetAllTeamsUseCase', () => {
|
||||
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('Repository error');
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user