Skip to content

Commit 25f3046

Browse files
committed
Update tests
1 parent 736b863 commit 25f3046

File tree

2 files changed

+121
-10
lines changed

2 files changed

+121
-10
lines changed

src/file-upload-input/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import i18n from '@dojo/framework/core/middleware/i18n';
33
import { createICacheMiddleware } from '@dojo/framework/core/middleware/icache';
44
import { create, tsx } from '@dojo/framework/core/vdom';
55
import { Button } from '../button';
6+
import { formatAriaProperties } from '../common/util';
67
import { Label } from '../label';
78
import theme from '../middleware/theme';
89
import bundle from './nls/FileUploadInput';
@@ -28,6 +29,9 @@ export interface FileUploadInputProperties {
2829
/** If `true` file drag-n-drop is allowed. Default is `true` */
2930
allowDnd?: boolean;
3031

32+
/** Custom aria attributes */
33+
aria?: { [key: string]: string | null };
34+
3135
/** The `disabled` attribute of the input */
3236
disabled?: boolean;
3337

@@ -114,6 +118,7 @@ export const FileUploadInput = factory(function FileUploadInput({
114118
const {
115119
accept,
116120
allowDnd = true,
121+
aria = {},
117122
disabled = false,
118123
labelHidden = false,
119124
multiple = false,
@@ -160,6 +165,7 @@ export const FileUploadInput = factory(function FileUploadInput({
160165
}
161166

162167
function onChange(event: DojoEvent<HTMLInputElement>) {
168+
console.log('onChange', event);
163169
if (onValue && event.target.files && event.target.files.length) {
164170
onValue(Array.from(event.target.files));
165171
}
@@ -168,6 +174,8 @@ export const FileUploadInput = factory(function FileUploadInput({
168174
return (
169175
<div
170176
key="root"
177+
{...formatAriaProperties(aria)}
178+
aria-disabled={disabled}
171179
classes={[
172180
theme.variant(),
173181
fixedCss.root,
@@ -200,7 +208,7 @@ export const FileUploadInput = factory(function FileUploadInput({
200208
<input
201209
key="nativeInput"
202210
accept={accept}
203-
aria="hidden"
211+
aria-hidden={true}
204212
classes={[baseCss.hidden]}
205213
click={function() {
206214
const shouldClick = Boolean(icache.getOrSet('shouldClick', false));

src/file-upload-input/tests/unit/FileUploadInput.spec.tsx

Lines changed: 112 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assertion, renderer, wrap } from '@dojo/framework/testing/renderer';
33
import { Button } from '../../../button';
44
import { FileUploadInput } from '../../index';
55
import { Label } from '../../../label';
6-
import { noop } from '../../../common/tests/support/test-helpers';
6+
import { noop, stubEvent } from '../../../common/tests/support/test-helpers';
77

88
import bundle from '../../nls/FileUploadInput';
99
import * as baseCss from '../../../theme/default/base.m.css';
@@ -13,15 +13,19 @@ import * as fixedCss from '../../styles/file-upload-input.m.css';
1313
import * as labelCss from '../../../theme/default/label.m.css';
1414

1515
const { it, describe } = intern.getInterface('bdd');
16+
const { assert } = intern.getPlugin('chai');
1617
const { messages } = bundle;
1718

1819
describe('FileUploadInput', function() {
1920
const WrappedRoot = wrap('div');
2021
const WrappedWrapper = wrap('div');
22+
const WrappedInput = wrap('input');
23+
const WrappedButton = wrap(Button);
2124
const WrappedLabel = wrap('span');
2225

2326
const baseRootProperties = {
2427
key: 'root',
28+
'aria-disabled': false,
2529
classes: [null, fixedCss.root, css.root, false, false],
2630
ondragenter: noop,
2731
ondragover: noop,
@@ -32,10 +36,10 @@ describe('FileUploadInput', function() {
3236
return (
3337
<WrappedRoot {...baseRootProperties}>
3438
<WrappedWrapper classes={[css.wrapper]}>
35-
<input
39+
<WrappedInput
3640
key="nativeInput"
3741
accept={undefined}
38-
aria="hidden"
42+
aria-hidden={true}
3943
classes={[baseCss.hidden]}
4044
click={noop}
4145
disabled={false}
@@ -45,7 +49,7 @@ describe('FileUploadInput', function() {
4549
required={false}
4650
type="file"
4751
/>
48-
<Button
52+
<WrappedButton
4953
disabled={false}
5054
onClick={noop}
5155
theme={{
@@ -59,7 +63,7 @@ describe('FileUploadInput', function() {
5963
}}
6064
>
6165
{messages.chooseFiles}
62-
</Button>
66+
</WrappedButton>
6367

6468
<WrappedLabel classes={[css.dndLabel]}>{messages.orDropFilesHere}</WrappedLabel>
6569
</WrappedWrapper>
@@ -149,14 +153,34 @@ describe('FileUploadInput', function() {
149153
);
150154
});
151155

156+
it('renders disabled', function() {
157+
const r = renderer(function() {
158+
return <FileUploadInput disabled={true} />;
159+
});
160+
161+
r.expect(
162+
baseAssertion
163+
.setProperty(WrappedRoot, 'aria-disabled', true)
164+
.setProperty(WrappedRoot, 'classes', [
165+
null,
166+
fixedCss.root,
167+
css.root,
168+
false,
169+
css.disabled
170+
])
171+
.setProperty(WrappedInput, 'disabled', true)
172+
.setProperty(WrappedButton, 'disabled', true)
173+
);
174+
});
175+
152176
it('handles dragenter, dragleave, and the overlay', function() {
153177
const r = renderer(function() {
154178
return <FileUploadInput />;
155179
});
156180
const WrappedOverlay = wrap('div');
157181

158182
r.expect(baseAssertion);
159-
r.property(WrappedRoot, 'ondragenter', { preventDefault: noop });
183+
r.property(WrappedRoot, 'ondragenter', stubEvent);
160184

161185
r.expect(
162186
baseAssertion
@@ -178,9 +202,88 @@ describe('FileUploadInput', function() {
178202
})
179203
);
180204

181-
// TODO: enable when testing bug is fixed
182-
// https://github.com/dojo/framework/issues/839
183-
// r.property(WrappedOverlay, 'ondragleave');
205+
// TODO: enable when https://github.com/dojo/framework/pull/840 is merged
206+
// r.property(WrappedOverlay, 'ondragleave', stubEvent);
184207
// r.expect(baseAssertion);
185208
});
209+
210+
it('handles file drop event', function() {
211+
const testValues = [1, 2, 3];
212+
let receivedFiles: number[] = [];
213+
214+
function onValue(value: any[]) {
215+
receivedFiles = value;
216+
}
217+
218+
const r = renderer(function() {
219+
return <FileUploadInput onValue={onValue} />;
220+
});
221+
222+
r.expect(baseAssertion);
223+
r.property(WrappedRoot, 'ondrop', {
224+
preventDefault: noop,
225+
dataTransfer: {
226+
files: testValues
227+
}
228+
});
229+
r.expect(baseAssertion);
230+
231+
assert.sameOrderedMembers(receivedFiles, testValues);
232+
});
233+
234+
it('validates files based on "accept"', function() {
235+
const accept = 'image/jpeg,image/*,.gif';
236+
const testFiles = [
237+
{ name: 'file1.jpg', type: 'image/jpeg' }, // test direct match: image/jpeg
238+
{ name: 'file2.png', type: 'image/png' }, // test wildcard match: image/*
239+
{ name: 'file3.gif', type: 'bad/type' }, // test extension match: .gif
240+
{ name: 'file4.doc', type: 'application/word' } // test match failure
241+
];
242+
const validFiles = testFiles.slice(0, 3);
243+
let receivedFiles: Array<typeof testFiles[0]> = [];
244+
245+
function onValue(value: any[]) {
246+
receivedFiles = value;
247+
}
248+
249+
const r = renderer(function() {
250+
return <FileUploadInput onValue={onValue} accept={accept} />;
251+
});
252+
const acceptAssertion = baseAssertion.setProperty(WrappedInput, 'accept', accept);
253+
254+
r.expect(acceptAssertion);
255+
r.property(WrappedRoot, 'ondrop', {
256+
preventDefault: noop,
257+
dataTransfer: {
258+
files: testFiles
259+
}
260+
});
261+
r.expect(acceptAssertion);
262+
263+
assert.sameOrderedMembers(receivedFiles, validFiles);
264+
});
265+
266+
it('calls onValue when files are selected from input', function() {
267+
const testValues = [1, 2, 3];
268+
let receivedFiles: number[] = [];
269+
270+
function onValue(value: any[]) {
271+
receivedFiles = value;
272+
}
273+
274+
const r = renderer(function() {
275+
return <FileUploadInput onValue={onValue} />;
276+
});
277+
278+
r.expect(baseAssertion);
279+
r.property(WrappedInput, 'onchange', {
280+
target: {
281+
files: testValues
282+
}
283+
});
284+
// TODO: the queued onchange is not triggering because it is for a node with a different id than expected
285+
r.expect(baseAssertion);
286+
287+
assert.sameOrderedMembers(receivedFiles, testValues);
288+
});
186289
});

0 commit comments

Comments
 (0)