-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathelse.cy.js
112 lines (100 loc) · 2.96 KB
/
else.cy.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/// <reference types="cypress" />
// @ts-check
import '../../src'
describe('else branch', () => {
it('takes the if branch', () => {
cy.wrap(42).if('equal', 42).log('if branch').else().log('else branch')
cy.log('**built-in log**')
cy.wrap(42).if('equal', 42).log('if branch').else('else branch')
})
it('takes the else branch', () => {
cy.wrap(42).if('equal', 1).log('if branch').else().log('else branch')
cy.log('**built-in log**')
cy.wrap(42).if('equal', 1).log('if branch').else('else branch')
cy.log('**prints numbers**')
cy.wrap(42).if('equal', 1).log('if branch').else(42)
})
it('logs the else message', () => {
cy.spy(cy, 'log').as('log')
cy.wrap(true).if('false').else('else branch')
cy.get('@log').should('have.been.calledWith', 'else branch')
})
it('logs the default else message', () => {
cy.spy(cy, 'log').as('log')
cy.wrap(true).if('false').else()
cy.get('@log').should('not.have.been.called')
})
it('can have multiple if-else', () => {
cy.wrap(1)
.if('equal', 2)
.log('is 2')
.else()
.if('equal', 3)
.log('is 3')
.if('equal', 1)
.log('is 1')
})
it('attaches should', () => {
cy.wrap(1).if('equal', 1).should('equal', 1).else().should('equal', 2)
})
context('with checks', () => {
it('calls actions in the if branch', () => {
cy.wrap(42)
.if('equal', 42)
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
cy.get('@if').should('have.been.calledOnce')
cy.get('@else').should('not.be.called')
})
it('skips the entire ELSE chain', () => {
cy.visit('cypress/index.html')
cy.get('#load')
.if()
.log('found it')
.get('#load')
.click()
.else()
.log('ughh, why execute the else branch')
.then(() => {
throw new Error('no!!!')
})
})
it('skips the entire ELSE chain even if it has parent commands', () => {
cy.visit('cypress/index.html')
cy.get('#load')
.if()
.log('found it')
.get('#load')
.click()
.else()
.get('#load')
.log('ughh, why execute the else branch after a parent command')
.then(() => {
throw new Error('no!!!')
})
})
it('calls actions in the else branch', () => {
cy.wrap(42)
.if('equal', 1)
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
cy.get('@else').should('have.been.calledOnce')
cy.get('@if').should('not.be.called')
})
it('attaches the .then block correctly', () => {
cy.wrap(42)
.if('equal', 1)
.then(cy.spy().as('if'))
.else()
.then(() => {
cy.spy().as('else1')()
cy.spy().as('else2')()
})
cy.get('@else1').should('have.been.calledOnce')
cy.get('@else2').should('have.been.calledOnce')
cy.get('@if').should('not.be.called')
})
})
})