Giter Site home page Giter Site logo

Comments (4)

kettanaito avatar kettanaito commented on May 22, 2024 3

Hey, @jmcrthrs. Thanks for reaching out.

When it comes to testing intermediate state (i.e. loading), consider using the ctx.delay() utility. It allows to set a response delay, thus making the loading state apparent in the UI. Here's how it'd look in your request handlers:

rest.post('/login', (req, res, ctx) => {
  return res(ctx.delay(5000), ctx.json({ some: 'response' })
})

The example above would delay the mocked response by 5000ms (5s), making it enough time for your testing framework to capture the loading state.

You can also consider using it like ctx.delay(Infinity), which would produce a mocked response that would never resolve.


So I assume there is no need for us to explicitly wait in our tests for the network response?

You're assuming correctly. Most of the test frameworks have a retry mechanism built-in, so the framework would run your assertion repeatedly for an N amount of time until it becomes true. This works beneficial when asserting UI derived from asynchronous operations like HTTP requests.

Certain frameworks may provide you with an API to explicitly await HTTP requests, but you'd have to look such API up in their respective documentations. The usage of such API is up to you.

Let us know if you have any other questions.

from examples.

timdeschryver avatar timdeschryver commented on May 22, 2024

I have forked this repo and added an assertion after the Login form is submitted, but it can never find the button.

In the html the button is marked with submit-button as test id

<button data-testid="submit-button" type="submit" className={loading ? "loading" : ""}>
          Submit
</button>

While the test is looking for button as test id

cy.get('[data-testid=button]').should('have.class', 'loading')

from examples.

jmcrthrs avatar jmcrthrs commented on May 22, 2024

@timdeschryver Hey Tim, I think perhaps you have an old commit. Can you pull and try again?

The latest commit should be jmcrthrs@8bae3d8

from examples.

timdeschryver avatar timdeschryver commented on May 22, 2024

So I assume there is no need for us to explicitly wait in our tests for the network response

Cypress has a retry ability, so even when the element isn't visible cypress will retry the query until it finds the element or times out. Because of this, I almost never wait for an HTTP request in my tests.

I have forked this repo and added an assertion after the Login form is submitted, but it can never find the button.

This is because the MSW handler returns the result immediately so React doesn't render the loading indicator.
When you add a delay to the handler (like the one you commented out), it will work.

from examples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.