test: fix unit tests

This commit is contained in:
abdou6666 2024-11-22 16:59:49 +01:00
parent 7d3a92e1d6
commit 9a885a328d
4 changed files with 49 additions and 29 deletions

View File

@ -22,7 +22,6 @@ import { TranslationService } from '../services/translation.service';
describe('TranslationService', () => { describe('TranslationService', () => {
let service: TranslationService; let service: TranslationService;
let settingService: SettingService;
let i18nService: I18nService; let i18nService: I18nService;
let pluginService: PluginService; let pluginService: PluginService;
@ -67,16 +66,36 @@ describe('TranslationService', () => {
{ {
provide: SettingService, provide: SettingService,
useValue: { useValue: {
getSettings: jest getSettings: jest.fn().mockResolvedValue({
.fn() chatbot_settings: {
.mockResolvedValue(['Global fallback message']), global_fallback: true,
find: jest.fn().mockResolvedValue([
{
translatable: false,
label: 'global_fallback',
fallback_message: ['Global fallback message'], 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(); }).compile();
service = module.get<TranslationService>(TranslationService); service = module.get<TranslationService>(TranslationService);
settingService = module.get<SettingService>(SettingService);
i18nService = module.get<I18nService>(I18nService); i18nService = module.get<I18nService>(I18nService);
pluginService = module.get<PluginService>(PluginService); pluginService = module.get<PluginService>(PluginService);
}); });
@ -168,15 +186,9 @@ describe('TranslationService', () => {
expect(result).toEqual(['String 2', 'String 3']); expect(result).toEqual(['String 2', 'String 3']);
}); });
it('should return an empty array from the settings when global fallback is disabled', async () => { it('should return the settings translation strings', async () => {
jest.spyOn(settingService, 'getSettings').mockResolvedValueOnce({
chatbot_settings: {
global_fallback: false,
fallback_message: ['Global fallback message'],
},
} as Settings);
const strings = await service.getSettingStrings(); 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', () => { it('should return an array of strings from a block with a quick reply message', () => {

View File

@ -60,7 +60,6 @@ export class TranslationService extends BaseService<Translation> {
// plugin // plugin
Object.entries(block.message.args).forEach(([l, arg]) => { Object.entries(block.message.args).forEach(([l, arg]) => {
const setting = plugin.settings.find(({ label }) => label === l); const setting = plugin.settings.find(({ label }) => label === l);
if (setting?.translatable) { if (setting?.translatable) {
if (Array.isArray(arg)) { if (Array.isArray(arg)) {
// array of text // array of text
@ -140,9 +139,8 @@ export class TranslationService extends BaseService<Translation> {
return Object.values(settings) return Object.values(settings)
.map((group: Record<string, string | string[]>) => Object.entries(group)) .map((group: Record<string, string | string[]>) => Object.entries(group))
.flat() .flat()
.filter(([l, value]) => { .filter(([l]) => {
const found = translatableSettings.find(({ label }) => label === l); return translatableSettings.find(({ label }) => label === l);
return found && !!value;
}) })
.map(([, v]) => v) .map(([, v]) => v)
.flat(); .flat();

View File

@ -82,7 +82,13 @@ describe('SettingController', () => {
); );
expect(settingService.find).toHaveBeenCalled(); 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); const result = await settingController.updateOne(id, payload);
expect(settingService.updateOne).toHaveBeenCalledWith(id, payload); expect(settingService.updateOne).toHaveBeenCalledWith(id, payload);
expect(result).toEqualPayload({ expect(result).toEqualPayload(
...settingFixtures.find( {
(settingFixture) => settingFixture.value === 'admin@example.com', ...settingFixtures.find(
), (settingFixture) => settingFixture.value === 'admin@example.com',
value: payload.value, ),
}); value: payload.value,
},
['id', 'createdAt', 'updatedAt', 'subgroup', 'translatable'],
);
}); });
}); });
}); });

View File

@ -84,6 +84,7 @@ describe('SettingService', () => {
expect(settingRepository.findAll).toHaveBeenCalled(); expect(settingRepository.findAll).toHaveBeenCalled();
expect(result).toEqualPayload( expect(result).toEqualPayload(
settingService.group(settingFixtures as Setting[]), settingService.group(settingFixtures as Setting[]),
['id', 'createdAt', 'updatedAt', 'subgroup', 'translatable'],
); );
}); });
}); });