Skip to content

Commit

Permalink
feat: 馃幐 Makre the plugin chainable
Browse files Browse the repository at this point in the history
Closes: #40
  • Loading branch information
NoriSte committed Sep 4, 2019
1 parent 887ad64 commit f3ea69b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ cy.waitUntil(() => cy.get("input[type=hidden]#recaptchatoken").then($el => $el.v
.then(token => expect(token).to.be.a("string").to.have.length.within(1, 1000));
```

The `waitUntil` command could be chained to other commands too. As an example, the following codes are equivalent
```javascript
cy.waitUntil(() => cy.getCookie('token').then(cookie => cookie.value === '<EXPECTED_VALUE>'));
// is equivalent to
cy.wrap('<EXPECTED_VALUE>')
.waitUntil((subject) => cy.getCookie('token').then(cookie => cookie.value === subject));
```
Please note: do not expect that the previous command are retried. Only what's inside the `checkFunction` code is retried
```javascript
cy.getCookie('token') // will not be retried
.waitUntil(cookie => cookie.value === '<EXPECTED_VALUE>');
```


### TypeScript

Expand Down
13 changes: 13 additions & 0 deletions cypress/integration/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,17 @@ context('Actions', () => {
cy.log("Chainable function");
cy.waitUntil(chainableCheckFunction, {interval})
})

it('Should be chainable', () => {
const result = 10;
const checkFunction = () => result;
const checkFunctionWithSubject = subject => subject;

cy.waitUntil(checkFunction)
.should('eq', result)

cy.wrap(result)
.waitUntil(checkFunctionWithSubject)
.should('eq', result)
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-wait-until",
"version": "1.3.2",
"version": "1.4.0",
"description": "A waiting plugin for Cypress",
"main": "src/index.js",
"dependencies": {},
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

function waitUntil(checkFunction, options) {
function waitUntil(subject, checkFunction, options) {
if (!(checkFunction instanceof Function)) {
throw new Error('`checkFunction` parameter should be a function. Found: ' + checkFunction)
}
Expand All @@ -25,7 +25,7 @@ function waitUntil(checkFunction, options) {
}

const resolveValue = () => {
const result = checkFunction()
const result = checkFunction(subject)
const isAPromise = Boolean(result && result.then)

if (isAPromise) {
Expand All @@ -38,4 +38,4 @@ function waitUntil(checkFunction, options) {
return resolveValue()
}

Cypress.Commands.add("waitUntil", waitUntil);
Cypress.Commands.add("waitUntil", {prevSubject:'optional'}, waitUntil);

0 comments on commit f3ea69b

Please sign in to comment.