Skip to content

TS example of custom-queries.js #889

Closed
@robbienohra

Description

@robbienohra

Describe the feature you'd like:

The current setup documentation has a TS example for the test-utils.js script (namely test-utils.tsx). It does not, however, have a TS example of the custom-queries.js script. The ask is to update the documentation with such an example — I've tried implementing one myself with some difficulty.

Suggested implementation:

Update the current document with an example as indicated above.

Example:

import { queryHelpers, buildQueries } from '@testing-library/react'

const queryAllByDataCy = (...args: unknown[]) =>
  queryHelpers.queryAllByAttribute('data-cy', ...args)

const getMultipleError = (c: HTMLElement, dataCyValue: unknown) =>
  `Found multiple elements with the data-cy attribute of: ${dataCyValue}`
const getMissingError = (c: HTMLElement, dataCyValue: unknown) =>
  `Unable to find an element with the data-cy attribute of: ${dataCyValue}`

const [
  queryByDataCy,
  getAllByDataCy,
  getByDataCy,
  findAllByDataCy,
  findByDataCy,
] = buildQueries(queryAllByDataCy, getMultipleError, getMissingError)

export {
  queryByDataCy,
  queryAllByDataCy,
  getByDataCy,
  getAllByDataCy,
  findAllByDataCy,
  findByDataCy,
}

Still seeing the following warning (not feeling comfortable with unknown[] but it looks like the underlying type is Arguments extends any[] and not sure how to correctly apply that here)

image

Describe alternatives you've considered:

NA

Teachability, Documentation, Adoption, Migration Strategy:

NA

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions