Skip to content

Commit 1840c48

Browse files
authored
Merge branch 'main' into global-regexp
2 parents db55959 + 90fba31 commit 1840c48

File tree

9 files changed

+59
-32
lines changed

9 files changed

+59
-32
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,15 @@
15151515
"contributions": [
15161516
"code"
15171517
]
1518+
},
1519+
{
1520+
"login": "jacksonhardaker",
1521+
"name": "Jackson Hardaker",
1522+
"avatar_url": "https://avatars.githubusercontent.com/u/7596320?v=4",
1523+
"profile": "https://www.jacksonhardaker.dev",
1524+
"contributions": [
1525+
"test"
1526+
]
15181527
}
15191528
],
15201529
"repoHost": "https://github.com"

.codesandbox/ci.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"installCommand": "install:csb",
23
"sandboxes": ["github/kentcdodds/react-testing-library-examples"],
34
"node": "12"
45
}

.github/workflows/validate.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,20 @@ jobs:
1818
# Otherwise we would not know if the problem is tied to the Node.js version
1919
fail-fast: false
2020
matrix:
21-
# TODO: relax `'16.9.1'` to `16` once GitHub has 16.9.1 cached. 16.9.0 is broken due to https://github.com/nodejs/node/issues/40030
22-
node: [12, 14, '16.9.1']
21+
node: [12, 14, 16]
2322
runs-on: ubuntu-latest
2423
steps:
2524
- name: 🛑 Cancel Previous Runs
2625
uses: styfle/[email protected]
2726

2827
- name: ⬇️ Checkout repo
29-
uses: actions/checkout@v2
28+
uses: actions/checkout@v3
3029
with:
3130
# required by codecov/codecov-action
3231
fetch-depth: 0
3332

3433
- name: ⎔ Setup node
35-
uses: actions/setup-node@v2
34+
uses: actions/setup-node@v3
3635
with:
3736
node-version: ${{ matrix.node }}
3837

@@ -66,10 +65,10 @@ jobs:
6665
uses: styfle/[email protected]
6766

6867
- name: ⬇️ Checkout repo
69-
uses: actions/checkout@v2
68+
uses: actions/checkout@v3
7069

7170
- name: ⎔ Setup node
72-
uses: actions/setup-node@v2
71+
uses: actions/setup-node@v3
7372
with:
7473
node-version: 14
7574

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ Thanks goes to these people ([emoji key][emojis]):
331331
</tr>
332332
<tr>
333333
<td align="center"><a href="https://github.com/Dennis273"><img src="https://avatars.githubusercontent.com/u/19815164?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dennis273</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=Dennis273" title="Code">💻</a></td>
334+
<td align="center"><a href="https://www.jacksonhardaker.dev"><img src="https://avatars.githubusercontent.com/u/7596320?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jackson Hardaker</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=jacksonhardaker" title="Tests">⚠️</a></td>
334335
</tr>
335336
</table>
336337

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"scripts": {
2727
"build": "kcd-scripts build --no-ts-defs --ignore \"**/__tests__/**,**/__node_tests__/**,**/__mocks__/**\" && kcd-scripts build --no-ts-defs --bundle --no-clean",
2828
"format": "kcd-scripts format",
29+
"install:csb": "npm install",
2930
"lint": "kcd-scripts lint",
3031
"setup": "npm install && npm run validate -s",
3132
"test": "kcd-scripts test",

src/__tests__/events.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,17 @@ eventTypes.forEach(({type, events, elementType}) => {
172172
expect(spy).toHaveBeenCalledTimes(1)
173173
})
174174
})
175-
176-
it('fires resize', () => {
177-
const node = document.defaultView
178-
const spy = jest.fn()
179-
node.addEventListener('resize', spy, {once: true})
180-
fireEvent.resize(node)
181-
expect(spy).toHaveBeenCalledTimes(1)
182-
})
183175
})
184176
})
185177

178+
it('fires resize', () => {
179+
const node = document.defaultView
180+
const spy = jest.fn()
181+
node.addEventListener('resize', spy, {once: true})
182+
fireEvent.resize(node)
183+
expect(spy).toHaveBeenCalledTimes(1)
184+
})
185+
186186
describe(`Bubbling Events`, () => {
187187
bubblingEvents.forEach(event =>
188188
it(`bubbles ${event}`, () => {

src/__tests__/helpers.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,22 @@ describe('window retrieval throws when given something other than a node', () =>
2525
`It looks like you passed an Array instead of a DOM node. Did you do something like \`fireEvent.click(screen.getAllBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`?`,
2626
)
2727
})
28+
test('window is not available for node', () => {
29+
const elem = document.createElement('div')
30+
Object.defineProperty(elem.ownerDocument, 'defaultView', {
31+
get: function get() {
32+
return null
33+
},
34+
})
35+
36+
expect(() => getWindowFromNode(elem)).toThrowErrorMatchingInlineSnapshot(
37+
`It looks like the window object is not available for the provided node.`,
38+
)
39+
})
40+
2841
test('unknown as node', () => {
2942
expect(() => getWindowFromNode({})).toThrowErrorMatchingInlineSnapshot(
30-
`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`,
43+
`The given node is not an Element, the node type is: object.`,
3144
)
3245
})
3346
})

src/helpers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ function getWindowFromNode(node) {
3333
} else if (node.window) {
3434
// node is window
3535
return node.window
36+
} else if (node.ownerDocument && node.ownerDocument.defaultView === null) {
37+
throw new Error(
38+
`It looks like the window object is not available for the provided node.`,
39+
)
3640
} else if (node.then instanceof Function) {
3741
throw new Error(
3842
`It looks like you passed a Promise object instead of a DOM node. Did you do something like \`fireEvent.click(screen.findBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`, or await the findBy query \`fireEvent.click(await screen.findBy...\`?`,
@@ -51,7 +55,7 @@ function getWindowFromNode(node) {
5155
} else {
5256
// The user passed something unusual to a calling function
5357
throw new Error(
54-
`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`,
58+
`The given node is not an Element, the node type is: ${typeof node}.`,
5559
)
5660
}
5761
}

src/matches.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,24 @@ function makeNormalizer({
9191
collapseWhitespace,
9292
normalizer,
9393
}: NormalizerOptions) {
94-
if (normalizer) {
95-
// User has specified a custom normalizer
96-
if (
97-
typeof trim !== 'undefined' ||
98-
typeof collapseWhitespace !== 'undefined'
99-
) {
100-
// They've also specified a value for trim or collapseWhitespace
101-
throw new Error(
102-
'trim and collapseWhitespace are not supported with a normalizer. ' +
103-
'If you want to use the default trim and collapseWhitespace logic in your normalizer, ' +
104-
'use "getDefaultNormalizer({trim, collapseWhitespace})" and compose that into your normalizer',
105-
)
106-
}
107-
108-
return normalizer
109-
} else {
94+
if (!normalizer) {
11095
// No custom normalizer specified. Just use default.
11196
return getDefaultNormalizer({trim, collapseWhitespace})
11297
}
98+
99+
if (
100+
typeof trim !== 'undefined' ||
101+
typeof collapseWhitespace !== 'undefined'
102+
) {
103+
// They've also specified a value for trim or collapseWhitespace
104+
throw new Error(
105+
'trim and collapseWhitespace are not supported with a normalizer. ' +
106+
'If you want to use the default trim and collapseWhitespace logic in your normalizer, ' +
107+
'use "getDefaultNormalizer({trim, collapseWhitespace})" and compose that into your normalizer',
108+
)
109+
}
110+
111+
return normalizer
113112
}
114113

115114
function matchRegExp(matcher: RegExp, text: string) {

0 commit comments

Comments
 (0)