You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
197 lines
5.4 KiB
197 lines
5.4 KiB
5 years ago
|
import React from 'react';
|
||
4 years ago
|
import CSSMotion from 'rc-motion';
|
||
4 years ago
|
import { act } from 'react-dom/test-utils';
|
||
4 years ago
|
import { genCSSMotion } from 'rc-motion/lib/CSSMotion';
|
||
5 years ago
|
import Modal from '..';
|
||
|
import Button from '../../button';
|
||
3 years ago
|
import Input from '../../input';
|
||
|
import ConfigProvider from '../../config-provider';
|
||
3 years ago
|
import { render, fireEvent } from '../../../tests/utils';
|
||
|
import type { ModalFunc } from '../confirm';
|
||
5 years ago
|
|
||
|
jest.mock('rc-util/lib/Portal');
|
||
4 years ago
|
jest.mock('rc-motion');
|
||
5 years ago
|
|
||
|
describe('Modal.hook', () => {
|
||
4 years ago
|
// Inject CSSMotion to replace with No transition support
|
||
|
const MockCSSMotion = genCSSMotion(false);
|
||
|
Object.keys(MockCSSMotion).forEach(key => {
|
||
3 years ago
|
// @ts-ignore
|
||
4 years ago
|
CSSMotion[key] = MockCSSMotion[key];
|
||
|
});
|
||
|
|
||
5 years ago
|
it('hooks support context', () => {
|
||
|
jest.useFakeTimers();
|
||
|
const Context = React.createContext('light');
|
||
3 years ago
|
let instance: ReturnType<ModalFunc>;
|
||
5 years ago
|
|
||
|
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>
|
||
|
);
|
||
|
};
|
||
|
|
||
3 years ago
|
const { container } = render(<Demo />);
|
||
|
fireEvent.click(container.querySelectorAll('button')[0]);
|
||
5 years ago
|
|
||
3 years ago
|
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();
|
||
5 years ago
|
|
||
|
// Update instance
|
||
4 years ago
|
act(() => {
|
||
|
instance.update({
|
||
|
content: <div className="updated-content" />,
|
||
|
});
|
||
5 years ago
|
});
|
||
3 years ago
|
expect(document.body.querySelectorAll('.updated-content')).toHaveLength(1);
|
||
5 years ago
|
|
||
|
// Destroy
|
||
4 years ago
|
act(() => {
|
||
|
instance.destroy();
|
||
|
jest.runAllTimers();
|
||
|
});
|
||
3 years ago
|
expect(document.body.querySelectorAll('Modal')).toHaveLength(0);
|
||
5 years ago
|
|
||
|
jest.useRealTimers();
|
||
|
});
|
||
4 years ago
|
|
||
3 years ago
|
it('context support config direction', () => {
|
||
|
jest.useFakeTimers();
|
||
|
const Demo = () => {
|
||
|
const [modal, contextHolder] = Modal.useModal();
|
||
|
return (
|
||
|
<>
|
||
|
<Button
|
||
|
onClick={() => {
|
||
|
modal.confirm({
|
||
|
content: <Input />,
|
||
|
});
|
||
|
}}
|
||
|
/>
|
||
|
{contextHolder}
|
||
|
</>
|
||
|
);
|
||
|
};
|
||
|
|
||
3 years ago
|
const { container } = render(
|
||
3 years ago
|
<ConfigProvider direction="rtl">
|
||
|
<Demo />
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
|
|
||
3 years ago
|
fireEvent.click(container.querySelectorAll('button')[0]);
|
||
|
expect(document.body.querySelectorAll('.ant-input-rtl').length).toBeTruthy();
|
||
3 years ago
|
});
|
||
|
|
||
4 years ago
|
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>
|
||
|
);
|
||
|
};
|
||
|
|
||
3 years ago
|
const { container } = render(<Demo />);
|
||
4 years ago
|
|
||
3 years ago
|
fireEvent.click(container.querySelectorAll('.open-hook-modal-btn')[0]);
|
||
|
fireEvent.click(document.body.querySelectorAll('.ant-modal-confirm-btns .ant-btn')[0]);
|
||
4 years ago
|
expect(cancelCount).toEqual(1); // click cancel btn, trigger onCancel
|
||
|
|
||
3 years ago
|
fireEvent.click(container.querySelectorAll('.open-hook-modal-btn')[0]);
|
||
|
fireEvent.click(document.body.querySelectorAll('.ant-modal-wrap')[0]);
|
||
4 years ago
|
expect(cancelCount).toEqual(2); // click modal wrapper, trigger onCancel
|
||
|
});
|
||
4 years ago
|
|
||
|
it('update before render', () => {
|
||
|
const Demo = () => {
|
||
|
const [modal, contextHolder] = Modal.useModal();
|
||
|
|
||
|
const openBrokenModal = React.useCallback(() => {
|
||
|
const instance = modal.info({
|
||
|
title: 'Light',
|
||
|
});
|
||
|
|
||
|
instance.update({
|
||
|
title: 'Bamboo',
|
||
|
});
|
||
|
}, [modal]);
|
||
|
|
||
|
return (
|
||
|
<div className="App">
|
||
|
{contextHolder}
|
||
|
<div className="open-hook-modal-btn" onClick={openBrokenModal}>
|
||
|
Test hook modal
|
||
|
</div>
|
||
|
</div>
|
||
|
);
|
||
|
};
|
||
|
|
||
3 years ago
|
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',
|
||
|
);
|
||
4 years ago
|
});
|
||
|
|
||
|
it('destroy before render', () => {
|
||
|
const Demo = () => {
|
||
|
const [modal, contextHolder] = Modal.useModal();
|
||
|
|
||
|
const openBrokenModal = React.useCallback(() => {
|
||
|
const instance = modal.info({
|
||
|
title: 'Light',
|
||
|
});
|
||
|
|
||
|
instance.destroy();
|
||
|
}, [modal]);
|
||
|
|
||
|
return (
|
||
|
<div className="App">
|
||
|
{contextHolder}
|
||
|
<div className="open-hook-modal-btn" onClick={openBrokenModal}>
|
||
|
Test hook modal
|
||
|
</div>
|
||
|
</div>
|
||
|
);
|
||
|
};
|
||
|
|
||
3 years ago
|
const { container } = render(<Demo />);
|
||
|
fireEvent.click(container.querySelectorAll('.open-hook-modal-btn')[0]);
|
||
|
expect(document.body.classList.contains('ant-modal-confirm-title')).toBeFalsy();
|
||
4 years ago
|
});
|
||
5 years ago
|
});
|