@instructure/ui-test-utils
Version:
A UI testing library made by Instructure Inc.
127 lines (126 loc) • 4.57 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _uiTestSandbox = require("@instructure/ui-test-sandbox");
var _index = require("../index");
var _jsxRuntime = require("@emotion/react/jsx-runtime");
var _div, _Component2;
/*
* 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.
*/
class Component extends _react.Component {
test(cb) {
cb('test');
}
componentWillUnmount() {
if (typeof this.props.onUnmount === 'function') {
this.props.onUnmount();
}
}
render() {
const foo = this.props.foo;
return foo === 'baz' ? null : _div || (_div = (0, _jsxRuntime.jsx)("div", {
children: "Hello World"
}));
}
}
Component.displayName = 'Component';
Component.propTypes = {
foo: _propTypes.default.string,
onUnmount: _propTypes.default.func,
componentRef: _propTypes.default.func
};
Component.defaultProps = {
foo: null,
// or 'foo'?
onUnmount: null,
componentRef: void 0
};
describe('reactComponentWrapper', async () => {
it('should handle components that render `null`', async () => {
const subject = await _uiTestSandbox.ReactComponentWrapper.mount(_Component2 || (_Component2 = (0, _jsxRuntime.jsx)(Component, {
foo: "baz"
})));
(0, _index.expect)(subject.getDOMNode()).to.equal(null);
});
it('should allow a componentRef to be passed via props', async () => {
let component;
await _uiTestSandbox.ReactComponentWrapper.mount((0, _jsxRuntime.jsx)(Component, {
componentRef: el => {
component = el;
}
}));
const test = (0, _index.stub)();
component.test(test);
(0, _index.expect)(test).to.have.been.calledWith('test');
});
it('should omit componentRef when passing props to component', async () => {
let component;
await _uiTestSandbox.ReactComponentWrapper.mount((0, _jsxRuntime.jsx)(Component, {
componentRef: el => {
component = el;
}
}));
(0, _index.expect)(component.props.componentRef).to.not.exist();
});
it('should unmount', async () => {
const handleUnmount = (0, _index.stub)();
const subject = await _uiTestSandbox.ReactComponentWrapper.mount((0, _jsxRuntime.jsx)(Component, {
onUnmount: handleUnmount
}));
(0, _index.expect)(subject.getDOMNode()).to.have.text('Hello World');
await _uiTestSandbox.ReactComponentWrapper.unmount();
(0, _index.expect)(handleUnmount).to.have.been.calledOnce();
(0, _index.expect)(subject.getDOMNode()).to.equal(null);
});
describe('options', async () => {
it('should support setting props', async () => {
let component;
await _uiTestSandbox.ReactComponentWrapper.mount((0, _jsxRuntime.jsx)(Component, {
componentRef: el => {
component = el;
}
}), {
props: {
foo: 'bar'
}
});
(0, _index.expect)(component.props.foo).to.equal('bar');
});
});
describe('#setProps', async () => {
it('should support setting props', async () => {
let component;
const subject = await _uiTestSandbox.ReactComponentWrapper.mount((0, _jsxRuntime.jsx)(Component, {
componentRef: el => {
component = el;
}
}));
await subject.setProps({
foo: 'bar'
});
(0, _index.expect)(component.props.foo).to.equal('bar');
});
});
});
;