refactor use cases
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import {
|
||||
@@ -15,15 +15,9 @@ vi.mock('uuid', () => ({
|
||||
v4: () => 'avatar-1',
|
||||
}));
|
||||
|
||||
interface TestOutputPort extends UseCaseOutputPort<UpdateAvatarResult> {
|
||||
present: Mock;
|
||||
result?: UpdateAvatarResult;
|
||||
}
|
||||
|
||||
describe('UpdateAvatarUseCase', () => {
|
||||
let avatarRepo: { findActiveByDriverId: Mock; save: Mock };
|
||||
let logger: Logger;
|
||||
let output: TestOutputPort;
|
||||
let useCase: UpdateAvatarUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -39,15 +33,8 @@ describe('UpdateAvatarUseCase', () => {
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn((result: UpdateAvatarResult) => {
|
||||
output.result = result;
|
||||
}),
|
||||
} as unknown as TestOutputPort;
|
||||
|
||||
useCase = new UpdateAvatarUseCase(
|
||||
avatarRepo as unknown as IAvatarRepository,
|
||||
output,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
@@ -73,7 +60,8 @@ describe('UpdateAvatarUseCase', () => {
|
||||
expect(saved.mediaUrl.value).toBe('https://example.com/avatar.png');
|
||||
expect(saved.isActive).toBe(true);
|
||||
|
||||
expect(output.present).toHaveBeenCalledWith({ avatarId: 'avatar-1', driverId: 'driver-1' });
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult).toEqual({ avatarId: 'avatar-1', driverId: 'driver-1' });
|
||||
});
|
||||
|
||||
it('deactivates current avatar before saving new avatar', async () => {
|
||||
@@ -105,7 +93,8 @@ describe('UpdateAvatarUseCase', () => {
|
||||
expect(secondSaved.id).toBe('avatar-1');
|
||||
expect(secondSaved.isActive).toBe(true);
|
||||
|
||||
expect(output.present).toHaveBeenCalledWith({ avatarId: 'avatar-1', driverId: 'driver-1' });
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult).toEqual({ avatarId: 'avatar-1', driverId: 'driver-1' });
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws', async () => {
|
||||
@@ -116,7 +105,7 @@ describe('UpdateAvatarUseCase', () => {
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
};
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<UpdateAvatarErrorCode, { message: string }>> =
|
||||
const result: Result<UpdateAvatarResult, ApplicationErrorCode<UpdateAvatarErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
@@ -124,7 +113,6 @@ describe('UpdateAvatarUseCase', () => {
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details?.message).toBe('DB error');
|
||||
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user