-
Notifications
You must be signed in to change notification settings - Fork 11
/
each-spec.js
163 lines (153 loc) · 4.07 KB
/
each-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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// @ts-check
/// <reference path="../../../src/index.d.ts" />
// in the user's code this import would be
// import { each } from 'cypress-recurse'
import { each, recurse } from '../../../src'
describe('each', { viewportWidth: 200 }, () => {
it('iterates over each row until it finds number 7', () => {
cy.visit('cypress/e2e/each/index.html')
cy.get('#lotto tbody tr button')
.should('have.length.greaterThan', 10)
.then(
each(
($button) => {
cy.wrap($button, { log: false })
.click()
// once the button is clicked,
// find the text in the cell next to it
.parent() // <TD>
.parent() // <TR>
.find('td')
.eq(1) // <TD> with the number
.should(($td) => expect($td.text()).to.match(/\d/))
},
($td) => $td.text() === '7',
),
)
})
it('iterates over every row', () => {
cy.visit('cypress/e2e/each/index.html')
cy.get('#lotto tbody tr')
.should('have.length.greaterThan', 10)
.its('length')
.then((n) => {
let count = 0
cy.get('#lotto tbody tr button')
.then(
each(($button) => {
count += 1
cy.wrap($button).click().wait(500, { log: false })
}),
)
.then(() => {
expect(count, 'number of calls').to.equal(n)
})
})
})
it('yields the values in a new array', () => {
const numbers = [1, 2, 3, 4]
cy.wrap(numbers)
.then(
each((x) => {
cy.log(x)
cy.wrap(x)
}),
)
.should('deep.equal', numbers)
.and('not.equal', numbers)
})
it('yields the values iterated over', () => {
const numbers = [1, 2, 3, 4]
cy.wrap(numbers)
.then(
each(
(x) => {
cy.log(x)
cy.wrap(x)
},
// stop when the value is 3
(x) => x === 3,
),
)
.should('deep.equal', [1, 2])
})
it('can change the value using commands', () => {
const numbers = [1, 2, 3, 4]
cy.wrap(numbers)
.then(
each(
(x) => {
cy.log(x)
cy.wrap(10 + x)
},
// stop when the value is 13
(x) => x === 13,
),
)
.should('deep.equal', [11, 12])
})
it('can change the value using return', () => {
const numbers = [1, 2, 3, 4]
cy.wrap(numbers)
.then(
each(
(x) => {
return 10 + x
},
// stop when the value is 13
(x) => x === 13,
),
)
.should('deep.equal', [11, 12])
})
it('overwrites cy.each command', () => {
// TODO: update the TypeScript types for cy.each method
Cypress.Commands.overwrite(
'each',
(originalFn, items, itemCallback, stopCallback) => {
// @ts-ignore
return each(itemCallback, stopCallback)(items)
},
)
cy.visit('cypress/e2e/each/index.html')
cy.get('#lotto tbody tr button')
.should('have.length.greaterThan', 10)
.each(
($button) => {
cy.wrap($button, { log: false })
.click()
// once the button is clicked,
// find the text in the cell next to it
.parent() // <TD>
.parent() // <TR>
.find('td')
.eq(1) // <TD> with the number
.should(($td) => expect($td.text()).to.match(/\d/))
},
// @ts-ignore
($td) => $td.text() === '7',
)
})
it('finds the lucky 7 using recursion', () => {
cy.visit('cypress/e2e/each/index.html')
cy.get('#lotto tbody tr button').should(
'have.length.greaterThan',
10,
)
recurse(
() => {
return cy.contains('tr', '???').then(($tr) => {
cy.wrap($tr).contains('button', 'Click me').click()
cy.wrap($tr).contains('td', /\d/)
})
},
($el) => {
return $el.text() === '7'
},
{
timeout: 60000,
log: false,
},
)
})
})