mocks-server / main

Node.js mock server running live, interactive mocks in place of real APIs
https://www.mocks-server.org
Apache License 2.0
288 stars 16 forks source link

Can I switch between two different requests to the same id in the same test? #445

Open feargalWag opened 2 years ago

feargalWag commented 2 years ago

Is your feature request related to a problem? Please describe. When I have a test that requires me to make a request to the same endpoint more than once but receive a different response For example: if I have submitted a form and then re-request the same endpoint that I requested at the beginning of the test but the backend will now send back a new response because it can see that the user has changed state. I can get around this sometimes by triggering a cy.reload() or refreshing the page in some other way, but that's not always an option as the initial request might be made again when I refresh the page and I need it to return what it initially returned.

Describe the solution you'd like The ability to call cy.mocksUseRouteVartiant(routeName) more than once in a test to get a different result so that I can switch the backend response for the endpoint out when the user performs an action.

Describe alternatives you've considered Refreshing the page, but this isn't always an optino.

Additional context

  cy.mocksApplyRouteVariant('get-walker-application-state:about-me');
      cy.location('pathname').should('eq', TestingFactory.routes.Routes.WalkerSignUp);
      cy.findByTestId(TestingFactory.constants.DataTestIds.Walker.Form.HeaderText)
        .should('contain', 'rifnpowifnpoewnf');

      cy.findByTestId(TestingFactory.constants.DataTestIds.Walker.Form.StreetAddress)
        .type(TestingFactory.fake.address());

      cy.findAllByTestId(TestingFactory.constants.DataTestIds.Walker
        .Form.AutoCompleteDropdownOption)
        .first().click();

      // TODO make gender random
      cy.findByTestId(`${TestingFactory.constants.DataTestIds.Walker.Form.Gender}-Male`)
        .click();

      // TODO select random document rather than first, update data test id in component
      cy.findAllByTestId(TestingFactory.constants.DataTestIds.Walker.Form.Documents).first()
        .click();

      cy.mocksRestoreRoutesVariants();
      cy.mocksApplyRouteVariant('get-walker-application-state:about-me-blocked');

    });

Here's an example of what I'm talking about. I call cy.mocksApplyRouteVariant('get-walker-application-state:about-me'); where cy.mocksApplyRouteVariant() is our internal name for cy.mocksUseRouteVariant() with the get-walker-application-state:about-me route, which returns a particular boolean set to false.

When I try and call the same route again with a different id get-walker-application-state:about-me-blocked towards the end of the test where I want the aforementioned boolean to be set to true in the test then I receive the same response from that endpoint as I did initially.

cy.mocksRestoreRouteVariants doesn't seem to clear the request cache and allow me to get a new response.

javierbrea commented 2 years ago

Hi @feargalWag,

The cy.mocksUseRouteVariant does not need to reload the page to make effect. It should be able to be used many times inside the same test. The only idea that I have about what can be happening to you is that the specs are running before the change is really applied, because the change config command is async. Some things that you could do to check it:

About your comment "cy.mocksRestoreRouteVariants doesn't seem to clear the request cache and allow me to get a new response", I don't understand it. This command is not related to cleaning any type of cache, it simply restores the mock to use the route variants originally defined in the collection (its like undoing all of the previous calls to cy.mocksUseRouteVariant)