I have a (form submit) button that having an attribute disabled when being clicked. It became available again when the call is finished.
Because it happens very fast. I want to find a way to capture this behaviour during the api call
What I tried:
cy.intercept('POST', '/api/item/1', (req) => {
req.continue((res) => {
cy.findByRole('button', { name: /button0/i })
.should('exist')
.should('have.attr', 'disabled');
res.send(200, { name: 'aa' })
});
});
cy.findByRole('button', { name: /button0/i }).click();
However it gives me error Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.
I found a workaround that If i do
cy.intercept('POST', '/api/item/1', (req) => {
req.continue((res) => {
res.setDelay(1000);
res.send(200, { name: 'aa' })
});
});
cy.findByRole('button', { name: /button0/i }).click();
cy.findByRole('button', { name: /button0/i })
.should('exist')
.should('have.attr', 'disabled');
Then it has enough time to capture the attribute change during the delay.
However I don't think it's robust using arbitrary delay in a test. Is there better way to test this behaviour using cyprss?