From 76a6886c89675bf862efea2da7dd5d76b0413017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zdunek?= Date: Fri, 15 May 2020 12:24:23 +0200 Subject: [PATCH 1/2] fix(TS): add fourth param to build findAllBy and findBy queries --- types/query-helpers.d.ts | 78 ++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/types/query-helpers.d.ts b/types/query-helpers.d.ts index 63a1f69b..de50a2d3 100644 --- a/types/query-helpers.d.ts +++ b/types/query-helpers.d.ts @@ -1,46 +1,62 @@ -import { Matcher, MatcherOptions } from './matches'; +import { Matcher, MatcherOptions } from './matches' +import { waitForOptions } from './wait-for' export interface SelectorMatcherOptions extends MatcherOptions { - selector?: string; + selector?: string } export type QueryByAttribute = ( - attribute: string, - container: HTMLElement, - id: Matcher, - options?: MatcherOptions, -) => HTMLElement | null; + attribute: string, + container: HTMLElement, + id: Matcher, + options?: MatcherOptions, +) => HTMLElement | null export type AllByAttribute = ( - attribute: string, - container: HTMLElement, - id: Matcher, - options?: MatcherOptions, -) => HTMLElement[]; + attribute: string, + container: HTMLElement, + id: Matcher, + options?: MatcherOptions, +) => HTMLElement[] -export const queryByAttribute: QueryByAttribute; -export const queryAllByAttribute: AllByAttribute; -export function getElementError(message: string, container: HTMLElement): Error; +export const queryByAttribute: QueryByAttribute +export const queryAllByAttribute: AllByAttribute +export function getElementError(message: string, container: HTMLElement): Error /** * query methods have a common call signature. Only the return type differs. */ -export type QueryMethod = (container: HTMLElement, ...args: Arguments) => Return; -export type QueryBy = QueryMethod; -export type GetAllBy = QueryMethod; -export type FindAllBy = QueryMethod>; -export type GetBy = QueryMethod; -export type FindBy = QueryMethod>; +export type QueryMethod = ( + container: HTMLElement, + ...args: Arguments +) => Return +export type QueryBy = QueryMethod< + Arguments, + HTMLElement | null +> +export type GetAllBy = QueryMethod< + Arguments, + HTMLElement[] +> +export type FindAllBy = QueryMethod< + [Arguments[0], Arguments[1], waitForOptions], + Promise +> +export type GetBy = QueryMethod +export type FindBy = QueryMethod< + [Arguments[0], Arguments[1], waitForOptions], + Promise +> export type BuiltQueryMethods = [ - QueryBy, - GetAllBy, - GetBy, - FindAllBy, - FindBy -]; + QueryBy, + GetAllBy, + GetBy, + FindAllBy, + FindBy, +] export function buildQueries( - queryByAll: GetAllBy, - getMultipleError: (container: HTMLElement, ...args: Arguments) => string, - getMissingError: (container: HTMLElement, ...args: Arguments) => string, -): BuiltQueryMethods; + queryByAll: GetAllBy, + getMultipleError: (container: HTMLElement, ...args: Arguments) => string, + getMissingError: (container: HTMLElement, ...args: Arguments) => string, +): BuiltQueryMethods From 5a9be5f429662d6b0f9d550d588ffa555b372ded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zdunek?= Date: Fri, 15 May 2020 13:08:15 +0200 Subject: [PATCH 2/2] add tests or query builder --- types/__tests__/type-tests.ts | 45 ++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/types/__tests__/type-tests.ts b/types/__tests__/type-tests.ts index 5203434e..cb8b56e0 100644 --- a/types/__tests__/type-tests.ts +++ b/types/__tests__/type-tests.ts @@ -2,9 +2,12 @@ import { fireEvent, isInaccessible, queries, + buildQueries, + queryAllByAttribute, screen, waitFor, waitForElementToBeRemoved, + MatcherOptions, } from '../index' const { @@ -42,6 +45,42 @@ async function testQueries() { await screen.findAllByText('bar', undefined, {timeout: 10}) } +async function testQueryHelpers() { + const element = document.createElement('div') + const includesAutomationId = (content: string, automationId: string) => + content.split(/\s+/).some(id => id === automationId) + const queryAllByAutomationId = ( + container: HTMLElement, + automationId: string | string[], + options?: MatcherOptions, + ) => + queryAllByAttribute( + 'testId', + container, + content => + Array.isArray(automationId) + ? automationId.every(id => includesAutomationId(content, id)) + : includesAutomationId(content, automationId), + options, + ) + const [ + queryByAutomationId, + getAllByAutomationId, + getByAutomationId, + findAllByAutomationId, + findByAutomationId, + ] = buildQueries( + queryAllByAutomationId, + () => 'Multiple Error', + () => 'Missing Error', + ) + queryByAutomationId(element, 'id') + getAllByAutomationId(element, 'id') + getByAutomationId(element, ['id', 'automationId']) + findAllByAutomationId(element, 'id', {}, {timeout: 1000}) + findByAutomationId(element, 'id', {}, {timeout: 1000}) +} + async function testByRole() { const element = document.createElement('button') element.setAttribute('aria-hidden', 'true') @@ -116,7 +155,11 @@ async function testWaitFors() { element.innerHTML = 'apple' - await waitForElementToBeRemoved(() => getByText(element, 'apple'), {interval: 3000, container: element, timeout: 5000}) + await waitForElementToBeRemoved(() => getByText(element, 'apple'), { + interval: 3000, + container: element, + timeout: 5000, + }) await waitForElementToBeRemoved(getByText(element, 'apple')) await waitForElementToBeRemoved(getAllByText(element, 'apple')) }