From 9a885a328d1fa20eed8cdcbcdfb3a4cd41a89940 Mon Sep 17 00:00:00 2001 From: abdou6666 Date: Fri, 22 Nov 2024 16:59:49 +0100 Subject: [PATCH] test: fix unit tests --- .../i18n/services/translation.service.spec.ts | 48 ++++++++++++------- api/src/i18n/services/translation.service.ts | 6 +-- .../controllers/setting.controller.spec.ts | 23 ++++++--- .../setting/services/setting.service.spec.ts | 1 + 4 files changed, 49 insertions(+), 29 deletions(-) diff --git a/api/src/i18n/services/translation.service.spec.ts b/api/src/i18n/services/translation.service.spec.ts index 6ad5737..5f0d6bb 100644 --- a/api/src/i18n/services/translation.service.spec.ts +++ b/api/src/i18n/services/translation.service.spec.ts @@ -22,7 +22,6 @@ import { TranslationService } from '../services/translation.service'; describe('TranslationService', () => { let service: TranslationService; - let settingService: SettingService; let i18nService: I18nService; let pluginService: PluginService; @@ -67,16 +66,36 @@ describe('TranslationService', () => { { provide: SettingService, useValue: { - getSettings: jest - .fn() - .mockResolvedValue(['Global fallback message']), - find: jest.fn().mockResolvedValue([ - { - translatable: false, - label: 'global_fallback', + getSettings: jest.fn().mockResolvedValue({ + chatbot_settings: { + global_fallback: true, fallback_message: ['Global fallback message'], }, - ]), + }), + find: jest + .fn() + .mockImplementation((criteria: { translatable?: boolean }) => + [ + { + translatable: false, + group: 'default', + value: 'Global fallback', + label: 'global_fallback', + type: SettingType.checkbox, + }, + { + translatable: true, + group: 'default', + value: 'Global fallback message', + label: 'fallback_message', + type: SettingType.text, + }, + ].filter((s) => + criteria && 'translatable' in criteria + ? s.translatable === criteria.translatable + : true, + ), + ), }, }, { @@ -90,7 +109,6 @@ describe('TranslationService', () => { }).compile(); service = module.get(TranslationService); - settingService = module.get(SettingService); i18nService = module.get(I18nService); pluginService = module.get(PluginService); }); @@ -168,15 +186,9 @@ describe('TranslationService', () => { expect(result).toEqual(['String 2', 'String 3']); }); - it('should return an empty array from the settings when global fallback is disabled', async () => { - jest.spyOn(settingService, 'getSettings').mockResolvedValueOnce({ - chatbot_settings: { - global_fallback: false, - fallback_message: ['Global fallback message'], - }, - } as Settings); + it('should return the settings translation strings', async () => { const strings = await service.getSettingStrings(); - expect(strings).toEqual([]); + expect(strings).toEqual(['Global fallback message']); }); it('should return an array of strings from a block with a quick reply message', () => { diff --git a/api/src/i18n/services/translation.service.ts b/api/src/i18n/services/translation.service.ts index 6a4d25d..434d062 100644 --- a/api/src/i18n/services/translation.service.ts +++ b/api/src/i18n/services/translation.service.ts @@ -60,7 +60,6 @@ export class TranslationService extends BaseService { // plugin Object.entries(block.message.args).forEach(([l, arg]) => { const setting = plugin.settings.find(({ label }) => label === l); - if (setting?.translatable) { if (Array.isArray(arg)) { // array of text @@ -140,9 +139,8 @@ export class TranslationService extends BaseService { return Object.values(settings) .map((group: Record) => Object.entries(group)) .flat() - .filter(([l, value]) => { - const found = translatableSettings.find(({ label }) => label === l); - return found && !!value; + .filter(([l]) => { + return translatableSettings.find(({ label }) => label === l); }) .map(([, v]) => v) .flat(); diff --git a/api/src/setting/controllers/setting.controller.spec.ts b/api/src/setting/controllers/setting.controller.spec.ts index aacf019..21ad5d3 100644 --- a/api/src/setting/controllers/setting.controller.spec.ts +++ b/api/src/setting/controllers/setting.controller.spec.ts @@ -82,7 +82,13 @@ describe('SettingController', () => { ); expect(settingService.find).toHaveBeenCalled(); - expect(result).toEqualPayload(settingFixtures); + expect(result).toEqualPayload(settingFixtures, [ + 'id', + 'createdAt', + 'updatedAt', + 'subgroup', + 'translatable', + ]); }); }); @@ -97,12 +103,15 @@ describe('SettingController', () => { const result = await settingController.updateOne(id, payload); expect(settingService.updateOne).toHaveBeenCalledWith(id, payload); - expect(result).toEqualPayload({ - ...settingFixtures.find( - (settingFixture) => settingFixture.value === 'admin@example.com', - ), - value: payload.value, - }); + expect(result).toEqualPayload( + { + ...settingFixtures.find( + (settingFixture) => settingFixture.value === 'admin@example.com', + ), + value: payload.value, + }, + ['id', 'createdAt', 'updatedAt', 'subgroup', 'translatable'], + ); }); }); }); diff --git a/api/src/setting/services/setting.service.spec.ts b/api/src/setting/services/setting.service.spec.ts index fee81eb..9007082 100644 --- a/api/src/setting/services/setting.service.spec.ts +++ b/api/src/setting/services/setting.service.spec.ts @@ -84,6 +84,7 @@ describe('SettingService', () => { expect(settingRepository.findAll).toHaveBeenCalled(); expect(result).toEqualPayload( settingService.group(settingFixtures as Setting[]), + ['id', 'createdAt', 'updatedAt', 'subgroup', 'translatable'], ); }); });