-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
Copy pathcat.controller.spec.ts
169 lines (163 loc) · 5.14 KB
/
cat.controller.spec.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { createMock } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { CatController } from './cat.controller';
import { CatDTO } from './cat.dto';
import { CatService } from './cat.service';
import { Cat } from './interfaces/cat.interface';
const testCat1 = 'Test Cat 1';
const testBreed1 = 'Test Breed 1';
describe('Cat Controller', () => {
let controller: CatController;
let service: CatService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CatController],
// If you've looked at the complex sample you'll notice that these functions
// are a little bit more in depth using mock implementation
// to give us a little bit more control and flexibility in our tests
// this is not necessary, but can sometimes be helpful in a test scenario
providers: [
{
provide: CatService,
useValue: {
getAll: jest.fn().mockResolvedValue([
{ name: testCat1, breed: testBreed1, age: 4 },
{ name: 'Test Cat 2', breed: 'Test Breed 2', age: 3 },
{ name: 'Test Cat 3', breed: 'Test Breed 3', age: 2 },
]),
getOne: jest.fn().mockImplementation((id: string) =>
Promise.resolve({
name: testCat1,
breed: testBreed1,
age: 4,
_id: id,
}),
),
getOneByName: jest
.fn()
.mockImplementation((name: string) =>
Promise.resolve({ name, breed: testBreed1, age: 4 }),
),
insertOne: jest
.fn()
.mockImplementation((cat: CatDTO) =>
Promise.resolve({ _id: 'a uuid', ...cat }),
),
updateOne: jest
.fn()
.mockImplementation((cat: CatDTO) =>
Promise.resolve({ _id: 'a uuid', ...cat }),
),
deleteOne: jest.fn().mockResolvedValue({ deleted: true }),
},
},
],
}).compile();
controller = module.get<CatController>(CatController);
service = module.get<CatService>(CatService);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
describe('getCats', () => {
it('should get an array of cats', () => {
expect(controller.getCats()).resolves.toEqual([
{
name: testCat1,
breed: testBreed1,
age: 4,
},
{
name: 'Test Cat 2',
breed: 'Test Breed 2',
age: 3,
},
{
name: 'Test Cat 3',
breed: 'Test Breed 3',
age: 2,
},
]);
});
});
describe('getById', () => {
it('should get a single cat', () => {
expect(controller.getById('a strange id')).resolves.toEqual({
name: testCat1,
breed: testBreed1,
age: 4,
_id: 'a strange id',
});
expect(controller.getById('a different id')).resolves.toEqual({
name: testCat1,
breed: testBreed1,
age: 4,
_id: 'a different id',
});
});
});
describe('getByName', () => {
it('should get a cat back', async () => {
await expect(controller.getByName('Ventus')).resolves.toEqual({
name: 'Ventus',
breed: testBreed1,
age: 4,
});
// using the really cool @golevelup/ts-jest module's utility function here
// otherwise we need to pass `as any` or we need to mock all 54+ attributes of Document
const aquaMock = createMock<Cat>({
name: 'Aqua',
breed: 'Maine Coon',
age: 5,
});
const getByNameSpy = jest
.spyOn(service, 'getOneByName')
.mockResolvedValueOnce(aquaMock);
const getResponse = await controller.getByName('Aqua');
expect(getResponse).toEqual(aquaMock);
expect(getByNameSpy).toBeCalledWith('Aqua');
});
});
describe('newCat', () => {
it('should create a new cat', () => {
const newCatDTO: CatDTO = {
name: 'New Cat 1',
breed: 'New Breed 1',
age: 4,
};
expect(controller.newCat(newCatDTO)).resolves.toEqual({
_id: 'a uuid',
...newCatDTO,
});
});
});
describe('updateCat', () => {
it('should update a new cat', () => {
const newCatDTO: CatDTO = {
name: 'New Cat 1',
breed: 'New Breed 1',
age: 4,
};
expect(controller.updateCat(newCatDTO)).resolves.toEqual({
_id: 'a uuid',
...newCatDTO,
});
});
});
describe('deleteCat', () => {
it('should return that it deleted a cat', () => {
expect(controller.deleteCat('a uuid that exists')).resolves.toEqual({
deleted: true,
});
});
it('should return that it did not delete a cat', () => {
const deleteSpy = jest
.spyOn(service, 'deleteOne')
.mockResolvedValueOnce({ deleted: false });
expect(
controller.deleteCat('a uuid that does not exist'),
).resolves.toEqual({ deleted: false });
expect(deleteSpy).toBeCalledWith('a uuid that does not exist');
});
});
});