-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathfind.cy.js
218 lines (177 loc) · 6.46 KB
/
find.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/// <reference types="cypress" />
describe('find* dom-testing-library commands', () => {
beforeEach(() => {
cy.visit('cypress/fixtures/test-app/')
})
// Test each of the types of queries: LabelText, PlaceholderText, Text, DisplayValue, AltText, Title, Role, TestId
it('findByLabelText', () => {
cy.findByLabelText('Label 1').click().type('Hello Input Labelled By Id')
})
it('findAllByLabelText', () => {
cy.findAllByLabelText(/^Label \d$/).should('have.length', 2)
})
it('findByPlaceholderText', () => {
cy.findByPlaceholderText('Input 1').click().type('Hello Placeholder')
})
it('findAllByPlaceholderText', () => {
cy.findAllByPlaceholderText(/^Input \d$/).should('have.length', 2)
})
it('findByText', () => {
cy.findByText('Button Text 1').click().should('contain', 'Button Clicked')
})
it('findAllByText', () => {
cy.findAllByText(/^Button Text \d$/)
.should('have.length', 2)
.click({multiple: true})
.should('contain', 'Button Clicked')
})
it('findByDisplayValue', () => {
cy.findByDisplayValue('Display Value 1')
.click()
.clear()
.type('Some new text')
})
it('findAllByDisplayValue', () => {
cy.findAllByDisplayValue(/^Display Value \d$/).should('have.length', 2)
})
it('findByAltText', () => {
cy.findByAltText('Image Alt Text 1').click()
})
it('findAllByAltText', () => {
cy.findAllByAltText(/^Image Alt Text \d$/).should('have.length', 2)
})
it('findByTitle', () => {
cy.findByTitle('Title 1').click()
})
it('findAllByTitle', () => {
cy.findAllByTitle(/^Title \d$/).should('have.length', 2)
})
it('findByRole', () => {
cy.findByRole('dialog').click()
})
it('findAllByRole', () => {
cy.findAllByRole('dialog').should('have.length', 1)
})
it('findByTestId', () => {
cy.findByTestId('image-with-random-alt-tag-1').click()
})
it('findAllByTestId', () => {
cy.findAllByTestId(/^image-with-random-alt-tag-\d$/).should(
'have.length',
2,
)
})
/* Test the behaviour around these queries */
it('findByText should handle non-existence', () => {
cy.findByText('Does Not Exist').should('not.exist')
})
it('findByText should handle eventual existence', () => {
cy.findByText('Eventually Exists').should('exist')
})
it('findByText should handle eventual non-existence', () => {
cy.findByText('Eventually Not exists').should('not.exist')
})
it("findByText with should('not.exist')", () => {
cy.findAllByText(/^Button Text \d$/).should('exist')
cy.findByText('Non-existing Button Text', {timeout: 100}).should(
'not.exist',
)
})
it('findByText with a previous subject', () => {
cy.get('#nested').findByText('Button Text 1').should('not.exist')
cy.get('#nested').findByText('Button Text 2').should('exist')
})
it('findByText within', () => {
cy.get('#nested').within(() => {
cy.findByText('Button Text 1').should('not.exist')
cy.findByText('Button Text 2').should('exist')
})
})
it('findByText in container', () => {
cy.get('#nested').then(subject => {
cy.findByText('Button Text 1', {container: subject}).should('not.exist')
cy.findByText('Button Text 2', {container: subject}).should('exist')
})
})
it('findByText inside within and with a previous subject', () => {
cy.get('section:nth-of-type(2)').within(() => {
cy.findByText(/Button Text 1/).should('exist')
})
cy.get('#nested')
.findByText(/Button Text/)
.should('exist')
cy.get('section:nth-of-type(2)').within(() => {
cy.get('#nested')
.findByText(/Button Text/)
.should('exist')
})
})
it('findByText works when another page loads', () => {
cy.findByText('Next Page').click()
cy.findByText('New Page Loaded').should('exist')
})
it('findByText should set the Cypress element to the found element', () => {
// This test is a little strange since snapshots show what element
// is selected, but snapshots themselves don't give access to those
// elements. I had to make the implementation specific so that the `$el`
// is the `subject` when the log is added and the `$el` is the `value`
// when the log is changed. It would be better to extract the `$el` from
// each snapshot
cy.on('log:changed', (attrs, log) => {
if (log.get('name') === 'findByText') {
expect(log.get('$el')).to.have.text('Button Text 1')
}
})
cy.findByText('Button Text 1')
})
it('findByText should error if no elements are found', () => {
const regex = /Supercalifragilistic/
const errorMessage = `Unable to find an element with the text: /Supercalifragilistic/`
cy.on('fail', err => {
expect(err.message).to.contain(errorMessage)
})
cy.findByText(regex, {timeout: 100})
})
it('findByText should default to Cypress non-existence error message', () => {
const errorMessage = `Expected <button> not to exist in the DOM, but it was continuously found.`
cy.on('fail', err => {
expect(err.message).to.contain(errorMessage)
})
cy.findByText('Button Text 1', {timeout: 100}).should('not.exist')
})
it('findByLabelText should forward useful error messages from @testing-library/dom', () => {
const errorMessage = `Found a label with the text of: Label 3, however no form control was found associated to that label.`
cy.on('fail', err => {
expect(err.message).to.contain(errorMessage)
})
cy.findByLabelText('Label 3', {timeout: 100})
})
it('findByText finding multiple items should error', () => {
const errorMessage = `Found multiple elements with the text: /^Button Text/i`
cy.on('fail', err => {
expect(err.message).to.contain(errorMessage)
})
cy.findByText(/^Button Text/i, {timeout: 100})
})
it('findByText should show as a parent command if it starts a chain', () => {
const assertLog = (attrs, log) => {
if (log.get('name') === 'findByText') {
expect(log.get('type')).to.equal('parent')
cy.off('log:added', assertLog)
}
}
cy.on('log:added', assertLog)
cy.findByText('Button Text 1')
})
it('findByText should show as a child command if it continues a chain', () => {
const assertLog = (attrs, log) => {
if (log.get('name') === 'findByText') {
expect(log.get('type')).to.equal('child')
cy.off('log:added', assertLog)
}
}
cy.on('log:added', assertLog)
cy.get('body').findByText('Button Text 1')
})
})
/* global cy */