Skip to content

Fix chain commands with previous subject different of document, element or window #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good test to verify the functionality 👍

.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