diff --git a/component/advanced/ProductsList.spec.js b/component/advanced/ProductsList.spec.js index 83bb3e7..1e86725 100644 --- a/component/advanced/ProductsList.spec.js +++ b/component/advanced/ProductsList.spec.js @@ -153,5 +153,32 @@ describe('Selecting by React props and state', () => { props: { name: 'Second item' }, }).should('have.length', '1'); }); + + it('retries until timeout or upcoming assertions succeed', () => { + cy.stub(window, 'fetch') + .withArgs('http://myapi.com/products') + .resolves({ + json: cy.stub().resolves({ + products: [ + { id: 1, name: 'First item' }, + { id: 2, name: 'Second item' }, + ], + }), + }); + mount(); + + // to find DOM elements by React component constructor name, props, or state + cy.waitForReact(); + + const assertFn = cy.stub().returns(false); + assertFn.onCall(5).returns(true); + + cy.getReact('ProductsContainer', { options: { timeout: 1000 } }).should(() => { + // should retry 10 times + + const value = assertFn(); + expect(value).to.equal(true); // this should ultimately succeed at the 5th retry + }); + }); }); }); diff --git a/cypress/integration/calculator.spec.js b/cypress/integration/calculator.spec.js index 7e2e9e6..c479b37 100644 --- a/cypress/integration/calculator.spec.js +++ b/cypress/integration/calculator.spec.js @@ -57,4 +57,16 @@ describe('It should validate cypress react selector', () => { .getProps('name') .should('eq', '5'); }); + + it('getReact should retry upcoming assertions', () => { + const assertFn = cy.stub().returns(false); + assertFn.onCall(5).returns(true); + + cy.getReact('t', { options: { timeout: 1000 } }).should(() => { + // should retry 10 times + + const value = assertFn(); + expect(value).to.equal(true); // this should ultimately succeed at the 5th retry + }); + }); }); diff --git a/src/reactHandler.js b/src/reactHandler.js index 013bd7f..1c6ad04 100644 --- a/src/reactHandler.js +++ b/src/reactHandler.js @@ -145,9 +145,7 @@ exports.react = (subject, component, reactOpts = {}) => { }); }; - return resolveValue().then((value) => { - return value; - }); + return resolveValue(); }; /** @@ -273,9 +271,7 @@ exports.getReact = (subject, component, reactOpts = {}) => { }); }; - return resolveValue().then((value) => { - return value; - }); + return resolveValue(); }; /**