2024-09-19 03:30:19 +08:00
|
|
|
/* eslint-disable jest/no-export */
|
2023-01-20 11:03:50 +08:00
|
|
|
import React from 'react';
|
2023-12-21 10:30:08 +08:00
|
|
|
|
2023-01-20 11:03:50 +08:00
|
|
|
import ConfigProvider from '../../components/config-provider';
|
|
|
|
import { render, waitFakeTimer } from '../utils';
|
|
|
|
import { TriggerMockContext } from './demoTestContext';
|
|
|
|
|
|
|
|
export interface Options {
|
|
|
|
name?: string;
|
|
|
|
findRootElements?: (
|
|
|
|
container: HTMLElement,
|
|
|
|
) => Element | HTMLCollection | Element[] | NodeListOf<Element>;
|
|
|
|
expectCount?: number;
|
|
|
|
beforeRender?: () => void;
|
|
|
|
afterRender?: (container: HTMLElement) => void;
|
|
|
|
props?: object;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isSingleNode(node: any): node is Element {
|
|
|
|
return node && node instanceof HTMLElement;
|
|
|
|
}
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
export default function rootPropsTest(
|
2023-12-21 10:30:08 +08:00
|
|
|
component: string | string[],
|
2023-01-20 11:03:50 +08:00
|
|
|
customizeRender?: (
|
|
|
|
component: React.ComponentType<any> & Record<string, any>,
|
|
|
|
props: any,
|
|
|
|
) => React.ReactNode,
|
|
|
|
options?: Options,
|
|
|
|
) {
|
2023-12-21 10:30:08 +08:00
|
|
|
const componentNames = Array.isArray(component) ? component : [component];
|
|
|
|
const [componentName, subComponentName] = componentNames;
|
|
|
|
|
2024-06-15 19:44:35 +08:00
|
|
|
const Component = require(`../../components/${componentName}`).default;
|
2023-01-20 11:03:50 +08:00
|
|
|
const name = options?.name ? `(${options.name})` : '';
|
|
|
|
|
|
|
|
describe(`RootProps${name}`, () => {
|
|
|
|
let passed = false;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
passed = false;
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2023-12-21 10:30:08 +08:00
|
|
|
document.body.innerHTML = '';
|
2023-01-20 11:03:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
if (!passed || process.env.DEBUG === 'true') {
|
|
|
|
console.log(document.body.innerHTML);
|
|
|
|
}
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2023-01-20 11:03:50 +08:00
|
|
|
});
|
|
|
|
|
2023-12-21 10:30:08 +08:00
|
|
|
it(['rootClassName', subComponentName].filter((v) => v).join(' '), async () => {
|
2023-01-20 11:03:50 +08:00
|
|
|
const rootClassName = 'TEST_ROOT_CLS';
|
|
|
|
|
|
|
|
if (options?.beforeRender) {
|
|
|
|
options?.beforeRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Demo = () => {
|
|
|
|
const holderRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
const [show, setShow] = React.useState(false);
|
|
|
|
React.useEffect(() => {
|
|
|
|
setShow(true);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const sharedProps = {
|
|
|
|
value: 1,
|
|
|
|
rootClassName,
|
|
|
|
open: true,
|
|
|
|
...options?.props,
|
|
|
|
};
|
|
|
|
|
|
|
|
const node = customizeRender ? (
|
|
|
|
customizeRender(Component, sharedProps)
|
|
|
|
) : (
|
|
|
|
<Component {...sharedProps} />
|
|
|
|
);
|
|
|
|
|
|
|
|
const triggerContext = React.useMemo(() => ({ mock: false }), []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TriggerMockContext.Provider value={triggerContext}>
|
|
|
|
<div id="holder" className="holder" ref={holderRef}>
|
|
|
|
{show && (
|
|
|
|
<ConfigProvider getPopupContainer={() => holderRef.current!}>{node}</ConfigProvider>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</TriggerMockContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const { container } = render(<Demo />);
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
if (options?.afterRender) {
|
|
|
|
options?.afterRender(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
const holder = container.querySelector<HTMLElement>('#holder')!;
|
|
|
|
let customizeFindNodes = options?.findRootElements?.(holder);
|
|
|
|
if (isSingleNode(customizeFindNodes)) {
|
|
|
|
customizeFindNodes = [customizeFindNodes];
|
|
|
|
}
|
|
|
|
const childList = Array.from(customizeFindNodes ?? holder.children);
|
|
|
|
|
|
|
|
expect(childList.length).toBeGreaterThan(0);
|
|
|
|
if (options?.expectCount) {
|
2023-12-21 10:30:08 +08:00
|
|
|
expect(childList).toHaveLength(options.expectCount);
|
2023-01-20 11:03:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
childList.forEach((ele) => {
|
|
|
|
expect(ele).toHaveClass(rootClassName);
|
|
|
|
|
|
|
|
// `rootClassName` should not show in children element
|
|
|
|
expect(ele.querySelector(`.${rootClassName}`)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
passed = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|