Skip to content

Commit 88ce9a9

Browse files
committed
Transform FileInputField jest tests into playwright. (#603)
1 parent 38424df commit 88ce9a9

File tree

39 files changed

+487
-83
lines changed

39 files changed

+487
-83
lines changed
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
import React from 'react';
2+
import {
3+
expect,
4+
test,
5+
} from '@playwright/experimental-ct-react';
6+
import {
7+
mixPropTests,
8+
propTests,
9+
} from '../../../../tests/playwright';
10+
import type {
11+
FileInputFieldForFormLayoutTests,
12+
FileInputFieldForFormLayoutTestsProps,
13+
FileInputFieldForRefTest,
14+
FileInputFieldForTest,
15+
FileInputFieldWithResetButtonForTest,
16+
} from './FileInputField.story';
17+
import { fileSelectedPropTest } from './_propTests/fileSelectedPropTest';
18+
19+
test.describe('FileInputField', () => {
20+
test.describe('base', () => {
21+
test.describe('visual', () => {
22+
[
23+
...propTests.defaultComponentPropTest,
24+
...mixPropTests([
25+
propTests.disabledPropTest,
26+
propTests.validationStatePropTest,
27+
]),
28+
...fileSelectedPropTest,
29+
...propTests.fullWidthPropTest,
30+
...propTests.helpTextPropTest,
31+
...propTests.isLabelVisiblePropTest,
32+
...propTests.labelPropTest,
33+
...propTests.layoutPropTest,
34+
...propTests.requiredPropTest,
35+
...propTests.sizePropTest,
36+
...propTests.validationTextPropTest,
37+
].forEach(({
38+
name,
39+
onBeforeTest,
40+
onBeforeSnapshot,
41+
props,
42+
}) => {
43+
test(name, async ({
44+
mount,
45+
page,
46+
}) => {
47+
if (onBeforeTest) {
48+
await onBeforeTest(page);
49+
}
50+
51+
const component = await mount(
52+
<FileInputFieldForTest
53+
{...props}
54+
/>,
55+
);
56+
57+
if (onBeforeSnapshot) {
58+
await onBeforeSnapshot(page, component);
59+
}
60+
61+
const screenshot = await component.screenshot();
62+
expect(screenshot).toMatchSnapshot();
63+
});
64+
});
65+
});
66+
67+
test.describe('non-visual', () => {
68+
test('id', async ({ mount }) => {
69+
const testId = 'testId';
70+
const testLabel = 'testLabel';
71+
const helpText = 'helpText';
72+
const validationText = 'validationText';
73+
74+
const component = await mount(
75+
<FileInputFieldForTest
76+
helpText={helpText}
77+
id={testId}
78+
label={testLabel}
79+
validationText={validationText}
80+
/>,
81+
);
82+
83+
expect(component.locator(`div[id="${testId}__root"]`)).toBeDefined();
84+
await expect(component.getByText(testLabel)).toHaveAttribute('id', `${testId}__labelText`);
85+
await expect(component.locator('input[type="file"]')).toHaveAttribute('id', testId);
86+
await expect(component.getByText(helpText)).toHaveAttribute('id', `${testId}__helpText`);
87+
await expect(component.getByText(validationText)).toHaveAttribute('id', `${testId}__validationText`);
88+
});
89+
90+
test('ref', async ({ mount }) => {
91+
const component = await mount(
92+
<FileInputFieldForRefTest
93+
testRefAttrName="test-ref"
94+
testRefAttrValue="test-ref-value"
95+
/>,
96+
);
97+
98+
await expect(component.locator('input[type="file"]')).toHaveAttribute('test-ref', 'test-ref-value');
99+
});
100+
});
101+
102+
test.describe('functionality', () => {
103+
test('Call onFilesChanged callback when file upload.', async ({ mount }) => {
104+
let called = false;
105+
106+
const component = await mount(
107+
<FileInputFieldForTest
108+
onFilesChanged={() => {
109+
called = true;
110+
}}
111+
/>,
112+
);
113+
114+
const virtualFile = {
115+
buffer: Buffer.from('This is test file.'),
116+
mimeType: 'text/plain',
117+
name: 'testfile.txt',
118+
};
119+
120+
const inputField = component.locator('input[type="file"]');
121+
await inputField.setInputFiles(virtualFile);
122+
123+
expect(called).toBe(true);
124+
});
125+
126+
test('Call onFilesChanged callback when file drag and drop into field.', async ({
127+
mount,
128+
page,
129+
}) => {
130+
const id = 'dropzoneId';
131+
let called = false;
132+
133+
const component = await mount(
134+
<FileInputFieldForTest
135+
id={id}
136+
onFilesChanged={() => {
137+
called = true;
138+
}}
139+
/>,
140+
);
141+
142+
const fileName = 'newFile.txt';
143+
const fileContent = 'This is a test file';
144+
145+
const dataTransfer = await page.evaluateHandle(({
146+
name,
147+
content,
148+
}) => {
149+
const dt = new DataTransfer();
150+
const file = new File(
151+
[content],
152+
name,
153+
{ type: 'text/plain' },
154+
);
155+
dt.items.add(file);
156+
return dt;
157+
}, {
158+
content: fileContent,
159+
name: fileName,
160+
});
161+
162+
const dropZone = component.locator(`div[id="${id}__root"]`);
163+
164+
await dropZone.dispatchEvent('dragenter', { dataTransfer });
165+
await dropZone.dispatchEvent('drop', { dataTransfer });
166+
167+
expect(called).toBe(true);
168+
});
169+
170+
test('Can upload multiple files.', async ({ mount }) => {
171+
let listLength = 0;
172+
173+
const component = await mount(
174+
<FileInputFieldForTest
175+
multiple
176+
onFilesChanged={(files: FileList) => {
177+
listLength = Object.keys(files).length;
178+
}}
179+
/>,
180+
);
181+
182+
const virtualFile1 = {
183+
buffer: Buffer.from('This is test file.'),
184+
mimeType: 'text/plain',
185+
name: 'testfile.txt',
186+
};
187+
188+
const virtualFile2 = {
189+
buffer: Buffer.from('This is another test file.'),
190+
mimeType: 'text/plain',
191+
name: 'testfile.txt',
192+
};
193+
194+
const inputField = component.locator('input[type="file"]');
195+
await inputField.setInputFiles([
196+
virtualFile1,
197+
virtualFile2,
198+
]);
199+
200+
expect(listLength).toBe(2);
201+
});
202+
203+
test('Can upload multiple files via drag and drop.', async ({
204+
mount,
205+
page,
206+
}) => {
207+
const id = 'dropzoneId';
208+
let numberOfCalls = 0;
209+
210+
const component = await mount(
211+
<FileInputFieldForTest
212+
id={id}
213+
multiple
214+
onFilesChanged={() => {
215+
numberOfCalls += 1;
216+
}}
217+
/>,
218+
);
219+
220+
const fileName1 = 'newFile1.txt';
221+
const fileContent1 = 'This is a test file';
222+
223+
const dataTransfer1 = await page.evaluateHandle(({
224+
name,
225+
content,
226+
}) => {
227+
const dt = new DataTransfer();
228+
const file = new File([content], name, { type: 'text/plain' });
229+
dt.items.add(file);
230+
return dt;
231+
}, {
232+
content: fileContent1,
233+
name: fileName1,
234+
});
235+
236+
const fileName2 = 'newFile2.txt';
237+
const fileContent2 = 'This another is a test file';
238+
239+
const dataTransfer2 = await page.evaluateHandle(({
240+
name,
241+
content,
242+
}) => {
243+
const dt = new DataTransfer();
244+
const file = new File([content], name, { type: 'text/plain' });
245+
dt.items.add(file);
246+
return dt;
247+
}, {
248+
content: fileContent2,
249+
name: fileName2,
250+
});
251+
252+
const dropZone = component.locator(`div[id="${id}__root"]`);
253+
254+
await dropZone.dispatchEvent('dragenter', { dataTransfer: dataTransfer1 });
255+
await dropZone.dispatchEvent('drop', { dataTransfer: dataTransfer1 });
256+
await page.waitForTimeout(1000);
257+
await dropZone.dispatchEvent('dragenter', { dataTransfer: dataTransfer2 });
258+
await dropZone.dispatchEvent('drop', { dataTransfer: dataTransfer2 });
259+
260+
expect(numberOfCalls).toBe(2);
261+
});
262+
263+
test('Able to reset selected file.', async ({ mount }) => {
264+
let keyLength;
265+
266+
const component = await mount(
267+
<FileInputFieldWithResetButtonForTest
268+
onFilesChanged={(files: FileList) => {
269+
keyLength = Object.keys(files).length;
270+
}}
271+
/>,
272+
);
273+
274+
const virtualFile = {
275+
buffer: Buffer.from('This is test file.'),
276+
mimeType: 'text/plain',
277+
name: 'testfile.txt',
278+
};
279+
280+
const inputField = component.locator('input[type="file"]');
281+
const resetButton = component.getByText('Reset');
282+
283+
await inputField.setInputFiles(virtualFile);
284+
expect(keyLength).toBe(1);
285+
286+
await resetButton.click();
287+
expect(keyLength).toBe(0);
288+
});
289+
});
290+
});
291+
292+
test.describe('formLayout', () => {
293+
test.describe('visual', () => {
294+
[
295+
...propTests.layoutPropTest,
296+
].forEach(({
297+
name,
298+
onBeforeTest,
299+
onBeforeSnapshot,
300+
props,
301+
}) => {
302+
test(name, async ({
303+
mount,
304+
page,
305+
}) => {
306+
if (onBeforeTest) {
307+
await onBeforeTest(page);
308+
}
309+
310+
const component = await mount(
311+
<FileInputFieldForFormLayoutTests
312+
{...props as unknown as FileInputFieldForFormLayoutTestsProps}
313+
/>,
314+
);
315+
316+
if (onBeforeSnapshot) {
317+
await onBeforeSnapshot(page, component);
318+
}
319+
320+
const screenshot = await component.screenshot();
321+
expect(screenshot).toMatchSnapshot();
322+
});
323+
});
324+
});
325+
});
326+
});
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)