Skip to content

Commit

Permalink
feat: 馃幐 Implement timeout. Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo committed Apr 27, 2019
1 parent 9577b15 commit 6448f3b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
33 changes: 29 additions & 4 deletions cypress/integration/plugin.spec.js
Expand Up @@ -7,29 +7,54 @@ context('Actions', () => {

it('Should work with an immediately-satisfied condition', () => {
const COOKIE_NAME = 'immediate-cookie'
const EXPECTED_COOKIE_VALUE = 'Set'
cy.get('#' + COOKIE_NAME).click()

const checkFunction = () => cy.getCookie(COOKIE_NAME)
.then(cookieValue => cookieValue && cookieValue.value === 'Set')
.then(cookieValue => cookieValue && cookieValue.value === EXPECTED_COOKIE_VALUE)

cy.waitUntil(checkFunction)

cy.getCookie(COOKIE_NAME).then(cookieValue => expect(cookieValue.value).to.be.equal('Set'));
cy.getCookie(COOKIE_NAME).then(cookieValue => expect(cookieValue.value).to.be.equal(EXPECTED_COOKIE_VALUE));
})

it('Should work with a condition satisfied after a random delay', () => {
const COOKIE_NAME = 'after-a-while-cookie'
const EXPECTED_COOKIE_VALUE = 'Set'
cy.get('#' + COOKIE_NAME).click()

const checkFunction = () => cy.getCookie(COOKIE_NAME)
.then(cookieValue => cookieValue && cookieValue.value === 'Set')
.then(cookieValue => cookieValue && cookieValue.value === EXPECTED_COOKIE_VALUE)

cy.waitUntil(checkFunction)

cy.getCookie(COOKIE_NAME).then(cookieValue => expect(cookieValue.value).to.be.equal('Set'));
cy.getCookie(COOKIE_NAME).then(cookieValue => expect(cookieValue.value).to.be.equal(EXPECTED_COOKIE_VALUE));
})

it('Should check value equality check', () => {
const COOKIE_NAME = 'change-after-a-while-cookie'
const EXPECTED_COOKIE_VALUE = '7'
cy.get('#' + COOKIE_NAME).click()

const checkFunction = () => cy.getCookie(COOKIE_NAME)
.then(cookieValue => cookieValue && cookieValue.value === EXPECTED_COOKIE_VALUE)

cy.waitUntil(checkFunction)

cy.getCookie(COOKIE_NAME).then(cookieValue => expect(cookieValue.value).to.be.equal(EXPECTED_COOKIE_VALUE));
})

it('Should make the test fail with an unsatisfied condition', () => {
const COOKIE_NAME = 'unknwon-cookie'
const EXPECTED_COOKIE_VALUE = 'Set'

cy.once('fail', err => {
expect(err.message).to.be.equal('Timed out retrying')
})

const checkFunction = () => cy.getCookie(COOKIE_NAME)
.then(cookieValue => cookieValue && cookieValue.value === EXPECTED_COOKIE_VALUE)

cy.waitUntil(checkFunction)
})
})
12 changes: 10 additions & 2 deletions public/index.html
Expand Up @@ -5,16 +5,23 @@
const cookieNames = {
immediate: "immediate-cookie",
delay: "after-a-while-cookie",
change: "change-after-a-while-cookie",
beforeOnLoad: "before-on-load-cookie"
};
const setCookie = (cookieName = cookieNames.immediate) => {
document.cookie = `${cookieName}=Set`;
const setCookie = (cookieName = cookieNames.immediate, value='Set') => {
document.cookie = `${cookieName}=${value}`;
}
const setCookieAfterAWhile = () => {
setTimeout(function(){
setCookie(cookieNames.delay);
}, 1000+Math.random()*2000);
}
const setCookieChangeAfterAWhile = () => {
setCookie(cookieNames.change, '5')
setTimeout(function(){
setCookie(cookieNames.change, '7');
}, 3000);
}
const clearCookies = () => {
Object.values(cookieNames).forEach(item => document.cookie = `${item} = ; expires = Thu, 01 Jan 1970 00:00:00 GMT` );
}
Expand All @@ -25,6 +32,7 @@
<body>
<button id="immediate-cookie" onclick="javascript:setCookie()">Set a cookie immediately</button>
<button id="after-a-while-cookie" onclick="javascript:setCookieAfterAWhile()">Set a cookie after a while</button>
<button id="change-after-a-while-cookie" onclick="javascript:setCookieChangeAfterAWhile()">Set a cookie after a while</button>
<button onclick="javascript:clearCookies()">Clear cookies</button>
</body>
</html>
15 changes: 12 additions & 3 deletions src/index.js
Expand Up @@ -5,13 +5,22 @@ export function waitUntil(checkFunction, options) {
return cy.wait(checkFunction, options)
}

options = options || {}

const TIMEOUT_INTERVAL = options.TIMEOUT_INTERVAL || 200
const TIMEOUT = options.timeout || 5000
let retries = Math.floor(TIMEOUT / TIMEOUT_INTERVAL)

const resolveValue = () => {
return checkFunction()
.then(b => {
if (!b) {
cy.wait(200)
return resolveValue()
if (b) return
if (retries < 1) {
throw new Error('Timed out retrying')
}
cy.wait(TIMEOUT_INTERVAL)
retries--
return resolveValue()
})
}

Expand Down

0 comments on commit 6448f3b

Please sign in to comment.