Skip to content

Commit 7b9ea32

Browse files
committed
fix: relax element parameter type
1 parent e4ccdb4 commit 7b9ea32

16 files changed

+101
-107
lines changed

src/get-node-text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {TEXT_NODE} from './helpers'
22

3-
function getNodeText(node: HTMLElement): string {
3+
function getNodeText(node: Element): string {
44
if (
55
node.matches('input[type=submit], input[type=button], input[type=reset]')
66
) {

src/get-queries-for-element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as defaultQueries from './queries'
55
*/
66

77
/**
8-
* @param {HTMLElement} element container
8+
* @param {Element} element container
99
* @param {FuncMap} queries object of functions
1010
* @param {Object} initialValue for reducer
1111
* @returns {FuncMap} returns object of functions bound to container

src/queries/css.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {checkContainerType, getTypeName} from '../helpers'
22
import {GetErrorFunction, Matcher} from '../../types'
33
import {buildQueries} from './all-utils'
44

5-
const queryAllByCSS = (container: HTMLElement, selector: Matcher) => {
5+
const queryAllByCSS = (container: Element, selector: Matcher) => {
66
checkContainerType(container)
77
if (typeof selector !== 'string') {
88
throw new TypeError(

src/queries/label-text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from './all-utils'
2121

2222
function queryAllLabels(
23-
container: HTMLElement,
23+
container: Element,
2424
): {textToMatch: string | null; node: HTMLElement}[] {
2525
return Array.from(container.querySelectorAll<HTMLElement>('label,input'))
2626
.map(node => {

src/queries/text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const queryAllByText: AllByText = (
3232
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
3333
let baseArray: HTMLElement[] = []
3434
if (typeof container.matches === 'function' && container.matches(selector)) {
35-
baseArray = [container]
35+
baseArray = [container as HTMLElement]
3636
}
3737
return (
3838
[

src/queries/title.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
buildQueries,
1515
} from './all-utils'
1616

17-
const isSvgTitle = (node: HTMLElement) =>
17+
const isSvgTitle = (node: Element) =>
1818
node.tagName.toLowerCase() === 'title' &&
1919
node.parentElement?.tagName.toLowerCase() === 'svg'
2020

src/query-helpers.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ import {fuzzyMatches, matches, makeNormalizer} from './matches'
1212
import {waitFor} from './wait-for'
1313
import {getConfig} from './config'
1414

15-
function getElementError(message: string | null, container: HTMLElement) {
15+
function getElementError(message: string | null, container: Element) {
1616
return getConfig().getElementError(message, container)
1717
}
1818

19-
function getMultipleElementsFoundError(
20-
message: string,
21-
container: HTMLElement,
22-
) {
19+
function getMultipleElementsFoundError(message: string, container: Element) {
2320
return getElementError(
2421
`${message}\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).`,
2522
container,
@@ -28,7 +25,7 @@ function getMultipleElementsFoundError(
2825

2926
function queryAllByAttribute(
3027
attribute: string,
31-
container: HTMLElement,
28+
container: Element,
3229
text: Matcher,
3330
{exact = true, collapseWhitespace, trim, normalizer}: MatcherOptions = {},
3431
): HTMLElement[] {
@@ -43,7 +40,7 @@ function queryAllByAttribute(
4340

4441
function queryByAttribute(
4542
attribute: string,
46-
container: HTMLElement,
43+
container: Element,
4744
text: Matcher,
4845
options?: MatcherOptions,
4946
) {
@@ -64,7 +61,7 @@ function makeSingleQuery<Arguments extends unknown[]>(
6461
allQuery: QueryMethod<Arguments, HTMLElement[]>,
6562
getMultipleError: GetErrorFunction<Arguments>,
6663
) {
67-
return (container: HTMLElement, ...args: Arguments) => {
64+
return (container: Element, ...args: Arguments) => {
6865
const els = allQuery(container, ...args)
6966
if (els.length > 1) {
7067
const elementStrings = els
@@ -86,7 +83,7 @@ ${elementStrings}`,
8683

8784
function getSuggestionError(
8885
suggestion: {toString(): string},
89-
container: HTMLElement,
86+
container: Element,
9087
) {
9188
return getConfig().getElementError(
9289
`A better query is available, try this:
@@ -99,10 +96,10 @@ ${suggestion.toString()}
9996
// this accepts a query function and returns a function which throws an error
10097
// if an empty list of elements is returned
10198
function makeGetAllQuery<Arguments extends unknown[]>(
102-
allQuery: (container: HTMLElement, ...args: Arguments) => HTMLElement[],
99+
allQuery: (container: Element, ...args: Arguments) => HTMLElement[],
103100
getMissingError: GetErrorFunction<Arguments>,
104101
) {
105-
return (container: HTMLElement, ...args: Arguments) => {
102+
return (container: Element, ...args: Arguments) => {
106103
const els = allQuery(container, ...args)
107104
if (!els.length) {
108105
throw getConfig().getElementError(
@@ -119,13 +116,13 @@ function makeGetAllQuery<Arguments extends unknown[]>(
119116
// waitFor and passing a function which invokes the getter.
120117
function makeFindQuery<QueryFor, QueryMatcher>(
121118
getter: (
122-
container: HTMLElement,
119+
container: Element,
123120
text: QueryMatcher,
124121
options: MatcherOptions,
125122
) => QueryFor,
126123
) {
127124
return (
128-
container: HTMLElement,
125+
container: Element,
129126
text: QueryMatcher,
130127
options: MatcherOptions,
131128
waitForOptions: WaitForOptions,
@@ -141,11 +138,11 @@ function makeFindQuery<QueryFor, QueryMatcher>(
141138

142139
const wrapSingleQueryWithSuggestion =
143140
<Arguments extends [...unknown[], WithSuggest]>(
144-
query: (container: HTMLElement, ...args: Arguments) => HTMLElement | null,
141+
query: (container: Element, ...args: Arguments) => HTMLElement | null,
145142
queryAllByName: string,
146143
variant: Variant,
147144
) =>
148-
(container: HTMLElement, ...args: Arguments) => {
145+
(container: Element, ...args: Arguments) => {
149146
const element = query(container, ...args)
150147
const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1) as [
151148
WithSuggest,
@@ -169,11 +166,11 @@ const wrapAllByQueryWithSuggestion =
169166
// But that's not supported by TS so we have to `@ts-expect-error` every callsite
170167
Arguments extends [...unknown[], WithSuggest],
171168
>(
172-
query: (container: HTMLElement, ...args: Arguments) => HTMLElement[],
169+
query: (container: Element, ...args: Arguments) => HTMLElement[],
173170
queryAllByName: string,
174171
variant: Variant,
175172
) =>
176-
(container: HTMLElement, ...args: Arguments) => {
173+
(container: Element, ...args: Arguments) => {
177174
const els = query(container, ...args)
178175

179176
const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1) as [

types/__tests__/type-tests.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function testQueryHelpers() {
5656
const includesAutomationId = (content: string, automationId: string) =>
5757
content.split(/\s+/).some(id => id === automationId)
5858
const queryAllByAutomationId = (
59-
container: HTMLElement,
59+
container: Element,
6060
automationId: string[] | string,
6161
options?: MatcherOptions,
6262
) =>
@@ -136,13 +136,13 @@ export async function testQueryHelpers() {
136136

137137
export function testBoundFunctions() {
138138
const boundfunctions = {} as BoundFunctions<{
139-
customQueryOne: (container: HTMLElement, text: string) => HTMLElement
139+
customQueryOne: (container: Element, text: string) => HTMLElement
140140
customQueryTwo: (
141-
container: HTMLElement,
141+
container: Element,
142142
text: string,
143143
text2: string,
144144
) => HTMLElement
145-
customQueryThree: (container: HTMLElement, number: number) => HTMLElement
145+
customQueryThree: (container: Element, number: number) => HTMLElement
146146
}>
147147

148148
boundfunctions.customQueryOne('one')

types/get-node-text.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export function getNodeText(node: HTMLElement): string
1+
export function getNodeText(node: Element): string

0 commit comments

Comments
 (0)