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

fix(component-testing): Correctly unmount react components #15250

Merged
merged 2 commits into from
Feb 26, 2021

Conversation

dmtrKovalenko
Copy link
Contributor

@dmtrKovalenko dmtrKovalenko commented Feb 26, 2021

This PR fixes how we unmounting and clearing root elements before the test. Cleaning up the platform between tests is the responsibility of the specific adapter because unmounting react/vue component should be done using a specific framework API (for better dev tools integration and to get rid of global event listeners from previous tests.) Also, this matches the real lifecycle of components in the user applications.

This fixes duplicated root elements for react because when we are doing unmount react cleans up information in devtools as well. The same approach should work for vue using $destroy.

@dmtrKovalenko dmtrKovalenko requested a review from a team February 26, 2021 16:14
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Feb 26, 2021

Thanks for taking the time to open a PR!

Copy link
Contributor

@lmiller1990 lmiller1990 left a comment

Choose a reason for hiding this comment

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

I am surprised how little code there is here - glad you got it working. I will investigate a similar approach for Vue.

// Before all tests we are mounting the root element, **not beforeEach**
// Cleaning up platform between tests is the responsibility of the specific adapter
// because unmounting react/vue component should be done using specific framework API
// (for devtools and to get rid of global event listeners from previous tests.)
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is a little confusing to me - Just for my understanding, unmounting the component won't actually get rid of the global listener (REACT_DEVTOOLS_GLOBAL_HOOK) though, right? It will just make sure the component is no longer present in the devtools.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean if you are doing document.addEventListener inside the useEffect and unsubscribing in return

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, sure. Makes sense. One thing we might need to be careful is if the devtools do window.addEventListener('postMessage'). Vue DevTools do this - we need to clean it up because window.addEventListener` will not replace the previous one but duplicate it.

Copy link
Contributor

@elevatebart elevatebart left a comment

Choose a reason for hiding this comment

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

The issue is fixed and the code is leaner.
I like

@@ -16,22 +16,6 @@ export function setupHooks (rootId: string) {
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Though this is not part of the fix, this clean-up is a very good idea.
I am going to check for the same issues in the vue side.

// Cleaning up platform between tests is the responsibility of the specific adapter
// because unmounting react/vue component should be done using specific framework API
// (for devtools and to get rid of global event listeners from previous tests.)
before(() => {
appendTargetIfNotExists('__cy_root')
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably need to fix vue with using this root too. Right now, @cypress/vue's mount function creates it's very own root.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did change this so it is matching the old react behavior (that I deleted here) – but yes, I do want to test this in scope of devtools PR

@dmtrKovalenko dmtrKovalenko merged commit 6b515c7 into develop Feb 26, 2021
@emilyrohrbough emilyrohrbough deleted the fix/correctly-unmount-react-components branch August 1, 2024 13:51
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

Successfully merging this pull request may close these issues.

3 participants