testing-library / testing-library/testing-library-docs

Setup for custom queries clashes with AllByAttribute type

Open
#804 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
479
Forks
740
PR merge metrics
No merged PRs in 30d

Description

Describe the bug
https://testing-library.com/docs/react-testing-library/setup#add-custom-queries

Existing setup for custom queries documentation does not work with typescript

To Reproduce
converting the following to typescript

const queryAllByDataCy = (...args) =>
  queryHelpers.queryAllByAttribute('data-cy', ...args)
const queryAllByDataCy: AllByAttribute = (_, ...args) =>
  queryHelpers.queryAllByAttribute('data-cy', ...args)

Produces a type error in the buildQueries function
type error

Argument of type 'AllByAttribute' is not assignable to parameter of type 'GetAllBy<[container: HTMLElement, id: Matcher, options?: MatcherOptions | undefined]>'.                                                                   
  Types of parameters 'attribute' and 'container' are incompatible.                                                                                                                                                                                                               
    Type 'HTMLElement' is not assignable to type 'string'.                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                  
buildQueries(queryAllByDataCy, getMultipleError, getMissingError) 

Expected behavior
When converting the provided example to typescript the setup example should be in a format that conforms to the libraries type AllByAttribute.

This can be done by using partial application the container argument on queryHelpers.queryAllByAttribute and then updating the type of AllByAttribute to omit the partial argument
e.g.

js only:

const queryAllByDataCy = queryHelpers.queryAllByAttribute,bind(null, 'data-cy')

ts only:

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

const queryAllByDataCy: QueryAllByDataCy = queryHelpers.queryAllByAttribute,bind(null, 'data-cy')

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Ubuntu 20.04.2 LTS
  • Typescript ^4.2.4
  • Node 15.14.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the custom queries setup page linked in the issue and compare its JavaScript example with the reported TypeScript conversion. Update the example so the custom query conforms to the documented type and buildQueries usage, then verify that the TypeScript version no longer produces the reported type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.