-
Notifications
You must be signed in to change notification settings - Fork 29
/
1-File.js
65 lines (61 loc) · 1.74 KB
/
1-File.js
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
import React from 'react';
import { Form, FormField, FileUpload } from 'elemental';
import api from '../../../client/lib/api';
const Test = React.createClass({
displayName: 'Upload File',
getInitialState () {
return {
file: null,
dataURI: null,
};
},
componentDidMount () {
this.props.ready();
},
handleFile (e, data) {
this.setState({
file: data.file,
dataURI: data.dataURI,
});
},
runTest () {
var formData = new window.FormData();
formData.append('name', 'Test ' + Date.now());
if (this.state.file) {
formData.append('file', 'upload:xyz123');
formData.append('xyz123', this.state.file);
}
api.post('/keystone/api/files/create', {
body: formData,
responseType: 'json',
}, (err, res, body) => {
this.props.result('Received response:', body);
if (this.state.file) {
this.props.assert('status code is 200').truthy(() => res.statusCode === 200);
// this.props.assert('file has been uploaded').truthy(() => body.fields.file.url.substr(0, 25) === 'http://res.cloudinary.com');
// this.props.complete({ gallery: body });
this.props.ready();
} else {
this.props.assert('status code is 400').truthy(() => res.statusCode === 400);
this.props.assert('error is "validation errors"').truthy(() => body.error === 'validation errors');
this.props.assert('file is required').truthy(() => body.detail.file.type === 'required');
this.props.ready();
}
});
},
render () {
return (
<Form type="horizontal">
<FormField label="File" style={localStyles.field}>
<FileUpload buttonLabelInitial="Upload File" buttonLabelChange="Change File" onChange={this.handleFile} />
</FormField>
</Form>
);
},
});
const localStyles = {
field: {
marginTop: 20,
},
};
module.exports = Test;