Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you rename data-testid to data-test-id because I am OCD? #204

Closed
bpkennedy opened this issue Oct 18, 2018 · 9 comments
Closed

Can you rename data-testid to data-test-id because I am OCD? #204

bpkennedy opened this issue Oct 18, 2018 · 9 comments

Comments

@bpkennedy
Copy link

Not sure why we can't hyphenate the whole thing is all. 馃槃

@gnapse
Copy link
Member

gnapse commented Oct 19, 2018

Please see testing-library/dom-testing-library#76

@6ewis
Copy link

6ewis commented Oct 22, 2018

How about we allow for both data-testid (and eventually deprecate it) and 'data-test-id`(which is more intuitive)

@kentcdodds
Copy link
Member

Sorry, no. We're going to stick with what we have. You can create a custom query and pass it as an argument to dom-testing-library and use that one instead if you want. Thanks anyway!

@Gpx
Copy link
Member

Gpx commented Oct 23, 2018

You can create a custom query and pass it as an argument to dom-testing-library and use that one instead if you want.

Would it be OK if I add a section in the README explaining how to make such a query?

@kentcdodds
Copy link
Member

Yes, please do @Gpx!

@ankitsinghaniyaz
Copy link
Contributor

ankitsinghaniyaz commented Oct 26, 2018

The example on the readme doesn't seem to work.

// test-utils.js
import {render, queryHelpers} from 'react-testing-library'

export const queryByTestId = queryHelpers.queryByAttribute.bind(
  null,
  'data-test-id',
)
export const queryAllByTestId = queryHelpers.queryAllByAttribute.bind(
  null,
  'data-test-id',
)

export function getAllByTestId(container, id, ...rest) {
  const els = queryAllByTestId(container, id, ...rest)
  if (!els.length) {
    throw getElementError(
      `Unable to find an element by: [data-test-id="${id}"]`,
      container,
    )
  }
  return els
}

export function getByTestId(...args) {
  return queryHelpers.firstResultOrNull(getAllByTestId, ...args)
}

const customRender = (node, ...options) => {
  const utils = render(node, ...options)

  return {
    ...utils,
    getByTestId: getByTestId.bind(container),
    getAllByTestId: getAllByTestId.bind(container),
    queryByTestId: queryByTestId.bind(container),
    queryAllByTestId: queryAllByTestId.bind(container),
  }
}

// re-export everything
export * from 'react-testing-library'

// override render method
export {customRender as render}

Here are the issues that I see:

  1. container is not defined in the customRender method
  2. At runtime I get an error saying TypeError: container.querySelectorAll is not a function for line 17
    const els = queryAllByTestId(container, id, ...rest)
  3. getElementError is not defined

@alexkrolick
Copy link
Collaborator

Woops

  • change node to container
  • should be queryHelpers.getElementError

PR appreciated

@ankitsinghaniyaz
Copy link
Contributor

Cool, I'll create a PR for this.

kentcdodds pushed a commit that referenced this issue Nov 6, 2018
This PR fixes the mistake in the custom renderer example as discussed on #204 (comment)
@eran-co
Copy link

eran-co commented Oct 7, 2019

the ability has been added to the library, see here

xbucks pushed a commit to xbucks/tauri-crypto-trading that referenced this issue Oct 15, 2023
This PR fixes the mistake in the custom renderer example as discussed on testing-library/react-testing-library#204 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants