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: improve error message when no window found #1089

Conversation

MatanBobi
Copy link
Member

What: Improves error message when window cannot be found

Why: Resolves #863

How: Narrow the else fallback

Checklist:

  • Documentation added to the
    docs site - N/A
  • Tests
  • TypeScript definitions updated - N/A
  • Ready to be merged

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 15, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 8b89090:

Sandbox Source
react-testing-library-examples Configuration

@MatanBobi
Copy link
Member Author

MatanBobi commented Jan 15, 2022

The validate is failing since coverage is not 100%. I tried to simulate a test when an HTMLElement is detached but I wasn't able to.
Any ideas how we can imitate that? I tried this:

  test('HTMLElement with no window', () => {
    const element = document.createElement('div')
    const removedElement = element.ownerDocument.removeChild(element)
    expect(() =>
      getWindowFromNode(removedElement),
    ).toThrowErrorMatchingInlineSnapshot()
  })

But it's failing saying that element is not a child of the ownerDocument.

Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

I think we can do something like this:

  test('htmlElement as node', () => {
    const elem = document.createElement('div')
    Object.defineProperty(elem, 'ownerDocument', {
      get: function get() {
        return null;
      },
    });
  
    expect(() => getWindowFromNode(elem)).toThrowErrorMatchingInlineSnapshot(
      `Unable to find the "window" object for the given node. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new`,
    )
  })

@MatanBobi
Copy link
Member Author

I think we can do something like this:

  test('htmlElement as node', () => {
    const elem = document.createElement('div')
    Object.defineProperty(elem, 'ownerDocument', {
      get: function get() {
        return null;
      },
    });
  
    expect(() => getWindowFromNode(elem)).toThrowErrorMatchingInlineSnapshot(
      `Unable to find the "window" object for the given node. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new`,
    )
  })

Good idea, that did the trick :)
I'm still wondering if that's even a valid use-case that can happen, but I pushed it :)
Thanks!

@codecov
Copy link

codecov bot commented Jan 15, 2022

Codecov Report

Merging #1089 (8b89090) into main (d578c7e) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main     #1089   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        25           
  Lines          954       956    +2     
  Branches       314       315    +1     
=========================================
+ Hits           954       956    +2     
Flag Coverage Δ
node-12 100.00% <100.00%> (ø)
node-14 100.00% <100.00%> (ø)
node-16.9.1 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/helpers.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d578c7e...8b89090. Read the comment docs.

src/__tests__/helpers.js Outdated Show resolved Hide resolved
src/helpers.js Outdated Show resolved Hide resolved
timdeschryver
timdeschryver previously approved these changes Jan 15, 2022
Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

I'm still wondering if that's even a valid use-case that can happen, but I pushed it :)

Me neither 😅 - but since someone opened an issue, there must be a few of those cases. Now, I hope this message will help them.

src/helpers.js Outdated Show resolved Hide resolved
…m:MatanBobi/dom-testing-library into feat/improve-error-message-when-no-window
@MatanBobi
Copy link
Member Author

MatanBobi commented Jan 25, 2022

Thanks for the review @eps1lon, anything preventing us from merging this?

Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

I'll also approve this PR since my review was still pending, just in case I was block this.

@eps1lon
Copy link
Member

eps1lon commented Jan 25, 2022

Just cant merge from the app. Will do when I'll have access to a desktop

@MatanBobi MatanBobi merged commit 7f5d421 into testing-library:main Jan 25, 2022
@github-actions
Copy link

🎉 This PR is included in version 8.11.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

Unable to find the "window" object for the given node for input event
3 participants