Skip to content

Commit

Permalink
fix: 馃悰 The interval is now respected
Browse files Browse the repository at this point in the history
Closes: #39
  • Loading branch information
NoriSte committed Sep 4, 2019
1 parent 40741f2 commit 887ad64
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ 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));
```


### TypeScript

If you use TypeScript you can add define the `checkFunction` returning type too. Here some examples with all the combinations of promises and chainable functions

```typescript
Expand All @@ -89,6 +92,17 @@ cy.waitUntil<boolean>(() => cy.wrap(true).then(result => Promise.resolve(result)
cy.waitUntil<string>(() => cy.wrap(true).then(result => Promise.resolve(result)) ); // Error
```

Please note: do not forget to add `cypress-wait-until` to the `cypress/tsconfig.json` file

```
{
"compilerOptions": {
"types": ["cypress", "cypress-wait-until"]
}
}
}
```



## Arguments
Expand Down
32 changes: 29 additions & 3 deletions cypress/integration/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,34 @@ context('Actions', () => {
const asyncCheckFunction = () => Promise.resolve(result);
const chainableCheckFunction = () => cy.wrap(result).then(wrappedResult => wrappedResult);

cy.waitUntil(checkFunction).should("eq", result)
cy.waitUntil(asyncCheckFunction).should("eq", result)
cy.waitUntil(chainableCheckFunction).should("eq", result)
cy.waitUntil(checkFunction).should('eq', result)
cy.waitUntil(asyncCheckFunction).should('eq', result)
cy.waitUntil(chainableCheckFunction).should('eq', result)
})

it('Should wait between every check', () => {
const interval = 100;
let previousTimestamp;

const checkFunction = () => {
const previousTimestampBackup = previousTimestamp;
const newTimestamp = Date.now();
previousTimestamp = newTimestamp;
if(previousTimestampBackup) {
const diff = newTimestamp - previousTimestampBackup;
return diff >= interval;
}
return false
}

const asyncCheckFunction = () => Promise.resolve(checkFunction());
const chainableCheckFunction = () => cy.wrap().then(() => checkFunction());

cy.log("Sync function");
cy.waitUntil(checkFunction, {interval})
cy.log("Async function");
cy.waitUntil(asyncCheckFunction, {interval})
cy.log("Chainable function");
cy.waitUntil(chainableCheckFunction, {interval})
})
})
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.1",
"version": "1.3.2",
"description": "A waiting plugin for Cypress",
"main": "src/index.js",
"dependencies": {},
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ function waitUntil(checkFunction, options) {
if (retries < 1) {
throw new Error(ERROR_MSG)
}
cy.wait(TIMEOUT_INTERVAL)
retries--
return resolveValue()
cy.wait(TIMEOUT_INTERVAL).then(() => {
retries--
return resolveValue()
})
}

const resolveValue = () => {
Expand Down

0 comments on commit 887ad64

Please sign in to comment.