Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(template): cover TemplateController handlers and ratchet thresholds

Yudhi Armyndharis339a19193644
diff --git a/docs/09-testing-strategy.md b/docs/09-testing-strategy.md
index 9d1853da2..761f73d92 100644
--- a/docs/09-testing-strategy.md
+++ b/docs/09-testing-strategy.md
@@ -211,7 +211,7 @@ authoritative gate. Current policy:
| `src/modules/stats/` | 67% | 63% | 71% | 69% |
| `src/modules/status-store/` | 79% | 83% | 92% | 91% |
| `src/modules/status/` | 47% | 45% | 62% | 60% |
-| `src/modules/template/` | 46% | 55% | 73% | 72% |
+| `src/modules/template/` | 77% | 99% | 92% | 89% |
| `src/modules/webhook/` | 72% | 89% | 90% | 87% |
Each floor sits roughly five points below that scope's measured coverage, so it catches a real
diff --git a/package.json b/package.json
index b894858cf..ffb6ad399 100644
--- a/package.json
+++ b/package.json
@@ -367,10 +367,10 @@
"statements": 60
},
"./src/modules/template/": {
- "branches": 46,
- "functions": 55,
- "lines": 73,
- "statements": 72
+ "branches": 77,
+ "functions": 99,
+ "lines": 92,
+ "statements": 89
},
"./src/modules/webhook/": {
"branches": 72,
diff --git a/src/modules/template/template.controller.spec.ts b/src/modules/template/template.controller.spec.ts
new file mode 100644
index 000000000..5813399d4
--- /dev/null
+++ b/src/modules/template/template.controller.spec.ts
@@ -0,0 +1,47 @@
+import { TemplateController } from './template.controller';
+import { TemplateService } from './template.service';
+
+describe('TemplateController', () => {
+ const service = {
+ create: jest.fn(),
+ findBySession: jest.fn(),
+ findOne: jest.fn(),
+ update: jest.fn(),
+ delete: jest.fn(),
+ };
+ const controller = new TemplateController(service as unknown as TemplateService);
+
+ beforeEach(() => jest.clearAllMocks());
+
+ it('create delegates with the session id and DTO', async () => {
+ const dto = { name: 'welcome', content: 'Hi {{name}}' };
+ service.create.mockResolvedValue({ id: 't1' });
+ await controller.create('s1', dto as never);
+ expect(service.create).toHaveBeenCalledWith('s1', dto);
+ });
+
+ it('findBySession delegates', async () => {
+ service.findBySession.mockResolvedValue([]);
+ await controller.findBySession('s1');
+ expect(service.findBySession).toHaveBeenCalledWith('s1');
+ });
+
+ it('findOne delegates with session + id', async () => {
+ service.findOne.mockResolvedValue({ id: 't1' });
+ await controller.findOne('s1', 't1');
+ expect(service.findOne).toHaveBeenCalledWith('s1', 't1');
+ });
+
+ it('update delegates with session, id and DTO', async () => {
+ const dto = { content: 'bye' };
+ service.update.mockResolvedValue({ id: 't1' });
+ await controller.update('s1', 't1', dto as never);
+ expect(service.update).toHaveBeenCalledWith('s1', 't1', dto);
+ });
+
+ it('delete delegates and resolves void', async () => {
+ service.delete.mockResolvedValue(undefined);
+ await expect(controller.delete('s1', 't1')).resolves.toBeUndefined();
+ expect(service.delete).toHaveBeenCalledWith('s1', 't1');
+ });
+});