@mui/internal-test-utils 1.0.6 → 1.0.7

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.
@@ -41,7 +41,7 @@ export interface ConformanceOptions {
41
41
  refInstanceof: any;
42
42
  after?: () => void;
43
43
  inheritComponent?: React.ElementType;
44
- render: (node: React.ReactElement<any>) => MuiRenderResult;
44
+ render: (node: React.ReactElement<any>) => MuiRenderResult | Promise<MuiRenderResult>;
45
45
  only?: Array<keyof typeof fullSuite>;
46
46
  skip?: Array<keyof typeof fullSuite | 'classesRoot'>;
47
47
  testComponentsRootPropWith?: string;
@@ -94,7 +94,7 @@ export function testClassName(
94
94
  element: React.ReactElement<any>,
95
95
  getOptions: () => ConformanceOptions,
96
96
  ) {
97
- it('applies the className to the root component', () => {
97
+ it('applies the className to the root component', async () => {
98
98
  const { render } = getOptions();
99
99
 
100
100
  if (!render) {
@@ -104,7 +104,7 @@ export function testClassName(
104
104
  const className = randomStringValue();
105
105
  const testId = randomStringValue();
106
106
 
107
- const { getByTestId } = render(
107
+ const { getByTestId } = await render(
108
108
  React.cloneElement(element, { className, 'data-testid': testId }),
109
109
  );
110
110
 
@@ -121,7 +121,7 @@ export function testComponentProp(
121
121
  getOptions: () => ConformanceOptions,
122
122
  ) {
123
123
  describe('prop: component', () => {
124
- it('can render another root component with the `component` prop', () => {
124
+ it('can render another root component with the `component` prop', async () => {
125
125
  const { render, testComponentPropWith: component = 'em' } = getOptions();
126
126
  if (!render) {
127
127
  throwMissingPropError('render');
@@ -130,7 +130,7 @@ export function testComponentProp(
130
130
  const testId = randomStringValue();
131
131
 
132
132
  if (typeof component === 'string') {
133
- const { getByTestId } = render(
133
+ const { getByTestId } = await render(
134
134
  React.cloneElement(element, { component, 'data-testid': testId }),
135
135
  );
136
136
  expect(getByTestId(testId)).not.to.equal(null);
@@ -138,7 +138,7 @@ export function testComponentProp(
138
138
  } else {
139
139
  const componentWithTestId = (props: {}) =>
140
140
  React.createElement(component, { ...props, 'data-testid': testId });
141
- const { getByTestId } = render(
141
+ const { getByTestId } = await render(
142
142
  React.cloneElement(element, {
143
143
  component: componentWithTestId,
144
144
  }),
@@ -156,7 +156,7 @@ export function testPropsSpread(
156
156
  element: React.ReactElement<any>,
157
157
  getOptions: () => ConformanceOptions,
158
158
  ) {
159
- it(`spreads props to the root component`, () => {
159
+ it(`spreads props to the root component`, async () => {
160
160
  // type def in ConformanceOptions
161
161
  const { render } = getOptions();
162
162
 
@@ -168,7 +168,7 @@ export function testPropsSpread(
168
168
  const value = randomStringValue();
169
169
  const testId = randomStringValue();
170
170
 
171
- const { getByTestId } = render(
171
+ const { getByTestId } = await render(
172
172
  React.cloneElement(element, { [testProp]: value, 'data-testid': testId }),
173
173
  );
174
174
 
@@ -187,7 +187,7 @@ export function describeRef(
187
187
  getOptions: () => ConformanceOptions,
188
188
  ) {
189
189
  describe('ref', () => {
190
- it(`attaches the ref`, () => {
190
+ it(`attaches the ref`, async () => {
191
191
  // type def in ConformanceOptions
192
192
  const { render, refInstanceof } = getOptions();
193
193
 
@@ -197,7 +197,7 @@ export function describeRef(
197
197
 
198
198
  const ref = React.createRef();
199
199
 
200
- render(React.cloneElement(element, { ref }));
200
+ await render(React.cloneElement(element, { ref }));
201
201
 
202
202
  expect(ref.current).to.be.instanceof(refInstanceof);
203
203
  });
@@ -211,7 +211,7 @@ export function testRootClass(
211
211
  element: React.ReactElement<any>,
212
212
  getOptions: () => ConformanceOptions,
213
213
  ) {
214
- it('applies the root class to the root component if it has this class', () => {
214
+ it('applies the root class to the root component if it has this class', async () => {
215
215
  const { classes, render, skip } = getOptions();
216
216
  if (classes.root == null) {
217
217
  return;
@@ -219,7 +219,7 @@ export function testRootClass(
219
219
 
220
220
  const className = randomStringValue();
221
221
  const classesRootClassname = randomStringValue();
222
- const { container } = render(
222
+ const { container } = await render(
223
223
  React.cloneElement(element, {
224
224
  className,
225
225
  classes: { ...classes, root: `${classes.root} ${classesRootClassname}` },
@@ -273,7 +273,7 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
273
273
  ));
274
274
 
275
275
  forEachSlot(slots, (slotName, slotOptions) => {
276
- it(`allows overriding the ${slotName} slot with a component using the slots.${slotName} prop`, () => {
276
+ it(`allows overriding the ${slotName} slot with a component using the slots.${slotName} prop`, async () => {
277
277
  if (!render) {
278
278
  throwMissingPropError('render');
279
279
  }
@@ -284,7 +284,7 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
284
284
  [slotName]: slotComponent,
285
285
  };
286
286
 
287
- const { queryByTestId } = render(React.cloneElement(element, { slots: components }));
287
+ const { queryByTestId } = await render(React.cloneElement(element, { slots: components }));
288
288
  const renderedElement = queryByTestId('custom');
289
289
  expect(renderedElement).not.to.equal(null);
290
290
  if (slotOptions.expectedClassName) {
@@ -293,7 +293,7 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
293
293
  });
294
294
 
295
295
  if (slotOptions.testWithElement !== null) {
296
- it(`allows overriding the ${slotName} slot with an element using the slots.${slotName} prop`, () => {
296
+ it(`allows overriding the ${slotName} slot with an element using the slots.${slotName} prop`, async () => {
297
297
  if (!render) {
298
298
  throwMissingPropError('render');
299
299
  }
@@ -310,7 +310,7 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
310
310
  },
311
311
  };
312
312
 
313
- const { queryByTestId } = render(
313
+ const { queryByTestId } = await render(
314
314
  React.cloneElement(element, { slots: components, slotProps }),
315
315
  );
316
316
 
@@ -328,7 +328,7 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
328
328
  if (testLegacyComponentsProp) {
329
329
  it(`allows overriding the ${slotName} slot with a component using the components.${capitalize(
330
330
  slotName,
331
- )} prop`, () => {
331
+ )} prop`, async () => {
332
332
  if (!render) {
333
333
  throwMissingPropError('render');
334
334
  }
@@ -339,7 +339,7 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
339
339
  [capitalize(slotName)]: slotComponent,
340
340
  };
341
341
 
342
- const { queryByTestId } = render(React.cloneElement(element, { components }));
342
+ const { queryByTestId } = await render(React.cloneElement(element, { components }));
343
343
  const renderedElement = queryByTestId('custom');
344
344
  expect(renderedElement).not.to.equal(null);
345
345
  if (slotOptions.expectedClassName) {
@@ -349,7 +349,7 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
349
349
 
350
350
  it(`prioritizes the 'slots.${slotName}' over components.${capitalize(
351
351
  slotName,
352
- )} if both are defined`, () => {
352
+ )} if both are defined`, async () => {
353
353
  if (!render) {
354
354
  throwMissingPropError('render');
355
355
  }
@@ -386,7 +386,7 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
386
386
  [slotName]: ComponentForSlotsProp,
387
387
  };
388
388
 
389
- const { queryByTestId } = render(
389
+ const { queryByTestId } = await render(
390
390
  React.cloneElement(element, { components, slots: slotOverrides }),
391
391
  );
392
392
 
@@ -397,7 +397,7 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
397
397
  if (slotOptions.testWithElement !== null) {
398
398
  it(`allows overriding the ${slotName} slot with an element using the components.${capitalize(
399
399
  slotName,
400
- )} prop`, () => {
400
+ )} prop`, async () => {
401
401
  if (!render) {
402
402
  throwMissingPropError('render');
403
403
  }
@@ -414,7 +414,7 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
414
414
  },
415
415
  };
416
416
 
417
- const { queryByTestId } = render(
417
+ const { queryByTestId } = await render(
418
418
  React.cloneElement(element, { components, componentsProps }),
419
419
  );
420
420
 
@@ -439,14 +439,14 @@ function testSlotPropsProp(element: React.ReactElement<any>, getOptions: () => C
439
439
  }
440
440
 
441
441
  forEachSlot(slots, (slotName, slotOptions) => {
442
- it(`sets custom properties on the ${slotName} slot's element with the slotProps.${slotName} prop`, () => {
442
+ it(`sets custom properties on the ${slotName} slot's element with the slotProps.${slotName} prop`, async () => {
443
443
  const slotProps = {
444
444
  [slotName]: {
445
445
  'data-testid': 'custom',
446
446
  },
447
447
  };
448
448
 
449
- const { queryByTestId } = render(React.cloneElement(element, { slotProps }));
449
+ const { queryByTestId } = await render(React.cloneElement(element, { slotProps }));
450
450
  const slotComponent = queryByTestId('custom');
451
451
  expect(slotComponent).not.to.equal(null);
452
452
 
@@ -456,7 +456,7 @@ function testSlotPropsProp(element: React.ReactElement<any>, getOptions: () => C
456
456
  });
457
457
 
458
458
  if (slotOptions.expectedClassName) {
459
- it(`merges the class names provided in slotsProps.${slotName} with the built-in ones`, () => {
459
+ it(`merges the class names provided in slotsProps.${slotName} with the built-in ones`, async () => {
460
460
  const slotProps = {
461
461
  [slotName]: {
462
462
  'data-testid': 'custom',
@@ -464,7 +464,7 @@ function testSlotPropsProp(element: React.ReactElement<any>, getOptions: () => C
464
464
  },
465
465
  };
466
466
 
467
- const { getByTestId } = render(React.cloneElement(element, { slotProps }));
467
+ const { getByTestId } = await render(React.cloneElement(element, { slotProps }));
468
468
 
469
469
  expect(getByTestId('custom')).to.have.class(slotOptions.expectedClassName);
470
470
  expect(getByTestId('custom')).to.have.class(slotProps[slotName].className);
@@ -472,14 +472,14 @@ function testSlotPropsProp(element: React.ReactElement<any>, getOptions: () => C
472
472
  }
473
473
 
474
474
  if (testLegacyComponentsProp) {
475
- it(`sets custom properties on the ${slotName} slot's element with the componentsProps.${slotName} prop`, () => {
475
+ it(`sets custom properties on the ${slotName} slot's element with the componentsProps.${slotName} prop`, async () => {
476
476
  const componentsProps = {
477
477
  [slotName]: {
478
478
  'data-testid': 'custom',
479
479
  },
480
480
  };
481
481
 
482
- const { queryByTestId } = render(React.cloneElement(element, { componentsProps }));
482
+ const { queryByTestId } = await render(React.cloneElement(element, { componentsProps }));
483
483
  const slotComponent = queryByTestId('custom');
484
484
  expect(slotComponent).not.to.equal(null);
485
485
 
@@ -488,7 +488,7 @@ function testSlotPropsProp(element: React.ReactElement<any>, getOptions: () => C
488
488
  }
489
489
  });
490
490
 
491
- it(`prioritizes the 'slotProps.${slotName}' over componentsProps.${slotName} if both are defined`, () => {
491
+ it(`prioritizes the 'slotProps.${slotName}' over componentsProps.${slotName} if both are defined`, async () => {
492
492
  const componentsProps = {
493
493
  [slotName]: {
494
494
  'data-testid': 'custom',
@@ -503,7 +503,7 @@ function testSlotPropsProp(element: React.ReactElement<any>, getOptions: () => C
503
503
  },
504
504
  };
505
505
 
506
- const { queryByTestId } = render(
506
+ const { queryByTestId } = await render(
507
507
  React.cloneElement(element, { componentsProps, slotProps }),
508
508
  );
509
509
  const slotComponent = queryByTestId('custom');
@@ -525,14 +525,14 @@ function testSlotPropsCallback(
525
525
  }
526
526
 
527
527
  forEachSlot(slots, (slotName) => {
528
- it(`sets custom properties on the ${slotName} slot's element with the slotProps.${slotName} callback`, () => {
528
+ it(`sets custom properties on the ${slotName} slot's element with the slotProps.${slotName} callback`, async () => {
529
529
  const slotProps = {
530
530
  [slotName]: (ownerState: Record<string, any>) => ({
531
531
  'data-testid': ownerState.className,
532
532
  }),
533
533
  };
534
534
 
535
- const { queryByTestId } = render(
535
+ const { queryByTestId } = await render(
536
536
  React.cloneElement(element, { slotProps, className: 'custom' }),
537
537
  );
538
538
  const slotComponent = queryByTestId('custom');
@@ -550,7 +550,7 @@ function testComponentsProp(
550
550
  getOptions: () => ConformanceOptions,
551
551
  ) {
552
552
  describe('prop components:', () => {
553
- it('can render another root component with the `components` prop', () => {
553
+ it('can render another root component with the `components` prop', async () => {
554
554
  const { render, testComponentsRootPropWith: component = 'em' } = getOptions();
555
555
  if (!render) {
556
556
  throwMissingPropError('render');
@@ -558,7 +558,7 @@ function testComponentsProp(
558
558
 
559
559
  const testId = randomStringValue();
560
560
 
561
- const { getByTestId } = render(
561
+ const { getByTestId } = await render(
562
562
  React.cloneElement(element, { components: { Root: component }, 'data-testid': testId }),
563
563
  );
564
564
  expect(getByTestId(testId)).not.to.equal(null);
@@ -576,7 +576,7 @@ function testThemeDefaultProps(
576
576
  getOptions: () => ConformanceOptions,
577
577
  ) {
578
578
  describe('theme default components:', () => {
579
- it("respect theme's defaultProps", () => {
579
+ it("respect theme's defaultProps", async () => {
580
580
  const testProp = 'data-id';
581
581
  const { muiName, render, ThemeProvider, createTheme } = getOptions();
582
582
 
@@ -606,7 +606,7 @@ function testThemeDefaultProps(
606
606
  },
607
607
  });
608
608
 
609
- const { container } = render(<ThemeProvider theme={theme}>{element}</ThemeProvider>);
609
+ const { container } = await render(<ThemeProvider theme={theme}>{element}</ThemeProvider>);
610
610
 
611
611
  expect(container.firstChild).to.have.attribute(testProp, 'testProp');
612
612
  });
@@ -622,7 +622,7 @@ function testThemeStyleOverrides(
622
622
  getOptions: () => ConformanceOptions,
623
623
  ) {
624
624
  describe('theme style overrides:', () => {
625
- it("respect theme's styleOverrides custom state", function test() {
625
+ it("respect theme's styleOverrides custom state", async function test() {
626
626
  if (/jsdom/.test(window.navigator.userAgent)) {
627
627
  this.skip();
628
628
  }
@@ -666,7 +666,7 @@ function testThemeStyleOverrides(
666
666
  return;
667
667
  }
668
668
 
669
- const { container } = render(
669
+ const { container } = await render(
670
670
  <ThemeProvider theme={theme}>
671
671
  {React.cloneElement(element, {
672
672
  [testStateOverrides.prop]: testStateOverrides.value,
@@ -677,7 +677,7 @@ function testThemeStyleOverrides(
677
677
  expect(container.firstChild).to.toHaveComputedStyle(testStyle);
678
678
  });
679
679
 
680
- it("respect theme's styleOverrides slots", function test() {
680
+ it("respect theme's styleOverrides slots", async function test() {
681
681
  if (/jsdom/.test(window.navigator.userAgent)) {
682
682
  this.skip();
683
683
  }
@@ -742,7 +742,7 @@ function testThemeStyleOverrides(
742
742
  },
743
743
  });
744
744
 
745
- const { container, setProps } = render(
745
+ const { container, setProps } = await render(
746
746
  <ThemeProvider theme={theme}>{element}</ThemeProvider>,
747
747
  );
748
748
 
@@ -788,7 +788,7 @@ function testThemeStyleOverrides(
788
788
  }
789
789
  });
790
790
 
791
- it('overrideStyles does not replace each other in slots', function test() {
791
+ it('overrideStyles does not replace each other in slots', async function test() {
792
792
  if (/jsdom/.test(window.navigator.userAgent)) {
793
793
  this.skip();
794
794
  }
@@ -844,7 +844,7 @@ function testThemeStyleOverrides(
844
844
  return;
845
845
  }
846
846
 
847
- render(
847
+ await render(
848
848
  <ThemeProvider theme={theme}>
849
849
  {React.cloneElement(element, {
850
850
  [testStateOverrides.prop]: testStateOverrides.value,
@@ -866,7 +866,7 @@ function testThemeStyleOverrides(
866
866
  */
867
867
  function testThemeVariants(element: React.ReactElement<any>, getOptions: () => ConformanceOptions) {
868
868
  describe('theme variants:', () => {
869
- it("respect theme's variants", function test() {
869
+ it("respect theme's variants", async function test() {
870
870
  if (/jsdom/.test(window.navigator.userAgent)) {
871
871
  this.skip();
872
872
  }
@@ -910,7 +910,7 @@ function testThemeVariants(element: React.ReactElement<any>, getOptions: () => C
910
910
  },
911
911
  });
912
912
 
913
- const { getByTestId } = render(
913
+ const { getByTestId } = await render(
914
914
  <ThemeProvider theme={theme}>
915
915
  {React.cloneElement(element, { ...testVariantProps, 'data-testid': 'with-props' })}
916
916
  {React.cloneElement(element, { 'data-testid': 'without-props' })}
@@ -921,7 +921,7 @@ function testThemeVariants(element: React.ReactElement<any>, getOptions: () => C
921
921
  expect(getByTestId('without-props')).not.to.toHaveComputedStyle(testStyle);
922
922
  });
923
923
 
924
- it('supports custom variant', function test() {
924
+ it('supports custom variant', async function test() {
925
925
  if (/jsdom/.test(window.navigator.userAgent)) {
926
926
  this.skip();
927
927
  }
@@ -954,7 +954,7 @@ function testThemeVariants(element: React.ReactElement<any>, getOptions: () => C
954
954
  },
955
955
  });
956
956
 
957
- const { getByTestId } = render(
957
+ const { getByTestId } = await render(
958
958
  <ThemeProvider theme={theme}>
959
959
  {React.cloneElement(element, { variant: 'unknown', 'data-testid': 'custom-variant' })}
960
960
  </ThemeProvider>,
@@ -1,5 +1,5 @@
1
- import * as React from 'react';
2
1
  import { configure, fireEvent, getConfig } from '@testing-library/react';
2
+ import reactMajor from './reactMajor';
3
3
 
4
4
  const noWrapper = (callback: () => void) => callback();
5
5
 
@@ -8,7 +8,7 @@ const noWrapper = (callback: () => void) => callback();
8
8
  * @returns {void}
9
9
  */
10
10
  function withMissingActWarningsIgnored(callback: () => void) {
11
- if (React.version.startsWith('18')) {
11
+ if (reactMajor >= 18) {
12
12
  callback();
13
13
  return;
14
14
  }
@@ -1,8 +1,5 @@
1
1
  import { act } from './createRenderer';
2
2
 
3
3
  export default async function flushMicrotasks() {
4
- if (/jsdom/.test(window.navigator.userAgent)) {
5
- // This is only needed for JSDOM and causes issues in real browsers
6
- await act(async () => {});
7
- }
4
+ await act(async () => {});
8
5
  }
package/src/index.ts CHANGED
@@ -15,9 +15,12 @@ export {} from './initMatchers';
15
15
  export * as fireDiscreteEvent from './fireDiscreteEvent';
16
16
  export * as userEvent from './userEvent';
17
17
  export { default as flushMicrotasks } from './flushMicrotasks';
18
+ export { default as reactMajor } from './reactMajor';
18
19
 
19
20
  /**
20
21
  * Set to true if console logs during [lifecycles that are invoked twice in `React.StrictMode`](https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects) are suppressed.
21
22
  * Useful for asserting on `console.warn` or `console.error` via `toErrorDev()`.
23
+ * TODO: Refactor to use reactMajor when fixing the React 17 cron test.
24
+ * https://github.com/mui/material-ui/issues/43153
22
25
  */
23
26
  export const strictModeDoubleLoggingSuppressed = React.version.startsWith('17');
@@ -0,0 +1,3 @@
1
+ import * as React from 'react';
2
+
3
+ export default Number(React.version.split('.')[0]);
package/src/userEvent.ts CHANGED
@@ -1,6 +1,6 @@
1
- import * as React from 'react';
2
1
  import { click, mouseDown, mouseUp, keyDown, keyUp } from './fireDiscreteEvent';
3
2
  import { act, fireEvent } from './createRenderer';
3
+ import reactMajor from './reactMajor';
4
4
 
5
5
  export function touch(target: Element): void {
6
6
  fireEvent.touchStart(target);
@@ -11,7 +11,7 @@ export const mousePress: (...args: Parameters<(typeof fireEvent)['mouseUp']>) =>
11
11
  target,
12
12
  options,
13
13
  ) => {
14
- if (React.version.startsWith('18')) {
14
+ if (reactMajor >= 18) {
15
15
  fireEvent.mouseDown(target, options);
16
16
  fireEvent.mouseUp(target, options);
17
17
  fireEvent.click(target, options);
@@ -24,7 +24,7 @@ export const mousePress: (...args: Parameters<(typeof fireEvent)['mouseUp']>) =>
24
24
  };
25
25
 
26
26
  export function keyPress(target: Element, options: { key: string; [key: string]: any }): void {
27
- if (React.version.startsWith('18')) {
27
+ if (reactMajor >= 18) {
28
28
  fireEvent.keyDown(target, options);
29
29
  fireEvent.keyUp(target, options);
30
30
  } else {