-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathload-survey-result-controller.spec.ts
More file actions
82 lines (70 loc) · 2.81 KB
/
Copy pathload-survey-result-controller.spec.ts
File metadata and controls
82 lines (70 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { LoadSurveyResultController } from '@/presentation/controllers'
import { forbidden, serverError, ok } from '@/presentation/helpers'
import { InvalidParamError } from '@/presentation/errors'
import { CheckSurveyByIdSpy, LoadSurveyResultSpy } from '@/tests/presentation/mocks'
import { throwError } from '@/tests/domain/mocks'
import MockDate from 'mockdate'
import faker from 'faker'
const mockRequest = (): LoadSurveyResultController.Request => ({
accountId: faker.datatype.uuid(),
surveyId: faker.datatype.uuid()
})
type SutTypes = {
sut: LoadSurveyResultController
checkSurveyByIdSpy: CheckSurveyByIdSpy
loadSurveyResultSpy: LoadSurveyResultSpy
}
const makeSut = (): SutTypes => {
const checkSurveyByIdSpy = new CheckSurveyByIdSpy()
const loadSurveyResultSpy = new LoadSurveyResultSpy()
const sut = new LoadSurveyResultController(checkSurveyByIdSpy, loadSurveyResultSpy)
return {
sut,
checkSurveyByIdSpy,
loadSurveyResultSpy
}
}
describe('LoadSurveyResult Controller', () => {
beforeAll(() => {
MockDate.set(new Date())
})
afterAll(() => {
MockDate.reset()
})
test('Should call CheckSurveyById with correct value', async () => {
const { sut, checkSurveyByIdSpy } = makeSut()
const request = mockRequest()
await sut.handle(request)
expect(checkSurveyByIdSpy.id).toBe(request.surveyId)
})
test('Should return 403 if CheckSurveyById returns false', async () => {
const { sut, checkSurveyByIdSpy } = makeSut()
checkSurveyByIdSpy.result = false
const httpResponse = await sut.handle(mockRequest())
expect(httpResponse).toEqual(forbidden(new InvalidParamError('surveyId')))
})
test('Should return 500 if CheckSurveyById throws', async () => {
const { sut, checkSurveyByIdSpy } = makeSut()
jest.spyOn(checkSurveyByIdSpy, 'checkById').mockImplementationOnce(throwError)
const httpResponse = await sut.handle(mockRequest())
expect(httpResponse).toEqual(serverError(new Error()))
})
test('Should call LoadSurveyResult with correct values', async () => {
const { sut, loadSurveyResultSpy } = makeSut()
const request = mockRequest()
await sut.handle(request)
expect(loadSurveyResultSpy.surveyId).toBe(request.surveyId)
expect(loadSurveyResultSpy.accountId).toBe(request.accountId)
})
test('Should return 500 if LoadSurveyResult throws', async () => {
const { sut, loadSurveyResultSpy } = makeSut()
jest.spyOn(loadSurveyResultSpy, 'load').mockImplementationOnce(throwError)
const httpResponse = await sut.handle(mockRequest())
expect(httpResponse).toEqual(serverError(new Error()))
})
test('Should return 200 on success', async () => {
const { sut, loadSurveyResultSpy } = makeSut()
const httpResponse = await sut.handle(mockRequest())
expect(httpResponse).toEqual(ok(loadSurveyResultSpy.result))
})
})