Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Dialog should not auto destroy #206

Merged
merged 2 commits into from Oct 14, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/DialogWrap.tsx
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
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