Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/DialogWrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { IDialogPropTypes } from './IDialogPropTypes';
* */

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

React.useEffect(() => {
Expand Down Expand Up @@ -42,6 +42,7 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props: IDialogPropTypes) => {
{(childProps: IDialogChildProps) => (
<Dialog
{...props}
destroyOnClose={destroyOnClose}
afterClose={() => {
afterClose?.();
setAnimatedVisible(false);
Expand Down
67 changes: 44 additions & 23 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,55 @@ describe('dialog', () => {
expect(onClose).toHaveBeenCalledTimes(1);
});

it('destroy on hide should unmount child components on close', () => {
const wrapper = mount(
<Dialog destroyOnClose>
<input className="test-input" />
</Dialog>,
{ attachTo: document.body },
);
describe('destroyOnClose', () => {
it('default is false', () => {
const wrapper = mount(
<Dialog visible>
<input className="test-destroy" />
</Dialog>,
{ 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(
<Dialog destroyOnClose>
<input className="test-input" />
</Dialog>,
{ 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', () => {
Expand Down