-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfinally.cy.js
124 lines (114 loc) · 3.05 KB
/
finally.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
113
114
115
116
117
118
119
120
121
122
123
124
/// <reference types="cypress" />
// @ts-check
import '../../src'
describe('finally', () => {
it('executes after the IF path', () => {
cy.visit('cypress/checkbox.html')
cy.get('#enrolled').if('not.checked').check().finally().should('be.checked')
})
it('executes after the ELSE path', () => {
cy.visit('cypress/checkbox.html')
cy.get('#agreed')
.if('not.checked')
.check()
.else()
.log('already checked')
.finally()
.should('be.checked')
})
it('executes the IF branch', () => {
cy.wrap(1)
.if('equal', 1)
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
.finally()
.then((/** @type number */ subject) => {
expect(subject, 'subject').to.equal(1)
})
.should('equal', 1)
cy.get('@if').should('have.been.calledOnce')
cy.get('@else').should('not.be.called')
})
it('executes the ELSE branch', () => {
cy.wrap(1)
.if('equal', 42)
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
.finally()
.should('equal', 1)
cy.get('@else').should('have.been.calledOnce')
cy.get('@if').should('not.be.called')
})
it('executes the FINALLY command', () => {
cy.wrap(1)
.if('equal', 42)
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
.finally()
.then(cy.spy().as('finally'))
cy.get('@else').should('have.been.calledOnce')
cy.get('@if').should('not.be.called')
cy.get('@finally').should('have.been.calledOnce')
})
it('yields the IF subject without ELSE branch', () => {
cy.wrap(1)
.if('equal', 1)
.then((n) => {
expect(n, 'if n').to.equal(1)
console.log('if path, n = %d', n)
return 101
})
.finally()
.should('equal', 101)
})
it('yields the IF subject', () => {
cy.wrap(1)
.if('equal', 1)
.then((n) => {
expect(n, 'if n').to.equal(1)
console.log('if path, n = %d', n)
return 101
})
.else()
.then(() => -1)
.finally()
.should('equal', 101)
})
it('yields the ELSE subject', () => {
cy.wrap(1)
.if('equal', 42)
.then((n) => {
expect(n, 'if n').to.equal(1)
console.log('if path, n = %d', n)
return 101
})
.else()
.then(() => -1)
.finally()
.should('equal', -1)
})
it('calls else command before finally', () => {
cy.wrap(1)
// .if() comes from the cypress-if plugin
// https://github.com/bahmutov/cypress-if
.if('equals', 2)
.log('if branch')
.then(cy.spy().as('if'))
.else()
.log('else branch')
.then(cy.spy().as('else'))
.finally()
.log('finally')
.then(cy.spy().as('finally'))
cy.get('@else').should('have.been.calledOnce')
cy.get('@finally').should('have.been.calledOnce')
cy.get('@if').should('not.be.called')
cy.log('**else was called before finally**')
cy.get('@finally').then((fin) => {
cy.get('@else').should('have.been.calledBefore', fin)
})
})
})