mirror of
https://github.com/portainer/portainer.git
synced 2025-08-05 13:55:21 +02:00
chore(account): write tests for CreateAccessToken [EE-2561] (#6578)
This commit is contained in:
parent
b7d18ef50f
commit
f1ea2b5c02
8 changed files with 178 additions and 27 deletions
39
app/__mocks__/i18next.test.ts
Normal file
39
app/__mocks__/i18next.test.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import * as i18nextMocks from './i18next';
|
||||
|
||||
describe('mockT', () => {
|
||||
it('should return correctly with no arguments', async () => {
|
||||
const testText = `The company's new IT initiative, code named Phoenix Project, is critical to the
|
||||
future of Parts Unlimited, but the project is massively over budget and very late. The CEO wants
|
||||
Bill to report directly to him and fix the mess in ninety days or else Bill's entire department
|
||||
will be outsourced.`;
|
||||
|
||||
const translatedText = i18nextMocks.mockT(testText);
|
||||
|
||||
expect(translatedText).toBe(testText);
|
||||
});
|
||||
|
||||
test.each`
|
||||
testText | args | expectedText
|
||||
${'{{fileName}} is invalid.'} | ${{ fileName: 'example_5.csv' }} | ${'example_5.csv is invalid.'}
|
||||
${'{{fileName}} {is}.'} | ${{ fileName: ' ' }} | ${' {is}.'}
|
||||
${'{{number}} of {{total}}'} | ${{ number: 0, total: 999 }} | ${'0 of 999'}
|
||||
${'There was an error:\n{{error}}'} | ${{ error: 'Failed' }} | ${'There was an error:\nFailed'}
|
||||
${'Click:{{li}}{{li2}}{{li_3}}'} | ${{ li: '', li2: 'https://', li_3: '!@#$%' }} | ${'Click:https://!@#$%'}
|
||||
${'{{happy}}😏y✔{{sad}}{{laugh}}'} | ${{ happy: '😃', sad: '😢', laugh: '🤣' }} | ${'😃😏y✔😢🤣'}
|
||||
`(
|
||||
'should return correctly while handling arguments in different scenarios',
|
||||
({ testText, args, expectedText }) => {
|
||||
const translatedText = i18nextMocks.mockT(testText, args);
|
||||
|
||||
expect(translatedText).toBe(expectedText);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
describe('language', () => {
|
||||
it('should return language', async () => {
|
||||
const { language } = i18nextMocks.default;
|
||||
|
||||
expect(language).toBe('en');
|
||||
});
|
||||
});
|
36
app/__mocks__/i18next.ts
Normal file
36
app/__mocks__/i18next.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
function replaceBetween(
|
||||
startIndex: number,
|
||||
endIndex: number,
|
||||
original: string,
|
||||
insertion: string
|
||||
) {
|
||||
const result =
|
||||
original.substring(0, startIndex) +
|
||||
insertion +
|
||||
original.substring(endIndex);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function mockT(i18nKey: string, args?: Record<string, string>) {
|
||||
let key = i18nKey;
|
||||
|
||||
while (key.includes('{{') && args) {
|
||||
const startIndex = key.indexOf('{{');
|
||||
const endIndex = key.indexOf('}}');
|
||||
|
||||
const currentArg = key.substring(startIndex + 2, endIndex);
|
||||
const value = args[currentArg];
|
||||
|
||||
key = replaceBetween(startIndex, endIndex + 2, key, value);
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
const i18next: Record<string, unknown> = jest.createMockFromModule('i18next');
|
||||
i18next.t = mockT;
|
||||
i18next.language = 'en';
|
||||
i18next.changeLanguage = () => new Promise(() => {});
|
||||
i18next.use = () => i18next;
|
||||
|
||||
export default i18next;
|
16
app/__mocks__/react-i18next.tsx
Normal file
16
app/__mocks__/react-i18next.tsx
Normal file
|
@ -0,0 +1,16 @@
|
|||
import { PropsWithChildren } from 'react';
|
||||
|
||||
import { mockT } from './i18next';
|
||||
|
||||
export function useTranslation() {
|
||||
return {
|
||||
t: mockT,
|
||||
i18n: {
|
||||
changeLanguage: () => new Promise(() => {}),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function Trans({ children }: PropsWithChildren<unknown>) {
|
||||
return <>{children}</>;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue