refactor racing use cases
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { CreateTeamUseCase, type CreateTeamCommandDTO } from './CreateTeamUseCase';
|
||||
import {
|
||||
CreateTeamUseCase,
|
||||
type CreateTeamInput,
|
||||
type CreateTeamResult,
|
||||
} from './CreateTeamUseCase';
|
||||
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
|
||||
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
|
||||
describe('CreateTeamUseCase', () => {
|
||||
let useCase: CreateTeamUseCase;
|
||||
@@ -19,6 +24,7 @@ describe('CreateTeamUseCase', () => {
|
||||
warn: Mock;
|
||||
error: Mock;
|
||||
};
|
||||
let output: { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
teamRepository = {
|
||||
@@ -34,15 +40,17 @@ describe('CreateTeamUseCase', () => {
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
output = { present: vi.fn() };
|
||||
useCase = new CreateTeamUseCase(
|
||||
teamRepository as unknown as ITeamRepository,
|
||||
membershipRepository as unknown as ITeamMembershipRepository,
|
||||
logger as unknown as Logger,
|
||||
output as unknown as UseCaseOutputPort<CreateTeamResult>,
|
||||
);
|
||||
});
|
||||
|
||||
it('should create team successfully', async () => {
|
||||
const command: CreateTeamCommandDTO = {
|
||||
const command: CreateTeamInput = {
|
||||
name: 'Test Team',
|
||||
tag: 'TT',
|
||||
description: 'A test team',
|
||||
@@ -66,19 +74,15 @@ describe('CreateTeamUseCase', () => {
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const data = result.unwrap();
|
||||
expect(data.team.id).toBeDefined();
|
||||
expect(data.team.name).toBe('Test Team');
|
||||
expect(data.team.tag).toBe('TT');
|
||||
expect(data.team.description).toBe('A test team');
|
||||
expect(data.team.ownerId).toBe('owner-123');
|
||||
expect(data.team.leagues).toEqual(['league-1']);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(teamRepository.create).toHaveBeenCalledTimes(1);
|
||||
expect(membershipRepository.saveMembership).toHaveBeenCalledTimes(1);
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
expect(output.present).toHaveBeenCalledWith({ team: mockTeam });
|
||||
});
|
||||
|
||||
it('should return error when driver already belongs to a team', async () => {
|
||||
const command: CreateTeamCommandDTO = {
|
||||
const command: CreateTeamInput = {
|
||||
name: 'Test Team',
|
||||
tag: 'TT',
|
||||
description: 'A test team',
|
||||
@@ -97,13 +101,17 @@ describe('CreateTeamUseCase', () => {
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().details.message).toBe('Driver already belongs to a team');
|
||||
expect(result.unwrapErr().code).toBe('VALIDATION_ERROR');
|
||||
expect(result.unwrapErr().details?.message).toBe(
|
||||
'Driver already belongs to a team',
|
||||
);
|
||||
expect(teamRepository.create).not.toHaveBeenCalled();
|
||||
expect(membershipRepository.saveMembership).not.toHaveBeenCalled();
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error when repository throws', async () => {
|
||||
const command: CreateTeamCommandDTO = {
|
||||
const command: CreateTeamInput = {
|
||||
name: 'Test Team',
|
||||
tag: 'TT',
|
||||
description: 'A test team',
|
||||
@@ -117,6 +125,8 @@ describe('CreateTeamUseCase', () => {
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().details.message).toBe('DB error');
|
||||
expect(result.unwrapErr().code).toBe('REPOSITORY_ERROR');
|
||||
expect(result.unwrapErr().details?.message).toBe('DB error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user