74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import type { IIssueRepository } from '../../../domain/repositories/issue.repository.interface.js';
|
|
import { Issue } from '../../../domain/entities/issue.entity.js';
|
|
import { IssueNotFoundException } from '../../../domain/exceptions/issue-not-found.exception.js';
|
|
import { DeleteIssueUseCase } from './delete-issue.use-case.js';
|
|
import { DeleteIssueCommand } from './delete-issue.command.js';
|
|
import { IssueEventsService } from '../../services/issue-events.service.js';
|
|
|
|
const buildIssue = () =>
|
|
Issue.reconstitute({
|
|
id: 1,
|
|
key: 'ISSUE-1',
|
|
title: 'Title',
|
|
content: null,
|
|
status: 'OPEN',
|
|
priority: 0,
|
|
assignee: null,
|
|
reporter: 'user-1',
|
|
projectId: 'project-1',
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
|
|
describe('DeleteIssueUseCase', () => {
|
|
let repository: IIssueRepository;
|
|
let issueEventsService: IssueEventsService;
|
|
let useCase: DeleteIssueUseCase;
|
|
|
|
beforeEach(() => {
|
|
repository = {
|
|
findById: vi.fn(),
|
|
findByCKey: vi.fn(),
|
|
findAll: vi.fn(),
|
|
countByStatus: vi.fn(),
|
|
create: vi.fn(),
|
|
update: vi.fn(),
|
|
delete: vi.fn(),
|
|
};
|
|
issueEventsService = new IssueEventsService();
|
|
useCase = new DeleteIssueUseCase(repository, issueEventsService);
|
|
});
|
|
|
|
it('deletes an existing issue', async () => {
|
|
vi.mocked(repository.findById).mockResolvedValue(buildIssue());
|
|
|
|
await useCase.execute(new DeleteIssueCommand(1, 'user-1'));
|
|
|
|
expect(repository.delete).toHaveBeenCalledWith(1);
|
|
});
|
|
|
|
it('throws IssueNotFoundException and does not call delete when missing', async () => {
|
|
vi.mocked(repository.findById).mockResolvedValue(null);
|
|
|
|
await expect(useCase.execute(new DeleteIssueCommand(999, 'user-1'))).rejects.toThrow(IssueNotFoundException);
|
|
expect(repository.delete).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('emits an issue-deleted event for SSE subscribers', async () => {
|
|
vi.mocked(repository.findById).mockResolvedValue(buildIssue());
|
|
|
|
const received: unknown[] = [];
|
|
issueEventsService.onIssueDeleted().subscribe((issue) => received.push(issue));
|
|
|
|
await useCase.execute(new DeleteIssueCommand(1, 'user-1'));
|
|
|
|
expect(received).toEqual([
|
|
expect.objectContaining({
|
|
id: 1,
|
|
key: 'ISSUE-1',
|
|
}),
|
|
]);
|
|
});
|
|
});
|