MCPcopy Index your code
hub / github.com/testing-library/dom-testing-library / wrapAllByQueryWithSuggestion

Function wrapAllByQueryWithSuggestion

src/query-helpers.ts:167–207  ·  view source on GitHub ↗
(
    query: (container: HTMLElement, ...args: Arguments) => HTMLElement[],
    queryAllByName: string,
    variant: Variant,
  )

Source from the content-addressed store, hash-verified

165
166const wrapAllByQueryWithSuggestion =
167 <
168 // We actually want `Arguments extends [args: ...unknown[], options?: Options]`
169 // But that's not supported by TS so we have to `@ts-expect-error` every callsite
170 Arguments extends [...unknown[], WithSuggest],
171 >(
172 query: (container: HTMLElement, ...args: Arguments) => HTMLElement[],
173 queryAllByName: string,
174 variant: Variant,
175 ) =>
176 (container: HTMLElement, ...args: Arguments) => {
177 const els = query(container, ...args)
178
179 const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1) as [
180 WithSuggest,
181 ]
182 if (els.length && suggest) {
183 // get a unique list of all suggestion messages. We are only going to make a suggestion if
184 // all the suggestions are the same
185 const uniqueSuggestionMessages = [
186 ...new Set(
187 els.map(
188 element =>
189 getSuggestedQuery(element, variant)?.toString() as string,
190 ),
191 ),
192 ]
193
194 if (
195 // only want to suggest if all the els have the same suggestion.
196 uniqueSuggestionMessages.length === 1 &&
197 !queryAllByName.endsWith(
198 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- TODO: Can this be null at runtime?
199 getSuggestedQuery(els[0], variant)!.queryName as string,
200 )
201 ) {
202 throw getSuggestionError(uniqueSuggestionMessages[0], container)
203 }
204 }
205
206 return els
207 }
208
209// TODO: This deviates from the published declarations
210// However, the implementation always required a dyadic (after `container`) not variadic `queryAllBy` considering the implementation of `makeFindQuery`

Callers 9

text.tsFile · 0.90
alt-text.tsFile · 0.90
title.tsFile · 0.90
display-value.tsFile · 0.90
test-id.tsFile · 0.90
role.tsFile · 0.90
buildQueriesFunction · 0.85
label-text.tsFile · 0.85

Calls 4

getConfigFunction · 0.90
getSuggestedQueryFunction · 0.90
getSuggestionErrorFunction · 0.85
toStringMethod · 0.80

Tested by

no test coverage detected