-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4906b2
commit 46c2e17
Showing
7 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { getTestRequestRepository } from '../../test-utils/infrastructure'; | ||
import { RequestRepository } from '../repository'; | ||
import { Request, Response } from '../entity'; | ||
import { GetRequestDetails } from './GetRequestDetails'; | ||
|
||
let useCase: GetRequestDetails; | ||
let requestRepository: RequestRepository; | ||
|
||
beforeEach(() => { | ||
requestRepository = getTestRequestRepository(); | ||
|
||
useCase = new GetRequestDetails({ requestRepository }); | ||
}); | ||
|
||
it('should return a tuple with the request and the response', async () => { | ||
// Given | ||
const requestId = 'request-id'; | ||
const request = new Request('GET', '/test', {}, ''); | ||
const response = new Response(201, {}, 'Hello world'); | ||
|
||
(requestRepository.getRequestById as jest.Mock).mockResolvedValue(request); | ||
(requestRepository.getResponseByRequestId as jest.Mock).mockResolvedValue( | ||
response | ||
); | ||
|
||
// When | ||
const result = await useCase.execute(requestId); | ||
|
||
//Then | ||
expect(result).toEqual([request, response]); | ||
expect(requestRepository.getRequestById).toHaveBeenCalledTimes(1); | ||
expect(requestRepository.getRequestById).toHaveBeenCalledWith(requestId); | ||
expect(requestRepository.getResponseByRequestId).toHaveBeenCalledTimes(1); | ||
expect(requestRepository.getResponseByRequestId).toHaveBeenCalledWith( | ||
requestId | ||
); | ||
}); | ||
|
||
it('should throw when the request is not found', async () => { | ||
expect.assertions(1); | ||
|
||
// Given | ||
(requestRepository.getRequestById as jest.Mock).mockResolvedValue(null); | ||
|
||
// When | ||
try { | ||
await useCase.execute(''); | ||
} catch (err) { | ||
// Then | ||
expect(err).toEqual(new Error('Request not found')); | ||
} | ||
}); | ||
|
||
it('should throw when the response is not found', async () => { | ||
expect.assertions(1); | ||
|
||
// Given | ||
const request = new Request('GET', '/test', {}, ''); | ||
(requestRepository.getRequestById as jest.Mock).mockResolvedValue(request); | ||
(requestRepository.getResponseByRequestId as jest.Mock).mockResolvedValue( | ||
null | ||
); | ||
|
||
// When | ||
try { | ||
await useCase.execute(''); | ||
} catch (err) { | ||
// Then | ||
expect(err).toEqual(new Error('Response not found')); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { RequestRepository } from '../repository'; | ||
import { Request, Response } from '../entity'; | ||
|
||
interface Dependencies { | ||
requestRepository: RequestRepository; | ||
} | ||
|
||
export class GetRequestDetails { | ||
private requestRepository: RequestRepository; | ||
|
||
public constructor({ requestRepository }: Dependencies) { | ||
this.requestRepository = requestRepository; | ||
} | ||
|
||
public async execute(requestId: string): Promise<[Request, Response]> { | ||
const [request, response] = await Promise.all([ | ||
this.requestRepository.getRequestById(requestId), | ||
this.requestRepository.getResponseByRequestId(requestId), | ||
]); | ||
|
||
if (!request) { | ||
throw new Error('Request not found'); | ||
} | ||
|
||
if (!response) { | ||
throw new Error('Response not found'); | ||
} | ||
|
||
return [request, response]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters