Newer
Older
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
import { Test, TestingModule } from '@nestjs/testing';
import { PersonalOffersController } from './personal-offers.controller';
import { PersonalOffersService } from './personal-offers.service';
import { PersonalOffersServiceMock } from '../../test/mock/services/personalOffers.mock.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { mockJwtAuthGuard } from '../../test/mock/guards/jwt-auth.mock.guard';
import { IsPersonalOfferOwnerGuard } from '../users/guards/isPersonalOfferOwner.guard';
import { mockIsPersonalOfferOwnerGuard } from '../../test/mock/guards/isPersonalOfferOwner.mock.guard';
import {
createPersonalOffersDtoDataMock,
updatePersonalOffersDtoDataMock,
personalOffersDataMock,
} from '../../test/mock/data/personalOffers.mock.data';
import { UsersService } from '../users/services/users.service';
import { UsersServiceMock } from '../../test/mock/services/user.mock.service';
import { StructuresService } from '../structures/services/structures.service';
import { StructuresServiceMock } from '../../test/mock/services/structures.mock.service';
describe('PersonalOffersController', () => {
let controller: PersonalOffersController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PersonalOffersController],
providers: [
{
provide: PersonalOffersService,
useClass: PersonalOffersServiceMock,
},
{
provide: UsersService,
useClass: UsersServiceMock,
},
{
provide: StructuresService,
useClass: StructuresServiceMock,
},
],
})
.overrideGuard(JwtAuthGuard)
.useValue(mockJwtAuthGuard)
.overrideGuard(IsPersonalOfferOwnerGuard)
.useValue(mockIsPersonalOfferOwnerGuard)
.compile();
controller = module.get<PersonalOffersController>(PersonalOffersController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
describe('find personal offer', () => {
it('should get personal offer', async () => {
expect(await controller.find('1234ba0e2ab5775cfc01ed3e')).toBe(personalOffersDataMock[0]);
});
it('should get personal offer does not exist', async () => {
try {
await controller.find('abcd');
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Personal offer does not exist');
expect(e.status).toBe(404);
}
});
});
describe('create personal offer', () => {
it('should create personal offer for existing user and structure', async () => {
const req = { user: { _id: '6036721022462b001334c4bb' } };
expect(await controller.create(req, createPersonalOffersDtoDataMock[0])).toEqual(personalOffersDataMock[0]);
});
it('should return personal offer already exist in the structure', async () => {
const req = { user: { _id: '6036721022462b001334c4bb' } };
try {
await controller.create(req, createPersonalOffersDtoDataMock[1]);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Personal offer already exist in the structure');
expect(e.status).toBe(400);
}
});
it('should return structure not found for the personal offer attachment', async () => {
const req = { user: { _id: '6036721022462b001334c4bb' } };
try {
await controller.create(req, createPersonalOffersDtoDataMock[2]);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Structure not found for the personal offer attachment');
expect(e.status).toBe(400);
}
});
it('should return user not found for the personal offer attachment', async () => {
const req = { user: { _id: 'unIdQuiExistePasTropTrop' } };
try {
await controller.create(req, createPersonalOffersDtoDataMock[0]);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('User not found for the personal offer attachment');
expect(e.status).toBe(400);
}
});
});
describe('update personal offer', () => {
it('should get updated personal offer', async () => {
expect(await controller.update('2345ba0e2ab5775cfc01ed4d', updatePersonalOffersDtoDataMock[1])).toEqual(
personalOffersDataMock[1]
);
});
it('should get invalid personal offer id', async () => {
try {
await controller.update('abcd', updatePersonalOffersDtoDataMock[1]);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Invalid personal offer id for update');
expect(e.status).toBe(400);
}
});
});
describe('update personal offer', () => {
it('should get deleted personal offer', async () => {
expect(await controller.delete('2345ba0e2ab5775cfc01ed4d')).toEqual(personalOffersDataMock[1]);
});
it('should get invalid personal offer id', async () => {
try {
await controller.delete('abcd');
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Invalid personal offer id for deletion');
expect(e.status).toBe(400);
}
});
});
});