[cypress]How to check that request has not been sent

977 Views Asked by At

I need to check that after the second click on the same button, an HTTP request is not sent. Is there a way to do it? I've already tried intercept and wait for this purpose, but can't make it work

1

There are 1 best solutions below

3
On

A single-use intercept will probably work

const interceptOnce = (method, url, response) => {
  let count = 0
  return cy.intercept(method, url, req => {
    count += 1
    if (count < 2) {
      req.reply(response)
    } else {
      throw 'Error: button click caused two requests'    // cause test to fail
    }
  })
}

it('tests that two button clicks only sends a request on first click', () => {
  interceptOnce('POST', myurl, {stubbed response object})
  cy.get('button').click()
  cy.get('button').click()    // test fails here if a second request occurs
}) 

I'm not sure you even need to stub the response in this case.