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', () => {
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>(TranslationService);
settingService = module.get<SettingService>(SettingService);
i18nService = module.get<I18nService>(I18nService);
pluginService = module.get<PluginService>(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', () => {

View File

@ -60,7 +60,6 @@ export class TranslationService extends BaseService<Translation> {
// 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<Translation> {
return Object.values(settings)
.map((group: Record<string, string | string[]>) => 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();

View File

@ -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'],
);
});
});
});

View File

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