@rockpack/localazer
Version:
This module can help you organize localization in your React application
168 lines (167 loc) • 8.22 kB
JavaScript
import { __awaiter } from "tslib";
import React, { useContext, createContext, useState } from 'react';
import { act } from 'react-dom/test-utils';
import { mount } from 'enzyme';
import { l, nl, sprintf } from './jed';
import Localization from './Localization';
import LocalizationObserver from './LocalizationObserver';
const WrapperContext = createContext(null);
let ctx;
let wrapper;
const localeData = {
domain: 'messages',
// eslint-disable-next-line @typescript-eslint/camelcase
locale_data: {
messages: {
'': {
domain: 'messages',
// eslint-disable-next-line @typescript-eslint/camelcase
plural_forms: 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);',
lang: 'ru'
},
'Alarm!': ['Ахтунг!'],
'Hello world': ['Привет мир'],
'%d click': ['%d клик', '%d кликов', '%d кликов'],
'Sort By': ['Сортировать по'],
Latest: ['По последнему'],
'Most Popular': ['Самый популярный'],
'Most Viewed': ['Самый просматриваемый'],
'Most Commented': ['Самый комментируемый'],
'USER\u0004Your name is %s and surname is %s': ['Ваше имя %s ваша фамилия %s']
}
}
};
beforeAll(() => {
const Wrapper = ({ children }) => {
const [name, setName] = useState('Ivan');
const [activeLang, setActiveLang] = useState('en');
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
const resetCounter = () => {
setCount(0);
};
const resetName = () => {
setName('Ivan');
};
return (React.createElement(WrapperContext.Provider, { value: {
setActiveLang,
setName,
increment,
resetCounter,
resetName
} }, children(activeLang, name, count)));
};
const App = ({ name, count }) => {
ctx = useContext(WrapperContext);
return (React.createElement("div", null,
React.createElement("p", { className: "test-case-1" },
React.createElement(Localization, null, l('Hello world'))),
React.createElement("p", { className: "test-case-2" },
React.createElement(Localization, null, sprintf(nl('%d click', '%d clicks', count), count))),
React.createElement("p", { className: "test-case-3" },
React.createElement(Localization, null, sprintf(l('Your name is %s and surname is %s', 'USER'), React.createElement("span", { style: { textDecoration: 'underline' } },
React.createElement("b", null, name)), React.createElement("span", { style: { textDecoration: 'underline' } },
React.createElement("b", null, "Pupkin")))))));
};
wrapper = mount(React.createElement(Wrapper, null, (activeLang, name, count) => (React.createElement(LocalizationObserver, { currentLanguage: activeLang, languages: { ru: localeData } },
React.createElement(App, { name: name, count: count })))));
});
describe('Test default language (English)', () => {
test('Basic l - Hello world example', () => __awaiter(void 0, void 0, void 0, function* () {
expect(wrapper.find('.test-case-1')
.text())
.toBe('Hello world');
}));
test('nl singular test with zero count', () => __awaiter(void 0, void 0, void 0, function* () {
expect(wrapper.find('.test-case-2')
.text())
.toBe('0 clicks');
}));
test('nl singular test with incremented count', () => __awaiter(void 0, void 0, void 0, function* () {
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
ctx.increment();
}));
expect(wrapper.find('.test-case-2')
.text())
.toBe('1 click');
}));
test('nl plural test with one more incrementation', () => __awaiter(void 0, void 0, void 0, function* () {
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
ctx.increment();
}));
expect(wrapper.find('.test-case-2')
.text())
.toBe('2 clicks');
}));
test('sprintf test with react components', () => __awaiter(void 0, void 0, void 0, function* () {
expect(wrapper.find('.test-case-3')
.find('.localization-node')
.html())
.toBe('<span class="localization-node ">Your name is <span style="text-decoration:underline"><b>Ivan</b></span> and surname is <span style="text-decoration:underline"><b>Pupkin</b></span></span>');
}));
test('sprintf change name in the sentence', () => __awaiter(void 0, void 0, void 0, function* () {
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
ctx.setName('Sergey');
}));
expect(wrapper.find('.test-case-3')
.find('.localization-node')
.html())
.toBe('<span class="localization-node ">Your name is <span style="text-decoration:underline"><b>Sergey</b></span> and surname is <span style="text-decoration:underline"><b>Pupkin</b></span></span>');
}));
});
describe('Test Russian language', () => {
test('Basic l - Привет мир example', () => __awaiter(void 0, void 0, void 0, function* () {
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
ctx.setActiveLang('ru');
}));
}));
test('Basic l - Привет мир example', () => __awaiter(void 0, void 0, void 0, function* () {
expect(wrapper.find('.test-case-1')
.text())
.toBe('Привет мир');
}));
test('nl singular test with zero count', () => __awaiter(void 0, void 0, void 0, function* () {
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
ctx.resetCounter();
}));
expect(wrapper.find('.test-case-2')
.text())
.toBe('0 кликов');
}));
test('nl singular test with incremented count', () => __awaiter(void 0, void 0, void 0, function* () {
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
ctx.increment();
}));
expect(wrapper.find('.test-case-2')
.text())
.toBe('1 клик');
}));
test('nl plural test with one more incrementation', () => __awaiter(void 0, void 0, void 0, function* () {
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
ctx.increment();
}));
expect(wrapper.find('.test-case-2')
.text())
.toBe('2 кликов');
}));
test('sprintf test with react components', () => __awaiter(void 0, void 0, void 0, function* () {
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
ctx.resetName();
}));
expect(wrapper.find('.test-case-3')
.find('.localization-node')
.html())
.toBe('<span class="localization-node ">Ваше имя <span style="text-decoration:underline"><b>Ivan</b></span> ваша фамилия <span style="text-decoration:underline"><b>Pupkin</b></span></span>');
}));
test('sprintf change name in the sentence', () => __awaiter(void 0, void 0, void 0, function* () {
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
ctx.setName('Sergey');
}));
expect(wrapper.find('.test-case-3')
.find('.localization-node')
.html())
.toBe('<span class="localization-node ">Ваше имя <span style="text-decoration:underline"><b>Sergey</b></span> ваша фамилия <span style="text-decoration:underline"><b>Pupkin</b></span></span>');
}));
});