Skip to content

Commit

Permalink
chore: addtl test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz committed Apr 17, 2024
1 parent 5f5252b commit 183e527
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Modal/ModalLayer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function ModalLayer({
return null;
}

const handleClose = !isBlocking ? onClose : null;
const handleClose = isBlocking ? null : onClose;

return (
<ModalContextProvider onClose={onClose} isOpen={isOpen} isBlocking={isBlocking}>
Expand Down
55 changes: 47 additions & 8 deletions src/Modal/tests/ModalLayer.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { FocusOn } from 'react-focus-on';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

Expand All @@ -15,12 +16,12 @@ jest.mock('../Portal', () => function PortalMock(props) {
});

jest.mock('react-focus-on', () => ({
FocusOn: (props) => {
FocusOn: jest.fn().mockImplementation((props) => {
const { children, ...otherProps } = props;
return (
<focus-on data-testid="focus-on" {...otherProps}>{children}</focus-on>
);
},
}),
}));

function Dialog() {
Expand All @@ -32,6 +33,10 @@ function Dialog() {
}

describe('<ModalLayer />', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('when isOpen', () => {
const isOpen = true;
const closeFn = jest.fn();
Expand Down Expand Up @@ -72,30 +77,64 @@ describe('<ModalLayer />', () => {
expect(dialog).not.toBeInTheDocument();
});

describe('Backdrop', () => {
it('closes a non-blocking modal layer when clicked', async () => {
describe('Dismiss modal', () => {
it('closes a non-blocking modal layer when backdrop is clicked', () => {
const closeFn = jest.fn();
render(
<ModalLayer isOpen onClose={closeFn} isBlocking={false}>
<Dialog />
</ModalLayer>,
);

const backdrop = screen.getByTestId('modal-backdrop');
await userEvent.click(backdrop);
userEvent.click(backdrop);
expect(closeFn).toHaveBeenCalled();
});

it('does not close a blocking modal layer when clicked', async () => {
it('should configure FocusOn to close a non-blocking modal layer when Esc key is pressed', () => {
const closeFn = jest.fn();
render(
<ModalLayer isOpen onClose={closeFn} isBlocking={false}>
<Dialog />
</ModalLayer>,
);
expect(FocusOn).toHaveBeenCalledWith(
expect.objectContaining({
onEscapeKey: closeFn,
}),
// note: this 2nd function argument represents the
// `refOrContext` (in this case, the context value
// provided by `ModalContextProvider`).
{},
);
});

it('should not configure FocusOn to close a blocking modal layer when Esc key is pressed', () => {
const closeFn = jest.fn();
render(
<ModalLayer isOpen onClose={closeFn} isBlocking>
<Dialog />
</ModalLayer>,
);
expect(FocusOn).toHaveBeenCalledWith(
expect.objectContaining({
onEscapeKey: null,
}),
// note: this 2nd function argument represents the
// `refOrContext` (in this case, the context value
// provided by `ModalContextProvider`).
{},
);
});

it('does not close a blocking modal layer when backdrop is clicked', () => {
const closeFn = jest.fn();
render(
<ModalLayer isOpen onClose={closeFn} isBlocking>
<Dialog />
</ModalLayer>,
);
const backdrop = screen.getByTestId('modal-backdrop');
await userEvent.click(backdrop);
userEvent.click(backdrop);
expect(closeFn).not.toHaveBeenCalled();
});
});
Expand Down

0 comments on commit 183e527

Please sign in to comment.