Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions src/__node_tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,32 @@ test('works without a global dom', async () => {
const data = JSON.parse(submittedDataPre.textContent)
expect(data).toEqual(fakeUser)
})

test('works without a browser context on a dom node (JSDOM Fragment)', () => {
const container = JSDOM.fragment(`
<html>
<body>
<form id="login-form">
<label for="username">Username</label>
<input id="username" />
<label for="password">Password</label>
<input id="password" type="password" />
<button type="submit">Submit</button>
<div id="data-container"></div>
</form>
</body>
</html>
`)

expect(dtl.getByLabelText(container, /username/i)).toMatchInlineSnapshot(`
<input
id="username"
/>
`)
expect(dtl.getByLabelText(container, /password/i)).toMatchInlineSnapshot(`
<input
id="password"
type="password"
/>
`)
})
2 changes: 1 addition & 1 deletion src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ function getWindowFromNode(node) {
if (node.defaultView) {
// 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.

// node is a DOM node
return node.ownerDocument.defaultView
} else if (node.window) {
Expand Down
8 changes: 2 additions & 6 deletions src/get-node-text.js
Original file line number Diff line number Diff line change
@@ -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 😄

function getNodeText(node) {
const window = node.ownerDocument.defaultView

if (node.matches('input[type=submit], input[type=button]')) {
return node.value
}

return Array.from(node.childNodes)
.filter(
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.

.map(c => c.textContent)
.join('')
}
Expand Down