Skip to content

Commit 0361cdd

Browse files
authored
[core] Fix test utils types and external buildApiUtils usage issues (#36310)
1 parent 1874773 commit 0361cdd

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

packages/api-docs-builder/buildApiUtils.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ import { replaceComponentLinks } from './utils/replaceUrl';
99
import findPagesMarkdownNew from './utils/findPagesMarkdown';
1010
import { TypeScriptProject } from './utils/createTypeScriptProject';
1111

12-
const systemComponents = fs
13-
.readdirSync(path.resolve('packages', 'mui-system', 'src'))
14-
.filter((pathname) => pathname.match(/^[A-Z][a-zA-Z]+$/));
12+
let systemComponents: string[] | undefined;
13+
// making the resolution lazy to avoid issues when importing something irrelevant from this file (i.e. `getSymbolDescription`)
14+
// the eager resolution results in errors when consuming externally (i.e. `mui-x`)
15+
function getSystemComponents() {
16+
if (!systemComponents) {
17+
systemComponents = fs
18+
.readdirSync(path.resolve('packages', 'mui-system', 'src'))
19+
.filter((pathname) => pathname.match(/^[A-Z][a-zA-Z]+$/));
20+
}
21+
return systemComponents;
22+
}
1523

1624
function getMuiName(name: string) {
1725
return `Mui${name.replace('Unstyled', '').replace('Styled', '')}`;
@@ -171,7 +179,7 @@ export const getMaterialComponentInfo = (filename: string): ComponentInfo => {
171179
muiName: getMuiName(name),
172180
apiPathname: `/material-ui/api/${kebabCase(name)}/`,
173181
apiPagesDirectory: path.join(process.cwd(), `docs/pages/material-ui/api`),
174-
isSystemComponent: systemComponents.includes(name),
182+
isSystemComponent: getSystemComponents().includes(name),
175183
readFile() {
176184
srcInfo = parseFile(filename);
177185
return srcInfo;
@@ -282,7 +290,7 @@ export const getBaseComponentInfo = (filename: string): ComponentInfo => {
282290
muiName: getMuiName(name),
283291
apiPathname: `/base/api/${kebabCase(name)}/`,
284292
apiPagesDirectory: path.join(process.cwd(), `docs/pages/base/api`),
285-
isSystemComponent: systemComponents.includes(name),
293+
isSystemComponent: getSystemComponents().includes(name),
286294
readFile() {
287295
srcInfo = parseFile(filename);
288296
return srcInfo;
@@ -390,7 +398,7 @@ export const getJoyComponentInfo = (filename: string): ComponentInfo => {
390398
muiName: getMuiName(name),
391399
apiPathname: `/joy-ui/api/${kebabCase(name)}/`,
392400
apiPagesDirectory: path.join(process.cwd(), `docs/pages/joy-ui/api`),
393-
isSystemComponent: systemComponents.includes(name),
401+
isSystemComponent: getSystemComponents().includes(name),
394402
readFile() {
395403
srcInfo = parseFile(filename);
396404
return srcInfo;

test/utils/createMount.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default function createMount(options: CreateMountOptions = {}) {
4646

4747
function computeTestName(test: Test | undefined) {
4848
let current: Test | Suite | undefined = test;
49-
const titles = [];
49+
const titles: string[] = [];
5050
while (current != null) {
5151
titles.push(current.title);
5252
current = current.parent;

test/utils/createRenderer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ fireEvent.keyUp = (desiredTarget, options = {}) => {
680680

681681
export function fireTouchChangedEvent(
682682
target: Element,
683-
type: 'touchmove' | 'touchend',
683+
type: 'touchstart' | 'touchmove' | 'touchend',
684684
options: { changedTouches: Array<Pick<TouchInit, 'clientX' | 'clientY'>> },
685685
): void {
686686
const { changedTouches } = options;

test/utils/fireDiscreteEvent.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as React from 'react';
22
import { configure, fireEvent, getConfig } from '@testing-library/react';
3-
import { FireFunction } from 'test/utils/createRenderer';
43

54
const noWrapper = (callback: () => void) => callback();
65

@@ -46,32 +45,32 @@ function withMissingActWarningsIgnored(callback: () => void) {
4645
// Note that using `fireEvent` from `@testing-library/dom` would not work since /react configures both `fireEvent` to use `act` as a wrapper.
4746
// -----------------------------------------
4847

49-
export function click(...args: Parameters<FireFunction>) {
48+
export function click(...args: Parameters<(typeof fireEvent)['click']>) {
5049
return withMissingActWarningsIgnored(() => {
5150
fireEvent.click(...args);
5251
});
5352
}
5453

55-
export function keyDown(...args: Parameters<FireFunction>) {
54+
export function keyDown(...args: Parameters<(typeof fireEvent)['keyDown']>) {
5655
return withMissingActWarningsIgnored(() => {
5756
fireEvent.keyDown(...args);
5857
});
5958
}
6059

61-
export function keyUp(...args: Parameters<FireFunction>) {
60+
export function keyUp(...args: Parameters<(typeof fireEvent)['keyUp']>) {
6261
return withMissingActWarningsIgnored(() => {
6362
fireEvent.keyUp(...args);
6463
});
6564
}
6665

67-
export function mouseDown(...args: Parameters<FireFunction>) {
66+
export function mouseDown(...args: Parameters<(typeof fireEvent)['mouseDown']>) {
6867
return withMissingActWarningsIgnored(() => {
6968
fireEvent.mouseDown(...args);
7069
});
7170
}
7271

73-
export function mouseUp(...args: Parameters<FireFunction>) {
72+
export function mouseUp(...args: Parameters<(typeof fireEvent)['mouseUp']>) {
7473
return withMissingActWarningsIgnored(() => {
75-
fireEvent.mouseDown(...args);
74+
fireEvent.mouseUp(...args);
7675
});
7776
}

test/utils/userEvent.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import * as React from 'react';
22
import { click, mouseDown, mouseUp, keyDown, keyUp } from './fireDiscreteEvent';
3-
import { act, fireEvent, FireFunction } from './createRenderer';
3+
import { act, fireEvent } from './createRenderer';
44

55
export function touch(target: Element): void {
66
fireEvent.touchStart(target);
77
fireEvent.touchEnd(target);
88
}
99

10-
export const mousePress: (...args: Parameters<FireFunction>) => void = (target, options) => {
10+
export const mousePress: (...args: Parameters<(typeof fireEvent)['mouseUp']>) => void = (
11+
target,
12+
options,
13+
) => {
1114
if (React.version.startsWith('18')) {
1215
fireEvent.mouseDown(target, options);
1316
fireEvent.mouseUp(target, options);
@@ -20,13 +23,13 @@ export const mousePress: (...args: Parameters<FireFunction>) => void = (target,
2023
}
2124
};
2225

23-
export function keyPress(target: Element, options: { key: string }): void {
26+
export function keyPress(target: Element, options: { key: string; [key: string]: any }): void {
2427
if (React.version.startsWith('18')) {
2528
fireEvent.keyDown(target, options);
2629
fireEvent.keyUp(target, options);
2730
} else {
28-
keyDown(target, options as unknown as Event);
29-
keyUp(target, options as unknown as Event);
31+
keyDown(target, options);
32+
keyUp(target, options);
3033
act(() => {});
3134
}
3235
}

0 commit comments

Comments
 (0)