@mui/internal-test-utils 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/build/.tsbuildinfo +1 -1
  2. package/build/createRenderer.d.ts +3 -3
  3. package/build/createRenderer.d.ts.map +1 -1
  4. package/build/describeConformance.d.ts +0 -6
  5. package/build/describeConformance.d.ts.map +1 -1
  6. package/build/describeConformance.js +1 -19
  7. package/build/describeConformance.js.map +1 -1
  8. package/build/flushMicrotasks.js +1 -1
  9. package/build/flushMicrotasks.js.map +1 -1
  10. package/build/index.d.ts +0 -2
  11. package/build/index.d.ts.map +1 -1
  12. package/build/index.js +1 -6
  13. package/build/index.js.map +1 -1
  14. package/build/init.js +0 -6
  15. package/build/init.js.map +1 -1
  16. package/package.json +4 -9
  17. package/src/createRenderer.tsx +3 -3
  18. package/src/describeConformance.tsx +0 -18
  19. package/src/flushMicrotasks.ts +1 -1
  20. package/src/index.ts +0 -2
  21. package/src/init.js +0 -4
  22. package/tsconfig.json +1 -0
  23. package/build/createMount.d.ts +0 -9
  24. package/build/createMount.d.ts.map +0 -1
  25. package/build/createMount.js +0 -120
  26. package/build/createMount.js.map +0 -1
  27. package/build/findOutermostIntrinsic.d.ts +0 -14
  28. package/build/findOutermostIntrinsic.d.ts.map +0 -1
  29. package/build/findOutermostIntrinsic.js +0 -22
  30. package/build/findOutermostIntrinsic.js.map +0 -1
  31. package/build/findOutermostIntrinsic.test.d.ts +0 -2
  32. package/build/findOutermostIntrinsic.test.d.ts.map +0 -1
  33. package/build/findOutermostIntrinsic.test.js +0 -70
  34. package/build/findOutermostIntrinsic.test.js.map +0 -1
  35. package/build/until.d.ts +0 -2
  36. package/build/until.d.ts.map +0 -1
  37. package/build/until.js +0 -26
  38. package/build/until.js.map +0 -1
  39. package/build/until.test.d.ts +0 -2
  40. package/build/until.test.d.ts.map +0 -1
  41. package/build/until.test.js +0 -118
  42. package/build/until.test.js.map +0 -1
  43. package/src/createMount.tsx +0 -136
  44. package/src/findOutermostIntrinsic.test.js +0 -68
  45. package/src/findOutermostIntrinsic.ts +0 -19
  46. package/src/until.js +0 -28
  47. package/src/until.test.js +0 -120
@@ -1,136 +0,0 @@
1
- /* eslint-env mocha */
2
- import * as React from 'react';
3
- import * as ReactDOM from 'react-dom';
4
- import * as ReactDOMTestUtils from 'react-dom/test-utils';
5
- import { Test, Suite } from 'mocha';
6
- import { mount as enzymeMount, MountRendererProps } from 'enzyme';
7
-
8
- interface ModeProps {
9
- /**
10
- * this is essentially children. However, we can't use children because then
11
- * using `wrapper.setProps({ children })` would work differently if this component
12
- * would be the root.
13
- */
14
- __element: React.ReactElement<any>;
15
- __strict: boolean;
16
- }
17
-
18
- /**
19
- * Can't just mount <React.Fragment>{node}</React.Fragment>
20
- * because that swallows wrapper.setProps
21
- *
22
- * why class component:
23
- * https://github.com/airbnb/enzyme/issues/2043
24
- */
25
- // eslint-disable-next-line react/prefer-stateless-function
26
- class Mode extends React.Component<ModeProps> {
27
- render() {
28
- // Excess props will come from e.g. enzyme setProps
29
- // eslint-disable-next-line @typescript-eslint/naming-convention
30
- const { __element, __strict, ...other } = this.props;
31
- const Component = __strict ? React.StrictMode : React.Fragment;
32
-
33
- return <Component>{React.cloneElement(__element, other)}</Component>;
34
- }
35
- }
36
-
37
- interface CreateMountOptions extends MountRendererProps {
38
- mount?: typeof enzymeMount;
39
- strict?: boolean;
40
- }
41
- // Generate an enhanced mount function.
42
- export default function createMount(options: CreateMountOptions = {}) {
43
- const { mount = enzymeMount, strict: globalStrict = true, ...globalEnzymeOptions } = options;
44
-
45
- let container: HTMLElement | null = null;
46
-
47
- function computeTestName(test: Test | undefined) {
48
- let current: Test | Suite | undefined = test;
49
- const titles: string[] = [];
50
- while (current != null) {
51
- titles.push(current.title);
52
- current = current.parent;
53
- }
54
-
55
- return titles.filter(Boolean).reverse().join(' -> ');
56
- }
57
-
58
- // save stack to re-use in test-hooks
59
- const { stack: createMountStack } = new Error();
60
-
61
- /**
62
- * Flag whether `createMount` was called in a suite i.e. describe() block.
63
- * For legacy reasons `createMount` might accidentally be called in a before(Each) hook.
64
- */
65
- let wasCalledInSuite = false;
66
- before(() => {
67
- wasCalledInSuite = true;
68
- });
69
-
70
- beforeEach(() => {
71
- if (!wasCalledInSuite) {
72
- const error = new Error(
73
- 'Unable to run `before` hook for `createMount`. This usually indicates that `createMount` was called in a `before` hook instead of in a `describe()` block.',
74
- );
75
- error.stack = createMountStack;
76
- throw error;
77
- }
78
- });
79
-
80
- beforeEach(function beforeEachMountTest() {
81
- container = document.createElement('div');
82
- container.setAttribute('data-test', computeTestName(this.currentTest));
83
- document.body.insertBefore(container, document.body.firstChild);
84
- });
85
-
86
- afterEach(() => {
87
- ReactDOMTestUtils.act(() => {
88
- // eslint-disable-next-line react/no-deprecated
89
- ReactDOM.unmountComponentAtNode(container!);
90
- });
91
- container!.parentElement!.removeChild(container!);
92
- container = null;
93
- });
94
-
95
- const mountWithContext = function mountWithContext(
96
- node: React.ReactElement<any>,
97
- localOptions: Omit<CreateMountOptions, 'mount'> = {},
98
- ) {
99
- const { strict = globalStrict, ...localEnzymeOptions } = localOptions;
100
-
101
- if (container === null) {
102
- throw new Error(
103
- `Tried to mount without setup. Mounting inside before() is not allowed. Try mounting in beforeEach or better: in each test`,
104
- );
105
- }
106
- ReactDOMTestUtils.act(() => {
107
- // eslint-disable-next-line react/no-deprecated
108
- ReactDOM.unmountComponentAtNode(container!);
109
- });
110
-
111
- // some tests require that no other components are in the tree
112
- // for example when doing .instance(), .state() etc.
113
- const wrapper = mount(
114
- strict == null ? node : <Mode __element={node} __strict={Boolean(strict)} />,
115
- {
116
- attachTo: container,
117
- ...globalEnzymeOptions,
118
- ...localEnzymeOptions,
119
- },
120
- );
121
- const originalUnmount = wrapper.unmount;
122
-
123
- wrapper.unmount = () => {
124
- // flush effect cleanup functions
125
- ReactDOMTestUtils.act(() => {
126
- originalUnmount.call(wrapper);
127
- });
128
-
129
- return wrapper;
130
- };
131
-
132
- return wrapper;
133
- };
134
-
135
- return mountWithContext;
136
- }
@@ -1,68 +0,0 @@
1
- import * as React from 'react';
2
- import { expect } from 'chai';
3
- import createMount from './createMount';
4
- import findOutermostIntrinsic from './findOutermostIntrinsic';
5
-
6
- describe('findOutermostIntrinsic', () => {
7
- const mount = createMount({ strict: null });
8
- const expectIntrinsic = (node, expected) => {
9
- const wrapper = mount(node);
10
- const outermostIntrinsic = findOutermostIntrinsic(wrapper);
11
-
12
- if (expected === null) {
13
- expect(outermostIntrinsic.exists()).to.equal(false);
14
- } else {
15
- expect(outermostIntrinsic.type()).to.equal(expected);
16
- expect(outermostIntrinsic.type()).to.equal(
17
- outermostIntrinsic.getDOMNode().nodeName.toLowerCase(),
18
- );
19
- }
20
- };
21
- const Headless = ({ children }) => children;
22
-
23
- it('returns immediate DOM nodes', () => {
24
- expectIntrinsic(<div>Hello, World!</div>, 'div');
25
- });
26
-
27
- it('only returns the outermost', () => {
28
- expectIntrinsic(
29
- <span>
30
- <div>Hello, World!</div>
31
- </span>,
32
- 'span',
33
- );
34
- });
35
-
36
- it('ignores components', () => {
37
- expectIntrinsic(
38
- <Headless>
39
- <div>Hello, World!</div>
40
- </Headless>,
41
- 'div',
42
- );
43
- expectIntrinsic(
44
- <Headless>
45
- <Headless>
46
- <div>Hello, World!</div>
47
- </Headless>
48
- </Headless>,
49
- 'div',
50
- );
51
- expectIntrinsic(
52
- <Headless>
53
- <Headless>
54
- <div>
55
- <Headless>
56
- <span>Hello, World!</span>
57
- </Headless>
58
- </div>
59
- </Headless>
60
- </Headless>,
61
- 'div',
62
- );
63
- });
64
-
65
- it('can handle that no DOM node is rendered', () => {
66
- expectIntrinsic(<Headless>{false && <Headless />}</Headless>, null);
67
- });
68
- });
@@ -1,19 +0,0 @@
1
- import { ReactWrapper } from 'enzyme';
2
-
3
- /**
4
- * checks if a given react wrapper wraps an intrinsic element i.e. a DOM node
5
- * @param {import('enzyme').ReactWrapper} reactWrapper
6
- * @returns {boolean} true if the given reactWrapper wraps an intrinsic element
7
- */
8
- export function wrapsIntrinsicElement(reactWrapper: ReactWrapper): boolean {
9
- return typeof reactWrapper.type() === 'string';
10
- }
11
-
12
- /**
13
- * like ReactWrapper#getDOMNode() but returns a ReactWrapper
14
- * @param {import('enzyme').ReactWrapper} reactWrapper
15
- * @returns {import('enzyme').ReactWrapper} the wrapper for the outermost DOM node
16
- */
17
- export default function findOutermostIntrinsic(reactWrapper: ReactWrapper): ReactWrapper {
18
- return reactWrapper.findWhere((n) => n.exists() && wrapsIntrinsicElement(n)).first();
19
- }
package/src/until.js DELETED
@@ -1,28 +0,0 @@
1
- function shallowRecursively(wrapper, selector, { context, ...other }) {
2
- if (wrapper.isEmptyRender() || typeof wrapper.getElement().type === 'string') {
3
- return wrapper;
4
- }
5
-
6
- let newContext = context;
7
-
8
- const instance = wrapper.root().instance();
9
- // The instance can be null with a stateless functional component and react >= 16.
10
- if (instance && instance.getChildContext) {
11
- newContext = {
12
- ...context,
13
- ...instance.getChildContext(),
14
- };
15
- }
16
-
17
- const nextWrapper = wrapper.shallow({ context: newContext, ...other });
18
-
19
- if (selector && wrapper.is(selector)) {
20
- return nextWrapper;
21
- }
22
-
23
- return shallowRecursively(nextWrapper, selector, { context: newContext });
24
- }
25
-
26
- export default function until(selector, options = {}) {
27
- return this.single('until', () => shallowRecursively(this, selector, options));
28
- }
package/src/until.test.js DELETED
@@ -1,120 +0,0 @@
1
- import * as React from 'react';
2
- import { expect } from 'chai';
3
- import PropTypes from 'prop-types';
4
- import { shallow } from 'enzyme';
5
- import until from './until';
6
-
7
- function Div() {
8
- return <div />;
9
- }
10
- const hoc = (Component) =>
11
- function Wrapper() {
12
- return <Component />;
13
- };
14
-
15
- describe('until', () => {
16
- it('shallow renders the current wrapper one level deep', () => {
17
- const EnhancedDiv = hoc(Div);
18
- const wrapper = until.call(shallow(<EnhancedDiv />), 'Div');
19
- expect(wrapper.contains(<div />)).to.equal(true);
20
- });
21
-
22
- it('shallow renders the current wrapper several levels deep', () => {
23
- const EnhancedDiv = hoc(hoc(hoc(Div)));
24
- const wrapper = until.call(shallow(<EnhancedDiv />), 'Div');
25
- expect(wrapper.contains(<div />)).to.equal(true);
26
- });
27
-
28
- it('stops shallow rendering when the wrapper is empty', () => {
29
- const nullHoc = () => () => null;
30
- const EnhancedDiv = nullHoc();
31
- const wrapper = until.call(shallow(<EnhancedDiv />), 'Div');
32
- expect(wrapper.html()).to.equal(null);
33
- });
34
-
35
- it('shallow renders as much as possible when no selector is provided', () => {
36
- const EnhancedDiv = hoc(hoc(Div));
37
- const wrapper = until.call(shallow(<EnhancedDiv />));
38
- expect(wrapper.contains(<div />)).to.equal(true);
39
- });
40
-
41
- it('shallow renders the current wrapper even if the selector never matches', () => {
42
- const EnhancedDiv = hoc(Div);
43
- const wrapper = until.call(shallow(<EnhancedDiv />), 'NotDiv');
44
- expect(wrapper.contains(<div />)).to.equal(true);
45
- });
46
-
47
- it('stops shallow rendering when it encounters a HTML element', () => {
48
- const wrapper = until.call(
49
- shallow(
50
- <div>
51
- <Div />
52
- </div>,
53
- ),
54
- 'Div',
55
- );
56
- expect(
57
- wrapper.contains(
58
- <div>
59
- <Div />
60
- </div>,
61
- ),
62
- ).to.equal(true);
63
- });
64
-
65
- it('throws when until called on an empty wrapper', () => {
66
- expect(() => {
67
- until.call(shallow(<Div />).find('Foo'), 'div');
68
- }).to.throw(Error);
69
- });
70
-
71
- it('shallow renders non-root wrappers', () => {
72
- function Container() {
73
- return (
74
- <div>
75
- <Div />
76
- </div>
77
- );
78
- }
79
- const wrapper = until.call(shallow(<Container />).find(Div));
80
- expect(wrapper.contains(<div />)).to.equal(true);
81
- });
82
-
83
- // eslint-disable-next-line react/prefer-stateless-function
84
- class Foo extends React.Component {
85
- render() {
86
- return <Div />;
87
- }
88
- }
89
-
90
- Foo.contextTypes = {
91
- quux: PropTypes.bool.isRequired,
92
- };
93
-
94
- it('context propagation passes down context from the root component', () => {
95
- const EnhancedFoo = hoc(Foo);
96
- const options = { context: { quux: true } };
97
- const wrapper = until.call(shallow(<EnhancedFoo />, options), 'Foo', options);
98
- expect(wrapper.context('quux')).to.equal(true);
99
- expect(wrapper.contains(<Div />)).to.equal(true);
100
- });
101
-
102
- class Bar extends React.Component {
103
- static childContextTypes = { quux: PropTypes.bool };
104
-
105
- getChildContext() {
106
- return { quux: true };
107
- }
108
-
109
- render() {
110
- return <Foo />;
111
- }
112
- }
113
-
114
- it('context propagation passes down context from an intermediary component', () => {
115
- const EnhancedBar = hoc(Bar);
116
- const wrapper = until.call(shallow(<EnhancedBar />), 'Foo');
117
- expect(wrapper.context('quux')).to.equal(true);
118
- expect(wrapper.contains(<Div />)).to.equal(true);
119
- });
120
- });