Skip to content

Commit

Permalink
feat: test file upload, close #5
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Nov 27, 2017
1 parent 00fc33e commit deff47a
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
cypress/videos
.DS_Store
20 changes: 19 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global Vue, Vuex, axios */
/* global Vue, Vuex, axios, FileReader */
;(function () {
Vue.use(Vuex)
Vue.use(window['bootstrap-vue'])

function randomId () {
return Math.random()
Expand Down Expand Up @@ -84,6 +85,9 @@
// app Vue instance
const app = new Vue({
store,
data: {
file: null
},
el: '.todoapp',

created () {
Expand Down Expand Up @@ -116,6 +120,20 @@

removeTodo (todo) {
this.$store.dispatch('removeTodo', todo)
},

uploadTodos (e) {
// either set component data.file to test file
// or read it off the native event
const f = this.file || e.target.files[0]
const reader = new FileReader()
reader.onload = e => {
const list = JSON.parse(e.target.result)
list.forEach(todo => {
this.$store.commit('ADD_TODO', todo)
})
}
reader.readAsText(f)
}
}
})
Expand Down
22 changes: 17 additions & 5 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
[{
"id": "1",
"title": "do first thing",
"completed": false
}, {
"id": "2",
"title": "do second thing",
"completed": true
}, {
"id": "3",
"title": "one more thing",
"completed": false
}, {
"id": "4",
"title": "last item on the agenda",
"completed": false
}]
23 changes: 16 additions & 7 deletions cypress/integration/api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ describe('API', () => {
beforeEach(stubMathRandom)

it('receives empty list of items', () => {
cy.request('todos').its('body').should('deep.equal', [])
cy
.request('todos')
.its('body')
.should('deep.equal', [])
})

it('adds two items', () => {
Expand Down Expand Up @@ -150,11 +153,14 @@ describe('API', () => {

// thanks to stubbed random id generator
// we can "predict" what the TODO object is going to look like
cy.wait('@postTodo').its('request.body').should('deep.equal', {
title: 'first item',
completed: false,
id: '1'
})
cy
.wait('@postTodo')
.its('request.body')
.should('deep.equal', {
title: 'first item',
completed: false,
id: '1'
})
})

it('is deleting a todo item', () => {
Expand All @@ -168,7 +174,10 @@ describe('API', () => {

// go through the UI
enterTodo('first item') // id "1"
getTodoItems().first().find('.destroy').click({ force: true })
getTodoItems()
.first()
.find('.destroy')
.click({ force: true })

cy.wait('@deleteTodo')
})
Expand Down
Loading

0 comments on commit deff47a

Please sign in to comment.