Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): allow all elements #834

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"import/prefer-default-export": "off",
"import/no-unassigned-import": "off",
"import/no-useless-path-segments": "off",
"no-console": "off"
"no-console": "off",
"jest-dom/prefer-in-document": "off"
}
},
"eslintIgnore": [
Expand Down
2 changes: 1 addition & 1 deletion src/get-queries-for-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as defaultQueries from './queries'
*/

/**
* @param {HTMLElement} element container
* @param {Element} element container
* @param {FuncMap} queries object of functions
* @param {Object} initialValue for reducer
* @returns {FuncMap} returns object of functions bound to container
Expand Down
9 changes: 4 additions & 5 deletions types/__tests__/type-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function testQueryHelpers() {
const includesAutomationId = (content: string, automationId: string) =>
content.split(/\s+/).some(id => id === automationId)
const queryAllByAutomationId = (
container: HTMLElement,
container: Element,
automationId: string | string[],
options?: MatcherOptions,
) =>
Expand Down Expand Up @@ -138,7 +138,7 @@ export function eventTest() {
state: {page: 1},
})

// HTMLElement
// Element
const element = document.createElement('div')
fireEvent.click(getByText(element, 'foo'))

Expand All @@ -161,9 +161,7 @@ export async function testWaitFors() {

await waitFor(() => getByText(element, 'apple'))
await waitFor(() => getAllByText(element, 'apple'))
const result: HTMLSpanElement = await waitFor(() =>
getByText(element, 'apple'),
)
const result: Element = await waitFor(() => getByText(element, 'apple'))
if (!result) {
// Use value
throw new Error(`Can't find result`)
Expand All @@ -185,4 +183,5 @@ export async function testWaitFors() {
/*
eslint
@typescript-eslint/no-unnecessary-condition: "off",
import/no-extraneous-dependencies: "off",
*/
2 changes: 1 addition & 1 deletion types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface Config {
defaultHidden: boolean
showOriginalStackTrace: boolean
throwSuggestions: boolean
getElementError: (message: string, container: HTMLElement) => Error
getElementError: (message: string, container: Element) => Error
}

export interface ConfigFn {
Expand Down
2 changes: 1 addition & 1 deletion types/get-node-text.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export function getNodeText(node: HTMLElement): string;
export function getNodeText(node: Element): string;
8 changes: 4 additions & 4 deletions types/get-queries-for-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as queries from './queries';

export type BoundFunction<T> = T extends (
attribute: string,
element: HTMLElement,
element: Element,
text: infer P,
options: infer Q,
) => infer R
Expand All @@ -15,15 +15,15 @@ export type BoundFunction<T> = T extends (
export type BoundFunctions<T> = { [P in keyof T]: BoundFunction<T[P]> };

export type Query = (
container: HTMLElement,
container: Element,
...args: any[]
) => Error | Promise<HTMLElement[]> | Promise<HTMLElement> | HTMLElement[] | HTMLElement | null;
) => Error | Promise<Element[]> | Promise<Element> | Element[] | Element | null;

export interface Queries {
[T: string]: Query;
}

export function getQueriesForElement<T extends Queries = typeof queries>(
element: HTMLElement,
element: Element,
queriesToBind?: T,
): BoundFunctions<T>;
4 changes: 2 additions & 2 deletions types/matches.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ARIARole} from 'aria-query'

export type MatcherFunction = (content: string, element: HTMLElement) => boolean
export type MatcherFunction = (content: string, element: Element) => boolean
export type Matcher = MatcherFunction | {}

// Get autocomplete for ARIARole union types, while still supporting another string
Expand All @@ -22,7 +22,7 @@ export interface MatcherOptions {

export type Match = (
textToMatch: string,
node: HTMLElement | null,
node: Element | null,
matcher: Matcher,
options?: MatcherOptions,
) => boolean
Expand Down
60 changes: 30 additions & 30 deletions types/queries.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,68 @@ import {SelectorMatcherOptions} from './query-helpers'
import {waitForOptions} from './wait-for'

export type QueryByBoundAttribute = (
container: HTMLElement,
container: Element,
id: Matcher,
options?: MatcherOptions,
) => HTMLElement | null
) => Element | null

export type AllByBoundAttribute = (
container: HTMLElement,
container: Element,
id: Matcher,
options?: MatcherOptions,
) => HTMLElement[]
) => Element[]

export type FindAllByBoundAttribute = (
container: HTMLElement,
container: Element,
id: Matcher,
options?: MatcherOptions,
waitForElementOptions?: waitForOptions,
) => Promise<HTMLElement[]>
) => Promise<Element[]>

export type GetByBoundAttribute = (
container: HTMLElement,
container: Element,
id: Matcher,
options?: MatcherOptions,
) => HTMLElement
) => Element

export type FindByBoundAttribute = (
container: HTMLElement,
container: Element,
id: Matcher,
options?: MatcherOptions,
waitForElementOptions?: waitForOptions,
) => Promise<HTMLElement>
) => Promise<Element>

export type QueryByText = (
container: HTMLElement,
container: Element,
id: Matcher,
options?: SelectorMatcherOptions,
) => HTMLElement | null
) => Element | null

export type AllByText = (
container: HTMLElement,
container: Element,
id: Matcher,
options?: SelectorMatcherOptions,
) => HTMLElement[]
) => Element[]

export type FindAllByText = (
container: HTMLElement,
container: Element,
id: Matcher,
options?: SelectorMatcherOptions,
waitForElementOptions?: waitForOptions,
) => Promise<HTMLElement[]>
) => Promise<Element[]>

export type GetByText = (
container: HTMLElement,
container: Element,
id: Matcher,
options?: SelectorMatcherOptions,
) => HTMLElement
) => Element

export type FindByText = (
container: HTMLElement,
container: Element,
id: Matcher,
options?: SelectorMatcherOptions,
waitForElementOptions?: waitForOptions,
) => Promise<HTMLElement>
) => Promise<Element>

export interface ByRoleOptions extends MatcherOptions {
/**
Expand Down Expand Up @@ -109,36 +109,36 @@ export interface ByRoleOptions extends MatcherOptions {
}

export type AllByRole = (
container: HTMLElement,
container: Element,
role: ByRoleMatcher,
options?: ByRoleOptions,
) => HTMLElement[]
) => Element[]

export type GetByRole = (
container: HTMLElement,
container: Element,
role: ByRoleMatcher,
options?: ByRoleOptions,
) => HTMLElement
) => Element

export type QueryByRole = (
container: HTMLElement,
container: Element,
role: ByRoleMatcher,
options?: ByRoleOptions,
) => HTMLElement | null
) => Element | null

export type FindByRole = (
container: HTMLElement,
container: Element,
role: ByRoleMatcher,
options?: ByRoleOptions,
waitForElementOptions?: waitForOptions,
) => Promise<HTMLElement>
) => Promise<Element>

export type FindAllByRole = (
container: HTMLElement,
container: Element,
role: ByRoleMatcher,
options?: ByRoleOptions,
waitForElementOptions?: waitForOptions,
) => Promise<HTMLElement[]>
) => Promise<Element[]>

export const getByLabelText: GetByText
export const getAllByLabelText: AllByText
Expand Down
26 changes: 13 additions & 13 deletions types/query-helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,45 @@ export interface SelectorMatcherOptions extends MatcherOptions {

export type QueryByAttribute = (
attribute: string,
container: HTMLElement,
container: Element,
id: Matcher,
options?: MatcherOptions,
) => HTMLElement | null
) => Element | null

export type AllByAttribute = (
attribute: string,
container: HTMLElement,
container: Element,
id: Matcher,
options?: MatcherOptions,
) => HTMLElement[]
) => Element[]

export const queryByAttribute: QueryByAttribute
export const queryAllByAttribute: AllByAttribute
export function getElementError(message: string, container: HTMLElement): Error
export function getElementError(message: string, container: Element): Error

/**
* query methods have a common call signature. Only the return type differs.
*/
export type QueryMethod<Arguments extends any[], Return> = (
container: HTMLElement,
container: Element,
...args: Arguments
) => Return
export type QueryBy<Arguments extends any[]> = QueryMethod<
Arguments,
HTMLElement | null
Element | null
>
export type GetAllBy<Arguments extends any[]> = QueryMethod<
Arguments,
HTMLElement[]
Element[]
>
export type FindAllBy<Arguments extends any[]> = QueryMethod<
[Arguments[0], Arguments[1]?, waitForOptions?],
Promise<HTMLElement[]>
Promise<Element[]>
>
export type GetBy<Arguments extends any[]> = QueryMethod<Arguments, HTMLElement>
export type GetBy<Arguments extends any[]> = QueryMethod<Arguments, Element>
export type FindBy<Arguments extends any[]> = QueryMethod<
[Arguments[0], Arguments[1]?, waitForOptions?],
Promise<HTMLElement>
Promise<Element>
>

export type BuiltQueryMethods<Arguments extends any[]> = [
Expand All @@ -57,6 +57,6 @@ export type BuiltQueryMethods<Arguments extends any[]> = [
]
export function buildQueries<Arguments extends any[]>(
queryByAll: GetAllBy<Arguments>,
getMultipleError: (container: HTMLElement, ...args: Arguments) => string,
getMissingError: (container: HTMLElement, ...args: Arguments) => string,
getMultipleError: (container: Element, ...args: Arguments) => string,
getMissingError: (container: Element, ...args: Arguments) => string,
): BuiltQueryMethods<Arguments>
6 changes: 3 additions & 3 deletions types/role-helpers.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function logRoles(container: HTMLElement): string
export function logRoles(container: Element): string
export function getRoles(
container: HTMLElement,
): {[index: string]: HTMLElement[]}
container: Element,
): {[index: string]: Element[]}
/**
* https://testing-library.com/docs/dom-testing-library/api-helpers#isinaccessible
*/
Expand Down
2 changes: 1 addition & 1 deletion types/suggestions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type Method =
| 'testid'

export function getSuggestedQuery(
element: HTMLElement,
element: Element,
variant?: Variant,
method?: Method,
): Suggestion | undefined
2 changes: 1 addition & 1 deletion types/wait-for.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface waitForOptions {
container?: HTMLElement
container?: Element
timeout?: number
interval?: number
onTimeout?: (error: Error) => Error
Expand Down