refactor use cases
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, beforeEach, vi, 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 {
|
||||
@@ -14,7 +14,6 @@ describe('RejectSponsorshipRequestUseCase', () => {
|
||||
let useCase: RejectSponsorshipRequestUseCase;
|
||||
let sponsorshipRequestRepo: { findById: Mock; update: Mock };
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<RejectSponsorshipRequestResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
sponsorshipRequestRepo = { findById: vi.fn(), update: vi.fn() };
|
||||
@@ -24,22 +23,18 @@ describe('RejectSponsorshipRequestUseCase', () => {
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
output = { present: vi.fn() } as unknown as UseCaseOutputPort<RejectSponsorshipRequestResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
|
||||
useCase = new RejectSponsorshipRequestUseCase(
|
||||
sponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
const unwrapError = (
|
||||
result: Result<void, ApplicationErrorCode<RejectSponsorshipRequestErrorCode, { message: string }>>,
|
||||
result: Result<RejectSponsorshipRequestResult, ApplicationErrorCode<RejectSponsorshipRequestErrorCode, { message: string }>>,
|
||||
): ApplicationErrorCode<RejectSponsorshipRequestErrorCode, { message: string }> => result.unwrapErr();
|
||||
|
||||
it('should return not found error when request does not exist and not call output', async () => {
|
||||
it('should return not found error when request does not exist', async () => {
|
||||
sponsorshipRequestRepo.findById.mockResolvedValue(null);
|
||||
|
||||
const input: RejectSponsorshipRequestInput = {
|
||||
@@ -55,10 +50,9 @@ describe('RejectSponsorshipRequestUseCase', () => {
|
||||
expect(error.code).toBe('SPONSORSHIP_REQUEST_NOT_FOUND');
|
||||
expect(error.details?.message).toBe('Sponsorship request not found');
|
||||
expect(sponsorshipRequestRepo.update).not.toHaveBeenCalled();
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return not pending error when request is not pending and not call output', async () => {
|
||||
it('should return not pending error when request is not pending', async () => {
|
||||
const mockRequest = {
|
||||
id: 'request-1',
|
||||
status: 'accepted',
|
||||
@@ -79,10 +73,9 @@ describe('RejectSponsorshipRequestUseCase', () => {
|
||||
expect(error.code).toBe('SPONSORSHIP_REQUEST_NOT_PENDING');
|
||||
expect(error.details?.message).toBe('Sponsorship request is not pending');
|
||||
expect(sponsorshipRequestRepo.update).not.toHaveBeenCalled();
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should reject the request successfully with reason and present result once', async () => {
|
||||
it('should reject the request successfully with reason', async () => {
|
||||
const respondedAt = new Date('2023-01-01T00:00:00Z');
|
||||
const mockRequest = {
|
||||
id: 'request-1',
|
||||
@@ -106,19 +99,17 @@ describe('RejectSponsorshipRequestUseCase', () => {
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult.requestId).toBe('request-1');
|
||||
expect(successResult.status).toBe('rejected');
|
||||
expect(successResult.respondedAt).toBe(respondedAt);
|
||||
expect(successResult.rejectionReason).toBe('Not interested');
|
||||
|
||||
expect(sponsorshipRequestRepo.update).toHaveBeenCalledTimes(1);
|
||||
expect(mockRequest.reject).toHaveBeenCalledWith('driver-1', 'Not interested');
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
|
||||
const [[presented]] = output.present.mock.calls as [[RejectSponsorshipRequestResult]];
|
||||
expect(presented.requestId).toBe('request-1');
|
||||
expect(presented.status).toBe('rejected');
|
||||
expect(presented.respondedAt).toBe(respondedAt);
|
||||
expect(presented.rejectionReason).toBe('Not interested');
|
||||
});
|
||||
|
||||
it('should reject the request successfully without reason and present result once', async () => {
|
||||
it('should reject the request successfully without reason', async () => {
|
||||
const respondedAt = new Date('2023-01-01T00:00:00Z');
|
||||
const mockRequest = {
|
||||
id: 'request-1',
|
||||
@@ -141,19 +132,17 @@ describe('RejectSponsorshipRequestUseCase', () => {
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult.requestId).toBe('request-1');
|
||||
expect(successResult.status).toBe('rejected');
|
||||
expect(successResult.respondedAt).toBe(respondedAt);
|
||||
expect(successResult.rejectionReason).toBeUndefined();
|
||||
|
||||
expect(sponsorshipRequestRepo.update).toHaveBeenCalledTimes(1);
|
||||
expect(mockRequest.reject).toHaveBeenCalledWith('driver-1', undefined);
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
|
||||
const [[presented]] = output.present.mock.calls as [[RejectSponsorshipRequestResult]];
|
||||
expect(presented.requestId).toBe('request-1');
|
||||
expect(presented.status).toBe('rejected');
|
||||
expect(presented.respondedAt).toBe(respondedAt);
|
||||
expect(presented.rejectionReason).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should wrap repository errors in REPOSITORY_ERROR and not call output', async () => {
|
||||
it('should wrap repository errors in REPOSITORY_ERROR', async () => {
|
||||
const error = new Error('DB failure');
|
||||
sponsorshipRequestRepo.findById.mockRejectedValue(error);
|
||||
|
||||
@@ -168,6 +157,5 @@ describe('RejectSponsorshipRequestUseCase', () => {
|
||||
const appError = unwrapError(result);
|
||||
expect(appError.code).toBe('REPOSITORY_ERROR');
|
||||
expect(appError.details?.message).toBe('DB failure');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user