UNPKG

@enact/i18n

Version:

Internationalization support for Enact using iLib

172 lines (171 loc) 6.92 kB
"use strict"; require("@testing-library/jest-dom"); var _react = require("@testing-library/react"); var _userEvent = _interopRequireDefault(require("@testing-library/user-event")); var _ilib = _interopRequireDefault(require("ilib")); var _locale = require("../../locale"); var _I18nDecorator = require("../I18nDecorator"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } describe('I18nDecorator', function () { // Suite-wide setup beforeEach(function () { (0, _locale.updateLocale)('en-US'); }); afterEach(function () { (0, _locale.updateLocale)(); }); test('should add rtl context parameter', function () { var Component = function Component(props) { return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { "data-testid": "i18n", children: 'rtl' in props ? 'has rtl prop' : 'does not have rtl prop' }); }; var Wrapped = (0, _I18nDecorator.I18nDecorator)({ sync: true }, (0, _I18nDecorator.I18nContextDecorator)({ rtlProp: 'rtl' }, Component)); (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, {})); var expected = 'has rtl prop'; var i18nDiv = _react.screen.getByTestId('i18n'); expect(i18nDiv).toHaveTextContent(expected); }); test('should add updateLocale context parameter', function () { var Component = function Component(_ref) { var update = _ref.updateLocale; return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { "data-testid": "i18n", children: typeof update }); }; var Wrapped = (0, _I18nDecorator.I18nDecorator)({ sync: true }, (0, _I18nDecorator.I18nContextDecorator)({ updateLocaleProp: 'updateLocale' }, Component)); (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, {})); var expected = 'function'; var i18nDiv = _react.screen.getByTestId('i18n'); expect(i18nDiv).toHaveTextContent(expected); }); test('should update the current locale when updateLocale is called', /*#__PURE__*/_asyncToGenerator(function* () { var user = _userEvent["default"].setup(); var Component = function Component(_ref3) { var _updateLocale = _ref3._updateLocale; var handleClick = function handleClick() { return _updateLocale('ar-SA'); }; return /*#__PURE__*/(0, _jsxRuntime.jsx)("button", { onClick: handleClick }); }; var Wrapped = (0, _I18nDecorator.I18nDecorator)({ sync: true }, (0, _I18nDecorator.I18nContextDecorator)({ updateLocaleProp: '_updateLocale' }, Component)); (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, {})); yield user.click(_react.screen.getByRole('button')); var expected = 'ar-SA'; var actual = _ilib["default"].getLocale(); expect(actual).toBe(expected); })); test('should update the rtl context parameter when RTL changes', /*#__PURE__*/_asyncToGenerator(function* () { var user = _userEvent["default"].setup(); var Component = function Component(_ref5) { var rtl = _ref5.rtl, _updateLocale = _ref5._updateLocale; var handleClick = function handleClick() { return _updateLocale('ar-SA'); }; return /*#__PURE__*/(0, _jsxRuntime.jsx)("button", { onClick: handleClick, children: rtl ? 'rtl' : 'ltr' }); }; var Wrapped = (0, _I18nDecorator.I18nDecorator)({ sync: true }, (0, _I18nDecorator.I18nContextDecorator)({ rtlProp: 'rtl', updateLocaleProp: '_updateLocale' }, Component)); (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, {})); var button = _react.screen.getByRole('button'); yield user.click(button); var expected = 'rtl'; expect(button).toHaveTextContent(expected); })); test('should set locale via props', function () { var Component = function Component(props) { return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: props.className }); }; var Wrapped = (0, _I18nDecorator.I18nDecorator)({ sync: true }, Component); (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, { locale: "ar-SA" })); var expected = 'ar-SA'; var actual = _ilib["default"].getLocale(); expect(actual).toBe(expected); }); test('should add locale classes to Wrapped', function () { var Component = function Component(props) { return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: props.className, "data-testid": "i18nDiv" }); }; var Wrapped = (0, _I18nDecorator.I18nDecorator)({ sync: true }, Component); // explicitly setting locale so we get a known class list regardless of runtime locale (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, { locale: "en-US" })); var i18nDiv = _react.screen.getByTestId('i18nDiv'); expect(i18nDiv).toHaveClass('enact-locale-en'); expect(i18nDiv).toHaveClass('enact-locale-en-US'); expect(i18nDiv).toHaveClass('enact-locale-US'); }); test('should treat "en-US" as latin locale', function () { var Component = function Component(props) { return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: props.className, "data-testid": "i18nDiv" }); }; var Wrapped = (0, _I18nDecorator.I18nDecorator)({ sync: true }, Component); // explicitly setting locale so we get a known class list regardless of runtime locale (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, { locale: "en-US" })); var i18nDiv = _react.screen.getByTestId('i18nDiv'); expect(i18nDiv).not.toHaveClass('enact-locale-non-latin'); }); test('should treat "ja-JP" as non-latin locale', function () { var Component = function Component(props) { return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: props.className, "data-testid": "i18nDiv" }); }; var Wrapped = (0, _I18nDecorator.I18nDecorator)({ sync: true }, Component); // explicitly setting locale so we get a known class list regardless of runtime locale (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, { locale: "ja-JP" })); var i18nDiv = _react.screen.getByTestId('i18nDiv'); expect(i18nDiv).toHaveClass('enact-locale-non-latin'); }); });