Skip to content

Commit

Permalink
fix: confirm GET vs POST listen
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Sep 22, 2022
1 parent 08bd9e3 commit c1cebd7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ cy.waitForNetworkIdle(2000)
.should('be.within', 2000, 3000)
```

## Limit the intercept

You can limit which requests to consider by using `method` and `pattern` parameters. For example, see the spec [get-vs-post.js](./cypress/e2e/get-vs-post.js)

```js
// listen to all POST calls
cy.waitForNetworkIdlePrepare({
method: 'POST',
pattern: '*',
alias: 'postCalls',
})

cy.visit('/get-vs-post')
cy.waitForNetworkIdle('@postCalls', 2000)
```

```js
// listen to "POST /add-user" calls
cy.waitForNetworkIdlePrepare({
method: 'POST',
pattern: '/add-user',
alias: 'addUser',
})

cy.visit('/get-vs-post')
cy.waitForNetworkIdle('@addUser', 2000)
```

## Overwrite commands

If you always want to want for network idle when calling `cy.visit` you can overwrite this command using the provided code in [src/register.js](./src/register.js) file
Expand Down
36 changes: 36 additions & 0 deletions cypress/e2e/get-vs-post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// <reference path="../../src/index.d.ts" />
// @ts-check

import '../..'

describe('Waiting for POST requests', () => {
it('ignores the GET requests', () => {
// listen to all POST calls
cy.waitForNetworkIdlePrepare({
method: 'POST',
pattern: '*',
alias: 'postCalls',
})

cy.visit('/get-vs-post')
cy.waitForNetworkIdle('@postCalls', 2000).then(({ waited, callCount }) => {
expect(callCount, 'call count').to.equal(1)
expect(waited, 'waited period').to.be.within(3500, 4500)
})
})

it('waits for specific POST route only', () => {
// listen to "POST /add-user" calls
cy.waitForNetworkIdlePrepare({
method: 'POST',
pattern: '/add-user',
alias: 'addUser',
})

cy.visit('/get-vs-post')
cy.waitForNetworkIdle('@addUser', 2000).then(({ waited, callCount }) => {
expect(callCount, 'call count').to.equal(1)
expect(waited, 'waited period').to.be.within(3500, 4500)
})
})
})
22 changes: 22 additions & 0 deletions server/get-vs-post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<html>
<head>
<title>Get vs post</title>
</head>
<body>
<h1>cypress-network-idle page with lots of GET requests</h1>
<p>but only a few POST requests</p>
<script>
// make constant GET requests
setInterval(() => {
fetch('/user')
}, 1000)

// make a single POST request
setTimeout(() => {
fetch('/add-user', {
method: 'POST',
})
}, 1500)
</script>
</body>
</html>
6 changes: 6 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const user = (req, res) => {
})
}

const addUser = (req, res) => {
micro.send(res, 200)
}

const userDelayed = (req, res) => {
setTimeout(() => {
micro.send(res, 200, {
Expand Down Expand Up @@ -46,4 +50,6 @@ module.exports = dispatch()
.dispatch('/after', 'GET', sendHtml('after.html'))
.dispatch('/after/:ms', 'GET', afterMs)
.dispatch('/busy-page', 'GET', sendHtml('busy-page.html'))
.dispatch('/get-vs-post', 'GET', sendHtml('get-vs-post.html'))
.dispatch('/add-user', 'POST', addUser)
.otherwise(html)

0 comments on commit c1cebd7

Please sign in to comment.