forked from cypress-io/cypress-vue-unit-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter-vuex-spec.js
70 lines (58 loc) · 1.85 KB
/
counter-vuex-spec.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
66
67
68
69
70
// testing Vuex component
// https://github.com/vuejs/vuex/tree/dev/examples/counter
import Counter from '../../components/Counter.vue'
import store from '../../components/store'
import Vuex from 'vuex'
import mountVue from '../..'
/* eslint-env mocha */
describe('Vuex Counter', () => {
// configure component
const extensions = {
plugins: [Vuex],
components: {
Counter
}
}
// define component template
const template = '<counter />'
// define count get and set helpers
const getCount = () => Cypress.vue.$store.state.count
const setCount = value => Cypress.vue.$store.commit('set', value)
// initialize a fresh Vue app before each test
beforeEach(mountVue({template, store}, {extensions}))
it('starts with zero', () => {
cy.contains('0 times')
})
it('increments the counter on click of "+"', () => {
cy.contains('button', '+').click()
cy.contains('1 times')
})
it('decrements the counter on click of "-"', () => {
cy.contains('button', '-').click()
cy.contains('0 times')
})
it('increments the counter if count is odd', () => {
setCount(3) // start with an odd number
cy.contains('odd')
cy.contains('button', 'Increment if odd').as('btn').click()
cy.contains('even')
cy.get('@btn').click()
cy.contains('even')
})
it('asynchronously increments counter', () => {
const count = getCount()
// increment mutation is delayed by 1 second
// Cypress waits 4 seconds by default
cy.contains('button', 'Increment async').click()
cy.contains(`${count + 1} times`)
})
it('count is zero when input is cleared', () => {
cy.get('input').type(`{selectall}{backspace}`)
cy.contains('0 times')
}),
it('set count via input field', () => {
const count = 42
cy.get('input').type(`{selectall}{backspace}${count}`)
cy.contains(`${count} times`)
})
})