Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: wrap React.StrictMode for test cases #35026

Merged
merged 26 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions components/_util/__tests__/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ describe('Test utils function', () => {
button
</button>
</Wave>,
).instance();
)
.find(Wave)
.instance();
expect(wrapper.bindAnimationEvent()).toBe(undefined);
});

Expand All @@ -159,7 +161,9 @@ describe('Test utils function', () => {
button
</button>
</Wave>,
).instance();
)
.find(Wave)
.instance();
expect(wrapper.bindAnimationEvent()).toBe(undefined);
});

Expand All @@ -168,7 +172,9 @@ describe('Test utils function', () => {
<Wave>
<input />
</Wave>,
).instance();
)
.find(Wave)
.instance();
expect(wrapper.bindAnimationEvent()).toBe(undefined);
});

Expand Down
5 changes: 5 additions & 0 deletions components/_util/devWarning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ export { resetWarned };

export default (valid: boolean, component: string, message: string): void => {
devWarning(valid, `[antd: ${component}] ${message}`);

// StrictMode will inject console which will not throw warning in React 17.
if (process.env.NODE_ENV === 'test') {
resetWarned();
}
};
42 changes: 17 additions & 25 deletions components/affix/__tests__/Affix.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getObserverEntities } from '../utils';
import Button from '../../button';
import rtlTest from '../../../tests/shared/rtlTest';
import accessibilityTest from '../../../tests/shared/accessibilityTest';
import { sleep } from '../../../tests/utils';
import { sleep, render } from '../../../tests/utils';

const events: Partial<Record<keyof HTMLElementEventMap, (ev: Partial<Event>) => void>> = {};

Expand Down Expand Up @@ -97,57 +97,47 @@ describe('Affix Render', () => {
};

it('Anchor render perfectly', async () => {
document.body.innerHTML = '<div id="mounter" />';

affixMounterWrapper = mount(<AffixMounter />, { attachTo: document.getElementById('mounter') });
const { container } = render(<AffixMounter />);
await sleep(20);

await movePlaceholder(0);
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeFalsy();
expect(container.querySelector('.ant-affix')).toBeFalsy();

await movePlaceholder(-100);
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeTruthy();
expect(container.querySelector('.ant-affix')).toBeTruthy();

await movePlaceholder(0);
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeFalsy();
expect(container.querySelector('.ant-affix')).toBeFalsy();
});

it('support offsetBottom', async () => {
document.body.innerHTML = '<div id="mounter" />';

affixMounterWrapper = mount(<AffixMounter offsetBottom={0} />, {
attachTo: document.getElementById('mounter'),
});
const { container } = render(<AffixMounter offsetBottom={0} />);

await sleep(20);

await movePlaceholder(300);
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeTruthy();
expect(container.querySelector('.ant-affix')).toBeTruthy();

await movePlaceholder(0);
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeFalsy();
expect(container.querySelector('.ant-affix')).toBeFalsy();

await movePlaceholder(300);
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeTruthy();
expect(container.querySelector('.ant-affix')).toBeTruthy();
});

it('updatePosition when offsetTop changed', async () => {
document.body.innerHTML = '<div id="mounter" />';
const onChange = jest.fn();

affixMounterWrapper = mount(<AffixMounter offsetTop={0} onChange={onChange} />, {
attachTo: document.getElementById('mounter'),
});
const { container, rerender } = render(<AffixMounter offsetTop={0} onChange={onChange} />);
await sleep(20);

await movePlaceholder(-100);
expect(onChange).toHaveBeenLastCalledWith(true);
expect(affixMounterWrapper.instance().affix.state.affixStyle?.top).toBe(0);
affixMounterWrapper.setProps({
offsetTop: 10,
});
expect(container.querySelector('.ant-affix')).toHaveStyle({ top: 0 });

rerender(<AffixMounter offsetTop={10} onChange={onChange} />);
await sleep(20);
expect(affixMounterWrapper.instance().affix.state.affixStyle?.top).toBe(10);
expect(container.querySelector('.ant-affix')).toHaveStyle({ top: `10px` });
});

describe('updatePosition when target changed', () => {
Expand Down Expand Up @@ -201,7 +191,9 @@ describe('Affix Render', () => {
await sleep(20);

await movePlaceholder(300);
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeTruthy();
expect(
(affixMounterWrapper.find(AffixMounter).instance() as any).affix.state.affixStyle,
).toBeTruthy();
await sleep(20);
affixMounterWrapper.update();

Expand Down