Skip to content

Commit

Permalink
Merge branch 'main' into global-regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Mar 23, 2022
2 parents db55959 + 90fba31 commit 1840c48
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 32 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,15 @@
"contributions": [
"code"
]
},
{
"login": "jacksonhardaker",
"name": "Jackson Hardaker",
"avatar_url": "https://avatars.githubusercontent.com/u/7596320?v=4",
"profile": "https://www.jacksonhardaker.dev",
"contributions": [
"test"
]
}
],
"repoHost": "https://github.com"
Expand Down
1 change: 1 addition & 0 deletions .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"installCommand": "install:csb",
"sandboxes": ["github/kentcdodds/react-testing-library-examples"],
"node": "12"
}
11 changes: 5 additions & 6 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ jobs:
# Otherwise we would not know if the problem is tied to the Node.js version
fail-fast: false
matrix:
# 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
node: [12, 14, '16.9.1']
node: [12, 14, 16]
runs-on: ubuntu-latest
steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.0

- name: ⬇️ Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
# required by codecov/codecov-action
fetch-depth: 0

- name: ⎔ Setup node
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

Expand Down Expand Up @@ -66,10 +65,10 @@ jobs:
uses: styfle/cancel-workflow-action@0.9.0

- name: ⬇️ Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: ⎔ Setup node
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: 14

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ Thanks goes to these people ([emoji key][emojis]):
</tr>
<tr>
<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>
<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>
</tr>
</table>

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"scripts": {
"build": "kcd-scripts build --no-ts-defs --ignore \"**/__tests__/**,**/__node_tests__/**,**/__mocks__/**\" && kcd-scripts build --no-ts-defs --bundle --no-clean",
"format": "kcd-scripts format",
"install:csb": "npm install",
"lint": "kcd-scripts lint",
"setup": "npm install && npm run validate -s",
"test": "kcd-scripts test",
Expand Down
16 changes: 8 additions & 8 deletions src/__tests__/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,17 @@ eventTypes.forEach(({type, events, elementType}) => {
expect(spy).toHaveBeenCalledTimes(1)
})
})

it('fires resize', () => {
const node = document.defaultView
const spy = jest.fn()
node.addEventListener('resize', spy, {once: true})
fireEvent.resize(node)
expect(spy).toHaveBeenCalledTimes(1)
})
})
})

it('fires resize', () => {
const node = document.defaultView
const spy = jest.fn()
node.addEventListener('resize', spy, {once: true})
fireEvent.resize(node)
expect(spy).toHaveBeenCalledTimes(1)
})

describe(`Bubbling Events`, () => {
bubblingEvents.forEach(event =>
it(`bubbles ${event}`, () => {
Expand Down
15 changes: 14 additions & 1 deletion src/__tests__/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,22 @@ describe('window retrieval throws when given something other than a node', () =>
`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...\`?`,
)
})
test('window is not available for node', () => {
const elem = document.createElement('div')
Object.defineProperty(elem.ownerDocument, 'defaultView', {
get: function get() {
return null
},
})

expect(() => getWindowFromNode(elem)).toThrowErrorMatchingInlineSnapshot(
`It looks like the window object is not available for the provided node.`,
)
})

test('unknown as node', () => {
expect(() => getWindowFromNode({})).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`,
`The given node is not an Element, the node type is: object.`,
)
})
})
Expand Down
6 changes: 5 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ function getWindowFromNode(node) {
} else if (node.window) {
// node is window
return node.window
} else if (node.ownerDocument && node.ownerDocument.defaultView === null) {
throw new Error(
`It looks like the window object is not available for the provided node.`,
)
} else if (node.then instanceof Function) {
throw new Error(
`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...\`?`,
Expand All @@ -51,7 +55,7 @@ function getWindowFromNode(node) {
} else {
// The user passed something unusual to a calling function
throw new Error(
`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`,
`The given node is not an Element, the node type is: ${typeof node}.`,
)
}
}
Expand Down
31 changes: 15 additions & 16 deletions src/matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,24 @@ function makeNormalizer({
collapseWhitespace,
normalizer,
}: NormalizerOptions) {
if (normalizer) {
// User has specified a custom normalizer
if (
typeof trim !== 'undefined' ||
typeof collapseWhitespace !== 'undefined'
) {
// They've also specified a value for trim or collapseWhitespace
throw new Error(
'trim and collapseWhitespace are not supported with a normalizer. ' +
'If you want to use the default trim and collapseWhitespace logic in your normalizer, ' +
'use "getDefaultNormalizer({trim, collapseWhitespace})" and compose that into your normalizer',
)
}

return normalizer
} else {
if (!normalizer) {
// No custom normalizer specified. Just use default.
return getDefaultNormalizer({trim, collapseWhitespace})
}

if (
typeof trim !== 'undefined' ||
typeof collapseWhitespace !== 'undefined'
) {
// They've also specified a value for trim or collapseWhitespace
throw new Error(
'trim and collapseWhitespace are not supported with a normalizer. ' +
'If you want to use the default trim and collapseWhitespace logic in your normalizer, ' +
'use "getDefaultNormalizer({trim, collapseWhitespace})" and compose that into your normalizer',
)
}

return normalizer
}

function matchRegExp(matcher: RegExp, text: string) {
Expand Down

0 comments on commit 1840c48

Please sign in to comment.