2023-02-21 02:21:04 -08:00
|
|
|
import { mock } from 'jest-mock-extended';
|
|
|
|
import type { ICredentialTypes } from 'n8n-workflow';
|
2023-12-27 02:50:43 -08:00
|
|
|
import config from '@/config';
|
2023-05-02 01:37:19 -07:00
|
|
|
import type { TranslationRequest } from '@/controllers/translation.controller';
|
2023-02-21 02:21:04 -08:00
|
|
|
import {
|
|
|
|
TranslationController,
|
|
|
|
CREDENTIAL_TRANSLATIONS_DIR,
|
|
|
|
} from '@/controllers/translation.controller';
|
2023-11-28 01:19:27 -08:00
|
|
|
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
2023-02-21 02:21:04 -08:00
|
|
|
|
|
|
|
describe('TranslationController', () => {
|
2023-12-27 02:50:43 -08:00
|
|
|
const configGetSpy = jest.spyOn(config, 'getEnv');
|
2023-02-21 02:21:04 -08:00
|
|
|
const credentialTypes = mock<ICredentialTypes>();
|
2023-12-27 02:50:43 -08:00
|
|
|
const controller = new TranslationController(credentialTypes);
|
2023-02-21 02:21:04 -08:00
|
|
|
|
|
|
|
describe('getCredentialTranslation', () => {
|
|
|
|
it('should throw 400 on invalid credential types', async () => {
|
|
|
|
const credentialType = 'not-a-valid-credential-type';
|
|
|
|
const req = mock<TranslationRequest.Credential>({ query: { credentialType } });
|
|
|
|
credentialTypes.recognizes.calledWith(credentialType).mockReturnValue(false);
|
|
|
|
|
2023-05-23 17:01:45 -07:00
|
|
|
await expect(controller.getCredentialTranslation(req)).rejects.toThrowError(
|
2023-02-21 02:21:04 -08:00
|
|
|
new BadRequestError(`Invalid Credential type: "${credentialType}"`),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return translation json on valid credential types', async () => {
|
|
|
|
const credentialType = 'credential-type';
|
|
|
|
const req = mock<TranslationRequest.Credential>({ query: { credentialType } });
|
2023-12-27 02:50:43 -08:00
|
|
|
configGetSpy.mockReturnValue('de');
|
2023-02-21 02:21:04 -08:00
|
|
|
credentialTypes.recognizes.calledWith(credentialType).mockReturnValue(true);
|
|
|
|
const response = { translation: 'string' };
|
|
|
|
jest.mock(`${CREDENTIAL_TRANSLATIONS_DIR}/de/credential-type.json`, () => response, {
|
|
|
|
virtual: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(await controller.getCredentialTranslation(req)).toEqual(response);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|