|
@@ -29,7 +29,7 @@ describe('GlobalModal', function () {
|
|
|
expect(screen.queryByTestId('modal-test')).not.toBeInTheDocument();
|
|
|
});
|
|
|
|
|
|
- it('calls onClose handler when modal is clicked out of', function () {
|
|
|
+ it('calls onClose handler when close button is clicked', function () {
|
|
|
renderGlobalModal();
|
|
|
const closeSpy = jest.fn();
|
|
|
|
|
@@ -49,6 +49,26 @@ describe('GlobalModal', function () {
|
|
|
expect(closeSpy).toHaveBeenCalled();
|
|
|
});
|
|
|
|
|
|
+ it('calls onClose handler when modal is clicked out of', function () {
|
|
|
+ renderGlobalModal();
|
|
|
+ const closeSpy = jest.fn();
|
|
|
+
|
|
|
+ act(() =>
|
|
|
+ openModal(
|
|
|
+ ({Header}) => (
|
|
|
+ <div id="modal-test">
|
|
|
+ <Header closeButton>Header</Header>Hi
|
|
|
+ </div>
|
|
|
+ ),
|
|
|
+ {onClose: closeSpy}
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ userEvent.click(screen.getByTestId('modal-backdrop'));
|
|
|
+
|
|
|
+ expect(closeSpy).toHaveBeenCalled();
|
|
|
+ });
|
|
|
+
|
|
|
it('calls onClose handler when escape key is pressed', function () {
|
|
|
renderGlobalModal();
|
|
|
const closeSpy = jest.fn();
|
|
@@ -85,8 +105,8 @@ describe('GlobalModal', function () {
|
|
|
expect(closeSpy).toHaveBeenCalled();
|
|
|
});
|
|
|
|
|
|
- it('calls ignores click out when the allowClickClose option is false', async function () {
|
|
|
- renderGlobalModal();
|
|
|
+ it("ignores click out with backdrop: 'static'", async function () {
|
|
|
+ const {waitForModalToHide} = renderGlobalModal();
|
|
|
render(<div data-test-id="outside-test">Hello</div>);
|
|
|
|
|
|
act(() =>
|
|
@@ -96,19 +116,106 @@ describe('GlobalModal', function () {
|
|
|
<Header closeButton>Header</Header>Hi
|
|
|
</div>
|
|
|
),
|
|
|
- {allowClickClose: false}
|
|
|
+ {backdrop: 'static'}
|
|
|
)
|
|
|
);
|
|
|
|
|
|
expect(screen.getByTestId('modal-test')).toBeInTheDocument();
|
|
|
|
|
|
+ // Clicking outside of modal doesn't close
|
|
|
userEvent.click(screen.getByTestId('outside-test'));
|
|
|
+ expect(screen.getByTestId('modal-test')).toBeInTheDocument();
|
|
|
|
|
|
+ // Pressing escape _does_ close
|
|
|
+ userEvent.keyboard('{Escape}');
|
|
|
expect(screen.getByTestId('modal-test')).toBeInTheDocument();
|
|
|
+ await waitForModalToHide();
|
|
|
+ });
|
|
|
|
|
|
- userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
|
|
|
+ it("ignores click out with closeEvents: 'escape-key'", async function () {
|
|
|
+ const {waitForModalToHide} = renderGlobalModal();
|
|
|
+ const closeSpy = jest.fn();
|
|
|
|
|
|
- await waitForElementToBeRemoved(screen.queryByTestId('modal-test'));
|
|
|
- expect(screen.queryByTestId('modal-test')).not.toBeInTheDocument();
|
|
|
+ act(() =>
|
|
|
+ openModal(
|
|
|
+ ({Header}) => (
|
|
|
+ <div data-test-id="modal-test">
|
|
|
+ <Header closeButton>Header</Header>Hi
|
|
|
+ </div>
|
|
|
+ ),
|
|
|
+ {closeEvents: 'escape-key', onClose: closeSpy}
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(screen.getByTestId('modal-test')).toBeInTheDocument();
|
|
|
+
|
|
|
+ // Clicking outside of modal doesn't close
|
|
|
+ userEvent.click(screen.getByTestId('modal-backdrop'));
|
|
|
+ expect(screen.getByTestId('modal-test')).toBeInTheDocument();
|
|
|
+ expect(closeSpy).not.toHaveBeenCalled();
|
|
|
+
|
|
|
+ // Pressing escape _does_ close
|
|
|
+ userEvent.keyboard('{Escape}');
|
|
|
+ await waitForModalToHide();
|
|
|
+ expect(closeSpy).toHaveBeenCalled();
|
|
|
+ });
|
|
|
+
|
|
|
+ it("ignores escape key with closeEvents: 'backdrop-click'", async function () {
|
|
|
+ const {waitForModalToHide} = renderGlobalModal();
|
|
|
+ const closeSpy = jest.fn();
|
|
|
+
|
|
|
+ act(() =>
|
|
|
+ openModal(
|
|
|
+ ({Header}) => (
|
|
|
+ <div data-test-id="modal-test">
|
|
|
+ <Header closeButton>Header</Header>Hi
|
|
|
+ </div>
|
|
|
+ ),
|
|
|
+ {closeEvents: 'backdrop-click', onClose: closeSpy}
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(screen.getByTestId('modal-test')).toBeInTheDocument();
|
|
|
+
|
|
|
+ // Pressing escape doesn't close
|
|
|
+ userEvent.keyboard('{Escape}');
|
|
|
+ expect(screen.getByTestId('modal-test')).toBeInTheDocument();
|
|
|
+ expect(closeSpy).not.toHaveBeenCalled();
|
|
|
+
|
|
|
+ // Clicking outside of modal _does_ close
|
|
|
+ userEvent.click(screen.getByTestId('modal-backdrop'));
|
|
|
+ expect(closeSpy).toHaveBeenCalled();
|
|
|
+ await waitForModalToHide();
|
|
|
+ });
|
|
|
+
|
|
|
+ it("ignores backdrop click and escape key when with closeEvents: 'none'", async function () {
|
|
|
+ const {waitForModalToHide} = renderGlobalModal();
|
|
|
+ const closeSpy = jest.fn();
|
|
|
+
|
|
|
+ act(() =>
|
|
|
+ openModal(
|
|
|
+ ({Header}) => (
|
|
|
+ <div data-test-id="modal-test">
|
|
|
+ <Header closeButton>Header</Header>Hi
|
|
|
+ </div>
|
|
|
+ ),
|
|
|
+ {closeEvents: 'none', onClose: closeSpy}
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(screen.getByTestId('modal-test')).toBeInTheDocument();
|
|
|
+
|
|
|
+ // Clicking outside of modal doesn't close
|
|
|
+ userEvent.click(screen.getByTestId('modal-backdrop'));
|
|
|
+ expect(closeSpy).not.toHaveBeenCalled();
|
|
|
+
|
|
|
+ // Pressing escape doesn't close
|
|
|
+ userEvent.keyboard('{Escape}');
|
|
|
+ expect(closeSpy).not.toHaveBeenCalled();
|
|
|
+
|
|
|
+ // Clicking an explicit close button does close
|
|
|
+ userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
|
|
|
+ expect(closeSpy).toHaveBeenCalled();
|
|
|
+ await waitForModalToHide();
|
|
|
});
|
|
|
});
|