@mui/internal-test-utils 1.0.0-dev.20240529-082515-213b5e33ab

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 (148) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +9 -0
  4. package/build/.tsbuildinfo +1 -0
  5. package/build/KarmaReporterReactProfiler.d.ts +51 -0
  6. package/build/KarmaReporterReactProfiler.d.ts.map +1 -0
  7. package/build/KarmaReporterReactProfiler.js +68 -0
  8. package/build/KarmaReporterReactProfiler.js.map +1 -0
  9. package/build/components.d.ts +36 -0
  10. package/build/components.d.ts.map +1 -0
  11. package/build/components.js +78 -0
  12. package/build/components.js.map +1 -0
  13. package/build/createDOM.d.ts +3 -0
  14. package/build/createDOM.d.ts.map +1 -0
  15. package/build/createDOM.js +60 -0
  16. package/build/createDOM.js.map +1 -0
  17. package/build/createDescribe.d.ts +8 -0
  18. package/build/createDescribe.d.ts.map +1 -0
  19. package/build/createDescribe.js +22 -0
  20. package/build/createDescribe.js.map +1 -0
  21. package/build/createMount.d.ts +9 -0
  22. package/build/createMount.d.ts.map +1 -0
  23. package/build/createMount.js +120 -0
  24. package/build/createMount.js.map +1 -0
  25. package/build/createRenderer.d.ts +231 -0
  26. package/build/createRenderer.d.ts.map +1 -0
  27. package/build/createRenderer.js +506 -0
  28. package/build/createRenderer.js.map +1 -0
  29. package/build/createRenderer.test.d.ts +2 -0
  30. package/build/createRenderer.test.d.ts.map +1 -0
  31. package/build/createRenderer.test.js +48 -0
  32. package/build/createRenderer.test.js.map +1 -0
  33. package/build/describeConformance.d.ts +152 -0
  34. package/build/describeConformance.d.ts.map +1 -0
  35. package/build/describeConformance.js +815 -0
  36. package/build/describeConformance.js.map +1 -0
  37. package/build/findOutermostIntrinsic.d.ts +14 -0
  38. package/build/findOutermostIntrinsic.d.ts.map +1 -0
  39. package/build/findOutermostIntrinsic.js +22 -0
  40. package/build/findOutermostIntrinsic.js.map +1 -0
  41. package/build/findOutermostIntrinsic.test.d.ts +2 -0
  42. package/build/findOutermostIntrinsic.test.d.ts.map +1 -0
  43. package/build/findOutermostIntrinsic.test.js +70 -0
  44. package/build/findOutermostIntrinsic.test.js.map +1 -0
  45. package/build/fireDiscreteEvent.d.ts +7 -0
  46. package/build/fireDiscreteEvent.d.ts.map +1 -0
  47. package/build/fireDiscreteEvent.js +98 -0
  48. package/build/fireDiscreteEvent.js.map +1 -0
  49. package/build/flushMicrotasks.d.ts +2 -0
  50. package/build/flushMicrotasks.d.ts.map +1 -0
  51. package/build/flushMicrotasks.js +11 -0
  52. package/build/flushMicrotasks.js.map +1 -0
  53. package/build/focusVisible.d.ts +7 -0
  54. package/build/focusVisible.d.ts.map +1 -0
  55. package/build/focusVisible.js +33 -0
  56. package/build/focusVisible.js.map +1 -0
  57. package/build/index.d.ts +18 -0
  58. package/build/index.d.ts.map +1 -0
  59. package/build/index.js +59 -0
  60. package/build/index.js.map +1 -0
  61. package/build/init.d.ts +2 -0
  62. package/build/init.d.ts.map +1 -0
  63. package/build/init.js +42 -0
  64. package/build/init.js.map +1 -0
  65. package/build/initMatchers.d.ts +81 -0
  66. package/build/initMatchers.d.ts.map +1 -0
  67. package/build/initMatchers.js +431 -0
  68. package/build/initMatchers.js.map +1 -0
  69. package/build/initMatchers.test.d.ts +2 -0
  70. package/build/initMatchers.test.d.ts.map +1 -0
  71. package/build/initMatchers.test.js +103 -0
  72. package/build/initMatchers.test.js.map +1 -0
  73. package/build/initPlaywrightMatchers.d.ts +25 -0
  74. package/build/initPlaywrightMatchers.d.ts.map +1 -0
  75. package/build/initPlaywrightMatchers.js +63 -0
  76. package/build/initPlaywrightMatchers.js.map +1 -0
  77. package/build/mochaHooks.d.ts +25 -0
  78. package/build/mochaHooks.d.ts.map +1 -0
  79. package/build/mochaHooks.js +171 -0
  80. package/build/mochaHooks.js.map +1 -0
  81. package/build/mochaHooks.test.d.ts +2 -0
  82. package/build/mochaHooks.test.d.ts.map +1 -0
  83. package/build/mochaHooks.test.js +112 -0
  84. package/build/mochaHooks.test.js.map +1 -0
  85. package/build/setup.d.ts +2 -0
  86. package/build/setup.d.ts.map +1 -0
  87. package/build/setup.js +10 -0
  88. package/build/setup.js.map +1 -0
  89. package/build/setupBabel.d.ts +2 -0
  90. package/build/setupBabel.d.ts.map +1 -0
  91. package/build/setupBabel.js +5 -0
  92. package/build/setupBabel.js.map +1 -0
  93. package/build/setupBabelPlaywright.d.ts +2 -0
  94. package/build/setupBabelPlaywright.d.ts.map +1 -0
  95. package/build/setupBabelPlaywright.js +11 -0
  96. package/build/setupBabelPlaywright.js.map +1 -0
  97. package/build/setupJSDOM.d.ts +7 -0
  98. package/build/setupJSDOM.d.ts.map +1 -0
  99. package/build/setupJSDOM.js +17 -0
  100. package/build/setupJSDOM.js.map +1 -0
  101. package/build/setupKarma.d.ts +2 -0
  102. package/build/setupKarma.d.ts.map +1 -0
  103. package/build/setupKarma.js +56 -0
  104. package/build/setupKarma.js.map +1 -0
  105. package/build/until.d.ts +2 -0
  106. package/build/until.d.ts.map +1 -0
  107. package/build/until.js +26 -0
  108. package/build/until.js.map +1 -0
  109. package/build/until.test.d.ts +2 -0
  110. package/build/until.test.d.ts.map +1 -0
  111. package/build/until.test.js +118 -0
  112. package/build/until.test.js.map +1 -0
  113. package/build/userEvent.d.ts +8 -0
  114. package/build/userEvent.d.ts.map +1 -0
  115. package/build/userEvent.js +61 -0
  116. package/build/userEvent.js.map +1 -0
  117. package/package.json +76 -0
  118. package/src/KarmaReporterReactProfiler.js +81 -0
  119. package/src/components.tsx +61 -0
  120. package/src/createDOM.d.ts +9 -0
  121. package/src/createDOM.js +67 -0
  122. package/src/createDescribe.ts +31 -0
  123. package/src/createMount.tsx +136 -0
  124. package/src/createRenderer.test.js +31 -0
  125. package/src/createRenderer.tsx +737 -0
  126. package/src/describeConformance.tsx +1117 -0
  127. package/src/findOutermostIntrinsic.test.js +68 -0
  128. package/src/findOutermostIntrinsic.ts +19 -0
  129. package/src/fireDiscreteEvent.ts +76 -0
  130. package/src/flushMicrotasks.ts +8 -0
  131. package/src/focusVisible.ts +28 -0
  132. package/src/index.ts +24 -0
  133. package/src/init.js +15 -0
  134. package/src/initMatchers.test.js +126 -0
  135. package/src/initMatchers.ts +658 -0
  136. package/src/initPlaywrightMatchers.ts +96 -0
  137. package/src/mochaHooks.js +209 -0
  138. package/src/mochaHooks.test.js +113 -0
  139. package/src/setup.js +10 -0
  140. package/src/setupBabel.js +3 -0
  141. package/src/setupBabelPlaywright.js +10 -0
  142. package/src/setupJSDOM.js +20 -0
  143. package/src/setupKarma.js +65 -0
  144. package/src/until.js +28 -0
  145. package/src/until.test.js +120 -0
  146. package/src/userEvent.ts +35 -0
  147. package/tsconfig.build.json +17 -0
  148. package/tsconfig.json +12 -0
@@ -0,0 +1,815 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.testReactTestRenderer = exports.testRootClass = exports.describeRef = exports.testPropsSpread = exports.testComponentProp = exports.testClassName = exports.randomStringValue = exports.findRootComponent = void 0;
30
+ /* eslint-env mocha */
31
+ const React = __importStar(require("react"));
32
+ const chai_1 = require("chai");
33
+ const react_test_renderer_1 = __importDefault(require("react-test-renderer"));
34
+ const createMount_1 = __importDefault(require("./createMount"));
35
+ const createDescribe_1 = __importDefault(require("./createDescribe"));
36
+ const findOutermostIntrinsic_1 = __importDefault(require("./findOutermostIntrinsic"));
37
+ function capitalize(string) {
38
+ return string.charAt(0).toUpperCase() + string.slice(1);
39
+ }
40
+ /**
41
+ * @param {object} node
42
+ * @returns
43
+ */
44
+ function assertDOMNode(node) {
45
+ // duck typing a DOM node
46
+ (0, chai_1.expect)(typeof node.nodeName).to.equal('string');
47
+ }
48
+ /**
49
+ * Utility method to make assertions about the ref on an element
50
+ * The element should have a component wrapped in withStyles as the root
51
+ */
52
+ function testRef(element, mount, onRef = assertDOMNode) {
53
+ if (!mount) {
54
+ throwMissingPropError('mount');
55
+ }
56
+ const ref = React.createRef();
57
+ const wrapper = mount(React.createElement(React.Fragment, null, React.cloneElement(element, { ref })));
58
+ onRef(ref.current, wrapper);
59
+ }
60
+ /**
61
+ * Glossary
62
+ * - root component:
63
+ * - renders the outermost host component
64
+ * - has the `root` class if the component has one
65
+ * - excess props are spread to this component
66
+ * - has the type of `inheritComponent`
67
+ */
68
+ /**
69
+ * Returns the component with the same constructor as `component` that renders
70
+ * the outermost host
71
+ */
72
+ function findRootComponent(wrapper, component) {
73
+ const outermostHostElement = (0, findOutermostIntrinsic_1.default)(wrapper).getElement();
74
+ return wrapper.find(component).filterWhere((componentWrapper) => {
75
+ return componentWrapper.contains(outermostHostElement);
76
+ });
77
+ }
78
+ exports.findRootComponent = findRootComponent;
79
+ function randomStringValue() {
80
+ return `s${Math.random().toString(36).slice(2)}`;
81
+ }
82
+ exports.randomStringValue = randomStringValue;
83
+ function throwMissingPropError(field) {
84
+ throw new Error(`missing "${field}" in options
85
+
86
+ > describeConformance(element, () => options)
87
+ `);
88
+ }
89
+ /**
90
+ * MUI components have a `className` prop. The `className` is applied to
91
+ * the root component.
92
+ */
93
+ function testClassName(element, getOptions) {
94
+ it('applies the className to the root component', () => {
95
+ const { mount } = getOptions();
96
+ if (!mount) {
97
+ throwMissingPropError('mount');
98
+ }
99
+ const className = randomStringValue();
100
+ const wrapper = mount(React.cloneElement(element, { className }));
101
+ (0, chai_1.expect)((0, findOutermostIntrinsic_1.default)(wrapper).instance()).to.have.class(className);
102
+ });
103
+ }
104
+ exports.testClassName = testClassName;
105
+ /**
106
+ * MUI components have a `component` prop that allows rendering a different
107
+ * Component from @inheritComponent
108
+ */
109
+ function testComponentProp(element, getOptions) {
110
+ describe('prop: component', () => {
111
+ it('can render another root component with the `component` prop', () => {
112
+ const { mount, testComponentPropWith: component = 'em' } = getOptions();
113
+ if (!mount) {
114
+ throwMissingPropError('mount');
115
+ }
116
+ const wrapper = mount(React.cloneElement(element, { component }));
117
+ (0, chai_1.expect)(findRootComponent(wrapper, component).exists()).to.equal(true);
118
+ });
119
+ });
120
+ }
121
+ exports.testComponentProp = testComponentProp;
122
+ /**
123
+ * MUI components can spread additional props to a documented component.
124
+ */
125
+ function testPropsSpread(element, getOptions) {
126
+ it(`spreads props to the root component`, () => {
127
+ // type def in ConformanceOptions
128
+ const { inheritComponent, mount } = getOptions();
129
+ if (!mount) {
130
+ throwMissingPropError('mount');
131
+ }
132
+ if (inheritComponent === undefined) {
133
+ throw new TypeError('Unable to test props spread without `inheritComponent`. Either skip the test or pass a React element type.');
134
+ }
135
+ const testProp = 'data-test-props-spread';
136
+ const value = randomStringValue();
137
+ const wrapper = mount(React.cloneElement(element, { [testProp]: value }));
138
+ const root = findRootComponent(wrapper, inheritComponent);
139
+ (0, chai_1.expect)(root.props()).to.have.property(testProp, value);
140
+ });
141
+ }
142
+ exports.testPropsSpread = testPropsSpread;
143
+ /**
144
+ * Tests that the `ref` of a component will return the correct instance
145
+ *
146
+ * This is determined by a given constructor i.e. a React.Component or HTMLElement for
147
+ * components that forward their ref and attach it to a host component.
148
+ */
149
+ function describeRef(element, getOptions) {
150
+ describe('ref', () => {
151
+ it(`attaches the ref`, () => {
152
+ // type def in ConformanceOptions
153
+ const { inheritComponent, mount, refInstanceof } = getOptions();
154
+ testRef(element, mount, (instance, wrapper) => {
155
+ (0, chai_1.expect)(instance).to.be.instanceof(refInstanceof);
156
+ if (inheritComponent !== undefined && instance.nodeType === 1) {
157
+ const rootHost = (0, findOutermostIntrinsic_1.default)(wrapper);
158
+ (0, chai_1.expect)(instance).to.equal(rootHost.instance());
159
+ }
160
+ });
161
+ });
162
+ });
163
+ }
164
+ exports.describeRef = describeRef;
165
+ /**
166
+ * Tests that the root component has the root class
167
+ */
168
+ function testRootClass(element, getOptions) {
169
+ it('applies the root class to the root component if it has this class', () => {
170
+ const { classes, render, skip } = getOptions();
171
+ if (classes.root == null) {
172
+ return;
173
+ }
174
+ const className = randomStringValue();
175
+ const classesRootClassname = randomStringValue();
176
+ const { container } = render(React.cloneElement(element, {
177
+ className,
178
+ classes: { ...classes, root: `${classes.root} ${classesRootClassname}` },
179
+ }));
180
+ // we established that the root component renders the outermost host previously. We immediately
181
+ // jump to the host component because some components pass the `root` class
182
+ // to the `classes` prop of the root component.
183
+ // https://github.com/mui/material-ui/blob/f9896bcd129a1209153106296b3d2487547ba205/packages/material-ui/src/OutlinedInput/OutlinedInput.js#L101
184
+ (0, chai_1.expect)(container.firstChild).to.have.class(className);
185
+ (0, chai_1.expect)(container.firstChild).to.have.class(classes.root);
186
+ (0, chai_1.expect)(document.querySelectorAll(`.${classes.root}`).length).to.equal(1);
187
+ // classes test only for @mui/material
188
+ if (!skip || !skip.includes('classesRoot')) {
189
+ // Test that classes prop works
190
+ (0, chai_1.expect)(container.firstChild).to.have.class(classesRootClassname);
191
+ // Test that `classes` does not spread to DOM
192
+ (0, chai_1.expect)(document.querySelectorAll('[classes]').length).to.equal(0);
193
+ }
194
+ });
195
+ }
196
+ exports.testRootClass = testRootClass;
197
+ /**
198
+ * Tests that the component can be rendered with react-test-renderer.
199
+ * This is important for snapshot testing with Jest (even if we don't encourage it).
200
+ */
201
+ function testReactTestRenderer(element) {
202
+ it('should render without errors in ReactTestRenderer', () => {
203
+ react_test_renderer_1.default.act(() => {
204
+ react_test_renderer_1.default.create(element, {
205
+ createNodeMock: (node) => {
206
+ return document.createElement(node.type);
207
+ },
208
+ });
209
+ });
210
+ });
211
+ }
212
+ exports.testReactTestRenderer = testReactTestRenderer;
213
+ function forEachSlot(slots, callback) {
214
+ if (!slots) {
215
+ return;
216
+ }
217
+ const slotNames = Object.keys(slots);
218
+ slotNames.forEach((slotName) => {
219
+ const slot = slots[slotName];
220
+ callback(slotName, slot);
221
+ });
222
+ }
223
+ function testSlotsProp(element, getOptions) {
224
+ const { render, slots, testLegacyComponentsProp } = getOptions();
225
+ const CustomComponent = React.forwardRef(({ className, children }, ref) => (React.createElement("i", { className: className, ref: ref, "data-testid": "custom" }, children)));
226
+ forEachSlot(slots, (slotName, slotOptions) => {
227
+ it(`allows overriding the ${slotName} slot with a component using the slots.${slotName} prop`, () => {
228
+ if (!render) {
229
+ throwMissingPropError('render');
230
+ }
231
+ const slotComponent = slotOptions.testWithComponent ?? CustomComponent;
232
+ const components = {
233
+ [slotName]: slotComponent,
234
+ };
235
+ const { queryByTestId } = render(React.cloneElement(element, { slots: components }));
236
+ const renderedElement = queryByTestId('custom');
237
+ (0, chai_1.expect)(renderedElement).not.to.equal(null);
238
+ if (slotOptions.expectedClassName) {
239
+ (0, chai_1.expect)(renderedElement).to.have.class(slotOptions.expectedClassName);
240
+ }
241
+ });
242
+ if (slotOptions.testWithElement !== null) {
243
+ it(`allows overriding the ${slotName} slot with an element using the slots.${slotName} prop`, () => {
244
+ if (!render) {
245
+ throwMissingPropError('render');
246
+ }
247
+ const slotElement = slotOptions.testWithElement ?? 'i';
248
+ const components = {
249
+ [slotName]: slotElement,
250
+ };
251
+ const slotProps = {
252
+ [slotName]: {
253
+ 'data-testid': 'customized',
254
+ },
255
+ };
256
+ const { queryByTestId } = render(React.cloneElement(element, { slots: components, slotProps }));
257
+ const renderedElement = queryByTestId('customized');
258
+ (0, chai_1.expect)(renderedElement).not.to.equal(null);
259
+ (0, chai_1.expect)(renderedElement.nodeName.toLowerCase()).to.equal(slotElement);
260
+ if (slotOptions.expectedClassName) {
261
+ (0, chai_1.expect)(renderedElement).to.have.class(slotOptions.expectedClassName);
262
+ }
263
+ });
264
+ }
265
+ // For testing Material UI components v5, and v6. Likely to be removed in v7.
266
+ if (testLegacyComponentsProp) {
267
+ it(`allows overriding the ${slotName} slot with a component using the components.${capitalize(slotName)} prop`, () => {
268
+ if (!render) {
269
+ throwMissingPropError('render');
270
+ }
271
+ const slotComponent = slotOptions.testWithComponent ?? CustomComponent;
272
+ const components = {
273
+ [capitalize(slotName)]: slotComponent,
274
+ };
275
+ const { queryByTestId } = render(React.cloneElement(element, { components }));
276
+ const renderedElement = queryByTestId('custom');
277
+ (0, chai_1.expect)(renderedElement).not.to.equal(null);
278
+ if (slotOptions.expectedClassName) {
279
+ (0, chai_1.expect)(renderedElement).to.have.class(slotOptions.expectedClassName);
280
+ }
281
+ });
282
+ it(`prioritizes the 'slots.${slotName}' over components.${capitalize(slotName)} if both are defined`, () => {
283
+ if (!render) {
284
+ throwMissingPropError('render');
285
+ }
286
+ const ComponentForComponentsProp = React.forwardRef(({ children }, ref) => {
287
+ const SlotComponent = slotOptions.testWithComponent ?? 'div';
288
+ return (React.createElement(SlotComponent, { ref: ref, "data-testid": "from-components" }, children));
289
+ });
290
+ const ComponentForSlotsProp = React.forwardRef(({ children }, ref) => {
291
+ const SlotComponent = slotOptions.testWithComponent ?? 'div';
292
+ return (React.createElement(SlotComponent, { ref: ref, "data-testid": "from-slots" }, children));
293
+ });
294
+ const components = {
295
+ [capitalize(slotName)]: ComponentForComponentsProp,
296
+ };
297
+ const slotOverrides = {
298
+ [slotName]: ComponentForSlotsProp,
299
+ };
300
+ const { queryByTestId } = render(React.cloneElement(element, { components, slots: slotOverrides }));
301
+ (0, chai_1.expect)(queryByTestId('from-slots')).not.to.equal(null);
302
+ (0, chai_1.expect)(queryByTestId('from-components')).to.equal(null);
303
+ });
304
+ if (slotOptions.testWithElement !== null) {
305
+ it(`allows overriding the ${slotName} slot with an element using the components.${capitalize(slotName)} prop`, () => {
306
+ if (!render) {
307
+ throwMissingPropError('render');
308
+ }
309
+ const slotElement = slotOptions.testWithElement ?? 'i';
310
+ const components = {
311
+ [capitalize(slotName)]: slotElement,
312
+ };
313
+ const componentsProps = {
314
+ [slotName]: {
315
+ 'data-testid': 'customized',
316
+ },
317
+ };
318
+ const { queryByTestId } = render(React.cloneElement(element, { components, componentsProps }));
319
+ const renderedElement = queryByTestId('customized');
320
+ (0, chai_1.expect)(renderedElement).not.to.equal(null);
321
+ (0, chai_1.expect)(renderedElement.nodeName.toLowerCase()).to.equal(slotElement);
322
+ if (slotOptions.expectedClassName) {
323
+ (0, chai_1.expect)(renderedElement).to.have.class(slotOptions.expectedClassName);
324
+ }
325
+ });
326
+ }
327
+ }
328
+ });
329
+ }
330
+ function testSlotPropsProp(element, getOptions) {
331
+ const { render, slots, testLegacyComponentsProp } = getOptions();
332
+ if (!render) {
333
+ throwMissingPropError('render');
334
+ }
335
+ forEachSlot(slots, (slotName, slotOptions) => {
336
+ it(`sets custom properties on the ${slotName} slot's element with the slotProps.${slotName} prop`, () => {
337
+ const slotProps = {
338
+ [slotName]: {
339
+ 'data-testid': 'custom',
340
+ },
341
+ };
342
+ const { queryByTestId } = render(React.cloneElement(element, { slotProps }));
343
+ const slotComponent = queryByTestId('custom');
344
+ (0, chai_1.expect)(slotComponent).not.to.equal(null);
345
+ if (slotOptions.expectedClassName) {
346
+ (0, chai_1.expect)(slotComponent).to.have.class(slotOptions.expectedClassName);
347
+ }
348
+ });
349
+ if (slotOptions.expectedClassName) {
350
+ it(`merges the class names provided in slotsProps.${slotName} with the built-in ones`, () => {
351
+ const slotProps = {
352
+ [slotName]: {
353
+ 'data-testid': 'custom',
354
+ className: randomStringValue(),
355
+ },
356
+ };
357
+ const { getByTestId } = render(React.cloneElement(element, { slotProps }));
358
+ (0, chai_1.expect)(getByTestId('custom')).to.have.class(slotOptions.expectedClassName);
359
+ (0, chai_1.expect)(getByTestId('custom')).to.have.class(slotProps[slotName].className);
360
+ });
361
+ }
362
+ if (testLegacyComponentsProp) {
363
+ it(`sets custom properties on the ${slotName} slot's element with the componentsProps.${slotName} prop`, () => {
364
+ const componentsProps = {
365
+ [slotName]: {
366
+ 'data-testid': 'custom',
367
+ },
368
+ };
369
+ const { queryByTestId } = render(React.cloneElement(element, { componentsProps }));
370
+ const slotComponent = queryByTestId('custom');
371
+ (0, chai_1.expect)(slotComponent).not.to.equal(null);
372
+ if (slotOptions.expectedClassName) {
373
+ (0, chai_1.expect)(slotComponent).to.have.class(slotOptions.expectedClassName);
374
+ }
375
+ });
376
+ it(`prioritizes the 'slotProps.${slotName}' over componentsProps.${slotName} if both are defined`, () => {
377
+ const componentsProps = {
378
+ [slotName]: {
379
+ 'data-testid': 'custom',
380
+ 'data-from-components-props': 'true',
381
+ },
382
+ };
383
+ const slotProps = {
384
+ [slotName]: {
385
+ 'data-testid': 'custom',
386
+ 'data-from-slot-props': 'true',
387
+ },
388
+ };
389
+ const { queryByTestId } = render(React.cloneElement(element, { componentsProps, slotProps }));
390
+ const slotComponent = queryByTestId('custom');
391
+ (0, chai_1.expect)(slotComponent).to.have.attribute('data-from-slot-props', 'true');
392
+ (0, chai_1.expect)(slotComponent).not.to.have.attribute('data-from-components-props');
393
+ });
394
+ }
395
+ });
396
+ }
397
+ function testSlotPropsCallback(element, getOptions) {
398
+ const { render, slots } = getOptions();
399
+ if (!render) {
400
+ throwMissingPropError('render');
401
+ }
402
+ forEachSlot(slots, (slotName) => {
403
+ it(`sets custom properties on the ${slotName} slot's element with the slotProps.${slotName} callback`, () => {
404
+ const slotProps = {
405
+ [slotName]: (ownerState) => ({
406
+ 'data-testid': ownerState.className,
407
+ }),
408
+ };
409
+ const { queryByTestId } = render(React.cloneElement(element, { slotProps, className: 'custom' }));
410
+ const slotComponent = queryByTestId('custom');
411
+ (0, chai_1.expect)(slotComponent).not.to.equal(null);
412
+ });
413
+ });
414
+ }
415
+ /**
416
+ * MUI components have a `components` prop that allows rendering a different
417
+ * Components from @inheritComponent
418
+ */
419
+ function testComponentsProp(element, getOptions) {
420
+ describe('prop components:', () => {
421
+ it('can render another root component with the `components` prop', () => {
422
+ const { mount, testComponentsRootPropWith: component = 'em' } = getOptions();
423
+ if (!mount) {
424
+ throwMissingPropError('mount');
425
+ }
426
+ const wrapper = mount(React.cloneElement(element, { components: { Root: component } }));
427
+ (0, chai_1.expect)(findRootComponent(wrapper, component).exists()).to.equal(true);
428
+ });
429
+ });
430
+ }
431
+ /**
432
+ * MUI theme has a components section that allows specifying default props.
433
+ * Components from @inheritComponent
434
+ */
435
+ function testThemeDefaultProps(element, getOptions) {
436
+ describe('theme default components:', () => {
437
+ it("respect theme's defaultProps", () => {
438
+ const testProp = 'data-id';
439
+ const { muiName, render, ThemeProvider, createTheme } = getOptions();
440
+ if (!muiName) {
441
+ throwMissingPropError('muiName');
442
+ }
443
+ if (!render) {
444
+ throwMissingPropError('render');
445
+ }
446
+ if (!ThemeProvider) {
447
+ throwMissingPropError('ThemeProvider');
448
+ }
449
+ if (!createTheme) {
450
+ throwMissingPropError('createTheme');
451
+ }
452
+ const theme = createTheme({
453
+ components: {
454
+ [muiName]: {
455
+ defaultProps: {
456
+ [testProp]: 'testProp',
457
+ },
458
+ },
459
+ },
460
+ });
461
+ const { container } = render(React.createElement(ThemeProvider, { theme: theme }, element));
462
+ (0, chai_1.expect)(container.firstChild).to.have.attribute(testProp, 'testProp');
463
+ });
464
+ });
465
+ }
466
+ /**
467
+ * MUI theme has a components section that allows specifying style overrides.
468
+ * Components from @inheritComponent
469
+ */
470
+ function testThemeStyleOverrides(element, getOptions) {
471
+ describe('theme style overrides:', () => {
472
+ it("respect theme's styleOverrides custom state", function test() {
473
+ if (/jsdom/.test(window.navigator.userAgent)) {
474
+ this.skip();
475
+ }
476
+ const { muiName, testStateOverrides, render, ThemeProvider, createTheme } = getOptions();
477
+ if (!testStateOverrides) {
478
+ return;
479
+ }
480
+ if (!muiName) {
481
+ throwMissingPropError('muiName');
482
+ }
483
+ if (!render) {
484
+ throwMissingPropError('render');
485
+ }
486
+ if (!ThemeProvider) {
487
+ throwMissingPropError('ThemeProvider');
488
+ }
489
+ if (!createTheme) {
490
+ throwMissingPropError('createTheme');
491
+ }
492
+ const testStyle = {
493
+ marginTop: '13px',
494
+ };
495
+ const theme = createTheme({
496
+ components: {
497
+ [muiName]: {
498
+ styleOverrides: {
499
+ [testStateOverrides.styleKey]: testStyle,
500
+ },
501
+ },
502
+ },
503
+ });
504
+ if (!testStateOverrides.prop) {
505
+ return;
506
+ }
507
+ const { container } = render(React.createElement(ThemeProvider, { theme: theme }, React.cloneElement(element, {
508
+ [testStateOverrides.prop]: testStateOverrides.value,
509
+ })));
510
+ (0, chai_1.expect)(container.firstChild).to.toHaveComputedStyle(testStyle);
511
+ });
512
+ it("respect theme's styleOverrides slots", function test() {
513
+ if (/jsdom/.test(window.navigator.userAgent)) {
514
+ this.skip();
515
+ }
516
+ const { muiName, testDeepOverrides, testRootOverrides = { slotName: 'root' }, render, ThemeProvider, createTheme, } = getOptions();
517
+ if (!ThemeProvider) {
518
+ throwMissingPropError('ThemeProvider');
519
+ }
520
+ if (!createTheme) {
521
+ throwMissingPropError('createTheme');
522
+ }
523
+ const testStyle = {
524
+ mixBlendMode: 'darken',
525
+ };
526
+ function resolveDeepOverrides(callback) {
527
+ if (!testDeepOverrides) {
528
+ return {};
529
+ }
530
+ const styles = {};
531
+ if (Array.isArray(testDeepOverrides)) {
532
+ testDeepOverrides.forEach((slot) => {
533
+ callback(styles, slot);
534
+ });
535
+ }
536
+ else {
537
+ callback(styles, testDeepOverrides);
538
+ }
539
+ return styles;
540
+ }
541
+ const theme = createTheme({
542
+ components: {
543
+ [muiName]: {
544
+ styleOverrides: {
545
+ [testRootOverrides.slotName]: {
546
+ ...testStyle,
547
+ ...resolveDeepOverrides((styles, slot) => {
548
+ styles[`& .${slot.slotClassName}`] = {
549
+ fontVariantCaps: 'all-petite-caps',
550
+ };
551
+ }),
552
+ },
553
+ ...resolveDeepOverrides((styles, slot) => {
554
+ styles[slot.slotName] = {
555
+ mixBlendMode: 'darken',
556
+ };
557
+ }),
558
+ },
559
+ },
560
+ },
561
+ });
562
+ const { container, setProps } = render(React.createElement(ThemeProvider, { theme: theme }, element));
563
+ if (testRootOverrides.slotClassName) {
564
+ (0, chai_1.expect)(document.querySelector(`.${testRootOverrides.slotClassName}`)).to.toHaveComputedStyle(testStyle);
565
+ }
566
+ else {
567
+ (0, chai_1.expect)(container.firstChild).to.toHaveComputedStyle(testStyle);
568
+ }
569
+ if (testDeepOverrides) {
570
+ (Array.isArray(testDeepOverrides) ? testDeepOverrides : [testDeepOverrides]).forEach((slot) => {
571
+ (0, chai_1.expect)(document.querySelector(`.${slot.slotClassName}`)).to.toHaveComputedStyle({
572
+ fontVariantCaps: 'all-petite-caps',
573
+ mixBlendMode: 'darken',
574
+ });
575
+ });
576
+ const themeWithoutRootOverrides = createTheme({
577
+ components: {
578
+ [muiName]: {
579
+ styleOverrides: {
580
+ ...resolveDeepOverrides((styles, slot) => {
581
+ styles[slot.slotName] = testStyle;
582
+ }),
583
+ },
584
+ },
585
+ },
586
+ });
587
+ setProps({ theme: themeWithoutRootOverrides });
588
+ (Array.isArray(testDeepOverrides) ? testDeepOverrides : [testDeepOverrides]).forEach((slot) => {
589
+ (0, chai_1.expect)(document.querySelector(`.${slot.slotClassName}`)).to.toHaveComputedStyle(testStyle);
590
+ });
591
+ }
592
+ });
593
+ it('overrideStyles does not replace each other in slots', function test() {
594
+ if (/jsdom/.test(window.navigator.userAgent)) {
595
+ this.skip();
596
+ }
597
+ const { muiName, classes, testStateOverrides, render, ThemeProvider, createTheme } = getOptions();
598
+ if (!ThemeProvider) {
599
+ throwMissingPropError('ThemeProvider');
600
+ }
601
+ if (!createTheme) {
602
+ throwMissingPropError('createTheme');
603
+ }
604
+ const classKeys = Object.keys(classes);
605
+ // only test the component that has `root` and other classKey
606
+ if (!testStateOverrides || !classKeys.includes('root') || classKeys.length === 1) {
607
+ return;
608
+ }
609
+ // `styleKey` in some tests is `foo` or `bar`, so need to check if it is a valid classKey.
610
+ const isStyleKeyExists = classKeys.indexOf(testStateOverrides.styleKey) !== -1;
611
+ if (!isStyleKeyExists) {
612
+ return;
613
+ }
614
+ const theme = createTheme({
615
+ components: {
616
+ [muiName]: {
617
+ styleOverrides: {
618
+ root: {
619
+ [`&.${classes.root}`]: {
620
+ filter: 'blur(1px)',
621
+ mixBlendMode: 'darken',
622
+ },
623
+ },
624
+ ...(testStateOverrides && {
625
+ [testStateOverrides.styleKey]: {
626
+ [`&.${classes.root}`]: {
627
+ mixBlendMode: 'color',
628
+ },
629
+ },
630
+ }),
631
+ },
632
+ },
633
+ },
634
+ });
635
+ if (!testStateOverrides.prop) {
636
+ return;
637
+ }
638
+ render(React.createElement(ThemeProvider, { theme: theme }, React.cloneElement(element, {
639
+ [testStateOverrides.prop]: testStateOverrides.value,
640
+ })));
641
+ (0, chai_1.expect)(document.querySelector(`.${classes.root}`)).toHaveComputedStyle({
642
+ filter: 'blur(1px)', // still valid in root
643
+ mixBlendMode: 'color', // overridden by `styleKey`
644
+ });
645
+ });
646
+ });
647
+ }
648
+ /**
649
+ * MUI theme has a components section that allows specifying custom variants.
650
+ * Components from @inheritComponent
651
+ */
652
+ function testThemeVariants(element, getOptions) {
653
+ describe('theme variants:', () => {
654
+ it("respect theme's variants", function test() {
655
+ if (/jsdom/.test(window.navigator.userAgent)) {
656
+ this.skip();
657
+ }
658
+ const { muiName, testVariantProps, render, ThemeProvider, createTheme } = getOptions();
659
+ if (!testVariantProps) {
660
+ throw new Error('missing testVariantProps');
661
+ }
662
+ if (!muiName) {
663
+ throwMissingPropError('muiName');
664
+ }
665
+ if (!render) {
666
+ throwMissingPropError('render');
667
+ }
668
+ if (!ThemeProvider) {
669
+ throwMissingPropError('ThemeProvider');
670
+ }
671
+ if (!createTheme) {
672
+ throwMissingPropError('createTheme');
673
+ }
674
+ const testStyle = {
675
+ mixBlendMode: 'darken',
676
+ };
677
+ const theme = createTheme({
678
+ components: {
679
+ [muiName]: {
680
+ variants: [
681
+ {
682
+ props: testVariantProps,
683
+ style: testStyle,
684
+ },
685
+ ],
686
+ },
687
+ },
688
+ });
689
+ const { getByTestId } = render(React.createElement(ThemeProvider, { theme: theme },
690
+ React.cloneElement(element, { ...testVariantProps, 'data-testid': 'with-props' }),
691
+ React.cloneElement(element, { 'data-testid': 'without-props' })));
692
+ (0, chai_1.expect)(getByTestId('with-props')).to.toHaveComputedStyle(testStyle);
693
+ (0, chai_1.expect)(getByTestId('without-props')).not.to.toHaveComputedStyle(testStyle);
694
+ });
695
+ it('supports custom variant', function test() {
696
+ if (/jsdom/.test(window.navigator.userAgent)) {
697
+ this.skip();
698
+ }
699
+ const { muiName, testCustomVariant, render, ThemeProvider, createTheme } = getOptions();
700
+ if (!ThemeProvider) {
701
+ throwMissingPropError('ThemeProvider');
702
+ }
703
+ if (!createTheme) {
704
+ throwMissingPropError('createTheme');
705
+ }
706
+ if (!testCustomVariant) {
707
+ return;
708
+ }
709
+ const theme = createTheme({
710
+ components: {
711
+ [muiName]: {
712
+ styleOverrides: {
713
+ root: ({ ownerState }) => ({
714
+ ...(ownerState.variant === 'unknown' && {
715
+ mixBlendMode: 'darken',
716
+ }),
717
+ }),
718
+ },
719
+ },
720
+ },
721
+ });
722
+ const { getByTestId } = render(React.createElement(ThemeProvider, { theme: theme }, React.cloneElement(element, { variant: 'unknown', 'data-testid': 'custom-variant' })));
723
+ (0, chai_1.expect)(getByTestId('custom-variant')).toHaveComputedStyle({ mixBlendMode: 'darken' });
724
+ });
725
+ });
726
+ }
727
+ /**
728
+ * MUI theme supports custom palettes.
729
+ * The components that iterate over the palette via `variants` should be able to render with or without applying the custom palette styles.
730
+ */
731
+ function testThemeCustomPalette(element, getOptions) {
732
+ describe('theme extended palette:', () => {
733
+ it('should render without errors', function test() {
734
+ const { render, ThemeProvider, createTheme } = getOptions();
735
+ if (!/jsdom/.test(window.navigator.userAgent) || !render || !ThemeProvider || !createTheme) {
736
+ this.skip();
737
+ }
738
+ const theme = createTheme({
739
+ palette: {
740
+ custom: {
741
+ main: '#ff5252',
742
+ },
743
+ unknown: null,
744
+ },
745
+ });
746
+ (0, chai_1.expect)(() => render(React.createElement(ThemeProvider, { theme: theme }, element))).not.to.throw();
747
+ });
748
+ });
749
+ }
750
+ const fullSuite = {
751
+ componentProp: testComponentProp,
752
+ componentsProp: testComponentsProp,
753
+ mergeClassName: testClassName,
754
+ propsSpread: testPropsSpread,
755
+ refForwarding: describeRef,
756
+ rootClass: testRootClass,
757
+ reactTestRenderer: testReactTestRenderer,
758
+ slotPropsProp: testSlotPropsProp,
759
+ slotPropsCallback: testSlotPropsCallback,
760
+ slotsProp: testSlotsProp,
761
+ themeDefaultProps: testThemeDefaultProps,
762
+ themeStyleOverrides: testThemeStyleOverrides,
763
+ themeVariants: testThemeVariants,
764
+ themeCustomPalette: testThemeCustomPalette,
765
+ };
766
+ /**
767
+ * Tests various aspects of a component that should be equal across MUI
768
+ * components.
769
+ */
770
+ function describeConformance(minimalElement, getOptions) {
771
+ let originalMatchmedia;
772
+ const storage = {};
773
+ beforeEach(() => {
774
+ originalMatchmedia = window.matchMedia;
775
+ // Create mocks of localStorage getItem and setItem functions
776
+ Object.defineProperty(global, 'localStorage', {
777
+ value: {
778
+ getItem: (key) => storage[key],
779
+ setItem: (key, value) => {
780
+ storage[key] = value;
781
+ },
782
+ },
783
+ configurable: true,
784
+ });
785
+ window.matchMedia = () => ({
786
+ addListener: () => { },
787
+ removeListener: () => { },
788
+ });
789
+ });
790
+ afterEach(() => {
791
+ window.matchMedia = originalMatchmedia;
792
+ });
793
+ const { after: runAfterHook = () => { }, only = Object.keys(fullSuite), slots, skip = [], wrapMount, } = getOptions();
794
+ let filteredTests = Object.keys(fullSuite).filter((testKey) => only.indexOf(testKey) !== -1 && skip.indexOf(testKey) === -1);
795
+ const slotBasedTests = ['slotsProp', 'slotPropsProp', 'slotPropsCallback'];
796
+ if (!slots) {
797
+ // if `slots` are not defined, do not run tests that depend on them
798
+ filteredTests = filteredTests.filter((testKey) => !slotBasedTests.includes(testKey));
799
+ }
800
+ const baseMount = (0, createMount_1.default)();
801
+ const mount = wrapMount !== undefined ? wrapMount(baseMount) : baseMount;
802
+ after(runAfterHook);
803
+ function getTestOptions() {
804
+ return {
805
+ ...getOptions(),
806
+ mount,
807
+ };
808
+ }
809
+ filteredTests.forEach((testKey) => {
810
+ const test = fullSuite[testKey];
811
+ test(minimalElement, getTestOptions);
812
+ });
813
+ }
814
+ exports.default = (0, createDescribe_1.default)('MUI component API', describeConformance);
815
+ //# sourceMappingURL=describeConformance.js.map