Skip to content

Commit

Permalink
Merge branch 'main' into docs/article-about-contrast
Browse files Browse the repository at this point in the history
  • Loading branch information
mrosvik authored Apr 11, 2024
2 parents 39a115e + c15e95e commit edb382c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 24 deletions.
90 changes: 66 additions & 24 deletions packages/react/src/components/Modal/Modal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import { useRef } from 'react';
import { act, render as renderRtl, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { Button } from '../Button';

import type { ModalRootProps } from './ModalRoot';

import type { ModalDialogProps } from './';
import { Modal } from './';

const HEADER_TITLE = 'Modal header title';
const OPEN_MODAL = 'Open Modal';

const Comp = (args: Partial<ModalRootProps>) => {
const modalRef = useRef<HTMLDialogElement>(null);

const openModal = () => {
console.log(modalRef.current);
modalRef.current?.showModal();
};

const Comp = (args: Partial<ModalDialogProps>) => {
return (
<>
<Button onClick={openModal}>Open Modal</Button>
<Modal
{...args}
open
ref={modalRef}
>
{args.children}
</Modal>
<Modal.Root>
<Modal.Trigger>{OPEN_MODAL}</Modal.Trigger>
<Modal.Dialog {...args} />
</Modal.Root>
</>
);
};

const render = async (props: Partial<ModalRootProps> = {}) => {
const render = async (props: Partial<ModalDialogProps> = {}) => {
/* Flush microtasks */
await act(async () => {});
const user = userEvent.setup();
Expand All @@ -44,8 +30,60 @@ const render = async (props: Partial<ModalRootProps> = {}) => {
};

describe('Modal', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('should open the modal', async () => {
await render({
children: (
<>
<Modal.Header>{HEADER_TITLE}</Modal.Header>
</>
),
});
const spy = vi.spyOn(HTMLDialogElement.prototype, 'showModal');

expect(screen.queryByRole('dialog')).not.toBeInTheDocument();

const button = screen.getByRole('button', { name: OPEN_MODAL });
await userEvent.click(button);

expect(spy).toHaveBeenCalledTimes(1);

expect(screen.queryByRole('dialog')).toBeInTheDocument();
});

it('should open and close the modal', async () => {
await render({
children: (
<>
<Modal.Header>{HEADER_TITLE}</Modal.Header>
</>
),
});
const showSpy = vi.spyOn(HTMLDialogElement.prototype, 'showModal');
const closeSpy = vi.spyOn(HTMLDialogElement.prototype, 'close');

expect(screen.queryByRole('dialog')).not.toBeInTheDocument();

const button = screen.getByRole('button', { name: OPEN_MODAL });
await userEvent.click(button);

expect(showSpy).toHaveBeenCalledTimes(1);

expect(screen.queryByRole('dialog')).toBeInTheDocument();

const closeButton = screen.getByRole('button', { name: /close/i });
await userEvent.click(closeButton);

expect(closeSpy).toHaveBeenCalledTimes(1);

expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});

it('should render the modal', async () => {
await render();
await render({ open: true });
expect(screen.getByRole('dialog')).toBeInTheDocument();
});

Expand All @@ -56,12 +94,14 @@ describe('Modal', () => {
<Modal.Header>{HEADER_TITLE}</Modal.Header>
</>
),
open: true,
});
expect(screen.getByRole('button', { name: /close/i })).toBeInTheDocument();
});

it('should not render the close button when closeButton is false', async () => {
await render({
open: true,
children: (
<>
<Modal.Header closeButton={false}>{HEADER_TITLE}</Modal.Header>
Expand All @@ -75,6 +115,7 @@ describe('Modal', () => {

it('should render the header title', async () => {
await render({
open: true,
children: (
<>
<Modal.Header>{HEADER_TITLE}</Modal.Header>
Expand All @@ -87,6 +128,7 @@ describe('Modal', () => {
it('should render the header subtitle', async () => {
const headerSubtitle = 'Modal header subtitle';
await render({
open: true,
children: (
<>
<Modal.Header subtitle={headerSubtitle}>{HEADER_TITLE}</Modal.Header>
Expand All @@ -98,7 +140,7 @@ describe('Modal', () => {

it('should render the children', async () => {
const children = 'Modal children';
await render({ children });
await render({ children, open: true });
expect(screen.getByText(children)).toBeInTheDocument();
});
});
18 changes: 18 additions & 0 deletions test/vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,23 @@ class ResizeObserver {
}
window.ResizeObserver = ResizeObserver;

HTMLDialogElement.prototype.show = vi.fn(function mock(
this: HTMLDialogElement,
) {
this.open = true;
});

HTMLDialogElement.prototype.showModal = vi.fn(function mock(
this: HTMLDialogElement,
) {
this.open = true;
});

HTMLDialogElement.prototype.close = vi.fn(function mock(
this: HTMLDialogElement,
) {
this.open = false;
});

const { setScreenWidth } = mockMediaQuery(800);
setScreenWidth(800);

0 comments on commit edb382c

Please sign in to comment.