add tests to core
This commit is contained in:
130
core/media/application/use-cases/UpdateAvatarUseCase.test.ts
Normal file
130
core/media/application/use-cases/UpdateAvatarUseCase.test.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import {
|
||||
UpdateAvatarUseCase,
|
||||
type UpdateAvatarErrorCode,
|
||||
type UpdateAvatarInput,
|
||||
type UpdateAvatarResult,
|
||||
} from './UpdateAvatarUseCase';
|
||||
import type { IAvatarRepository } from '../../domain/repositories/IAvatarRepository';
|
||||
import { Avatar } from '../../domain/entities/Avatar';
|
||||
|
||||
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(() => {
|
||||
avatarRepo = {
|
||||
findActiveByDriverId: vi.fn(),
|
||||
save: vi.fn(),
|
||||
};
|
||||
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
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,
|
||||
);
|
||||
});
|
||||
|
||||
it('creates new avatar when no current active avatar exists', async () => {
|
||||
avatarRepo.findActiveByDriverId.mockResolvedValue(null);
|
||||
|
||||
const input: UpdateAvatarInput = {
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result).toBeInstanceOf(Result);
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
expect(avatarRepo.findActiveByDriverId).toHaveBeenCalledWith('driver-1');
|
||||
expect(avatarRepo.save).toHaveBeenCalledTimes(1);
|
||||
|
||||
const saved = (avatarRepo.save as unknown as Mock).mock.calls[0]![0] as Avatar;
|
||||
expect(saved.driverId).toBe('driver-1');
|
||||
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' });
|
||||
});
|
||||
|
||||
it('deactivates current avatar before saving new avatar', async () => {
|
||||
const currentAvatar = Avatar.reconstitute({
|
||||
id: 'old-avatar',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/old.png',
|
||||
selectedAt: new Date('2020-01-01T00:00:00.000Z'),
|
||||
isActive: true,
|
||||
});
|
||||
|
||||
avatarRepo.findActiveByDriverId.mockResolvedValue(currentAvatar);
|
||||
|
||||
const input: UpdateAvatarInput = {
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/new.png',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(avatarRepo.save).toHaveBeenCalledTimes(2);
|
||||
|
||||
const firstSaved = (avatarRepo.save as unknown as Mock).mock.calls[0]![0] as Avatar;
|
||||
expect(firstSaved.id).toBe('old-avatar');
|
||||
expect(firstSaved.isActive).toBe(false);
|
||||
|
||||
const secondSaved = (avatarRepo.save as unknown as Mock).mock.calls[1]![0] as Avatar;
|
||||
expect(secondSaved.id).toBe('avatar-1');
|
||||
expect(secondSaved.isActive).toBe(true);
|
||||
|
||||
expect(output.present).toHaveBeenCalledWith({ avatarId: 'avatar-1', driverId: 'driver-1' });
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws', async () => {
|
||||
avatarRepo.findActiveByDriverId.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const input: UpdateAvatarInput = {
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
};
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<UpdateAvatarErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr();
|
||||
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