Skip to content

Commit d7e82b8

Browse files
committed
chore(components): add tests for overlays
1 parent 302eb0b commit d7e82b8

5 files changed

Lines changed: 145 additions & 7 deletions

File tree

packages/components/src/Dialog/__tests__/Dialog.test.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { render } from '@testing-library/react';
1+
import { render, screen, waitFor } from '@testing-library/react';
22
import { createRef } from 'react';
33
import { testA11y } from '@interlay/test-utils';
4+
import userEvent from '@testing-library/user-event';
45

56
import { Dialog, DialogBody, DialogDivider, DialogFooter, DialogHeader } from '..';
67

@@ -43,4 +44,38 @@ describe('Dialog', () => {
4344
</Dialog>
4445
);
4546
});
47+
48+
it('should dialog sections', async () => {
49+
render(
50+
<Dialog>
51+
<DialogHeader>title</DialogHeader>
52+
<DialogDivider />
53+
<DialogBody>body</DialogBody>
54+
<DialogFooter>footer</DialogFooter>
55+
</Dialog>
56+
);
57+
58+
expect(screen.getByRole('heading', { level: 3, name: /title/i })).toBeInTheDocument();
59+
expect(screen.getByText(/body/i)).toBeInTheDocument();
60+
expect(screen.getByText(/footer/i)).toBeInTheDocument();
61+
});
62+
63+
it('should emit onClose using close btn', async () => {
64+
const handleClose = jest.fn();
65+
66+
render(
67+
<Dialog onClose={handleClose}>
68+
<DialogHeader>title</DialogHeader>
69+
<DialogDivider />
70+
<DialogBody>body</DialogBody>
71+
<DialogFooter>footer</DialogFooter>
72+
</Dialog>
73+
);
74+
75+
userEvent.click(screen.getByRole('button', { name: /dismiss/i }));
76+
77+
await waitFor(() => {
78+
expect(handleClose).toHaveBeenCalledTimes(1);
79+
});
80+
});
4681
});

packages/components/src/Modal/__tests__/Modal.test.tsx

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { render } from '@testing-library/react';
2-
import { createRef } from 'react';
1+
import { render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react';
2+
import { createRef, useState } from 'react';
33
import { testA11y } from '@interlay/test-utils';
4+
import userEvent from '@testing-library/user-event';
45

56
import { Modal, ModalBody, ModalDivider, ModalFooter, ModalHeader } from '..';
67

@@ -43,4 +44,106 @@ describe('Modal', () => {
4344
</Modal>
4445
);
4546
});
47+
48+
it('should control open state', async () => {
49+
const Component = () => {
50+
const [isOpen, setOpen] = useState(false);
51+
52+
return (
53+
<>
54+
<button onClick={() => setOpen(true)}>trigger</button>
55+
<Modal isOpen={isOpen} onClose={() => setOpen(false)}>
56+
<ModalHeader>title</ModalHeader>
57+
<ModalDivider />
58+
<ModalBody>body</ModalBody>
59+
<ModalFooter>footer</ModalFooter>
60+
</Modal>
61+
</>
62+
);
63+
};
64+
65+
render(<Component />);
66+
67+
userEvent.click(screen.getByRole('button', { name: /trigger/i }));
68+
69+
await waitFor(() => {
70+
expect(screen.getByRole('dialog', { name: /title/i }));
71+
});
72+
73+
userEvent.click(screen.getByRole('button', { name: /dismiss/i }));
74+
75+
await waitForElementToBeRemoved(screen.getByRole('dialog', { name: /title/i }));
76+
});
77+
78+
it('should modal sections', async () => {
79+
render(
80+
<Modal isOpen onClose={jest.fn}>
81+
<ModalHeader>title</ModalHeader>
82+
<ModalDivider />
83+
<ModalBody>body</ModalBody>
84+
<ModalFooter>footer</ModalFooter>
85+
</Modal>
86+
);
87+
88+
expect(screen.getByRole('heading', { level: 3, name: /title/i })).toBeInTheDocument();
89+
expect(screen.getByText(/body/i)).toBeInTheDocument();
90+
expect(screen.getByText(/footer/i)).toBeInTheDocument();
91+
});
92+
93+
it('should emit onClose using close btn', async () => {
94+
const handleClose = jest.fn();
95+
96+
render(
97+
<Modal isOpen onClose={handleClose}>
98+
<ModalHeader>title</ModalHeader>
99+
<ModalDivider />
100+
<ModalBody>body</ModalBody>
101+
<ModalFooter>footer</ModalFooter>
102+
</Modal>
103+
);
104+
105+
userEvent.click(screen.getByRole('button', { name: /dismiss/i }));
106+
107+
await waitFor(() => {
108+
expect(handleClose).toHaveBeenCalledTimes(1);
109+
});
110+
});
111+
112+
it('should emit onClose using ESC key', async () => {
113+
const handleClose = jest.fn();
114+
115+
render(
116+
<Modal isOpen onClose={handleClose}>
117+
<ModalHeader>title</ModalHeader>
118+
<ModalDivider />
119+
<ModalBody>body</ModalBody>
120+
<ModalFooter>footer</ModalFooter>
121+
</Modal>
122+
);
123+
124+
userEvent.keyboard('{Escape}');
125+
126+
await waitFor(() => {
127+
expect(handleClose).toHaveBeenCalledTimes(1);
128+
});
129+
});
130+
131+
it('should emit onClose by clicking modal wrapper', async () => {
132+
const handleClose = jest.fn();
133+
134+
render(
135+
<Modal isOpen onClose={handleClose}>
136+
<ModalHeader>title</ModalHeader>
137+
<ModalDivider />
138+
<ModalBody>body</ModalBody>
139+
<ModalFooter>footer</ModalFooter>
140+
</Modal>
141+
);
142+
143+
userEvent.click(screen.getByRole('dialog', { name: /title/i }).parentElement?.parentElement as any);
144+
145+
await waitFor(() => {
146+
expect(handleClose).toHaveBeenCalledTimes(1);
147+
});
148+
});
46149
});

packages/components/src/Overlay/OpenTransition.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ type OpenTransitionProps = Props & InheritAttrs;
2121
const OpenTransition = (props: OpenTransitionProps): any => {
2222
// Do not apply any transition if in chromatic (based on react-spectrum)
2323
if (process.env.NODE_ENV === 'test') {
24-
return Children.map(props.children, (child) => child && cloneElement(child as any, { isOpen: props.in }));
24+
// MEMO: removed { isOpen: props.in } because of error with prop for component that do not recognize it
25+
return Children.map(props.children, (child) => child && cloneElement(child as any));
2526
}
2627

2728
return (

packages/components/src/Overlay/__tests__/Overlay.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('Overlay', () => {
99
const ref = createRef<HTMLDivElement>();
1010

1111
const wrapper = render(
12-
<Overlay nodeRef={ref}>
12+
<Overlay isOpen nodeRef={ref}>
1313
<div />
1414
</Overlay>
1515
);

packages/components/src/Tooltip/__tests__/Tooltip.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { testA11y } from '@interlay/test-utils';
44

55
import { Tooltip } from '..';
66

7-
// FIXME: isOpen prop throwing error
8-
describe.skip('Tooltip', () => {
7+
describe('Tooltip', () => {
98
it('should render correctly', () => {
109
const wrapper = render(
1110
<Tooltip isOpen label='tooltip'>

0 commit comments

Comments
 (0)