-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathadmin-context.controller.spec.ts
More file actions
98 lines (79 loc) · 2.72 KB
/
admin-context.controller.spec.ts
File metadata and controls
98 lines (79 loc) · 2.72 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { Test, TestingModule } from '@nestjs/testing';
import { AdminContextController } from './admin-context.controller';
import { ContextService } from '../context/context.service';
jest.mock('../auth/platform-admin.guard', () => ({
PlatformAdminGuard: class {
canActivate() {
return true;
}
},
}));
jest.mock('../auth/auth.server', () => ({
auth: { api: {} },
}));
jest.mock('@db', () => ({ db: {} }));
describe('AdminContextController', () => {
let controller: AdminContextController;
const mockService = {
findAllByOrganization: jest.fn(),
create: jest.fn(),
updateById: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AdminContextController],
providers: [{ provide: ContextService, useValue: mockService }],
}).compile();
controller = module.get<AdminContextController>(AdminContextController);
jest.clearAllMocks();
});
describe('list', () => {
it('should list context entries', async () => {
const entries = { data: [{ id: 'ctx_1' }], count: 1 };
mockService.findAllByOrganization.mockResolvedValue(entries);
const result = await controller.list('org_1');
expect(mockService.findAllByOrganization).toHaveBeenCalledWith('org_1', {
search: undefined,
page: undefined,
perPage: undefined,
});
expect(result).toEqual(entries);
});
it('should pass search and pagination', async () => {
mockService.findAllByOrganization.mockResolvedValue({
data: [],
count: 0,
});
await controller.list('org_1', 'auth', '2', '10');
expect(mockService.findAllByOrganization).toHaveBeenCalledWith('org_1', {
search: 'auth',
page: 2,
perPage: 10,
});
});
});
describe('create', () => {
it('should create a context entry', async () => {
const dto = { question: 'How?', answer: 'Like this.' };
const created = { id: 'ctx_1', ...dto };
mockService.create.mockResolvedValue(created);
const result = await controller.create('org_1', dto as never);
expect(mockService.create).toHaveBeenCalledWith('org_1', dto);
expect(result).toEqual(created);
});
});
describe('update', () => {
it('should update a context entry', async () => {
const dto = { answer: 'Updated answer' };
const updated = { id: 'ctx_1', answer: 'Updated answer' };
mockService.updateById.mockResolvedValue(updated);
const result = await controller.update('org_1', 'ctx_1', dto as never);
expect(mockService.updateById).toHaveBeenCalledWith(
'ctx_1',
'org_1',
dto,
);
expect(result).toEqual(updated);
});
});
});