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 1 commit
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: 6 additions & 6 deletions components/input/__tests__/focus.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { mount } from 'enzyme';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import { InputRef } from '../Input';
import Input from '..';
import { render, fireEvent } from '../../../tests/utils';

const { TextArea } = Input;

Expand Down Expand Up @@ -58,12 +59,11 @@ describe('Input.Focus', () => {
});

it('disabled should reset focus', () => {
const wrapper = mount(<Input allowClear />);
wrapper.find('input').simulate('focus');
expect(wrapper.exists('.ant-input-affix-wrapper-focused')).toBeTruthy();
const { rerender, container } = render(<Input allowClear />);
fireEvent.focus(container.querySelector('input')!);
expect(container.querySelector('.ant-input-affix-wrapper-focused')).toBeTruthy();

wrapper.setProps({ disabled: true });
wrapper.update();
expect(wrapper.exists('.ant-input-affix-wrapper-focused')).toBeFalsy();
rerender(<Input allowClear disabled />);
expect(container.querySelector('.ant-input-affix-wrapper-focused')).toBeFalsy();
});
});
10 changes: 7 additions & 3 deletions tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ Object.assign(Enzyme.ReactWrapper.prototype, {
// React.StrictMode wrapper
jest.mock('enzyme', () => {
const enzyme = jest.requireActual('enzyme');
const { StrictMode } = jest.requireActual('react');
const { StrictMode, cloneElement } = jest.requireActual('react');
const { mount, render } = enzyme;

function EnzymeWrapper({ children, ...props }) {
return <StrictMode>{cloneElement(children, props)}</StrictMode>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这么神奇,这样包一下有啥意义?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setProps 需要透传,否则 wrapper.setProps 的测试全部会挂掉

}

return {
...enzyme,
mount: (ui, ...args) => mount(<StrictMode>{ui}</StrictMode>, ...args),
render: (ui, ...args) => render(<StrictMode>{ui}</StrictMode>, ...args),
mount: (ui, ...args) => mount(<EnzymeWrapper>{ui}</EnzymeWrapper>, ...args),
render: (ui, ...args) => render(<EnzymeWrapper>{ui}</EnzymeWrapper>, ...args),
};
});