UNPKG

2.68 kBJavaScriptView Raw
1import React from 'react';
2import { render, fireEvent, cleanup } from '@testing-library/react';
3import Upload from '..';
4
5afterEach(cleanup);
6
7describe('Upload', () => {
8 test('should render', () => {
9 const { container } = render(
10 <Upload clientId="a" bucketId="b" customerId="c" />
11 );
12
13 expect(container).toMatchSnapshot();
14 });
15
16 test('adding a file', () => {
17 const { getByTestId } = render(
18 <Upload clientId="a" bucketId="b" customerId="c" showFileDrop />
19 );
20
21 const file = new Buffer.from('hello world'.split('')); // eslint-disable-line new-cap
22 file.name = 'fileName.png';
23 const fileEvent = { target: { files: [file] } };
24
25 const inputNode = getByTestId('file-picker');
26
27 fireEvent.change(inputNode, fileEvent);
28
29 expect(inputNode.files.length).toBe(1);
30 });
31
32 test('removing a file', () => {
33 const { getByTestId, queryByTestId } = render(
34 <Upload clientId="a" bucketId="b" customerId="c" />
35 );
36
37 // Create a new file
38 const file = new Buffer.from('hello world'.split('')); // eslint-disable-line new-cap
39 file.name = 'fileName.png';
40
41 const fileEvent = { target: { files: [file] } };
42
43 const inputNode = getByTestId('file-picker');
44
45 // Simulate the upload to the Components
46 fireEvent.change(inputNode, fileEvent);
47
48 expect(inputNode.files.length).toBe(1);
49
50 const filerow = getByTestId('remove-file-btn');
51
52 fireEvent.click(filerow);
53
54 expect(queryByTestId('remove-file-btn')).toBeNull();
55 });
56
57 test('calls onFileRemove callback', () => {
58 const mockFunc = jest.fn();
59
60 const { getByTestId } = render(
61 <Upload
62 clientId="a"
63 bucketId="b"
64 customerId="c"
65 onFileRemove={mockFunc}
66 />
67 );
68
69 const inputNode = getByTestId('file-picker');
70
71 const file = new Buffer.from('hello world'.split('')); // eslint-disable-line new-cap
72 file.name = 'fileName.png';
73 const fileEvent = { target: { files: [file] } };
74
75 fireEvent.change(inputNode, fileEvent);
76
77 expect(inputNode.files.length).toBe(1);
78
79 const filerow = getByTestId('remove-file-btn');
80
81 fireEvent.click(filerow);
82
83 expect(mockFunc).toHaveBeenCalled();
84 });
85
86 test('adds file via dropzone', () => {
87 const { getByTestId } = render(
88 <Upload clientId="a" bucketId="b" customerId="c" showFileDrop />
89 );
90 const file = new Buffer.from('hello world'.split('')); // eslint-disable-line new-cap
91 file.name = 'fileName.png';
92
93 const inputNode = getByTestId('file-picker');
94 const fileEvent = { target: { files: [file] } };
95
96 fireEvent.drop(inputNode, fileEvent);
97
98 expect(inputNode.files.length).toBe(1);
99 });
100});