Skip to content

Commit 9143b35

Browse files
feat: should call the AddOccurenceUseCase with correct values
1 parent 88c8853 commit 9143b35

File tree

6 files changed

+44
-5
lines changed

6 files changed

+44
-5
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"husky": "4.2.5",
3434
"jest": "^26.6.3",
3535
"nodemon": "^2.0.7",
36-
"prettier": "^2.3.0",
3736
"rimraf": "^3.0.2",
3837
"supertest": "^6.1.3",
3938
"ts-jest": "^26.5.6",

src/domain/usecases/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './AddOccurrenceUseCase';

src/presentation/controllers/occurrence/AddOccurrenceController.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
import { AddOccurrenceUseCase } from '@/domain/usecases';
12
import {
23
Controller,
34
HttpRequest,
4-
HttpResponse,
5+
HttpResponse
56
} from '@/presentation/protocols';
67

78
export class AddOccurrenceController implements Controller {
9+
constructor(private readonly addOccurrenceUseCase: AddOccurrenceUseCase) { }
10+
811
async handle(httpRequest: HttpRequest): Promise<HttpResponse> {
12+
this.addOccurrenceUseCase.add(httpRequest.body);
13+
914
return {
1015
statusCode: 400,
1116
body: {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Occurrence } from '@/domain/models';
2+
import { AddOccurrenceUseCase } from '@/domain/usecases';
3+
import { Either, InternalServerError, right } from '@/shared';
4+
import { makeOccurrenceMock } from '@tests/domain/models/mock';
5+
6+
class DBAddOccurrenceSpy implements AddOccurrenceUseCase {
7+
async add(
8+
newOccurrence: Occurrence
9+
): Promise<Either<InternalServerError, Occurrence>> {
10+
return right(makeOccurrenceMock());
11+
}
12+
}
13+
14+
export const makeDBAddOccurrenceSpy = (): AddOccurrenceUseCase =>
15+
new DBAddOccurrenceSpy();

tests/data/implementations/mock/index.ts

Whitespace-only changes.

tests/presentation/controllers/occurrence/AddOccurrenceController.spec.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
import { AddOccurrenceUseCase } from '@/domain/usecases/AddOccurrenceUseCase';
12
import { AddOccurrenceController } from '@/presentation/controllers/occurrence';
3+
import { makeDBAddOccurrenceSpy } from '@tests/data/implementations/mock/DBAddOccurrenceSpy';
24
import { makeHttpRequestMock } from './mock/HttpRequestMock';
35

46
type MakeSutType = {
57
sut: AddOccurrenceController;
8+
addOccurrenceUseCaseSpy: AddOccurrenceUseCase;
69
};
710

8-
const makeSut = (): MakeSutType => ({
9-
sut: new AddOccurrenceController(),
10-
});
11+
const makeSut = (): MakeSutType => {
12+
const addOccurrenceUseCaseSpy = makeDBAddOccurrenceSpy();
13+
const sut = new AddOccurrenceController(addOccurrenceUseCaseSpy);
14+
15+
return {
16+
sut,
17+
addOccurrenceUseCaseSpy,
18+
};
19+
};
1120

1221
describe('Unit Test: AddOccurrenceController', () => {
1322
it('should return 400 and InvalidRequestError if titulo is not provider', async () => {
@@ -159,4 +168,14 @@ describe('Unit Test: AddOccurrenceController', () => {
159168
message: 'Request inválido.',
160169
});
161170
});
171+
172+
it('should call the AddOccurrenceUseCase with correct values', async () => {
173+
const { sut, addOccurrenceUseCaseSpy } = makeSut();
174+
const httpRequest = makeHttpRequestMock();
175+
const spy = jest.spyOn(addOccurrenceUseCaseSpy, 'add');
176+
177+
await sut.handle(httpRequest);
178+
179+
expect(spy).toBeCalledWith(httpRequest.body);
180+
});
162181
});

0 commit comments

Comments
 (0)