cypress-map
Advanced tools
Comparing version 1.20.1 to 1.21.0
@@ -154,2 +154,3 @@ /** | ||
* Uses `_.sample` under the hood. | ||
* @param n Maximum number of items to pick, 1 by default | ||
* @see https://github.com/bahmutov/cypress-map | ||
@@ -161,3 +162,3 @@ * @example | ||
*/ | ||
sample(): Chainable<any> | ||
sample(n?: number): Chainable<any> | ||
@@ -164,0 +165,0 @@ /** |
@@ -5,15 +5,37 @@ /// <reference types="cypress" /> | ||
registerQuery('sample', () => { | ||
const log = Cypress.log({ name: 'sample' }) | ||
registerQuery('sample', (n = 1) => { | ||
if (n < 1) { | ||
throw new Error(`Sample size should be positive, was ${n}`) | ||
} | ||
return (subject) => { | ||
if (Cypress.dom.isJquery(subject)) { | ||
const randomElement = Cypress._.sample(subject.toArray()) | ||
// wrap into jQuery object so other commands | ||
// can be attached, like cy.click | ||
return Cypress.$(randomElement) | ||
if (n === 1) { | ||
const log = Cypress.log({ name: 'sample' }) | ||
return (subject) => { | ||
if (Cypress.dom.isJquery(subject)) { | ||
const randomElement = Cypress._.sample(subject.toArray()) | ||
// wrap into jQuery object so other commands | ||
// can be attached, like cy.click | ||
return Cypress.$(randomElement) | ||
} | ||
return Cypress._.sample(subject) | ||
} | ||
} else { | ||
const log = Cypress.log({ name: 'sample', message: String(n) }) | ||
return Cypress._.sample(subject) | ||
return (subject) => { | ||
if (Cypress.dom.isJquery(subject)) { | ||
const randomElement = Cypress._.sampleSize( | ||
subject.toArray(), | ||
n, | ||
) | ||
// wrap into jQuery object so other commands | ||
// can be attached, like cy.click | ||
return Cypress.$(randomElement) | ||
} | ||
return Cypress._.sampleSize(subject, n) | ||
} | ||
} | ||
}) |
{ | ||
"name": "cypress-map", | ||
"version": "1.20.1", | ||
"version": "1.21.0", | ||
"description": "Extra Cypress query commands for v12+", | ||
@@ -24,3 +24,3 @@ "main": "commands/index.js", | ||
"cy-spok": "^1.5.2", | ||
"cypress": "^12.11.0", | ||
"cypress": "^12.13.0", | ||
"prettier": "^2.8.0", | ||
@@ -27,0 +27,0 @@ "semantic-release": "^20.1.3", |
@@ -10,2 +10,3 @@ # cypress-map [![ci](https://github.com/bahmutov/cypress-map/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/bahmutov/cypress-map/actions/workflows/ci.yml) ![cypress version](https://img.shields.io/badge/cypress-12.11.0-brightgreen) | ||
- [How To Check Visibility Of Many Elements](https://youtu.be/puCZGCeUb5k) | ||
- [Pick A Random Menu Link](https://youtu.be/xvvL3GRjXCY) | ||
- 📝 Read the blog posts | ||
@@ -272,2 +273,9 @@ - [Cypress V12 Is A Big Deal](https://glebbahmutov.com/blog/cypress-v12/) | ||
If you pass a positive number, then it picks multiple elements or items | ||
```js | ||
// yields jQuery object with 3 random items | ||
cy.get('#items li').sample(3).should('have.length', 3) | ||
``` | ||
See [sample.cy.js](./cypress/e2e/sample.cy.js) | ||
@@ -274,0 +282,0 @@ |
38906
659
475