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', () => {
|
||||
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', () => {
|
||||
|
@ -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();
|
||||
|
@ -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'],
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -84,6 +84,7 @@ describe('SettingService', () => {
|
||||
expect(settingRepository.findAll).toHaveBeenCalled();
|
||||
expect(result).toEqualPayload(
|
||||
settingService.group(settingFixtures as Setting[]),
|
||||
['id', 'createdAt', 'updatedAt', 'subgroup', 'translatable'],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user