Return different results for the same Cypress intercept

When you for example have a polling mechanisme, you want to check that the request is executed and a value is correctly set.

In a Cypress test, we can use date, tick and shift to accomplish this.

describe('Polling', () => {
    it('should get the status every 5 seconds', () => {
        // Setup a custom clock (so we can play with time)
        cy.clock(new Date());

        // Setup to return multiple things using shift()
        const firstItem = 'hello';
        const secondItem = 'world';
        const replies = [firstItem, secondItem];

        cy.intercept(
            {
            method: 'GET',
            url: '/polling_endpoint', 
            },
            [replies.shift() as string]
        ).as('getReply')

        cy.visit('/polling_page')

        // Assert first value
        cy.wait('@getReply');
        cy.get('input').should('have.value', firstItem);

        // Advance time 5 seconds
        cy.tick(5000);

        // Assert second value
        cy.wait('@getReply');
        cy.get('input').should('have.value', secondItem);
    });
});