mirror of
https://github.com/hexastack/hexabot
synced 2024-11-24 04:53:41 +00:00
test: fix unit tests
This commit is contained in:
parent
7d3a92e1d6
commit
9a885a328d
@ -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', () => {
|
||||||
|
@ -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();
|
||||||
|
@ -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(
|
...settingFixtures.find(
|
||||||
(settingFixture) => settingFixture.value === 'admin@example.com',
|
(settingFixture) => settingFixture.value === 'admin@example.com',
|
||||||
),
|
),
|
||||||
value: payload.value,
|
value: payload.value,
|
||||||
});
|
},
|
||||||
|
['id', 'createdAt', 'updatedAt', 'subgroup', 'translatable'],
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -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'],
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user