Skip to content

Commit

Permalink
fix: do not ignore the current subject in cy.getInOrder (#228)
Browse files Browse the repository at this point in the history
bahmutov authored Nov 27, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent ae58edc commit 5ac43d9
Showing 4 changed files with 37 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -424,6 +424,12 @@ You can also use a single array of selector strings
cy.getInOrder(['h1', 'h2', 'h3'])
```

Supports parent subject

```js
cy.get('...').getInOrder('...', '...')
```

### stable

Sometimes you just want to wait until the element is stable. For example, if the element's text content does not change for N milliseconds, then we can consider the element to be `text` stable.
16 changes: 16 additions & 0 deletions cypress/e2e/get-in-order.cy.js
Original file line number Diff line number Diff line change
@@ -49,3 +49,19 @@ it('spreads an array of strings', () => {
.map('innerText')
.should('deep.equal', ['fifth', 'first', 'third'])
})

// https://github.com/bahmutov/cypress-map/issues/219
it(
'uses the current subject',
{ viewportWidth: 500, viewportHeight: 500 },
() => {
cy.visit('cypress/e2e/filter-table/index.html')
cy.get('table tbody tr').should('have.length', 5)
cy.get('table tbody tr:eq(2)')
.should('include.text', 'Dave')
// get the age and the name cells
.getInOrder('td:eq(2)', 'td:eq(0)')
.map('innerText')
.should('deep.equal', ['25', 'Dave'])
},
)
17 changes: 11 additions & 6 deletions src/commands/get-in-order.js
Original file line number Diff line number Diff line change
@@ -20,21 +20,26 @@ registerQuery('getInOrder', (...selectors) => {
message: selectors.join(','),
})

return () => {
let subject = Cypress.$(selectors[0])
if (subject.length === 0) {
return ($subject) => {
let found = $subject
? $subject.find(selectors[0])
: Cypress.$(selectors[0])

if (found.length === 0) {
throw new Error(`No elements found for ${selectors[0]}`)
}

selectors.slice(1).forEach((selector) => {
const next = Cypress.$(selector)
const next = $subject
? $subject.find(selector)
: Cypress.$(selector)
if (next.length === 0) {
throw new Error(`No elements found for ${selector}`)
}
// avoid sorting elements by passing the elements as a single array
subject = Cypress.$(subject.toArray().concat(next.toArray()))
found = Cypress.$(found.toArray().concat(next.toArray()))
})

return subject
return found
}
})
4 changes: 4 additions & 0 deletions src/commands/index.d.ts
Original file line number Diff line number Diff line change
@@ -326,13 +326,17 @@ declare namespace Cypress {
/**
* Queries each selector and returns the found elements _in the specified order_.
* Retries if the elements are not found for any of the selectors.
* Supports parent subject.
* @example cy.getInOrder('h1', 'h2', 'h3')
* @example cy.get('tr').getInOrder('td', 'th')
*/
getInOrder(...selector: string[]): Chainable<JQuery<HTMLElement>>
/**
* Queries each selector and returns the found elements _in the specified order_.
* Retries if the elements are not found for any of the selectors.
* Supports parent subject.
* @example cy.getInOrder(['h1', 'h2', 'h3'])
* @example cy.get('tr').getInOrder(['td', 'th'])
*/
getInOrder(selectors: string[]): Chainable<JQuery<HTMLElement>>

0 comments on commit 5ac43d9

Please sign in to comment.