forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
91 lines (82 loc) · 2.87 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>File Upload</title>
</head>
<body>
<p>
File upload testing using
<a href="https://www.cypress.io/">Cypress Test Runner</a>.
See the source of this page (React component) and the Cypress integration
spec file.
</p>
<div id="app"></div>
<script src="https://fb.me/react-15.0.0.js"></script>
<script src="https://fb.me/react-dom-15.0.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/papaparse@latest/papaparse.min.js"></script>
<script type="text/babel">
let FileUploadApp = React.createClass({
render: function() {
return (
<main>
<div>
<h3>File upload</h3>
<input id="file1" type="file" onChange={this.fileUpload}/>
</div>
<div>
<h3>CSV File parse</h3>
<input id="csv-file" type="file" onChange={this.csvFileParse}/>
</div>
</main>
)
},
getFileToUpload (e) {
// either upload file directly from the event (from a test)
// or from the DOM file input list
return e.nativeEvent.testFile || e.nativeEvent.target.files[0]
},
fileUpload (e) {
const file = this.getFileToUpload(e)
const data = new FormData()
// log just for the demo
console.log('uploading file', file.name)
console.log('file object', file)
// https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
// FormData.append(name, value, filename)
data.append("file", file, file.name)
axios.post('https://some-server.com/upload', data)
},
csvFileParse (e) {
const file = e.nativeEvent.target.files[0];
console.log('file', file)
// parse CSV file using
// https://www.papaparse.com/
Papa.parse(file, {
delimiter: ',',
header: true,
skipEmptyLines: true,
complete (results) {
console.log('finished parsing CSV file')
console.log(results)
// set the results on the window object
// and the tests can verify them
window.__csvResults = results
}
})
}
});
let app = ReactDOM.render(
<FileUploadApp/>,
document.getElementById('app')
);
// if the application is being tested from Cypress
// pass the reference to the application instance
// via "window" object, so the tests can directly interact with the app
if (window.Cypress) {
window.app = app
}
</script>
</body>
</html>