Skip to content

Modal tests #2377

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

Merged
merged 4 commits into from
Aug 14, 2023
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
5 changes: 5 additions & 0 deletions .storybook/preview-head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
// https://github.com/pmmmwh/react-refresh-webpack-plugin/issues/176#issuecomment-683150213
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => () => {};
</script>
17 changes: 17 additions & 0 deletions client/modules/IDE/components/Modal.stories.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import Modal from './Modal';

export default {
title: 'IDE/Modal',
component: Modal,
argTypes: {
onClose: { action: 'onClose' }
}
};

export const ModalDefault = {
args: {
title: 'Example Modal Title',
children: <p>Example modal body</p>
}
};
70 changes: 70 additions & 0 deletions client/modules/IDE/components/Modal.unit.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from 'react';
import { fireEvent, render, screen } from '../../../test-utils';
import Modal from './Modal';

describe('Modal', () => {
it('can render title', () => {
render(
<Modal title="Foo" closeAriaLabel="Foo" onClose={jest.fn()}>
Bar
</Modal>
);

expect(screen.getByRole('heading', { name: 'Foo' })).toBeVisible();
});

it('can render child content', () => {
render(
<Modal title="Foo" closeAriaLabel="Foo" onClose={jest.fn()}>
Bar
</Modal>
);

expect(screen.getByText('Bar')).toBeVisible();
});

it('can call onClose when close button clicked', () => {
const handleClose = jest.fn();
render(
<Modal title="Foo" closeAriaLabel="Foo" onClose={handleClose}>
Bar
</Modal>
);

const buttonEl = screen.getByRole('button');
fireEvent.click(buttonEl);

expect(handleClose).toHaveBeenCalledTimes(1);
});

it('can call onClose when click event occurs in a parent node', () => {
const handleClose = jest.fn();
render(
<div>
<h1>Parent</h1>
<Modal title="Foo" closeAriaLabel="Foo" onClose={handleClose}>
Bar
</Modal>
</div>
);

const headingEl = screen.getByRole('heading', { name: 'Parent' });
fireEvent.click(headingEl);

expect(handleClose).toHaveBeenCalledTimes(1);
});

it('can ignore click event for closing when it occurs inside component', () => {
const handleClose = jest.fn();
render(
<Modal title="Foo" closeAriaLabel="Foo" onClose={handleClose}>
Bar
</Modal>
);

const headingEl = screen.getByRole('heading', { name: 'Foo' });
fireEvent.click(headingEl);

expect(handleClose).not.toHaveBeenCalled();
});
});