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(src/index.js): Fix exception when testing with Jest #682

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class ReactTooltip extends React.Component {
let domRoot;

switch (parentNode.constructor.name) {
case 'Document':

Choose a reason for hiding this comment

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

Could you explain why this fixes the issue?

Choose a reason for hiding this comment

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

I would love to know as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because without this, the code tries to insert child style element at that wrong place therefore leading to exception whereas Document and HTMLDocument both have a header section where the style child can be added without any issue

Copy link

Choose a reason for hiding this comment

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

I think the problem is that document.constructor.name property returns different results for browser and jsdom environments.
In browser it is 'HTMLDocument' so injectStyles works perfectly:
image
and in jsdom it is just 'Document':
image
which leads to default case and sets domRoot to be document
and calling appendChild on 'document' leads to DOMException (HierarchyRequestError: The operation would yield an incorrect node tree.)
This PR fixes this problem

case 'HTMLDocument':
domRoot = parentNode.head;
break;
Expand Down