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

Proposal: Promise based version of scope.isDone() #2429

Open
2 tasks done
craig-davis opened this issue Dec 20, 2022 · 0 comments
Open
2 tasks done

Proposal: Promise based version of scope.isDone() #2429

craig-davis opened this issue Dec 20, 2022 · 0 comments
Labels

Comments

@craig-davis
Copy link

Please avoid duplicates

Context

There are times when it would be convenient to await the completion of network requests. This would be a promise based version of scope.isDone(). It would resolve when the scope is done and network requests have been sent.

When used in combination of the act() test utilities this could provide a compact way to assert that a response has been sent and component updates are complete.

const searchResults = nock(API_HOST)
  .post(SEARCH_ROUTE, searchMatcher)
  .reply(searchResultReply)

// ... a component is rendered that makes a request

await act(async () => {
  await searchResults.isDoneP();
});

expect(
  renderer.root.findAllByType('span').map(({children}) => children)
).toEqual(
  [....]
);

Alternatives

Here's a draft of a function that provides similar functionality:

async function nockScopeResolved(scope, ms = 500, triesLeft = 5) {
  return new Promise((resolve, reject) => {
    const interval = setInterval(async () => {
      if (scope.isDone()) {
        resolve();
        clearInterval(interval);
      } else if (triesLeft <= 1) {
        reject();
        clearInterval(interval);
      }
      triesLeft--;
    }, ms);
  });
}

can be used as

const searchResults = nock(API_HOST)
  .post(SEARCH_ROUTE, searchMatcher)
  .reply(searchResultReply)

await nockScopeResolved(searchResults);

If the feature request is accepted, would you be willing to submit a PR?

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

No branches or pull requests

1 participant