Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delay to Model mutation methods #226

Closed
ZengLawrence opened this issue Nov 28, 2019 · 3 comments
Closed

Add delay to Model mutation methods #226

ZengLawrence opened this issue Nov 28, 2019 · 3 comments

Comments

@ZengLawrence
Copy link

I want to test scenarios where database writes take longer than expected. For example, after saving a piece of data, then navigate to a dashboard to view. If writing to database takes a long time, the dashboard might not be able to retrieve the newly added data.

If I use "timing" config, it only delay the response but writing to Mirage DB still happens in real time. Can you add a timing config to db mutation methods e.g. create, update, save?

@ZengLawrence
Copy link
Author

I found that you can use a Promise to accomplish this. See below code snippets.

function delayRouteHandler(routeHandler, delay) {
    return new Promise(resolve => {
      setTimeout(() => resolve(routeHandler())
      , delay);
    });
  };

You can use delayRouteHnadler in regular setup like such:

      this.post("/movies", (schema, request) => {
        return delayRouteHandler(() => {
          const attrs = JSON.parse(request.requestBody);
          return schema.movies.create(attrs);
        }, 1000);  //delay db operation by 1 second
      });

@ryanto
Copy link
Contributor

ryanto commented Dec 4, 2019

Yup, exactly! We use this pattern whenever we need to simulate a slow backend.

Sounds like you got the issue solved, so I'm going to close this. Feel free to reopen if there's anything else you think is needed.

@ryanto ryanto closed this as completed Dec 4, 2019
@ZengLawrence
Copy link
Author

No problem. Can you add an example in your doc? I had to look through Pretender doc to figure that out. It would make it easier for others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants