Skip to content

Commit 44fb747

Browse files
authored
fix: Dialog should not auto destroy (#206)
* fix: default destoryOnClose false * test: add test case
1 parent af18417 commit 44fb747

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

src/DialogWrap.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { IDialogPropTypes } from './IDialogPropTypes';
1313
* */
1414

1515
const DialogWrap: React.FC<IDialogPropTypes> = (props: IDialogPropTypes) => {
16-
const { visible, getContainer, forceRender, destroyOnClose, afterClose } = props;
16+
const { visible, getContainer, forceRender, destroyOnClose = false, afterClose } = props;
1717
const [animatedVisible, setAnimatedVisible] = React.useState<boolean>(visible);
1818

1919
React.useEffect(() => {
@@ -42,6 +42,7 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props: IDialogPropTypes) => {
4242
{(childProps: IDialogChildProps) => (
4343
<Dialog
4444
{...props}
45+
destroyOnClose={destroyOnClose}
4546
afterClose={() => {
4647
afterClose?.();
4748
setAnimatedVisible(false);

tests/index.spec.js

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,55 @@ describe('dialog', () => {
5757
expect(onClose).toHaveBeenCalledTimes(1);
5858
});
5959

60-
it('destroy on hide should unmount child components on close', () => {
61-
const wrapper = mount(
62-
<Dialog destroyOnClose>
63-
<input className="test-input" />
64-
</Dialog>,
65-
{ attachTo: document.body },
66-
);
60+
describe('destroyOnClose', () => {
61+
it('default is false', () => {
62+
const wrapper = mount(
63+
<Dialog visible>
64+
<input className="test-destroy" />
65+
</Dialog>,
66+
{ attachTo: document.body },
67+
);
6768

68-
// Show
69-
wrapper.setProps({ visible: true });
70-
jest.runAllTimers();
71-
wrapper.update();
69+
act(() => {
70+
wrapper.setProps({ visible: false });
71+
jest.runAllTimers();
72+
wrapper.update();
73+
});
7274

73-
document.getElementsByClassName('.test-input').value = 'test';
74-
expect(document.getElementsByClassName('.test-input').value).toBe('test');
75+
expect(document.querySelectorAll('.test-destroy')).toHaveLength(1);
7576

76-
// Hide
77-
wrapper.setProps({ visible: false });
78-
jest.runAllTimers();
79-
wrapper.update();
77+
wrapper.unmount();
78+
});
8079

81-
// Show
82-
wrapper.setProps({ visible: true });
83-
jest.runAllTimers();
84-
wrapper.update();
80+
it('destroy on hide should unmount child components on close', () => {
81+
const wrapper = mount(
82+
<Dialog destroyOnClose>
83+
<input className="test-input" />
84+
</Dialog>,
85+
{ attachTo: document.body },
86+
);
8587

86-
expect(document.getElementsByClassName('.test-input').value).toBeUndefined();
87-
wrapper.unmount();
88+
// Show
89+
wrapper.setProps({ visible: true });
90+
jest.runAllTimers();
91+
wrapper.update();
92+
93+
document.getElementsByClassName('.test-input').value = 'test';
94+
expect(document.getElementsByClassName('.test-input').value).toBe('test');
95+
96+
// Hide
97+
wrapper.setProps({ visible: false });
98+
jest.runAllTimers();
99+
wrapper.update();
100+
101+
// Show
102+
wrapper.setProps({ visible: true });
103+
jest.runAllTimers();
104+
wrapper.update();
105+
106+
expect(document.getElementsByClassName('.test-input').value).toBeUndefined();
107+
wrapper.unmount();
108+
});
88109
});
89110

90111
it('esc to close', () => {

0 commit comments

Comments
 (0)