@instructure/ui-react-utils
Version:
A React utility library made by Instructure Inc.
140 lines (138 loc) • 5.45 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react2 = require("@testing-library/react");
require("@testing-library/jest-dom");
var _callRenderProp = require("../callRenderProp");
var _jsxRuntime = require("@emotion/react/jsx-runtime");
var _div, _div2;
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
describe('callRenderProp', () => {
it('strings', () => {
expect((0, _callRenderProp.callRenderProp)('foo')).toEqual('foo');
});
it('numbers', () => {
expect((0, _callRenderProp.callRenderProp)(2)).toEqual(2);
});
it('arrays', () => {
const prop = ['foo', 'bar', 'baz'];
expect((0, _callRenderProp.callRenderProp)(prop)).toStrictEqual(prop);
expect((0, _callRenderProp.callRenderProp)([])).toStrictEqual([]);
});
it('booleans', () => {
expect((0, _callRenderProp.callRenderProp)(false)).toEqual(false);
});
it('JSX literals', () => {
const Foo = () => _div || (_div = (0, _jsxRuntime.jsx)("div", {
children: "hello"
}));
expect((0, _callRenderProp.callRenderProp)((0, _jsxRuntime.jsx)(Foo, {}))).toStrictEqual((0, _jsxRuntime.jsx)(Foo, {}));
});
it('React classes', () => {
class Foo extends _react.Component {
render() {
return _div2 || (_div2 = (0, _jsxRuntime.jsx)("div", {
children: "hello"
}));
}
}
Foo.displayName = "Foo";
const Result = (0, _callRenderProp.callRenderProp)(Foo);
expect(Result).toStrictEqual((0, _jsxRuntime.jsx)(Foo, {}));
const _render = (0, _react2.render)(Result),
getByText = _render.getByText;
expect(getByText('hello')).toBeInTheDocument();
});
it('functions', () => {
const Baz = function () {
return 'some text';
};
const result = (0, _callRenderProp.callRenderProp)(Baz);
const _render2 = (0, _react2.render)((0, _jsxRuntime.jsx)("div", {
children: result
})),
getByText = _render2.getByText;
expect(getByText('some text')).toBeInTheDocument();
});
it('fat arrow functions', () => {
const Baz = () => 'some text';
// in this test we are trying to test that it works with fat arrow functions,
// but the babel config when we run these tests is currently configured
// to transpile fat arrow functions down to normal functions.
// Real, untranspiled, fat-arrow functions don't have a `prototype` but when
// they are transpiled down they do. So this next line is to make sure
// the thing we are testing really doesn't have a prototype like it would
// if it was a real untranspiled fat arrow function.
if (Baz.prototype) Baz.prototype = void 0;
const result = (0, _callRenderProp.callRenderProp)(Baz);
const _render3 = (0, _react2.render)((0, _jsxRuntime.jsx)("div", {
children: result
})),
getByText = _render3.getByText;
expect(getByText('some text')).toBeInTheDocument();
});
describe('passing props', () => {
it('should pass props correctly to functions', () => {
const someFunc = ({
shape
}) => (0, _jsxRuntime.jsx)("div", {
children: shape
});
const result = (0, _callRenderProp.callRenderProp)(someFunc, {
shape: 'rectangle'
});
const _render4 = (0, _react2.render)((0, _jsxRuntime.jsx)("div", {
children: result
})),
getByText = _render4.getByText;
expect(getByText('rectangle')).toBeInTheDocument();
});
it('should pass props correctly to React classes', () => {
class Foo extends _react.Component {
render() {
return (0, _jsxRuntime.jsx)("div", {
children: this.props.shape
});
}
}
Foo.displayName = "Foo";
Foo.propTypes = {
shape: _propTypes.default.oneOf(['circle', 'rectangle'])
};
Foo.defaultProps = {
shape: 'circle'
};
const result = (0, _callRenderProp.callRenderProp)(Foo, {
shape: 'rectangle'
});
const _render5 = (0, _react2.render)((0, _jsxRuntime.jsx)("div", {
children: result
})),
getByText = _render5.getByText;
expect(getByText('rectangle')).toBeInTheDocument();
});
});
});