Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

✨ Add cleanup option to createMount in react-testing#2102

Merged
marutypes merged 3 commits intomainfrom
react-testing-cleanup-handler
Dec 7, 2021
Merged

✨ Add cleanup option to createMount in react-testing#2102
marutypes merged 3 commits intomainfrom
react-testing-cleanup-handler

Conversation

@marutypes
Copy link
Contributor

Description

This PR adds a cleanup function to createMount in @shopify/react-testing that can be used to handle any additional cleanup a wrapper or its context might need. This was originally proposed by @GoodForOneFare for stopping any ongoing gql requests and other such async handlers that may be leaking out of our context objects.

Type of change

  • @shopify/react-testing Minor: New feature (non-breaking change which adds functionality)

Checklist

  • I have added a changelog entry, prefixed by the type of change noted above (Documentation fix and Test update does not need a changelog as we do not publish new version)

@marutypes marutypes requested a review from a team December 6, 2021 21:36
Copy link
Contributor

@dahukish dahukish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Copy link
Member

@BPScott BPScott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great to me!

Remember the PR link in the changelog and :shipit:

if (cleanup) {
const originalDestroy = wrapper.destroy.bind(wrapper);
wrapper.destroy = () => {
cleanup(wrapper, options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be a lot of effort to support async cleanup? A common pattern in web is "loading state tests need end with a graphQL.resolveAll() to complete pending React.act calls".

If we could automate that, adding an afterEach check for dangling acts becomes less noisy.

Copy link
Contributor Author

@marutypes marutypes Dec 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My worry is that destroy is currently sync, which means that existing uses could break if its unexpectedly changed, and simply overwriting it with an async function in some cases doesn't fix types. I can take a crack at it as a follow-up though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also @GoodForOneFare if you have any ideas about how to deal with the points above (or think they're not a big deal) that's also good info :P

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are good points! I figured it wouldn't be trivial.

Overnight, I talked myself into wanting an alternative solution. Something more explicit, and part of general test flow.

The bad idea version of this looks like:

await component.trigger('onFocus', {andTotesResolveGraphQLForMe: true});


### Added

- Added new `cleanup` option to `createMount`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:yells-at-pr-self-reference:

Copy link
Member

@GoodForOneFare GoodForOneFare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I have a question about async support, but this can ship as is.

marutypes and others added 2 commits December 7, 2021 13:40
Co-authored-by: Gord P <GoodForOneFare@users.noreply.github.com>
@marutypes marutypes merged commit fc99e60 into main Dec 7, 2021
@marutypes marutypes deleted the react-testing-cleanup-handler branch December 7, 2021 19:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants