Browse Source

test: move test cases to @testing/library for Modal (#35785)

* test: move test cases to @testing/library for Modal

* update

* fix: test

* test: add test case for mouse position

* chore: revert
pull/35832/head
Yuki Zhang 3 years ago
committed by GitHub
parent
commit
b46e839a5f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 76
      components/modal/__tests__/Modal.test.js
  2. 93
      components/modal/__tests__/Modal.test.tsx
  3. 0
      components/modal/__tests__/__snapshots__/Modal.test.tsx.snap
  4. 0
      components/modal/__tests__/__snapshots__/demo.test.ts.snap
  5. 0
      components/modal/__tests__/demo.test.ts
  6. 54
      components/modal/__tests__/hook.test.tsx

76
components/modal/__tests__/Modal.test.js

@ -1,76 +0,0 @@
import React from 'react';
import { mount } from 'enzyme';
import Modal from '..';
import Button from '../../button';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
jest.mock('rc-util/lib/Portal');
class ModalTester extends React.Component {
constructor(props) {
super(props);
this.state = { visible: false };
}
componentDidMount() {
this.setState({ visible: true }); // eslint-disable-line react/no-did-mount-set-state
}
saveContainer = container => {
this.container = container;
};
getContainer = () => this.container;
render() {
const { visible } = this.state;
return (
<div>
<div ref={this.saveContainer} />
<Modal {...this.props} visible={visible} getContainer={this.getContainer}>
Here is content of Modal
</Modal>
</div>
);
}
}
describe('Modal', () => {
mountTest(Modal);
rtlTest(Modal);
it('render correctly', () => {
const wrapper = mount(<ModalTester />);
expect(wrapper.render()).toMatchSnapshot();
});
it('render without footer', () => {
const wrapper = mount(<ModalTester footer={null} />);
expect(wrapper.render()).toMatchSnapshot();
});
it('onCancel should be called', () => {
const onCancel = jest.fn();
const wrapper = mount(<Modal visible onCancel={onCancel} />);
wrapper.find('.ant-btn').first().simulate('click');
expect(onCancel).toHaveBeenCalled();
});
it('onOk should be called', () => {
const onOk = jest.fn();
const wrapper = mount(<Modal visible onOk={onOk} />);
wrapper.find('.ant-btn').last().simulate('click');
expect(onOk).toHaveBeenCalled();
});
it('support closeIcon', () => {
const wrapper = mount(<Modal closeIcon={<a>closeIcon</a>} visible />);
expect(wrapper.render()).toMatchSnapshot();
});
it('danger type', () => {
const wrapper = mount(<Modal okType="danger" visible />);
expect(wrapper.find(Button).last().props().danger).toBeTruthy();
});
});

93
components/modal/__tests__/Modal.test.tsx

@ -0,0 +1,93 @@
import React from 'react';
import Modal from '..';
import type { ModalProps } from '..';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { render, fireEvent } from '../../../tests/utils';
jest.mock('rc-util/lib/Portal');
class ModalTester extends React.Component<ModalProps, { visible: boolean }> {
state = { visible: false };
componentDidMount() {
this.setState({ visible: true }); // eslint-disable-line react/no-did-mount-set-state
}
container = React.createRef<HTMLDivElement>();
getContainer = () => this.container?.current!;
render() {
const { visible } = this.state;
return (
<div>
<div ref={this.container} />
<Modal {...this.props} visible={visible} getContainer={this.getContainer}>
Here is content of Modal
</Modal>
</div>
);
}
}
describe('Modal', () => {
mountTest(Modal);
rtlTest(Modal);
it('support closeIcon', () => {
render(<Modal closeIcon={<a>closeIcon</a>} visible />);
expect(document.body.querySelectorAll('.ant-modal-root')[0]).toMatchSnapshot();
});
it('render correctly', () => {
const { asFragment } = render(<ModalTester />);
expect(asFragment().firstChild).toMatchSnapshot();
});
it('render without footer', () => {
const { asFragment } = render(<ModalTester footer={null} />);
expect(asFragment().firstChild).toMatchSnapshot();
});
it('onCancel should be called', () => {
const onCancel = jest.fn();
render(<Modal visible onCancel={onCancel} />);
fireEvent.click(document.body.querySelectorAll('.ant-btn')[0]);
expect(onCancel).toHaveBeenCalled();
});
it('onOk should be called', () => {
const onOk = jest.fn();
render(<Modal visible onOk={onOk} />);
const btns = document.body.querySelectorAll('.ant-btn');
fireEvent.click(btns[btns.length - 1]);
expect(onOk).toHaveBeenCalled();
});
it('danger type', () => {
render(<Modal okType="danger" okText="123" visible />);
const btns = document.body.querySelectorAll('.ant-btn');
expect(btns[btns.length - 1].classList.contains('ant-btn-dangerous')).toBeTruthy();
});
it('mouse position', () => {
const Demo = () => {
const [visible, setVisible] = React.useState(false);
const containerRef = React.useRef<HTMLDivElement>(null);
return (
<div ref={containerRef}>
<div id="trigger" onClick={() => setVisible(true)}>
click me
</div>
<Modal visible={visible} getContainer={() => containerRef.current!} />
</div>
);
};
const { container } = render(<Demo />);
fireEvent.click(container.querySelectorAll('#trigger')[0]);
expect(
(container.querySelectorAll('.ant-modal')[0] as HTMLDivElement).style.transformOrigin,
).toBeTruthy();
});
});

0
components/modal/__tests__/__snapshots__/Modal.test.js.snap → components/modal/__tests__/__snapshots__/Modal.test.tsx.snap

0
components/modal/__tests__/__snapshots__/demo.test.js.snap → components/modal/__tests__/__snapshots__/demo.test.ts.snap

0
components/modal/__tests__/demo.test.js → components/modal/__tests__/demo.test.ts

54
components/modal/__tests__/hook.test.js → components/modal/__tests__/hook.test.tsx

@ -2,11 +2,12 @@ import React from 'react';
import CSSMotion from 'rc-motion';
import { act } from 'react-dom/test-utils';
import { genCSSMotion } from 'rc-motion/lib/CSSMotion';
import { mount } from 'enzyme';
import Modal from '..';
import Button from '../../button';
import Input from '../../input';
import ConfigProvider from '../../config-provider';
import { render, fireEvent } from '../../../tests/utils';
import type { ModalFunc } from '../confirm';
jest.mock('rc-util/lib/Portal');
jest.mock('rc-motion');
@ -15,13 +16,14 @@ describe('Modal.hook', () => {
// Inject CSSMotion to replace with No transition support
const MockCSSMotion = genCSSMotion(false);
Object.keys(MockCSSMotion).forEach(key => {
// @ts-ignore
CSSMotion[key] = MockCSSMotion[key];
});
it('hooks support context', () => {
jest.useFakeTimers();
const Context = React.createContext('light');
let instance;
let instance: ReturnType<ModalFunc>;
const Demo = () => {
const [modal, contextHolder] = Modal.useModal();
@ -43,12 +45,12 @@ describe('Modal.hook', () => {
);
};
const wrapper = mount(<Demo />);
wrapper.find('button').simulate('click');
const { container } = render(<Demo />);
fireEvent.click(container.querySelectorAll('button')[0]);
expect(wrapper.find('.test-hook').text()).toEqual('bamboo');
expect(wrapper.find('.ant-btn').length).toBeTruthy();
expect(wrapper.find('.ant-modal-body').length).toBeTruthy();
expect(document.body.querySelectorAll('.test-hook')[0].textContent).toBe('bamboo');
expect(document.body.querySelectorAll('.ant-btn').length).toBeTruthy();
expect(document.body.querySelectorAll('.ant-modal-body').length).toBeTruthy();
// Update instance
act(() => {
@ -56,16 +58,14 @@ describe('Modal.hook', () => {
content: <div className="updated-content" />,
});
});
wrapper.update();
expect(wrapper.find('.updated-content')).toHaveLength(1);
expect(document.body.querySelectorAll('.updated-content')).toHaveLength(1);
// Destroy
act(() => {
instance.destroy();
jest.runAllTimers();
});
wrapper.update();
expect(wrapper.find('Modal')).toHaveLength(0);
expect(document.body.querySelectorAll('Modal')).toHaveLength(0);
jest.useRealTimers();
});
@ -88,14 +88,14 @@ describe('Modal.hook', () => {
);
};
const wrapper = mount(
const { container } = render(
<ConfigProvider direction="rtl">
<Demo />
</ConfigProvider>,
);
wrapper.find('button').simulate('click');
expect(wrapper.find('.ant-input-rtl').length).toBeTruthy();
fireEvent.click(container.querySelectorAll('button')[0]);
expect(document.body.querySelectorAll('.ant-input-rtl').length).toBeTruthy();
});
it('hooks modal should trigger onCancel', () => {
@ -125,14 +125,14 @@ describe('Modal.hook', () => {
);
};
const wrapper = mount(<Demo />);
const { container } = render(<Demo />);
wrapper.find('.open-hook-modal-btn').simulate('click');
wrapper.find('.ant-modal-confirm-btns .ant-btn').first().simulate('click');
fireEvent.click(container.querySelectorAll('.open-hook-modal-btn')[0]);
fireEvent.click(document.body.querySelectorAll('.ant-modal-confirm-btns .ant-btn')[0]);
expect(cancelCount).toEqual(1); // click cancel btn, trigger onCancel
wrapper.find('.open-hook-modal-btn').simulate('click');
wrapper.find('.ant-modal-wrap').simulate('click');
fireEvent.click(container.querySelectorAll('.open-hook-modal-btn')[0]);
fireEvent.click(document.body.querySelectorAll('.ant-modal-wrap')[0]);
expect(cancelCount).toEqual(2); // click modal wrapper, trigger onCancel
});
@ -160,10 +160,11 @@ describe('Modal.hook', () => {
);
};
const wrapper = mount(<Demo />);
wrapper.find('.open-hook-modal-btn').simulate('click');
expect(wrapper.find('.ant-modal-confirm-title').text()).toEqual('Bamboo');
const { container } = render(<Demo />);
fireEvent.click(container.querySelectorAll('.open-hook-modal-btn')[0]);
expect(document.body.querySelectorAll('.ant-modal-confirm-title')[0].textContent).toEqual(
'Bamboo',
);
});
it('destroy before render', () => {
@ -188,9 +189,8 @@ describe('Modal.hook', () => {
);
};
const wrapper = mount(<Demo />);
wrapper.find('.open-hook-modal-btn').simulate('click');
expect(wrapper.exists('.ant-modal-confirm-title')).toBeFalsy();
const { container } = render(<Demo />);
fireEvent.click(container.querySelectorAll('.open-hook-modal-btn')[0]);
expect(document.body.classList.contains('ant-modal-confirm-title')).toBeFalsy();
});
});
Loading…
Cancel
Save