2020-02-03 13:00:35 +08:00
|
|
|
import React from 'react';
|
2020-09-29 19:33:37 +08:00
|
|
|
import CSSMotion from 'rc-motion';
|
|
|
|
import { genCSSMotion } from 'rc-motion/lib/CSSMotion';
|
2020-02-03 13:00:35 +08:00
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Modal from '..';
|
|
|
|
import Button from '../../button';
|
|
|
|
|
|
|
|
jest.mock('rc-util/lib/Portal');
|
2020-09-29 19:33:37 +08:00
|
|
|
jest.mock('rc-motion');
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
|
|
describe('Modal.hook', () => {
|
2020-09-29 19:33:37 +08:00
|
|
|
// Inject CSSMotion to replace with No transition support
|
|
|
|
const MockCSSMotion = genCSSMotion(false);
|
|
|
|
Object.keys(MockCSSMotion).forEach(key => {
|
|
|
|
CSSMotion[key] = MockCSSMotion[key];
|
|
|
|
});
|
|
|
|
|
2020-02-03 13:00:35 +08:00
|
|
|
it('hooks support context', () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const Context = React.createContext('light');
|
|
|
|
let instance;
|
|
|
|
|
|
|
|
const Demo = () => {
|
|
|
|
const [modal, contextHolder] = Modal.useModal();
|
|
|
|
return (
|
|
|
|
<Context.Provider value="bamboo">
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
instance = modal.confirm({
|
|
|
|
content: (
|
|
|
|
<Context.Consumer>
|
|
|
|
{name => <div className="test-hook">{name}</div>}
|
|
|
|
</Context.Consumer>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{contextHolder}
|
|
|
|
</Context.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(<Demo />);
|
|
|
|
wrapper.find('button').simulate('click');
|
|
|
|
|
|
|
|
expect(wrapper.find('.test-hook').text()).toEqual('bamboo');
|
2020-08-03 11:41:08 +08:00
|
|
|
expect(wrapper.find('.ant-btn').length).toBeTruthy();
|
|
|
|
expect(wrapper.find('.ant-modal-body').length).toBeTruthy();
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
|
|
// Update instance
|
|
|
|
instance.update({
|
|
|
|
content: <div className="updated-content" />,
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.updated-content')).toHaveLength(1);
|
|
|
|
|
|
|
|
// Destroy
|
|
|
|
instance.destroy();
|
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('Modal')).toHaveLength(0);
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2020-11-30 15:25:36 +08:00
|
|
|
|
|
|
|
it('hooks modal should trigger onCancel', () => {
|
|
|
|
let cancelCount = 0;
|
|
|
|
const Demo = () => {
|
|
|
|
const [modal, contextHolder] = Modal.useModal();
|
|
|
|
|
|
|
|
const openBrokenModal = React.useCallback(() => {
|
|
|
|
modal.info({
|
|
|
|
okType: 'default',
|
|
|
|
maskClosable: true,
|
|
|
|
okCancel: true,
|
|
|
|
onCancel: () => {
|
|
|
|
cancelCount += 1;
|
|
|
|
},
|
|
|
|
content: 'Hello!',
|
|
|
|
});
|
|
|
|
}, [modal]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="App">
|
|
|
|
{contextHolder}
|
|
|
|
<div className="open-hook-modal-btn" onClick={openBrokenModal}>
|
|
|
|
Test hook modal
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(<Demo />);
|
|
|
|
|
|
|
|
wrapper.find('.open-hook-modal-btn').simulate('click');
|
|
|
|
wrapper.find('.ant-modal-confirm-btns .ant-btn').first().simulate('click');
|
|
|
|
expect(cancelCount).toEqual(1); // click cancel btn, trigger onCancel
|
|
|
|
|
|
|
|
wrapper.find('.open-hook-modal-btn').simulate('click');
|
|
|
|
wrapper.find('.ant-modal-wrap').simulate('click');
|
|
|
|
expect(cancelCount).toEqual(2); // click modal wrapper, trigger onCancel
|
|
|
|
});
|
2020-02-03 13:00:35 +08:00
|
|
|
});
|