Skip to content

Commit 36bb792

Browse files
lukasbrizabedrich-schindler
authored andcommitted
Transform FileInputField jest tests into playwright. (#603)
1 parent 6ddc1df commit 36bb792

File tree

43 files changed

+503
-96
lines changed

Some content is hidden

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

43 files changed

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

0 commit comments

Comments
 (0)