refactor use cases
This commit is contained in:
@@ -5,8 +5,6 @@ import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamM
|
||||
import type { ITeamStatsRepository } from '../../domain/repositories/ITeamStatsRepository';
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
|
||||
describe('GetAllTeamsUseCase', () => {
|
||||
const mockTeamFindAll = vi.fn();
|
||||
const mockTeamRepo: ITeamRepository = {
|
||||
@@ -61,24 +59,16 @@ describe('GetAllTeamsUseCase', () => {
|
||||
error: vi.fn(),
|
||||
};
|
||||
|
||||
let output: UseCaseOutputPort<GetAllTeamsResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetAllTeamsResult> & { present: Mock };
|
||||
});
|
||||
});
|
||||
|
||||
it('should return teams data', async () => {
|
||||
const useCase = new GetAllTeamsUseCase(
|
||||
mockTeamRepo,
|
||||
const useCase = new GetAllTeamsUseCase(mockTeamRepo,
|
||||
mockTeamMembershipRepo,
|
||||
mockTeamStatsRepo,
|
||||
mockResultRepo,
|
||||
mockLogger,
|
||||
output,
|
||||
);
|
||||
mockLogger);
|
||||
|
||||
const team1 = {
|
||||
id: 'team1',
|
||||
@@ -138,10 +128,7 @@ describe('GetAllTeamsUseCase', () => {
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]?.[0] as GetAllTeamsResult;
|
||||
|
||||
expect(presented).toEqual({
|
||||
const presented = expect(presented).toEqual({
|
||||
teams: [
|
||||
{
|
||||
id: 'team1',
|
||||
@@ -191,14 +178,11 @@ describe('GetAllTeamsUseCase', () => {
|
||||
});
|
||||
|
||||
it('should return empty result when no teams', async () => {
|
||||
const useCase = new GetAllTeamsUseCase(
|
||||
mockTeamRepo,
|
||||
const useCase = new GetAllTeamsUseCase(mockTeamRepo,
|
||||
mockTeamMembershipRepo,
|
||||
mockTeamStatsRepo,
|
||||
mockResultRepo,
|
||||
mockLogger,
|
||||
output,
|
||||
);
|
||||
mockLogger);
|
||||
|
||||
mockTeamFindAll.mockResolvedValue([]);
|
||||
|
||||
@@ -207,24 +191,18 @@ describe('GetAllTeamsUseCase', () => {
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]?.[0] as GetAllTeamsResult;
|
||||
|
||||
expect(presented).toEqual({
|
||||
const presented = expect(presented).toEqual({
|
||||
teams: [],
|
||||
totalCount: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('should return error when repository throws', async () => {
|
||||
const useCase = new GetAllTeamsUseCase(
|
||||
mockTeamRepo,
|
||||
const useCase = new GetAllTeamsUseCase(mockTeamRepo,
|
||||
mockTeamMembershipRepo,
|
||||
mockTeamStatsRepo,
|
||||
mockResultRepo,
|
||||
mockLogger,
|
||||
output,
|
||||
);
|
||||
mockLogger);
|
||||
|
||||
const error = new Error('Repository error');
|
||||
mockTeamFindAll.mockRejectedValue(error);
|
||||
@@ -237,6 +215,5 @@ describe('GetAllTeamsUseCase', () => {
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('Repository error');
|
||||
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user