diff --git a/src/DialogWrap.tsx b/src/DialogWrap.tsx index 458f3c9d..ed787b20 100644 --- a/src/DialogWrap.tsx +++ b/src/DialogWrap.tsx @@ -13,7 +13,7 @@ import { IDialogPropTypes } from './IDialogPropTypes'; * */ const DialogWrap: React.FC = (props: IDialogPropTypes) => { - const { visible, getContainer, forceRender, destroyOnClose, afterClose } = props; + const { visible, getContainer, forceRender, destroyOnClose = false, afterClose } = props; const [animatedVisible, setAnimatedVisible] = React.useState(visible); React.useEffect(() => { @@ -42,6 +42,7 @@ const DialogWrap: React.FC = (props: IDialogPropTypes) => { {(childProps: IDialogChildProps) => ( { afterClose?.(); setAnimatedVisible(false); diff --git a/tests/index.spec.js b/tests/index.spec.js index a5996d93..4e3b8a18 100644 --- a/tests/index.spec.js +++ b/tests/index.spec.js @@ -57,34 +57,55 @@ describe('dialog', () => { expect(onClose).toHaveBeenCalledTimes(1); }); - it('destroy on hide should unmount child components on close', () => { - const wrapper = mount( - - - , - { attachTo: document.body }, - ); + describe('destroyOnClose', () => { + it('default is false', () => { + const wrapper = mount( + + + , + { attachTo: document.body }, + ); - // Show - wrapper.setProps({ visible: true }); - jest.runAllTimers(); - wrapper.update(); + act(() => { + wrapper.setProps({ visible: false }); + jest.runAllTimers(); + wrapper.update(); + }); - document.getElementsByClassName('.test-input').value = 'test'; - expect(document.getElementsByClassName('.test-input').value).toBe('test'); + expect(document.querySelectorAll('.test-destroy')).toHaveLength(1); - // Hide - wrapper.setProps({ visible: false }); - jest.runAllTimers(); - wrapper.update(); + wrapper.unmount(); + }); - // Show - wrapper.setProps({ visible: true }); - jest.runAllTimers(); - wrapper.update(); + it('destroy on hide should unmount child components on close', () => { + const wrapper = mount( + + + , + { attachTo: document.body }, + ); - expect(document.getElementsByClassName('.test-input').value).toBeUndefined(); - wrapper.unmount(); + // Show + wrapper.setProps({ visible: true }); + jest.runAllTimers(); + wrapper.update(); + + document.getElementsByClassName('.test-input').value = 'test'; + expect(document.getElementsByClassName('.test-input').value).toBe('test'); + + // Hide + wrapper.setProps({ visible: false }); + jest.runAllTimers(); + wrapper.update(); + + // Show + wrapper.setProps({ visible: true }); + jest.runAllTimers(); + wrapper.update(); + + expect(document.getElementsByClassName('.test-input').value).toBeUndefined(); + wrapper.unmount(); + }); }); it('esc to close', () => {