Skip to content

Commit

Permalink
test: wrap React.StrictMode for test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Apr 14, 2022
1 parent 4c8041d commit 095ca78
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
6 changes: 5 additions & 1 deletion tests/shared/accessibilityTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export default function accessibilityTest(Component: React.ComponentType) {
describe(`accessibility`, () => {
it(`component does not have any violations`, async () => {
jest.useRealTimers();
const wrapper = mount(<Component />);
const wrapper = mount(
<React.StrictMode>
<Component />
</React.StrictMode>,
);
const results = await axe(wrapper.getDOMNode());
expect(results).toHaveNoViolations();
});
Expand Down
2 changes: 1 addition & 1 deletion tests/shared/demoTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function baseText(doInject: boolean, component: string, options: Options = {}) {
);
}

const wrapper = render(Demo);
const wrapper = render(<React.StrictMode>{Demo}</React.StrictMode>);

// Convert aria related content
ariaConvert(wrapper);
Expand Down
61 changes: 48 additions & 13 deletions tests/shared/focusTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export default function focusTest(
it('Test: focus() and onFocus', () => {
const handleFocus = jest.fn();
const ref = React.createRef<any>();
const { unmount } = render(<Component onFocus={handleFocus} ref={ref} />);
const { unmount } = render(
<React.StrictMode>
<Component onFocus={handleFocus} ref={ref} />
</React.StrictMode>,
);
ref.current.focus();
expect(handleFocus).toHaveBeenCalled();

Expand All @@ -41,7 +45,11 @@ export default function focusTest(
it('Test: blur() and onBlur', async () => {
const handleBlur = jest.fn();
const ref = React.createRef<any>();
const { unmount } = render(<Component ref={ref} onBlur={handleBlur} />);
const { unmount } = render(
<React.StrictMode>
<Component ref={ref} onBlur={handleBlur} />
</React.StrictMode>,
);
ref.current.focus();
ref.current.blur();
expect(handleBlur).toHaveBeenCalled();
Expand All @@ -51,7 +59,11 @@ export default function focusTest(

it('Test: autoFocus', () => {
const handleFocus = jest.fn();
const { unmount } = render(<Component autoFocus onFocus={handleFocus} />);
const { unmount } = render(
<React.StrictMode>
<Component autoFocus onFocus={handleFocus} />
</React.StrictMode>,
);
expect(handleFocus).toHaveBeenCalled();

unmount();
Expand Down Expand Up @@ -97,9 +109,11 @@ export default function focusTest(
const onFocus = jest.fn();
const ref = React.createRef<any>();
const wrapper = mount(
<div>
<Component onFocus={onFocus} ref={ref} />
</div>,
<React.StrictMode>
<div>
<Component onFocus={onFocus} ref={ref} />
</div>
</React.StrictMode>,
);
ref.current.focus();
expect(focused).toBeTruthy();
Expand All @@ -113,9 +127,11 @@ export default function focusTest(
const onBlur = jest.fn();
const ref = React.createRef<any>();
const wrapper = mount(
<div>
<Component onBlur={onBlur} ref={ref} />
</div>,
<React.StrictMode>
<div>
<Component onBlur={onBlur} ref={ref} />
</div>
</React.StrictMode>,
);

ref.current.blur();
Expand All @@ -128,7 +144,11 @@ export default function focusTest(

it('Ref: autoFocus', () => {
const onFocus = jest.fn();
const wrapper = mount(<Component autoFocus onFocus={onFocus} />);
const wrapper = mount(
<React.StrictMode>
<Component autoFocus onFocus={onFocus} />
</React.StrictMode>,
);

expect(focused).toBeTruthy();

Expand All @@ -138,15 +158,25 @@ export default function focusTest(
} else {
it('focus() and onFocus', () => {
const handleFocus = jest.fn();
const wrapper = mount(<Component onFocus={handleFocus} />, { attachTo: container });
const wrapper = mount(
<React.StrictMode>
<Component onFocus={handleFocus} />
</React.StrictMode>,
{ attachTo: container },
);
(wrapper.instance() as any).focus();
expect(handleFocus).toHaveBeenCalled();
});

it('blur() and onBlur', async () => {
jest.useRealTimers();
const handleBlur = jest.fn();
const wrapper = mount(<Component onBlur={handleBlur} />, { attachTo: container });
const wrapper = mount(
<React.StrictMode>
<Component onBlur={handleBlur} />
</React.StrictMode>,
{ attachTo: container },
);
(wrapper.instance() as any).focus();
await sleep(0);
(wrapper.instance() as any).blur();
Expand All @@ -156,7 +186,12 @@ export default function focusTest(

it('autoFocus', () => {
const handleFocus = jest.fn();
mount(<Component autoFocus onFocus={handleFocus} />, { attachTo: container });
mount(
<React.StrictMode>
<Component autoFocus onFocus={handleFocus} />
</React.StrictMode>,
{ attachTo: container },
);
expect(handleFocus).toHaveBeenCalled();
});
}
Expand Down
6 changes: 5 additions & 1 deletion tests/shared/imageTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ export function imageDemoTest(component: string, options: Options = {}) {
// eslint-disable-next-line global-require,import/no-dynamic-require
let Demo = require(`../.${file}`).default;
if (typeof Demo === 'function') {
Demo = <Demo />;
Demo = (
<React.StrictMode>
<Demo />
</React.StrictMode>
);
}
imageTest(Demo);
});
Expand Down
6 changes: 5 additions & 1 deletion tests/shared/mountTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export default function mountTest(Component: React.ComponentType) {
describe(`mount and unmount`, () => {
// https://github.com/ant-design/ant-design/pull/18441
it(`component could be updated and unmounted without errors`, () => {
const wrapper = mount(<Component />);
const wrapper = mount(
<React.StrictMode>
<Component />
</React.StrictMode>,
);
expect(() => {
wrapper.setProps({});
wrapper.unmount();
Expand Down
8 changes: 5 additions & 3 deletions tests/shared/rtlTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ export default function rtlTest(Component: React.ComponentType, mockDate?: boole
MockDate.set(Moment('2000-09-28').valueOf());
}
const wrapper = mount(
<ConfigProvider direction="rtl">
<Component />
</ConfigProvider>,
<React.StrictMode>
<ConfigProvider direction="rtl">
<Component />
</ConfigProvider>
</React.StrictMode>,
);
expect(wrapper.render()).toMatchSnapshot();
if (mockDate) {
Expand Down

0 comments on commit 095ca78

Please sign in to comment.