forked from boos/BoosAPI
fix: wire site-created dialog cancel cleanup (#341)
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { act, create } from 'react-test-renderer';
|
||||
import SiteCreatedModal from './SiteCreatedModal.js';
|
||||
|
||||
describe('SiteCreatedModal', () => {
|
||||
it('routes native dialog cancel events through onClose cleanup', async () => {
|
||||
const onChoice = vi.fn();
|
||||
const onClose = vi.fn();
|
||||
const root = create(
|
||||
<SiteCreatedModal
|
||||
siteName="Demo Site"
|
||||
platform="new-api"
|
||||
onChoice={onChoice}
|
||||
onClose={onClose}
|
||||
/>,
|
||||
);
|
||||
|
||||
const dialog = root.root.findByType('dialog');
|
||||
expect(typeof dialog.props.onCancel).toBe('function');
|
||||
|
||||
const preventDefault = vi.fn();
|
||||
await act(async () => {
|
||||
dialog.props.onCancel({ preventDefault });
|
||||
});
|
||||
|
||||
expect(preventDefault).toHaveBeenCalledTimes(1);
|
||||
expect(onClose).toHaveBeenCalledTimes(1);
|
||||
expect(onChoice).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -27,18 +27,14 @@ export default function SiteCreatedModal({ siteName, platform, onChoice, onClose
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === 'Escape') {
|
||||
e.preventDefault();
|
||||
onChoice('later');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<dialog
|
||||
ref={dialogRef}
|
||||
className="modal"
|
||||
onKeyDown={handleKeyDown}
|
||||
onCancel={(event) => {
|
||||
event.preventDefault();
|
||||
onClose();
|
||||
}}
|
||||
onClick={(e) => {
|
||||
if (e.target === dialogRef.current) {
|
||||
onChoice('later');
|
||||
@@ -80,4 +76,4 @@ export default function SiteCreatedModal({ siteName, platform, onChoice, onClose
|
||||
</div>
|
||||
</dialog>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user