Skip to content

Commit

Permalink
fix(buildQuery): get*Error functions receive same Arguments type as q…
Browse files Browse the repository at this point in the history
…ueryAllBy parameter (#1041)
  • Loading branch information
jacklaurencegaray committed Sep 30, 2021
1 parent 99bc2c0 commit 6171979
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
31 changes: 29 additions & 2 deletions types/__tests__/type-tests.ts
Expand Up @@ -68,6 +68,22 @@ export async function testQueryHelpers() {
: includesAutomationId(content, automationId),
options,
)

const createIdRelatedErrorHandler =
(errorMessage: string, defaultErrorMessage: string) =>
<T>(container: Element | null, ...args: T[]) => {
const [key, value] = args
if (!container) {
return 'Container element not specified'
}
if (key && value) {
return errorMessage
.replace('[key]', String(key))
.replace('[value]', String(value))
}
return defaultErrorMessage
}

const [
queryByAutomationId,
getAllByAutomationId,
Expand All @@ -76,8 +92,14 @@ export async function testQueryHelpers() {
findByAutomationId,
] = buildQueries(
queryAllByAutomationId,
() => 'Multiple Error',
() => 'Missing Error',
createIdRelatedErrorHandler(
`Found multiple with key [key] and value [value]`,
'Multiple error',
),
createIdRelatedErrorHandler(
`Unable to find an element with the [key] attribute of: [value]`,
'Missing error',
),
)
queryByAutomationId(element, 'id')
getAllByAutomationId(element, 'id')
Expand All @@ -89,6 +111,11 @@ export async function testQueryHelpers() {
await findByAutomationId(element, 'id', {})
await findAllByAutomationId(element, 'id')
await findByAutomationId(element, 'id')

await findAllByAutomationId(element, ['id', 'id'], {})
await findByAutomationId(element, ['id', 'id'], {})
await findAllByAutomationId(element, ['id', 'id'])
await findByAutomationId(element, ['id', 'id'])
}

export function testBoundFunctions() {
Expand Down
4 changes: 2 additions & 2 deletions types/query-helpers.d.ts
Expand Up @@ -69,6 +69,6 @@ export type BuiltQueryMethods<Arguments extends any[]> = [

export function buildQueries<Arguments extends any[]>(
queryAllBy: GetAllBy<Arguments>,
getMultipleError: GetErrorFunction,
getMissingError: GetErrorFunction,
getMultipleError: GetErrorFunction<Arguments>,
getMissingError: GetErrorFunction<Arguments>,
): BuiltQueryMethods<Arguments>

0 comments on commit 6171979

Please sign in to comment.