forked from cypress-io/testing-workshop-cypress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec.answer.js
64 lines (60 loc) · 1.31 KB
/
spec.answer.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
/// <reference types="cypress" />
beforeEach(() => {
cy.request('POST', '/reset', {
todos: []
})
})
beforeEach(() => {
cy.visit('/')
})
beforeEach(function stubRandomId() {
let count = 1
cy.window()
.its('Math')
.then(Math => {
cy.stub(Math, 'random', () => {
return `0.${count++}`
}).as('random') // save reference to the spy
})
})
/**
* Adds a todo item
* @param {string} text
*/
const addItem = text => {
cy.get('.new-todo').type(`${text}{enter}`)
}
it('adds items to store', () => {
addItem('something')
addItem('something else')
cy.window()
.its('app.$store.state.todos')
.should('have.length', 2)
})
it('creates an item with id 1', () => {
cy.server()
cy.route('POST', '/todos').as('new-item')
addItem('something')
cy.wait('@new-item')
.its('request.body')
.should('deep.equal', {
id: '1',
title: 'something',
completed: false
})
})
it('calls spy twice', () => {
addItem('something')
addItem('else')
cy.get('@random').should('have.been.calledTwice')
})
it('puts todos in the store', () => {
addItem('something')
addItem('else')
cy.window()
.its('app.$store.state.todos')
.should('deep.equal', [
{ title: 'something', completed: false, id: '1' },
{ title: 'else', completed: false, id: '2' }
])
})