Skip to content

Commit

Permalink
test(Modal): add missing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shervinchen committed Apr 22, 2024
1 parent d3e5dd5 commit 73c40b9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
15 changes: 15 additions & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
import '@testing-library/jest-dom';

beforeEach(() => {
let time = 0;
jest.spyOn(window, 'requestAnimationFrame').mockImplementation((callback) => {
setTimeout(() => {
time += 100;
callback(time);
});
return time;
});
});

afterEach(() => {
(window.requestAnimationFrame as jest.Mock).mockRestore();
});
2 changes: 1 addition & 1 deletion packages/Modal/ModalWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ModalWrapperProps {

const ModalWrapper: FC<PropsWithChildren<ModalWrapperProps>> = ({
visible,
className = '',
className,
children,
...restProps
}) => {
Expand Down
28 changes: 20 additions & 8 deletions packages/Modal/__tests__/Modal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React, { useState } from 'react';
import { fireEvent, render } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';
import { Button, Modal, ModalProps } from '../..';

describe('Modal', () => {
jest.useFakeTimers();

const closeHandler = jest.fn();

const Component = (props: ModalProps) => {
Expand Down Expand Up @@ -88,14 +90,14 @@ describe('Modal', () => {
test('should open modal and close modal when click target', () => {
const { getByTestId } = render(<Component closeOnOverlayClick={true} />);
fireEvent.click(getByTestId('openModal'));
setTimeout(() => {
expect('This is a modal.').toBeInTheDocument();
}, 50);
jest.runAllTimers();
expect(screen.getByText('This is a modal.')).toBeInTheDocument();
fireEvent.click(document.querySelector('.raw-modal-container'));
setTimeout(() => {
expect('This is a modal.').not.toBeInTheDocument();
expect(closeHandler).toHaveBeenCalledTimes(1);
}, 350);
jest.runAllTimers();
expect(
document.querySelector('.raw-modal-container')
).not.toBeInTheDocument();
expect(closeHandler).toHaveBeenCalledTimes(1);
});

test('should not close modal when disabled overlay clicked', () => {
Expand All @@ -104,4 +106,14 @@ describe('Modal', () => {
fireEvent.click(document.querySelector('.raw-modal-container'));
expect(closeHandler).toHaveBeenCalledTimes(0);
});

test('should not propagate the click event', () => {
const { getByTestId } = render(<Component closeOnOverlayClick={true} />);
fireEvent.click(getByTestId('openModal'));
const modalWrapper = screen.getByTestId('modalWrapper') as Element;
expect(screen.getByText('This is a modal.')).toBeInTheDocument();
fireEvent.click(modalWrapper);
expect(screen.getByText('This is a modal.')).toBeInTheDocument();
expect(closeHandler).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 73c40b9

Please sign in to comment.