Skip to content
This repository was archived by the owner on Feb 28, 2023. It is now read-only.

Commit a479474

Browse files
committed
show single use intercept
1 parent 0d673fc commit a479474

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

cypress/integration/05-xhr/answer-intercept-spec.js

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="cypress" />
22

3-
import { resetDatabase } from '../../support/utils'
3+
import { resetDatabase, resetDatabaseTo } from '../../support/utils'
44
import twoItems from '../../fixtures/two-items.json'
55

66
// this spec shows several "gotchas" our users experience
@@ -382,4 +382,79 @@ describe('intercept', () => {
382382
cy.get('.todo').should('have.length', 3)
383383
})
384384
})
385+
386+
context('single use intercept', () => {
387+
beforeEach(() => {
388+
// let's reset the server to always have 2 todos
389+
resetDatabaseTo('two-items.json')
390+
})
391+
392+
it('stubs the first load (does not work)', () => {
393+
// this test wants to have no todos at first
394+
cy.intercept('GET', '/todos', []).as('todos')
395+
cy.visit('/')
396+
cy.wait('@todos')
397+
cy.get('.todo-list li').should('have.length', 0)
398+
399+
cy.log('adding an item')
400+
cy.get('.new-todo').type('new todo{enter}')
401+
cy.contains('li.todo', 'new todo').should('be.visible')
402+
403+
// reload and expect to see the new todo again
404+
cy.reload()
405+
cy.contains('li.todo', 'new todo').should('be.visible')
406+
})
407+
408+
/**
409+
* Intercept the first matching request and send the response object.
410+
* Do nothing on the second and other requests.
411+
* @param {string} method HTTP method to intercept
412+
* @param {string} url URL to intercept
413+
* @param {any} response Response to send back on the first request
414+
*/
415+
const interceptOnce = (method, url, response) => {
416+
// I am using "count" to show how easy you can implement
417+
// different responses for different interceptors
418+
let count = 0
419+
return cy.intercept(method, url, req => {
420+
count += 1
421+
if (count < 2) {
422+
req.reply(response)
423+
} else {
424+
// do nothing
425+
}
426+
})
427+
}
428+
429+
it('stubs the first load and does nothing after that', () => {
430+
// this test wants to have no todos at first
431+
interceptOnce('GET', '/todos', []).as('todos')
432+
cy.visit('/')
433+
cy.wait('@todos')
434+
cy.get('.todo-list li').should('have.length', 0)
435+
436+
cy.log('adding an item')
437+
cy.get('.new-todo').type('new todo{enter}')
438+
cy.contains('li.todo', 'new todo').should('be.visible')
439+
440+
// reload and expect to see the new todo again
441+
cy.reload()
442+
cy.contains('li.todo', 'new todo').should('be.visible')
443+
// since we reset the database with 2 todos, plus entered a new todo
444+
// thus the total number of items should be 3
445+
cy.get('.todo-list li').should('have.length', 3)
446+
447+
// Tip: you can still spy on "todos" intercept
448+
// for example let's validate the server response has the new item
449+
// at index 2 and it has the title and completed properties
450+
cy.get('@todos')
451+
.its('response.body')
452+
.should('have.length', 3)
453+
.its('2')
454+
.should('include', {
455+
title: 'new todo',
456+
completed: false
457+
})
458+
})
459+
})
385460
})

cypress/support/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Sets the database to the empty list of todos
3+
*/
14
export const resetDatabase = () => {
25
console.log('resetDatabase')
36
cy.request({
@@ -9,6 +12,23 @@ export const resetDatabase = () => {
912
})
1013
}
1114

15+
/**
16+
*
17+
* @param {string} fixtureName The fixture with todos to load and send to the server
18+
*/
19+
export const resetDatabaseTo = fixtureName => {
20+
cy.log(`**resetDatabaseTo** ${fixtureName}`)
21+
cy.fixture(fixtureName).then(todos => {
22+
cy.request({
23+
method: 'POST',
24+
url: '/reset',
25+
body: {
26+
todos
27+
}
28+
})
29+
})
30+
}
31+
1232
export const visit = skipWaiting => {
1333
console.log('visit this =', this)
1434

0 commit comments

Comments
 (0)