Skip to content

Conversation

@DylanPiercey
Copy link
Contributor

@DylanPiercey DylanPiercey commented Jun 19, 2019

What:
This PR builds on top of c4b4eff and adds support for using the JSDOM.fragment API for server side tests.

Why:

I think using the JSDOM.fragment API makes sense for SSR only tests. In theory if there was client side logic you would instead be running these tests in a browser context.

With this fragment API there is no window, which caused the existing jsdom test to break.
My current thinking is that for SSR tests you really shouldn't be testing events and what not, instead use a browser test for that. Using the fragment API outputs a sane error if you try to do so, while still supporting the same API for everything else.

How:

I've made a couple small changes that account for the scenario when the DOM is not in a browser context.

Checklist:

  • Documentation added to the
    docs site
  • Typescript definitions updated
  • Tests
  • Ready to be merged

child =>
child.nodeType === window.Node.TEXT_NODE && Boolean(child.textContent),
)
.filter(child => child.nodeType === TEXT_NODE && Boolean(child.textContent))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I opted to just inline the TEXT_NODE constant. Alternatively it looks like this is also available as document.TEXT_NODE although I'm not entirely sure what the browser support for that is. Using the constant seemed sane as it is only in one place so far.

// node is document
return node.defaultView
} else if (node.ownerDocument) {
} else if (node.ownerDocument && node.ownerDocument.defaultView) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With JSDOM fragments there is still an ownerDocument, but no defaultView.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Looks pretty good. Just one thing.

@@ -1,15 +1,11 @@
const TEXT_NODE = 3
Copy link
Member

Choose a reason for hiding this comment

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

Could we add a link to MDN docs that explain that this is window.Node.TEXT_NODE and the value is 3 (per the spec). Otherwise this is surprising.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, updated the pr 😄

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@kentcdodds kentcdodds merged commit 792c5b7 into testing-library:master Jun 19, 2019
@kentcdodds
Copy link
Member

@all-contributors please add @DylanPiercey for code and tests

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @DylanPiercey! 🎉

@DylanPiercey
Copy link
Contributor Author

@kentcdodds not sure if you use gitter, but I reached out to you on there with some quick questions regarding adding a testing-library/marko module. Is there an appropriate place other than twitter to have a bit of a back and forth? 😛

@kentcdodds
Copy link
Member

Go ahead and open an issue on dom-testing-library :)

@kentcdodds
Copy link
Member

🎉 This PR is included in version 5.2.1 🎉

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.

2 participants