@mui/internal-test-utils 1.0.20 → 1.0.22

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.
@@ -8,6 +8,10 @@ function capitalize(string: string): string {
8
8
  return string.charAt(0).toUpperCase() + string.slice(1);
9
9
  }
10
10
 
11
+ interface DataProps {
12
+ [key: `data-${string}`]: string;
13
+ }
14
+
11
15
  export interface SlotTestingOptions {
12
16
  /**
13
17
  * A custom React component to test if the receiving props are correct.
@@ -95,7 +99,11 @@ function throwMissingPropError(field: string): never {
95
99
  * the root component.
96
100
  */
97
101
  export function testClassName(
98
- element: React.ReactElement<any>,
102
+ element: React.ReactElement<
103
+ DataProps & {
104
+ className: string;
105
+ }
106
+ >,
99
107
  getOptions: () => ConformanceOptions,
100
108
  ) {
101
109
  it('applies the className to the root component', async () => {
@@ -121,7 +129,12 @@ export function testClassName(
121
129
  * Component from @inheritComponent
122
130
  */
123
131
  export function testComponentProp(
124
- element: React.ReactElement<any>,
132
+ element: React.ReactElement<
133
+ DataProps & {
134
+ className: string;
135
+ component?: string | React.ElementType;
136
+ }
137
+ >,
125
138
  getOptions: () => ConformanceOptions,
126
139
  ) {
127
140
  describe('prop: component', () => {
@@ -157,7 +170,13 @@ export function testComponentProp(
157
170
  * MUI components spread additional props to its root.
158
171
  */
159
172
  export function testPropsSpread(
160
- element: React.ReactElement<any>,
173
+ element: React.ReactElement<
174
+ DataProps & {
175
+ className: string;
176
+ component: string | React.ElementType;
177
+ }
178
+ >,
179
+
161
180
  getOptions: () => ConformanceOptions,
162
181
  ) {
163
182
  it(`spreads props to the root component`, async () => {
@@ -187,7 +206,9 @@ export function testPropsSpread(
187
206
  * components that forward their ref and attach it to a host component.
188
207
  */
189
208
  export function describeRef(
190
- element: React.ReactElement<any>,
209
+ element: React.ReactElement<{
210
+ ref: React.RefObject<any>;
211
+ }>,
191
212
  getOptions: () => ConformanceOptions,
192
213
  ) {
193
214
  describe('ref', () => {
@@ -212,7 +233,10 @@ export function describeRef(
212
233
  * Tests that the root component has the root class
213
234
  */
214
235
  export function testRootClass(
215
- element: React.ReactElement<any>,
236
+ element: React.ReactElement<{
237
+ className: string;
238
+ classes: Record<string, string>;
239
+ }>,
216
240
  getOptions: () => ConformanceOptions,
217
241
  ) {
218
242
  it('applies the root class to the root component if it has this class', async () => {
@@ -264,7 +288,39 @@ function forEachSlot(
264
288
  });
265
289
  }
266
290
 
267
- function testSlotsProp(element: React.ReactElement<any>, getOptions: () => ConformanceOptions) {
291
+ function testSlotsProp(
292
+ element: React.ReactElement<{
293
+ className: string;
294
+ classes: Record<string, string>;
295
+ slots: {
296
+ [x: string]:
297
+ | SlotTestingOptions['testWithComponent']
298
+ | keyof React.JSX.IntrinsicElements
299
+ | React.ForwardRefExoticComponent<
300
+ {
301
+ children: React.ReactNode;
302
+ } & React.RefAttributes<HTMLDivElement>
303
+ >;
304
+ };
305
+ components?: {
306
+ [x: string]:
307
+ | SlotTestingOptions['testWithComponent']
308
+ | keyof React.JSX.IntrinsicElements
309
+ | React.ForwardRefExoticComponent<
310
+ {
311
+ children: React.ReactNode;
312
+ } & React.RefAttributes<HTMLDivElement>
313
+ >;
314
+ };
315
+ slotProps: {
316
+ [x: string]: DataProps;
317
+ };
318
+ componentsProps?: {
319
+ [x: string]: DataProps;
320
+ };
321
+ }>,
322
+ getOptions: () => ConformanceOptions,
323
+ ) {
268
324
  const { render, slots, testLegacyComponentsProp } = getOptions();
269
325
 
270
326
  const CustomComponent = React.forwardRef<
@@ -321,7 +377,9 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
321
377
  const renderedElement = queryByTestId('customized');
322
378
  expect(renderedElement).not.to.equal(null);
323
379
 
324
- expect(renderedElement!.nodeName.toLowerCase()).to.equal(slotElement);
380
+ if (typeof slotElement === 'string') {
381
+ expect(renderedElement!.nodeName.toLowerCase()).to.equal(slotElement);
382
+ }
325
383
  if (slotOptions.expectedClassName) {
326
384
  expect(renderedElement).to.have.class(slotOptions.expectedClassName);
327
385
  }
@@ -425,7 +483,9 @@ function testSlotsProp(element: React.ReactElement<any>, getOptions: () => Confo
425
483
  const renderedElement = queryByTestId('customized');
426
484
  expect(renderedElement).not.to.equal(null);
427
485
 
428
- expect(renderedElement!.nodeName.toLowerCase()).to.equal(slotElement);
486
+ if (typeof slotElement === 'string') {
487
+ expect(renderedElement!.nodeName.toLowerCase()).to.equal(slotElement);
488
+ }
429
489
  if (slotOptions.expectedClassName) {
430
490
  expect(renderedElement).to.have.class(slotOptions.expectedClassName);
431
491
  }
@@ -530,6 +590,33 @@ function testSlotPropsCallback(
530
590
 
531
591
  forEachSlot(slots, (slotName) => {
532
592
  it(`sets custom properties on the ${slotName} slot's element with the slotProps.${slotName} callback`, async () => {
593
+ const slotProps = {
594
+ [slotName]: () => ({
595
+ 'data-testid': 'custom',
596
+ }),
597
+ };
598
+
599
+ const { queryByTestId } = await render(
600
+ React.cloneElement(element, { slotProps, className: 'custom' }),
601
+ );
602
+ const slotComponent = queryByTestId('custom');
603
+ expect(slotComponent).not.to.equal(null);
604
+ });
605
+ });
606
+ }
607
+
608
+ function testSlotPropsCallbackWithPropsAsOwnerState(
609
+ element: React.ReactElement<any>,
610
+ getOptions: () => ConformanceOptions,
611
+ ) {
612
+ const { render, slots } = getOptions();
613
+
614
+ if (!render) {
615
+ throwMissingPropError('render');
616
+ }
617
+
618
+ forEachSlot(slots, (slotName) => {
619
+ it(`sets custom properties on the ${slotName} slot's element with the slotProps.${slotName} callback using the ownerState`, async () => {
533
620
  const slotProps = {
534
621
  [slotName]: (ownerState: Record<string, any>) => ({
535
622
  'data-testid': ownerState.className,
@@ -617,12 +704,14 @@ function testThemeDefaultProps(
617
704
  });
618
705
 
619
706
  describe('default props provider:', () => {
620
- it('respect custom default props', async function test() {
707
+ it('respect custom default props', async function test(t = {}) {
621
708
  const testProp = 'data-id';
622
709
  const { muiName, render, DefaultPropsProvider } = getOptions();
623
710
 
624
711
  if (!DefaultPropsProvider) {
625
- this.skip();
712
+ // @ts-ignore
713
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
714
+ this?.skip?.() ?? t?.skip();
626
715
  }
627
716
 
628
717
  if (!muiName) {
@@ -634,6 +723,7 @@ function testThemeDefaultProps(
634
723
  }
635
724
 
636
725
  const { container } = await render(
726
+ // @ts-expect-error we skip it above.
637
727
  <DefaultPropsProvider
638
728
  value={{
639
729
  [muiName]: {
@@ -1061,6 +1151,7 @@ const fullSuite = {
1061
1151
  rootClass: testRootClass,
1062
1152
  slotPropsProp: testSlotPropsProp,
1063
1153
  slotPropsCallback: testSlotPropsCallback,
1154
+ slotPropsCallbackWithPropsAsOwnerState: testSlotPropsCallbackWithPropsAsOwnerState,
1064
1155
  slotsProp: testSlotsProp,
1065
1156
  themeDefaultProps: testThemeDefaultProps,
1066
1157
  themeStyleOverrides: testThemeStyleOverrides,
@@ -1113,7 +1204,12 @@ function describeConformance(
1113
1204
  (testKey) => only.includes(testKey) && !skip.includes(testKey as keyof typeof fullSuite),
1114
1205
  ) as (keyof typeof fullSuite)[];
1115
1206
 
1116
- const slotBasedTests = ['slotsProp', 'slotPropsProp', 'slotPropsCallback'];
1207
+ const slotBasedTests = [
1208
+ 'slotsProp',
1209
+ 'slotPropsProp',
1210
+ 'slotPropsCallback',
1211
+ 'slotPropsCallbackWithPropsAsOwnerState',
1212
+ ];
1117
1213
 
1118
1214
  if (!slots) {
1119
1215
  // if `slots` are not defined, do not run tests that depend on them