refactor racing use cases
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { UpdateLeagueMemberRoleUseCase } from './UpdateLeagueMemberRoleUseCase';
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
UpdateLeagueMemberRoleUseCase,
|
||||
type UpdateLeagueMemberRoleInput,
|
||||
type UpdateLeagueMemberRoleResult,
|
||||
type UpdateLeagueMemberRoleErrorCode,
|
||||
} from './UpdateLeagueMemberRoleUseCase';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('UpdateLeagueMemberRoleUseCase', () => {
|
||||
it('updates league member role successfully', async () => {
|
||||
const mockMembership = {
|
||||
id: 'league-1:driver-1',
|
||||
leagueId: 'league-1',
|
||||
driverId: 'driver-1',
|
||||
role: 'member',
|
||||
@@ -17,22 +25,33 @@ describe('UpdateLeagueMemberRoleUseCase', () => {
|
||||
saveMembership: vi.fn().mockResolvedValue(undefined),
|
||||
} as unknown as ILeagueMembershipRepository;
|
||||
|
||||
const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository);
|
||||
const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
const params = {
|
||||
const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);
|
||||
|
||||
const input: UpdateLeagueMemberRoleInput = {
|
||||
leagueId: 'league-1',
|
||||
targetDriverId: 'driver-1',
|
||||
newRole: 'admin',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(mockLeagueMembershipRepository.getLeagueMembers).toHaveBeenCalledWith('league-1');
|
||||
expect(mockLeagueMembershipRepository.saveMembership).toHaveBeenCalledWith({
|
||||
...mockMembership,
|
||||
role: 'admin',
|
||||
});
|
||||
expect(mockLeagueMembershipRepository.saveMembership).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = (output.present as Mock).mock.calls[0]![0] as UpdateLeagueMemberRoleResult;
|
||||
|
||||
expect(presented.membership.id).toBe('league-1:driver-1');
|
||||
expect(presented.membership.leagueId.toString()).toBe('league-1');
|
||||
expect(presented.membership.driverId.toString()).toBe('driver-1');
|
||||
expect(presented.membership.role.toString()).toBe('admin');
|
||||
});
|
||||
|
||||
it('returns error if membership not found', async () => {
|
||||
@@ -40,17 +59,60 @@ describe('UpdateLeagueMemberRoleUseCase', () => {
|
||||
getLeagueMembers: vi.fn().mockResolvedValue([]),
|
||||
} as unknown as ILeagueMembershipRepository;
|
||||
|
||||
const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository);
|
||||
const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
const params = {
|
||||
const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);
|
||||
|
||||
const input: UpdateLeagueMemberRoleInput = {
|
||||
leagueId: 'league-1',
|
||||
targetDriverId: 'driver-1',
|
||||
newRole: 'admin',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({ code: 'MEMBERSHIP_NOT_FOUND' });
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UpdateLeagueMemberRoleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('MEMBERSHIP_NOT_FOUND');
|
||||
expect(error.details.message).toBe('League membership not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('handles repository errors', async () => {
|
||||
const mockLeagueMembershipRepository = {
|
||||
getLeagueMembers: vi
|
||||
.fn()
|
||||
.mockRejectedValue(new Error('Database connection failed')),
|
||||
} as unknown as ILeagueMembershipRepository;
|
||||
|
||||
const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);
|
||||
|
||||
const input: UpdateLeagueMemberRoleInput = {
|
||||
leagueId: 'league-1',
|
||||
targetDriverId: 'driver-1',
|
||||
newRole: 'admin',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UpdateLeagueMemberRoleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('Failed to update league member role');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user