-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathcontext-var.controller.spec.ts
214 lines (186 loc) · 7.07 KB
/
context-var.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { getUpdateOneError } from '@/utils/test/errors/messages';
import {
contextVarFixtures,
installContextVarFixtures,
} from '@/utils/test/fixtures/contextvar';
import { getPageQuery } from '@/utils/test/pagination';
import { sortRowsBy } from '@/utils/test/sort';
import {
closeInMongodConnection,
rootMongooseTestModule,
} from '@/utils/test/test';
import { buildTestingMocks } from '@/utils/test/utils';
import {
ContextVarCreateDto,
ContextVarUpdateDto,
} from '../dto/context-var.dto';
import { ContextVarRepository } from '../repositories/context-var.repository';
import { ContextVar, ContextVarModel } from '../schemas/context-var.schema';
import { ContextVarService } from '../services/context-var.service';
import { ContextVarController } from './context-var.controller';
describe('ContextVarController', () => {
let contextVarController: ContextVarController;
let contextVarService: ContextVarService;
let contextVar: ContextVar;
let contextVarToDelete: ContextVar;
beforeAll(async () => {
const { getMocks } = await buildTestingMocks({
controllers: [ContextVarController],
imports: [
rootMongooseTestModule(installContextVarFixtures),
MongooseModule.forFeature([ContextVarModel]),
],
providers: [ContextVarService, ContextVarRepository],
});
[contextVarController, contextVarService] = await getMocks([
ContextVarController,
ContextVarService,
]);
contextVar = (await contextVarService.findOne({
label: 'test context var 1',
}))!;
contextVarToDelete = (await contextVarService.findOne({
label: 'test context var 2',
}))!;
});
afterEach(jest.clearAllMocks);
afterAll(closeInMongodConnection);
describe('count', () => {
it('should count the contextVars', async () => {
jest.spyOn(contextVarService, 'count');
const result = await contextVarController.filterCount();
expect(contextVarService.count).toHaveBeenCalled();
expect(result).toEqual({ count: contextVarFixtures.length });
});
});
describe('findPage', () => {
it('should return an array of contextVars', async () => {
const pageQuery = getPageQuery<ContextVar>();
jest.spyOn(contextVarService, 'find');
const result = await contextVarController.findPage(pageQuery, {});
expect(contextVarService.find).toHaveBeenCalledWith({}, pageQuery);
expect(result).toEqualPayload(contextVarFixtures.sort(sortRowsBy));
});
});
describe('findOne', () => {
it('should return the existing contextVar', async () => {
jest.spyOn(contextVarService, 'findOne');
const result = await contextVarController.findOne(contextVar.id);
expect(contextVarService.findOne).toHaveBeenCalledWith(contextVar.id);
expect(result).toEqualPayload(
contextVarFixtures.find(({ label }) => label === contextVar.label)!,
);
});
});
describe('create', () => {
it('should return created contextVar', async () => {
jest.spyOn(contextVarService, 'create');
const contextVarCreateDto: ContextVarCreateDto = {
label: 'contextVarLabel2',
name: 'test_add',
permanent: false,
pattern: '/.+/',
};
const result = await contextVarController.create(contextVarCreateDto);
expect(contextVarService.create).toHaveBeenCalledWith(
contextVarCreateDto,
);
expect(result).toEqualPayload(contextVarCreateDto);
});
});
describe('deleteOne', () => {
it('should delete a contextVar by id', async () => {
jest.spyOn(contextVarService, 'deleteOne');
const result = await contextVarController.deleteOne(
contextVarToDelete.id,
);
expect(contextVarService.deleteOne).toHaveBeenCalledWith(
contextVarToDelete.id,
);
expect(result).toEqual({
acknowledged: true,
deletedCount: 1,
});
});
it('should throw a NotFoundException when attempting to delete a contextVar by id', async () => {
await expect(
contextVarController.deleteOne(contextVarToDelete.id),
).rejects.toThrow(
new NotFoundException(
`Context var with ID ${contextVarToDelete.id} not found.`,
),
);
});
});
describe('deleteMany', () => {
const deleteResult = { acknowledged: true, deletedCount: 2 };
it('should delete contextVars when valid IDs are provided', async () => {
jest
.spyOn(contextVarService, 'deleteMany')
.mockResolvedValue(deleteResult);
const result = await contextVarController.deleteMany([
contextVarToDelete.id,
contextVar.id,
]);
expect(contextVarService.deleteMany).toHaveBeenCalledWith({
_id: { $in: [contextVarToDelete.id, contextVar.id] },
});
expect(result).toEqual(deleteResult);
});
it('should throw BadRequestException when no IDs are provided', async () => {
await expect(contextVarController.deleteMany([])).rejects.toThrow(
new BadRequestException('No IDs provided for deletion.'),
);
});
it('should throw NotFoundException when no contextVars are deleted', async () => {
jest.spyOn(contextVarService, 'deleteMany').mockResolvedValue({
acknowledged: true,
deletedCount: 0,
});
await expect(
contextVarController.deleteMany([contextVarToDelete.id, contextVar.id]),
).rejects.toThrow(
new NotFoundException('Context vars with provided IDs not found'),
);
});
});
describe('updateOne', () => {
const contextVarUpdatedDto: ContextVarUpdateDto = {
name: 'updated_context_var_name',
};
it('should return updated contextVar', async () => {
jest.spyOn(contextVarService, 'updateOne');
const result = await contextVarController.updateOne(
contextVar.id,
contextVarUpdatedDto,
);
expect(contextVarService.updateOne).toHaveBeenCalledWith(
contextVar.id,
contextVarUpdatedDto,
);
expect(result).toEqualPayload({
...contextVarFixtures.find(({ label }) => label === contextVar.label),
...contextVarUpdatedDto,
});
});
it('should throw a NotFoundException when attempting to update an non existing contextVar by id', async () => {
await expect(
contextVarController.updateOne(
contextVarToDelete.id,
contextVarUpdatedDto,
),
).rejects.toThrow(
getUpdateOneError(ContextVar.name, contextVarToDelete.id),
);
});
});
});