forked from symfony/ux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.test.ts
More file actions
160 lines (128 loc) · 6.14 KB
/
Copy pathcontroller.test.ts
File metadata and controls
160 lines (128 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
'use strict';
import { Application, Controller } from '@hotwired/stimulus';
import { getByTestId, waitFor } from '@testing-library/dom';
import user from '@testing-library/user-event';
import { clearDOM, mountDOM } from '@symfony/stimulus-testing';
import DropzoneController from '../src/controller';
// Controller used to check the actual controller was properly booted
class CheckController extends Controller {
connect() {
this.element.addEventListener('dropzone:connect', () => {
this.element.classList.add('connected');
});
}
}
const startStimulus = () => {
const application = Application.start();
application.register('check', CheckController);
application.register('dropzone', DropzoneController);
};
describe('DropzoneController', () => {
let container;
beforeEach(() => {
container = mountDOM(`
<div class="dropzone-container" data-controller="check dropzone" data-testid="container">
<input type="file"
style="display: none"
data-dropzone-target="input"
data-testid="input" multiple />
<div class="dropzone-placeholder"
data-dropzone-target="placeholder"
data-testid="placeholder">
Placeholder
</div>
<div class="dropzone-preview"
data-dropzone-target="preview"
data-testid="preview"
style="display: none">
<button type="button"
class="dropzone-preview-button"
data-dropzone-target="previewClearButton"
data-testid="button"></button>
<div class="dropzone-preview-image"
data-dropzone-target="previewImage"
data-testid="preview-image"
style="display: none"></div>
<div class="dropzone-preview-filename"
data-dropzone-target="previewFilename"
data-testid="preview-filename"></div>
</div>
</div>
`);
});
afterEach(() => {
clearDOM();
});
it('connect', async () => {
expect(getByTestId(container, 'container')).not.toHaveClass('connected');
startStimulus();
await waitFor(() => expect(getByTestId(container, 'container')).toHaveClass('connected'));
});
it('clear', async () => {
startStimulus();
await waitFor(() => expect(getByTestId(container, 'input')).toHaveStyle({ display: 'block' }));
// Attach a listener to ensure the event is dispatched
let dispatched = false;
getByTestId(container, 'container').addEventListener('dropzone:clear', () => (dispatched = true));
// Manually show preview
getByTestId(container, 'input').style.display = 'none';
getByTestId(container, 'placeholder').style.display = 'none';
getByTestId(container, 'preview').style.display = 'block';
// Click the clear button
getByTestId(container, 'button').click();
await waitFor(() => expect(getByTestId(container, 'input')).toHaveStyle({ display: 'block' }));
await waitFor(() => expect(getByTestId(container, 'placeholder')).toHaveStyle({ display: 'block' }));
await waitFor(() => expect(getByTestId(container, 'preview')).toHaveStyle({ display: 'none' }));
// The event should have been dispatched
expect(dispatched).toBe(true);
});
it('single file chosen', async () => {
startStimulus();
await waitFor(() => expect(getByTestId(container, 'input')).toHaveStyle({ display: 'block' }));
// Attach a listener to ensure the event is dispatched
let dispatched = null;
getByTestId(container, 'container').addEventListener('dropzone:change', (event) => (dispatched = event));
// Select the file
const input = getByTestId(container, 'input');
const file = new File(['hello'], 'hello.png', { type: 'image/png' });
user.upload(input, file);
expect(input.files[0]).toStrictEqual(file);
// The dropzone should be in preview mode
await waitFor(() => expect(getByTestId(container, 'input')).toHaveStyle({ display: 'none' }));
await waitFor(() => expect(getByTestId(container, 'placeholder')).toHaveStyle({ display: 'none' }));
// The event should have been dispatched
expect(dispatched).not.toBeNull();
expect(dispatched.detail[0]).toStrictEqual(file);
});
it('multiple files chosen', async () => {
startStimulus();
await waitFor(() => expect(getByTestId(container, 'input')).toHaveStyle({ display: 'block' }));
// Attach a listener to ensure the event is dispatched
let dispatched = null;
getByTestId(container, 'container').addEventListener('dropzone:change', (event) => (dispatched = event));
// Select the file
const input = getByTestId(container, 'input');
const files = [
new File(['hello'], 'hello.png', { type: 'image/png' }),
new File(['again'], 'again.png', { type: 'image/png' }),
]
user.upload(input, files);
expect(input.files[0]).toStrictEqual(files[0]);
expect(input.files[1]).toStrictEqual(files[1]);
// The dropzone should be in preview mode
await waitFor(() => expect(getByTestId(container, 'input')).toHaveStyle({ display: 'none' }));
await waitFor(() => expect(getByTestId(container, 'placeholder')).toHaveStyle({ display: 'none' }));
// The event should have been dispatched
expect(dispatched).not.toBeNull();
expect(dispatched.detail[0]).toStrictEqual(files[0]);
expect(dispatched.detail[1]).toStrictEqual(files[1]);
});
});