You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
6.0 KiB
172 lines
6.0 KiB
4 years ago
|
import React from 'react';
|
||
|
import { shallow } from 'enzyme';
|
||
|
import { getMessage } from './i18n-helper';
|
||
5 years ago
|
|
||
4 years ago
|
describe('i18n helper', () => {
|
||
4 years ago
|
const TEST_LOCALE_CODE = 'TEST_LOCALE_CODE';
|
||
5 years ago
|
|
||
4 years ago
|
const TEST_KEY_1 = 'TEST_KEY_1';
|
||
|
const TEST_KEY_2 = 'TEST_KEY_2';
|
||
|
const TEST_KEY_3 = 'TEST_KEY_3';
|
||
|
const TEST_KEY_4 = 'TEST_KEY_4';
|
||
|
const TEST_KEY_5 = 'TEST_KEY_5';
|
||
|
const TEST_KEY_6 = 'TEST_KEY_6';
|
||
|
const TEST_KEY_6_HELPER = 'TEST_KEY_6_HELPER';
|
||
|
const TEST_KEY_7 = 'TEST_KEY_7';
|
||
|
const TEST_KEY_7_HELPER_1 = 'TEST_KEY_7_HELPER_1';
|
||
|
const TEST_KEY_7_HELPER_2 = 'TEST_KEY_7_HELPER_2';
|
||
|
const TEST_KEY_8 = 'TEST_KEY_8';
|
||
|
const TEST_KEY_8_HELPER_1 = 'TEST_KEY_8_HELPER_1';
|
||
|
const TEST_KEY_8_HELPER_2 = 'TEST_KEY_8_HELPER_2';
|
||
5 years ago
|
|
||
4 years ago
|
const TEST_SUBSTITUTION_1 = 'TEST_SUBSTITUTION_1';
|
||
|
const TEST_SUBSTITUTION_2 = 'TEST_SUBSTITUTION_2';
|
||
|
const TEST_SUBSTITUTION_3 = 'TEST_SUBSTITUTION_3';
|
||
|
const TEST_SUBSTITUTION_4 = 'TEST_SUBSTITUTION_4';
|
||
|
const TEST_SUBSTITUTION_5 = 'TEST_SUBSTITUTION_5';
|
||
5 years ago
|
|
||
|
const testLocaleMessages = {
|
||
|
[TEST_KEY_1]: {
|
||
|
message: 'This is a simple message.',
|
||
|
expectedResult: 'This is a simple message.',
|
||
|
},
|
||
|
[TEST_KEY_2]: {
|
||
|
message: 'This is a message with a single non-react substitution $1.',
|
||
|
},
|
||
|
[TEST_KEY_3]: {
|
||
|
message: 'This is a message with two non-react substitutions $1 and $2.',
|
||
|
},
|
||
|
[TEST_KEY_4]: {
|
||
|
message: '$1 - $2 - $3 - $4 - $5',
|
||
|
},
|
||
|
[TEST_KEY_5]: {
|
||
|
message: '$1 - $2 - $3',
|
||
|
},
|
||
|
[TEST_KEY_6]: {
|
||
4 years ago
|
message: 'Testing a react substitution $1.',
|
||
5 years ago
|
},
|
||
|
[TEST_KEY_6_HELPER]: {
|
||
4 years ago
|
message: TEST_SUBSTITUTION_1,
|
||
5 years ago
|
},
|
||
|
[TEST_KEY_7]: {
|
||
4 years ago
|
message: 'Testing a react substitution $1 and another $2.',
|
||
5 years ago
|
},
|
||
|
[TEST_KEY_7_HELPER_1]: {
|
||
4 years ago
|
message: TEST_SUBSTITUTION_1,
|
||
5 years ago
|
},
|
||
|
[TEST_KEY_7_HELPER_2]: {
|
||
4 years ago
|
message: TEST_SUBSTITUTION_2,
|
||
5 years ago
|
},
|
||
|
[TEST_KEY_8]: {
|
||
4 years ago
|
message:
|
||
|
'Testing a mix $1 of react substitutions $2 and string substitutions $3 + $4.',
|
||
5 years ago
|
},
|
||
|
[TEST_KEY_8_HELPER_1]: {
|
||
4 years ago
|
message: TEST_SUBSTITUTION_3,
|
||
5 years ago
|
},
|
||
|
[TEST_KEY_8_HELPER_2]: {
|
||
4 years ago
|
message: TEST_SUBSTITUTION_4,
|
||
5 years ago
|
},
|
||
4 years ago
|
};
|
||
|
const t = getMessage.bind(null, TEST_LOCALE_CODE, testLocaleMessages);
|
||
5 years ago
|
|
||
|
const TEST_SUBSTITUTION_6 = (
|
||
4 years ago
|
<div style={{ color: 'red' }} key="test-react-substitutions-1">
|
||
|
{t(TEST_KEY_6_HELPER)}
|
||
5 years ago
|
</div>
|
||
4 years ago
|
);
|
||
5 years ago
|
const TEST_SUBSTITUTION_7_1 = (
|
||
4 years ago
|
<div style={{ color: 'red' }} key="test-react-substitutions-1">
|
||
|
{t(TEST_KEY_7_HELPER_1)}
|
||
5 years ago
|
</div>
|
||
4 years ago
|
);
|
||
5 years ago
|
const TEST_SUBSTITUTION_7_2 = (
|
||
4 years ago
|
<div style={{ color: 'blue' }} key="test-react-substitutions-1">
|
||
|
{t(TEST_KEY_7_HELPER_2)}
|
||
5 years ago
|
</div>
|
||
4 years ago
|
);
|
||
5 years ago
|
const TEST_SUBSTITUTION_8_1 = (
|
||
4 years ago
|
<div style={{ color: 'orange' }} key="test-react-substitutions-1">
|
||
|
{t(TEST_KEY_8_HELPER_1)}
|
||
5 years ago
|
</div>
|
||
4 years ago
|
);
|
||
5 years ago
|
const TEST_SUBSTITUTION_8_2 = (
|
||
4 years ago
|
<div style={{ color: 'pink' }} key="test-react-substitutions-1">
|
||
|
{t(TEST_KEY_8_HELPER_2)}
|
||
5 years ago
|
</div>
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
describe('getMessage', () => {
|
||
|
it('should return the exact message paired with key if there are no substitutions', () => {
|
||
4 years ago
|
const result = t(TEST_KEY_1);
|
||
4 years ago
|
expect(result).toStrictEqual('This is a simple message.');
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
it('should return the correct message when a single non-react substitution is made', () => {
|
||
4 years ago
|
const result = t(TEST_KEY_2, [TEST_SUBSTITUTION_1]);
|
||
4 years ago
|
expect(result).toStrictEqual(
|
||
4 years ago
|
`This is a message with a single non-react substitution ${TEST_SUBSTITUTION_1}.`,
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
it('should return the correct message when two non-react substitutions are made', () => {
|
||
4 years ago
|
const result = t(TEST_KEY_3, [TEST_SUBSTITUTION_1, TEST_SUBSTITUTION_2]);
|
||
4 years ago
|
expect(result).toStrictEqual(
|
||
4 years ago
|
`This is a message with two non-react substitutions ${TEST_SUBSTITUTION_1} and ${TEST_SUBSTITUTION_2}.`,
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
it('should return the correct message when multiple non-react substitutions are made', () => {
|
||
4 years ago
|
const result = t(TEST_KEY_4, [
|
||
|
TEST_SUBSTITUTION_1,
|
||
|
TEST_SUBSTITUTION_2,
|
||
|
TEST_SUBSTITUTION_3,
|
||
|
TEST_SUBSTITUTION_4,
|
||
|
TEST_SUBSTITUTION_5,
|
||
4 years ago
|
]);
|
||
4 years ago
|
expect(result).toStrictEqual(
|
||
4 years ago
|
`${TEST_SUBSTITUTION_1} - ${TEST_SUBSTITUTION_2} - ${TEST_SUBSTITUTION_3} - ${TEST_SUBSTITUTION_4} - ${TEST_SUBSTITUTION_5}`,
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
it('should correctly render falsey substitutions', () => {
|
||
4 years ago
|
const result = t(TEST_KEY_4, [0, -0, '', false, NaN]);
|
||
4 years ago
|
expect(result).toStrictEqual('0 - 0 - - false - NaN');
|
||
4 years ago
|
});
|
||
4 years ago
|
|
||
4 years ago
|
it('should render nothing for "null" and "undefined" substitutions', () => {
|
||
4 years ago
|
const result = t(TEST_KEY_5, [null, TEST_SUBSTITUTION_2]);
|
||
4 years ago
|
expect(result).toStrictEqual(` - ${TEST_SUBSTITUTION_2} - `);
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
it('should return the correct message when a single react substitution is made', () => {
|
||
4 years ago
|
const result = t(TEST_KEY_6, [TEST_SUBSTITUTION_6]);
|
||
4 years ago
|
expect(shallow(result).html()).toStrictEqual(
|
||
4 years ago
|
'<span> Testing a react substitution <div style="color:red">TEST_SUBSTITUTION_1</div>. </span>',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
it('should return the correct message when two react substitutions are made', () => {
|
||
4 years ago
|
const result = t(TEST_KEY_7, [
|
||
|
TEST_SUBSTITUTION_7_1,
|
||
|
TEST_SUBSTITUTION_7_2,
|
||
4 years ago
|
]);
|
||
4 years ago
|
expect(shallow(result).html()).toStrictEqual(
|
||
4 years ago
|
'<span> Testing a react substitution <div style="color:red">TEST_SUBSTITUTION_1</div> and another <div style="color:blue">TEST_SUBSTITUTION_2</div>. </span>',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
it('should return the correct message when substituting a mix of react elements and strings', () => {
|
||
4 years ago
|
const result = t(TEST_KEY_8, [
|
||
|
TEST_SUBSTITUTION_1,
|
||
|
TEST_SUBSTITUTION_8_1,
|
||
|
TEST_SUBSTITUTION_2,
|
||
|
TEST_SUBSTITUTION_8_2,
|
||
4 years ago
|
]);
|
||
4 years ago
|
expect(shallow(result).html()).toStrictEqual(
|
||
4 years ago
|
'<span> Testing a mix TEST_SUBSTITUTION_1 of react substitutions <div style="color:orange">TEST_SUBSTITUTION_3</div> and string substitutions TEST_SUBSTITUTION_2 + <div style="color:pink">TEST_SUBSTITUTION_4</div>. </span>',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
|
});
|