UNPKG

@roderickhsiao/react-i13n

Version:

[Experiment] React I13n provides a performant and scalable solution to application instrumentation.

115 lines (97 loc) 4.04 kB
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * Copyright 2015 - Present, Yahoo! Inc. * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ import React, { Component, useContext } from 'react'; import { render } from '@testing-library/react'; import setupI13n from '../setupI13n'; import I13nContext from '../../components/core/I13nContext'; var mockData = { options: {}, plugin: { name: 'test' } }; describe('setupI13n', () => { beforeAll(() => { // http://fb.me/react-polyfills global.requestAnimationFrame = function (callback) { setTimeout(callback, 0); }; }); it('should generate a component with setupI13n', () => { var testContext; var TestApp = () => { var context = useContext(I13nContext); testContext = context; return /*#__PURE__*/React.createElement("div", null); }; TestApp.displayName = 'TestApp'; // check the initial state is correct after render var I13nTestApp = setupI13n(TestApp, mockData.options, [mockData.plugin]); expect(I13nTestApp.displayName).toEqual('RootI13nTestApp'); render( /*#__PURE__*/React.createElement(I13nTestApp, null)); var reactI13n = testContext.i13nInstance; expect(reactI13n._plugins.test).toEqual(mockData.plugin); expect(typeof reactI13n._rootI13nNode).toEqual('object'); }); it('should generate a component with custom displayName', () => { var TestApp = () => { return /*#__PURE__*/React.createElement("div", null); }; // check the initial state is correct after render var I13nTestApp = setupI13n(TestApp, { displayName: 'Foo' }, [mockData.plugin]); expect(I13nTestApp.displayName).toEqual('Foo'); render( /*#__PURE__*/React.createElement(I13nTestApp, null)); }); it('should generate a component with setupI13n and custom display name', () => { var TestApp = () => /*#__PURE__*/React.createElement("div", null); TestApp.displayName = 'TestApp'; // check the initial state is correct after render var I13nTestApp = setupI13n(TestApp, { displayName: 'CustomName' }); expect(I13nTestApp.displayName).toEqual('CustomName'); }); it('should get i13n util functions via context', done => { class TestApp extends Component { render() { expect(typeof this.context.i13nInstance).toEqual('object'); expect(typeof this.context.executeEvent).toEqual('function'); done(); return /*#__PURE__*/React.createElement("div", null); } } _defineProperty(TestApp, "displayName", 'TestApp'); _defineProperty(TestApp, "contextType", I13nContext); var I13nTestApp = setupI13n(TestApp, {}, [mockData.plugin]); render( /*#__PURE__*/React.createElement(I13nTestApp, null)); }); it('should get i13n util functions via props', done => { class TestApp extends Component { render() { expect(typeof this.props.i13n.i13nInstance).toEqual('object'); expect(typeof this.props.i13n.executeEvent).toEqual('function'); done(); return /*#__PURE__*/React.createElement("div", null); } } _defineProperty(TestApp, "displayName", 'TestApp'); var I13nTestApp = setupI13n(TestApp, {}, [mockData.plugin]); render( /*#__PURE__*/React.createElement(I13nTestApp, null)); }); it('should get not i13n util functions via props if skipUtilFunctionsByProps is true', done => { class TestApp extends Component { render() { expect(this.props.i13n).toBeUndefined(); done(); return /*#__PURE__*/React.createElement("div", null); } } _defineProperty(TestApp, "displayName", 'TestApp'); var I13nTestApp = setupI13n(TestApp, { skipUtilFunctionsByProps: true }, [mockData.plugin]); render( /*#__PURE__*/React.createElement(I13nTestApp, null)); }); });