Skip to content
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

fix: .contains() properly respects multiple incoming subjects #25035

Merged
merged 1 commit into from Dec 7, 2022

Conversation

BlueWinds
Copy link
Contributor

@BlueWinds BlueWinds commented Dec 7, 2022

User facing changelog

In Cypress 12.0.0, we introduced a regression where when .contains() receives multiple elements as a subject, it only searched inside the first one. This PR fixes that issue.

Additional details

This PR includes a new driver test covering the case. Another reproduction that doesn't rely on any of our fixtures:

it("fails", () => {
  cy.visit("https://example.cypress.io");
  cy.get(".container").eq(2).contains("p", "Commands "); // passes
  cy.get(".container").contains("Commands "); // passes
  cy.get(".container").contains("p", "Commands "); // fails in 12.0.1, passes in this branch
})

Steps to test

How has the user experience changed?

PR Tasks

  • Have tests been added/updated?
  • Has the original issue (or this PR, if no issue exists) been tagged with a release in ZenHub? (user-facing changes only)
  • [n/a] Has a PR for user-facing changes been opened in cypress-documentation?
  • [n/a] Have API changes been updated in the type definitions?

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 7, 2022

Thanks for taking the time to open a PR!

@BlueWinds BlueWinds changed the title fix: .contains() properly respects multiple incoming subjects (run ci) fix: .contains() properly respects multiple incoming subjects Dec 7, 2022
// https://github.com/cypress-io/cypress/issues/25025
it('searches multiple subject elements', () => {
cy.get('ul').contains('li', 'asdf 3')
})
Copy link
Member

Choose a reason for hiding this comment

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

to ensure this change touched within subject, can we we add something like this as well?

    it('searches multiple subject elements in .within', () => {
      cy.get('body').within(() => {
         cy.get('ul').contains('li', 'asdf 3')
       })
    })

Copy link
Contributor Author

@BlueWinds BlueWinds Dec 7, 2022

Choose a reason for hiding this comment

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

I believe this sort of case is already well covered inside the .within() tests.

it('scopes additional CONTAINS finders to the subject', () => {
      const span = cy.$$('#nested-div span:contains(foo)')

      cy.contains('foo').then(($span) => {
        expect($span.get(0)).not.to.eq(span.get(0))
      })

      cy.get('#nested-div').within(() => {
        cy.contains('foo').then(($span) => {
          expect($span.get(0)).to.eq(span.get(0))
        })
      })
    })

and a couple of other similar tests in within.cy.js I don't think it's valuable to make this test more complicated.

Copy link
Member

Choose a reason for hiding this comment

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

@BlueWinds wasn't my suggestion for a test this issue? #25019

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That issue has to do with a <form> as the subject or withinSubject - it's incredibly specific to forms + contains, which took me quite a while to figure out (I did not expect HTML elements to have different properties).

let form = $("<form><input /><input /></form>").get(0) // Raw html element
let ul = $('<ul><li /><li /></ul>').get(0) // Raw html element

form[0] // An <input> element
ul[0] // undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hadn't figured that out when I made this PR, was taking them one at a time. But that other PR has its own test covering that case. .within() isn't necessary to hit either of these two bugs - they're entirely within .contains(), users just happened to run into them using .within() blocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had a lazy check that was looking for subject[0] as equivelent for "is a jquery object", which worked for every element... except <form>. Today I learned.

Copy link
Contributor Author

@BlueWinds BlueWinds Dec 7, 2022

Choose a reason for hiding this comment

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

The fact that .contains() hijacks the "withinSubject" property of .get() was pre-existing behavior that I really should have cleaned up / renamed when I moved them to be queries, but didn't think of at the time. It's really more a "container element to query inside" - which we call "withinSubject" because .within() is our "scope commands to a container element" command.

cypress-testing-library calls this a "container", and we probably should too. Some future variable renaming to consider.

@cypress
Copy link

cypress bot commented Dec 7, 2022



Test summary

199 0 26 0Flakiness 0


Run details

Project cypress
Status Passed
Commit b82fb95
Started Dec 7, 2022 8:42 PM
Ended Dec 7, 2022 8:52 PM
Duration 09:58 💡
OS Linux Debian -
Browser Chrome 106

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@BlueWinds BlueWinds merged commit 0e457b8 into develop Dec 7, 2022
@BlueWinds BlueWinds deleted the issue-25025-multiple-contains-subjects branch December 7, 2022 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cy.contains with selector and cy.get as a subject stopped working on v12
4 participants