Skip to content

Commit c74417d

Browse files
committed
Transform Modal jest tests into playwright (#596)
1 parent 38424df commit c74417d

File tree

65 files changed

+967
-671
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+967
-671
lines changed
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
import React from 'react';
2+
import {
3+
expect,
4+
test,
5+
} from '@playwright/experimental-ct-react';
6+
import { propTests } from '../../../../tests/playwright';
7+
import {
8+
ModalForCallbackTest,
9+
ModalForTest,
10+
} from './Modal.story';
11+
import { positionPropTest } from './_propTests/Modal/positionPropTest';
12+
import { sizePropTest } from './_propTests/Modal/sizePropTest';
13+
14+
test.describe('Modal', () => {
15+
test.describe('visual', () => {
16+
[
17+
...propTests.defaultComponentPropTest,
18+
...propTests.feedbackColorPropTest,
19+
...positionPropTest,
20+
...sizePropTest,
21+
].forEach(({
22+
name,
23+
onBeforeTest,
24+
onBeforeSnapshot,
25+
props,
26+
}) => {
27+
test(name, async ({
28+
mount,
29+
page,
30+
}) => {
31+
if (onBeforeTest) {
32+
await onBeforeTest(page);
33+
}
34+
35+
const component = await mount(
36+
<ModalForTest
37+
{...props}
38+
/>,
39+
);
40+
41+
if (onBeforeSnapshot) {
42+
await onBeforeSnapshot(page, component);
43+
}
44+
45+
const screenshot = await component.screenshot({ animations: 'disabled' });
46+
expect(screenshot).toMatchSnapshot();
47+
});
48+
});
49+
});
50+
51+
test.describe('non-visual', () => {
52+
test('id', async ({ mount }) => {
53+
const testId = 'testId';
54+
55+
const component = await mount(
56+
<div>
57+
<ModalForTest id={testId} />
58+
</div>,
59+
);
60+
61+
await expect(component.locator('dialog')).toHaveAttribute('id', testId);
62+
});
63+
});
64+
65+
test.describe('functionality', () => {
66+
test('close on esc key press when allowCloseOnEscapeKey enabled', async ({ mount }) => {
67+
let called = false;
68+
69+
const component = await mount(
70+
<ModalForCallbackTest
71+
allowCloseOnEscapeKey
72+
closeButtonOnClick={() => {
73+
called = true;
74+
}}
75+
/>,
76+
);
77+
78+
const dialog = component.locator('dialog');
79+
await dialog.focus();
80+
await dialog.press('Escape');
81+
expect(called).toBe(true);
82+
});
83+
84+
test('do not close on esc key press when allowCloseOnEscapeKey disabled', async ({ mount }) => {
85+
let called = false;
86+
87+
const component = await mount(
88+
<ModalForCallbackTest
89+
allowCloseOnEscapeKey={false}
90+
closeButtonOnClick={() => {
91+
called = true;
92+
}}
93+
/>,
94+
);
95+
96+
const dialog = component.locator('dialog');
97+
await dialog.focus();
98+
await dialog.press('Escape');
99+
expect(called).toBe(false);
100+
});
101+
102+
test('call primary action on enter key press when input/select content focused', async ({ mount }) => {
103+
let called = false;
104+
105+
const component = await mount(
106+
<ModalForCallbackTest
107+
allowPrimaryActionOnEnterKey
108+
primaryButtonOnClick={() => {
109+
called = true;
110+
}}
111+
/>,
112+
);
113+
114+
const input = component.locator('input[name="input1"]');
115+
await input.focus();
116+
await input.press('Enter');
117+
expect(called).toBe(true);
118+
});
119+
120+
test('do not call primary action on enter key press when allowPrimaryActionOnEnterKey is disabled', async ({ mount }) => {
121+
let called = false;
122+
123+
const component = await mount(
124+
<ModalForCallbackTest
125+
allowPrimaryActionOnEnterKey={false}
126+
primaryButtonOnClick={() => {
127+
called = true;
128+
}}
129+
/>,
130+
);
131+
132+
const input = component.locator('input[name="input1"]');
133+
await input.focus();
134+
await input.press('Enter');
135+
expect(called).toBe(false);
136+
});
137+
138+
test('focus first input element when autoFocus enabled', async ({ mount }) => {
139+
const component = await mount(
140+
<ModalForCallbackTest autoFocus />,
141+
);
142+
143+
const input = component.locator('input[name="input1"]');
144+
await expect(input).toBeFocused();
145+
});
146+
147+
test('no focused element when autoFocus disabled', async ({ mount }) => {
148+
const component = await mount(
149+
<ModalForCallbackTest autoFocus={false} />,
150+
);
151+
152+
const promises: Promise<void>[] = [];
153+
const inputs = await component.locator('input').all();
154+
const buttons = await component.locator('button').all();
155+
156+
inputs.forEach((input) => promises.push(expect(input).not.toBeFocused()));
157+
buttons.forEach((button) => promises.push(expect(button).not.toBeFocused()));
158+
159+
await Promise.allSettled(promises);
160+
});
161+
162+
test('close on backdrop click when allowCloseOnBackdropClick enabled', async ({
163+
mount,
164+
page,
165+
}) => {
166+
let called = false;
167+
168+
const component = await mount(
169+
<ModalForCallbackTest
170+
allowCloseOnBackdropClick
171+
closeButtonOnClick={() => {
172+
called = true;
173+
}}
174+
/>,
175+
);
176+
177+
const dialog = component.locator('dialog');
178+
const box = await dialog.evaluate((element) => element.getBoundingClientRect());
179+
await page.mouse.click(box.x - 50, box.y - 50);
180+
await page.waitForTimeout(2000);
181+
expect(called).toBe(true);
182+
});
183+
184+
test('do not close on backdrop click when allowCloseOnBackdropClick disabled', async ({
185+
mount,
186+
page,
187+
}) => {
188+
let called = false;
189+
190+
const component = await mount(
191+
<div>
192+
<ModalForCallbackTest
193+
allowCloseOnBackdropClick={false}
194+
closeButtonOnClick={() => {
195+
called = true;
196+
}}
197+
/>
198+
</div>,
199+
);
200+
201+
const dialog = component.locator('dialog');
202+
const box = await dialog.evaluate((element) => element.getBoundingClientRect());
203+
await page.mouse.click(box.x - 50, box.y - 50);
204+
expect(called).toBe(false);
205+
});
206+
207+
test('render in portal when portalId defined', async ({
208+
mount,
209+
page,
210+
}) => {
211+
const portalId = 'portal-id';
212+
213+
await page.evaluate(() => {
214+
document.body.innerHTML += '<div id="portal-id" />';
215+
});
216+
217+
await mount(
218+
<ModalForTest portalId={portalId} />,
219+
);
220+
221+
const portalHTMLContent = await page
222+
.evaluate((id) => document.getElementById(id).innerHTML, portalId);
223+
224+
expect(portalHTMLContent).toContain('dialog');
225+
});
226+
});
227+
});
11.5 KB
Loading
11.5 KB
Loading
11.6 KB
Loading
11.5 KB
Loading
11.6 KB
Loading
11.5 KB
Loading
11.4 KB
Loading
11.4 KB
Loading
11.4 KB
Loading

0 commit comments

Comments
 (0)