Skip to content

Commit

Permalink
fix: set previous subject to be optional (#115)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
leosuncin authored Feb 3, 2020
1 parent 7a47f3c commit 93e6d5c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
14 changes: 14 additions & 0 deletions cypress/fixtures/test-app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
section {
padding: 10px;
}
input:valid + span {
display: none;
}
input:invalid + span {
display: block;
color: red;
}
</style>
</head>
<body>
Expand Down Expand Up @@ -110,6 +117,13 @@ <h2>Eventual non-existence</h2>
}, 500)
</script>
</section>
<section>
<h2>Chain selectors</h2>
<form onsubmit="return false" action="#">
<label>Required: <input type="text" required /><span>Error message</span></label>
<button type="submit">Submit</button>
</form>
</section>
<!-- Prettier unindents the script tag below -->
<script>
document
Expand Down
32 changes: 21 additions & 11 deletions cypress/integration/find.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,15 @@ describe('find* dom-testing-library commands', () => {
/* Test the behaviour around these queries */

it('findByText should handle non-existence', () => {
cy.findByText('Does Not Exist')
.should('not.exist')
cy.findByText('Does Not Exist').should('not.exist')
})

it('findByText should handle eventual existence', () => {
cy.findByText('Eventually Exists')
.should('exist')
cy.findByText('Eventually Exists').should('exist')
})

it('findByText should handle eventual non-existence', () => {
cy.findByText('Eventually Not exists')
.should('not.exist')
cy.findByText('Eventually Not exists').should('not.exist')
})

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

it('findByText with a previous subject', () => {
cy.get('#nested')
.findByText('Button Text 1', { fallbackRetryWithoutPreviousSubject: false })
.findByText('Button Text 1', {fallbackRetryWithoutPreviousSubject: false})
.should('not.exist')
cy.get('#nested')
.findByText('Button Text 2')
Expand Down Expand Up @@ -170,8 +167,7 @@ describe('find* dom-testing-library commands', () => {
expect(err.message).to.contain(errorMessage)
})

cy.findByText('Button Text 1', {timeout: 100})
.should('not.exist')
cy.findByText('Button Text 1', {timeout: 100}).should('not.exist')
})

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

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

it('findByText should show as a child command if it continues a chain', () => {
const assertLog = (attrs, log) => {
if(log.get('name') === 'findByText') {
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')
})

it('should chain findBy* with subject different of document, element or window', () => {
cy.wrap(true)
.should('be.true')
.findByText('Error message')
.findByLabelText(/Required/i)
.type('something')
.findByText('Submit')
.queryByText('Error message')
.should('not.be.visible')
})
})

/* global cy */
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const findCommands = findQueryNames.map(queryName => {
function createCommand(queryName, implementationName) {
return {
name: queryName,
options: {prevSubject: ['optional', 'document', 'element', 'window']},
options: {prevSubject: ['optional']},
command: (prevSubject, ...args) => {
const lastArg = args[args.length - 1]
const defaults = {
Expand Down

0 comments on commit 93e6d5c

Please sign in to comment.