Skip to content

Commit

Permalink
Add findByCy method (#16)
Browse files Browse the repository at this point in the history
This update adds a new method, `findByCy`. After the initial `get` or
`getByCy`, this method can be used to (easily) find DOM selectors that
match a specific `data-cy` query.
  • Loading branch information
Jon Quach authored Jun 19, 2019
1 parent 45993ae commit 998eb8e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/api/traversing.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ Get descendent DOM elements that match a specific selector.
cy.get('ul').find('li.item')
```

## findByCy

`Function(selector: string): Cyan instance`

Get descendent DOM elements that match a specific `data-cy` selector.

#### Parameters

- `selector` `{string}` A selector used for descendent matching.

#### Example

```javascript
cy.get('ul').findByCy('Item')
```

## first

Get the first DOM element from the main DOM elements.
Expand Down
19 changes: 19 additions & 0 deletions src/commands/__tests__/selector.commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,25 @@ describe('commands/selector', () => {
})
})

describe('findByCy', () => {
test('Can find matches', () => {
cy.render(
<ul>
<li className="list-item">One</li>
<li>Two</li>
<li className="list-item" data-cy="Item">
Three
</li>
</ul>,
)

const assert = cy.get('ul').findByCy('Item')

expect(assert.length).toBe(1)
expect(assert.first().text()).toBe('Three')
})
})

describe('first', () => {
test('Returns the first item', () => {
cy.render(
Expand Down
17 changes: 17 additions & 0 deletions src/commands/selector.commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ function filter(selector) {
* @returns {Cyan} The Cyan instance.
*
* @example
* cy.get('ul').findByCy('Item')
*/
function findByCy(selector) {
const cySelector = `[data-cy="${selector}"]`
const node = this.__getNode('findByCy', cySelector)
this.get(node.querySelectorAll(cySelector))
return this
}

/**
* Get descendent DOM elements that match a specific data-cy selector.
*
* @param {string} selector A selector used for descendent matching.
* @returns {Cyan} The Cyan instance.
*
* @example
* cy.get('ul').find('li.item')
*/
function find(selector) {
Expand Down Expand Up @@ -175,6 +191,7 @@ const commands = {
eq,
filter,
find,
findByCy,
first,
last,
next,
Expand Down
11 changes: 11 additions & 0 deletions src/types/Cyan.interface.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,17 @@ export interface CyanInterface extends CyanEventInterface {
*/
find(selector: string): this

/**
* Get descendent DOM elements that match a specific selector.
*
* @param {string} selector A selector used for descendent matching.
* @returns {Cyan} The Cyan instance.
*
* @example
* cy.get('ul').findByCy('Item')
*/
findByCy(selector: string): this

/**
* Get the first DOM element from the main DOM elements.
*
Expand Down

0 comments on commit 998eb8e

Please sign in to comment.