website refactor
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { ApplyPenaltyUseCase, type ApplyPenaltyResult } from './ApplyPenaltyUseCase';
|
||||
import type { PenaltyRepository } from '../../domain/repositories/PenaltyRepository';
|
||||
import type { ProtestRepository } from '../../domain/repositories/ProtestRepository';
|
||||
import type { RaceRepository } from '../../domain/repositories/RaceRepository';
|
||||
import type { LeagueMembershipRepository } from '../../domain/repositories/LeagueMembershipRepository';
|
||||
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
|
||||
import { ApplyPenaltyUseCase } from './ApplyPenaltyUseCase';
|
||||
|
||||
describe('ApplyPenaltyUseCase', () => {
|
||||
let mockPenaltyRepo: {
|
||||
create: Mock;
|
||||
@@ -46,15 +43,11 @@ describe('ApplyPenaltyUseCase', () => {
|
||||
});
|
||||
|
||||
it('should return error when race does not exist', async () => {
|
||||
const output: { present: Mock } = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as unknown as IPenaltyRepository,
|
||||
mockProtestRepo as unknown as IProtestRepository,
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
||||
mockLogger as unknown as Logger);
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as any,
|
||||
mockProtestRepo as any,
|
||||
mockRaceRepo as any,
|
||||
mockLeagueMembershipRepo as any,
|
||||
mockLogger as any);
|
||||
|
||||
mockRaceRepo.findById.mockResolvedValue(null);
|
||||
|
||||
@@ -68,19 +61,15 @@ describe('ApplyPenaltyUseCase', () => {
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(false);
|
||||
expect(result.error!.code).toBe('RACE_NOT_FOUND');
|
||||
expect(result.unwrapErr().code).toBe('RACE_NOT_FOUND');
|
||||
});
|
||||
|
||||
it('should return error when steward does not have authority', async () => {
|
||||
const output: { present: Mock } = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as unknown as IPenaltyRepository,
|
||||
mockProtestRepo as unknown as IProtestRepository,
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
||||
mockLogger as unknown as Logger);
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as any,
|
||||
mockProtestRepo as any,
|
||||
mockRaceRepo as any,
|
||||
mockLeagueMembershipRepo as any,
|
||||
mockLogger as any);
|
||||
|
||||
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
|
||||
|
||||
@@ -102,19 +91,15 @@ describe('ApplyPenaltyUseCase', () => {
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(false);
|
||||
expect(result.error!.code).toBe('INSUFFICIENT_AUTHORITY');
|
||||
expect(result.unwrapErr().code).toBe('INSUFFICIENT_AUTHORITY');
|
||||
});
|
||||
|
||||
it('should return error when protest does not exist', async () => {
|
||||
const output: { present: Mock } = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as unknown as IPenaltyRepository,
|
||||
mockProtestRepo as unknown as IProtestRepository,
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
||||
mockLogger as unknown as Logger);
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as any,
|
||||
mockProtestRepo as any,
|
||||
mockRaceRepo as any,
|
||||
mockLeagueMembershipRepo as any,
|
||||
mockLogger as any);
|
||||
|
||||
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
|
||||
|
||||
@@ -138,19 +123,15 @@ describe('ApplyPenaltyUseCase', () => {
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(false);
|
||||
expect(result.error!.code).toBe('PROTEST_NOT_FOUND');
|
||||
expect(result.unwrapErr().code).toBe('PROTEST_NOT_FOUND');
|
||||
});
|
||||
|
||||
it('should return error when protest is not upheld', async () => {
|
||||
const output: { present: Mock } = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as unknown as IPenaltyRepository,
|
||||
mockProtestRepo as unknown as IProtestRepository,
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
||||
mockLogger as unknown as Logger);
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as any,
|
||||
mockProtestRepo as any,
|
||||
mockRaceRepo as any,
|
||||
mockLeagueMembershipRepo as any,
|
||||
mockLogger as any);
|
||||
|
||||
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
|
||||
|
||||
@@ -174,19 +155,15 @@ describe('ApplyPenaltyUseCase', () => {
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(false);
|
||||
expect(result.error!.code).toBe('PROTEST_NOT_UPHELD');
|
||||
expect(result.unwrapErr().code).toBe('PROTEST_NOT_UPHELD');
|
||||
});
|
||||
|
||||
it('should return error when protest is not for this race', async () => {
|
||||
const output: { present: Mock } = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as unknown as IPenaltyRepository,
|
||||
mockProtestRepo as unknown as IProtestRepository,
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
||||
mockLogger as unknown as Logger);
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as any,
|
||||
mockProtestRepo as any,
|
||||
mockRaceRepo as any,
|
||||
mockLeagueMembershipRepo as any,
|
||||
mockLogger as any);
|
||||
|
||||
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
|
||||
|
||||
@@ -210,19 +187,15 @@ describe('ApplyPenaltyUseCase', () => {
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(false);
|
||||
expect(result.error!.code).toBe('PROTEST_NOT_FOR_RACE');
|
||||
expect(result.unwrapErr().code).toBe('PROTEST_NOT_FOR_RACE');
|
||||
});
|
||||
|
||||
it('should create penalty and return result on success', async () => {
|
||||
const output: { present: Mock } = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as unknown as IPenaltyRepository,
|
||||
mockProtestRepo as unknown as IProtestRepository,
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
||||
mockLogger as unknown as Logger);
|
||||
const useCase = new ApplyPenaltyUseCase(mockPenaltyRepo as any,
|
||||
mockProtestRepo as any,
|
||||
mockRaceRepo as any,
|
||||
mockLeagueMembershipRepo as any,
|
||||
mockLogger as any);
|
||||
|
||||
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
|
||||
|
||||
@@ -246,22 +219,11 @@ describe('ApplyPenaltyUseCase', () => {
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
const presented = (expect(presented).toEqual({ penaltyId: expect.any(String) });
|
||||
const presented = result.unwrap();
|
||||
expect(presented.penaltyId).toBeDefined();
|
||||
|
||||
expect(mockPenaltyRepo.create).toHaveBeenCalledTimes(1);
|
||||
const createdPenalty = (mockPenaltyRepo.create as Mock).mock.calls[0]?.[0] as unknown as {
|
||||
leagueId: unknown;
|
||||
raceId: unknown;
|
||||
driverId: unknown;
|
||||
type: string;
|
||||
value?: number;
|
||||
reason: string;
|
||||
issuedBy: unknown;
|
||||
status: unknown;
|
||||
notes?: string;
|
||||
};
|
||||
const createdPenalty = (mockPenaltyRepo.create as Mock).mock.calls[0]?.[0] as any;
|
||||
|
||||
type ToStringable = { toString(): string };
|
||||
const asString = (value: unknown): string => {
|
||||
@@ -287,4 +249,4 @@ describe('ApplyPenaltyUseCase', () => {
|
||||
expect(asString(createdPenalty.status)).toBe('pending');
|
||||
expect(createdPenalty.notes).toBe('Test notes');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user