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

Resolve all manual resolution requests by calling pretender.resolve() with no arguments? #306

Open
ctaylor-nurx opened this issue Jul 17, 2020 · 1 comment

Comments

@ctaylor-nurx
Copy link

So the docs are a little thin on the use case of manual async handlers (manual delay in the docs). I'm looking for an easier way to resolve all the manual requests that are sitting around waiting.

// register the handler as manually being resolved
pretender.get('/api/songs', songHandler, true);
fetch('/api/songs');
// then to resolve it, we need a reference to the request
pretender.resolve(request);

Right now I'm using this util function I wrote to do it:

function resolveManualRequests(pretender) {
  pretender.requestReferences.forEach((r) => {
    if (pretender.requiresManualResolution(r.request.method, r.request.url)) {
      pretender.resolve(r.request);
    }
  });
}

pulling a reference to the request itself to pass in can be a pain. It'd be great to be able to just manually resolve all standing requests. I think just calling pretender.resolve() with no arguments would be a clear way to achieve this. It'd be very easy to implement by just using this.requiresManualResolution(verb, path) and then recursing on itself with the request.

resolve: function resolve(request) {
  if (request) {
    for (let i = 0, len = this.requestReferences.length; i < len; i++) {
      let res = this.requestReferences[i];
      if (res.request === request) {
        res.callback();
        this.requestReferences.splice(i, 1);
        break;
      }
    }
  } else {
    this.requestReferences.forEach((request) => {
      if (this.requiresManualResolution(request.method, request.url)) {
        this.resolve(request);
      }
    });
  }
},
@xg-wang
Copy link
Member

xg-wang commented Apr 24, 2021

+1
Maybe add a resolveAllRequests() to avoid any possible breaking change?

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