-
-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathcontroller.test.ts
226 lines (183 loc) · 9.54 KB
/
controller.test.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { Application, Controller } from '@hotwired/stimulus';
import { clearDOM, mountDOM } from '@symfony/stimulus-testing';
import { getByTestId, waitFor } from '@testing-library/dom';
import user from '@testing-library/user-event';
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: HTMLElement;
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" />
<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>
<div class="dropzone-container" data-controller="check dropzone" data-testid="container-multiple">
<input type="file"
style="display: none"
multiple="multiple"
data-dropzone-target="input"
data-testid="input-multiple" />
<div class="dropzone-placeholder"
data-dropzone-target="placeholder"
data-testid="placeholder-multiple">
Placeholder
</div>
<div class="dropzone-preview"
data-dropzone-target="preview"
data-testid="preview-multiple"
style="display: none">
<button type="button"
class="dropzone-preview-button"
data-dropzone-target="previewClearButton"
data-testid="button-multiple"></button>
<div class="dropzone-preview-image"
data-dropzone-target="previewImage"
data-testid="preview-image-multiple"
style="display: none"></div>
<div class="dropzone-preview-filename"
data-dropzone-target="previewFilename"
data-testid="preview-filename-multiple"></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('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);
await waitFor(() => 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('on drag', async () => {
startStimulus();
// Simulate dragenter event
const dragEnterEvent = new Event('dragenter');
getByTestId(container, 'container').dispatchEvent(dragEnterEvent);
// Check that the input and placeholder are visible, and preview hidden
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' }));
// Simulate dragleave event with relatedTarget set to outside the dropzone
const dragLeaveEvent = new Event('dragleave', { bubbles: true });
Object.defineProperty(dragLeaveEvent, 'relatedTarget', { value: document.body });
getByTestId(container, 'container').dispatchEvent(dragLeaveEvent);
// Check that the input and placeholder are hidden, and preview shown
await waitFor(() => expect(getByTestId(container, 'input')).toHaveStyle({ display: 'none' }));
await waitFor(() => expect(getByTestId(container, 'placeholder')).toHaveStyle({ display: 'none' }));
await waitFor(() => expect(getByTestId(container, 'preview')).toHaveStyle({ display: 'block' }));
});
it('multiple files chosen', async () => {
startStimulus();
await waitFor(() => expect(getByTestId(container, 'container-multiple')).toHaveClass('connected'));
// Attach a listener to ensure the event is dispatched
let dispatched = null;
getByTestId(container, 'container-multiple').addEventListener('dropzone:change', (event) => {
dispatched = event;
});
// Select multiple files
const input = getByTestId(container, 'input-multiple');
const file1 = new File(['hello1'], 'hello1.png', { type: 'image/png' });
const file2 = new File(['hello2'], 'hello2.txt', { type: 'text/plain' });
const files = [file1, file2];
user.upload(input, files);
// The dropzone should be in preview mode
await waitFor(() => expect(getByTestId(container, 'input-multiple')).toHaveStyle({ display: 'none' }));
await waitFor(() => expect(getByTestId(container, 'placeholder-multiple')).toHaveStyle({ display: 'none' }));
await waitFor(() => expect(getByTestId(container, 'preview-multiple')).toHaveStyle({ display: 'flex' }));
// The event should have been dispatched with both files
expect(dispatched).not.toBeNull();
expect(dispatched.detail[0]).toStrictEqual(file1);
expect(dispatched.detail[1]).toStrictEqual(file2);
// Check preview content shows first file name plus count
const previewFilename = getByTestId(container, 'preview-filename-multiple');
expect(previewFilename.textContent).toBe('hello1.png +1 file');
// Only the first file (image) should show preview
const previewImage = getByTestId(container, 'preview-image-multiple');
await waitFor(() => expect(previewImage).toHaveStyle({ display: 'block' }));
});
});