UNPKG

3.08 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import * as React from 'react';
4import * as ReactDOM from 'react-dom';
5import * as PropTypes from 'prop-types';
6import { mount as enzymeMount } from 'enzyme';
7/**
8 * Can't just mount <React.Fragment>{node}</React.Fragment>
9 * because that swallows wrapper.setProps
10 *
11 * why class component:
12 * https://github.com/airbnb/enzyme/issues/2043
13 */
14// eslint-disable-next-line react/prefer-stateless-function
15
16class Mode extends React.Component {
17 render() {
18 // Excess props will come from e.g. enzyme setProps
19 const _this$props = this.props,
20 {
21 __element,
22 __strict
23 } = _this$props,
24 other = _objectWithoutPropertiesLoose(_this$props, ["__element", "__strict"]);
25
26 const Component = __strict ? React.StrictMode : React.Fragment;
27 return /*#__PURE__*/React.createElement(Component, null, /*#__PURE__*/React.cloneElement(__element, other));
28 }
29
30}
31
32process.env.NODE_ENV !== "production" ? Mode.propTypes = {
33 /**
34 * this is essentially children. However we can't use children because then
35 * using `wrapper.setProps({ children })` would work differently if this component
36 * would be the root.
37 */
38 __element: PropTypes.element.isRequired,
39 __strict: PropTypes.bool.isRequired
40} : void 0;
41let warnedOnce = false; // Generate an enhanced mount function.
42
43export default function createMount(options = {}) {
44 if (!warnedOnce) {
45 warnedOnce = true;
46 console.warn(['Material-UI: the test utils are deprecated, they are no longer present in v5.', 'The helpers were designed to work with enzyme.', 'However, the tests of the core components were moved to react-testing-library.'].join('\n'));
47 }
48
49 const {
50 mount = enzymeMount,
51 strict: globalStrict
52 } = options,
53 globalEnzymeOptions = _objectWithoutPropertiesLoose(options, ["mount", "strict"]);
54
55 const attachTo = document.createElement('div');
56 attachTo.className = 'app';
57 attachTo.setAttribute('id', 'app');
58 document.body.insertBefore(attachTo, document.body.firstChild);
59
60 const mountWithContext = function mountWithContext(node, localOptions = {}) {
61 const {
62 disableUnnmount = false,
63 strict = globalStrict
64 } = localOptions,
65 localEnzymeOptions = _objectWithoutPropertiesLoose(localOptions, ["disableUnnmount", "strict"]);
66
67 if (!disableUnnmount) {
68 ReactDOM.unmountComponentAtNode(attachTo);
69 } // some tests require that no other components are in the tree
70 // e.g. when doing .instance(), .state() etc.
71
72
73 return mount(strict == null ? node : /*#__PURE__*/React.createElement(Mode, {
74 __element: node,
75 __strict: Boolean(strict)
76 }), _extends({
77 attachTo
78 }, globalEnzymeOptions, localEnzymeOptions));
79 };
80
81 mountWithContext.attachTo = attachTo;
82
83 mountWithContext.cleanUp = () => {
84 ReactDOM.unmountComponentAtNode(attachTo);
85 attachTo.parentElement.removeChild(attachTo);
86 };
87
88 return mountWithContext;
89}
\No newline at end of file