|
1 | 1 | /// <reference types="cypress" /> |
2 | 2 |
|
3 | | -import { resetDatabase } from '../../support/utils' |
| 3 | +import { resetDatabase, resetDatabaseTo } from '../../support/utils' |
4 | 4 | import twoItems from '../../fixtures/two-items.json' |
5 | 5 |
|
6 | 6 | // this spec shows several "gotchas" our users experience |
@@ -382,4 +382,79 @@ describe('intercept', () => { |
382 | 382 | cy.get('.todo').should('have.length', 3) |
383 | 383 | }) |
384 | 384 | }) |
| 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 | + }) |
385 | 460 | }) |
0 commit comments