File tree 3 files changed +35
-7
lines changed
3 files changed +35
-7
lines changed Original file line number Diff line number Diff line change @@ -47,3 +47,32 @@ test('works without a global dom', async () => {
47
47
const data = JSON . parse ( submittedDataPre . textContent )
48
48
expect ( data ) . toEqual ( fakeUser )
49
49
} )
50
+
51
+ test ( 'works without a browser context on a dom node (JSDOM Fragment)' , ( ) => {
52
+ const container = JSDOM . fragment ( `
53
+ <html>
54
+ <body>
55
+ <form id="login-form">
56
+ <label for="username">Username</label>
57
+ <input id="username" />
58
+ <label for="password">Password</label>
59
+ <input id="password" type="password" />
60
+ <button type="submit">Submit</button>
61
+ <div id="data-container"></div>
62
+ </form>
63
+ </body>
64
+ </html>
65
+ ` )
66
+
67
+ expect ( dtl . getByLabelText ( container , / u s e r n a m e / i) ) . toMatchInlineSnapshot ( `
68
+ <input
69
+ id="username"
70
+ />
71
+ ` )
72
+ expect ( dtl . getByLabelText ( container , / p a s s w o r d / i) ) . toMatchInlineSnapshot ( `
73
+ <input
74
+ id="password"
75
+ type="password"
76
+ />
77
+ ` )
78
+ } )
Original file line number Diff line number Diff line change @@ -344,7 +344,7 @@ function getWindowFromNode(node) {
344
344
if ( node . defaultView ) {
345
345
// node is document
346
346
return node . defaultView
347
- } else if ( node . ownerDocument ) {
347
+ } else if ( node . ownerDocument && node . ownerDocument . defaultView ) {
348
348
// node is a DOM node
349
349
return node . ownerDocument . defaultView
350
350
} else if ( node . window ) {
Original file line number Diff line number Diff line change 1
- function getNodeText ( node ) {
2
- const window = node . ownerDocument . defaultView
1
+ // Constant node.nodeType for text nodes, see:
2
+ // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#Node_type_constants
3
+ const TEXT_NODE = 3
3
4
5
+ function getNodeText ( node ) {
4
6
if ( node . matches ( 'input[type=submit], input[type=button]' ) ) {
5
7
return node . value
6
8
}
7
9
8
10
return Array . from ( node . childNodes )
9
- . filter (
10
- child =>
11
- child . nodeType === window . Node . TEXT_NODE && Boolean ( child . textContent ) ,
12
- )
11
+ . filter ( child => child . nodeType === TEXT_NODE && Boolean ( child . textContent ) )
13
12
. map ( c => c . textContent )
14
13
. join ( '' )
15
14
}
You can’t perform that action at this time.
0 commit comments