Skip to content

Commit 93e6d5c

Browse files
authored
fix: set previous subject to be optional (#115)
* test: chain commands with subject different of document, element or window * fix: set previous subject to be optional This allows to commands to be chained with a previous command that not returns a document, element or window FIX #114
1 parent 7a47f3c commit 93e6d5c

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

cypress/fixtures/test-app/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
section {
1616
padding: 10px;
1717
}
18+
input:valid + span {
19+
display: none;
20+
}
21+
input:invalid + span {
22+
display: block;
23+
color: red;
24+
}
1825
</style>
1926
</head>
2027
<body>
@@ -110,6 +117,13 @@ <h2>Eventual non-existence</h2>
110117
}, 500)
111118
</script>
112119
</section>
120+
<section>
121+
<h2>Chain selectors</h2>
122+
<form onsubmit="return false" action="#">
123+
<label>Required: <input type="text" required /><span>Error message</span></label>
124+
<button type="submit">Submit</button>
125+
</form>
126+
</section>
113127
<!-- Prettier unindents the script tag below -->
114128
<script>
115129
document

cypress/integration/find.spec.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,15 @@ describe('find* dom-testing-library commands', () => {
8888
/* Test the behaviour around these queries */
8989

9090
it('findByText should handle non-existence', () => {
91-
cy.findByText('Does Not Exist')
92-
.should('not.exist')
91+
cy.findByText('Does Not Exist').should('not.exist')
9392
})
9493

9594
it('findByText should handle eventual existence', () => {
96-
cy.findByText('Eventually Exists')
97-
.should('exist')
95+
cy.findByText('Eventually Exists').should('exist')
9896
})
9997

10098
it('findByText should handle eventual non-existence', () => {
101-
cy.findByText('Eventually Not exists')
102-
.should('not.exist')
99+
cy.findByText('Eventually Not exists').should('not.exist')
103100
})
104101

105102
it("findByText with should('not.exist')", () => {
@@ -111,7 +108,7 @@ describe('find* dom-testing-library commands', () => {
111108

112109
it('findByText with a previous subject', () => {
113110
cy.get('#nested')
114-
.findByText('Button Text 1', { fallbackRetryWithoutPreviousSubject: false })
111+
.findByText('Button Text 1', {fallbackRetryWithoutPreviousSubject: false})
115112
.should('not.exist')
116113
cy.get('#nested')
117114
.findByText('Button Text 2')
@@ -170,8 +167,7 @@ describe('find* dom-testing-library commands', () => {
170167
expect(err.message).to.contain(errorMessage)
171168
})
172169

173-
cy.findByText('Button Text 1', {timeout: 100})
174-
.should('not.exist')
170+
cy.findByText('Button Text 1', {timeout: 100}).should('not.exist')
175171
})
176172

177173
it('findByLabelText should forward useful error messages from @testing-library/dom', () => {
@@ -196,11 +192,14 @@ describe('find* dom-testing-library commands', () => {
196192
cy.window()
197193
.findByText('Button Text 1')
198194
.should('exist')
195+
cy.location()
196+
.findByText('Button Text 1')
197+
.should('exist')
199198
})
200199

201200
it('findByText should show as a parent command if it starts a chain', () => {
202201
const assertLog = (attrs, log) => {
203-
if(log.get('name') === 'findByText') {
202+
if (log.get('name') === 'findByText') {
204203
expect(log.get('type')).to.equal('parent')
205204
cy.off('log:added', assertLog)
206205
}
@@ -211,14 +210,25 @@ describe('find* dom-testing-library commands', () => {
211210

212211
it('findByText should show as a child command if it continues a chain', () => {
213212
const assertLog = (attrs, log) => {
214-
if(log.get('name') === 'findByText') {
213+
if (log.get('name') === 'findByText') {
215214
expect(log.get('type')).to.equal('child')
216215
cy.off('log:added', assertLog)
217216
}
218217
}
219218
cy.on('log:added', assertLog)
220219
cy.get('body').findByText('Button Text 1')
221220
})
221+
222+
it('should chain findBy* with subject different of document, element or window', () => {
223+
cy.wrap(true)
224+
.should('be.true')
225+
.findByText('Error message')
226+
.findByLabelText(/Required/i)
227+
.type('something')
228+
.findByText('Submit')
229+
.queryByText('Error message')
230+
.should('not.be.visible')
231+
})
222232
})
223233

224234
/* global cy */

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const findCommands = findQueryNames.map(queryName => {
4444
function createCommand(queryName, implementationName) {
4545
return {
4646
name: queryName,
47-
options: {prevSubject: ['optional', 'document', 'element', 'window']},
47+
options: {prevSubject: ['optional']},
4848
command: (prevSubject, ...args) => {
4949
const lastArg = args[args.length - 1]
5050
const defaults = {

0 commit comments

Comments
 (0)