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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing-library can't find i18next Trans texts #964

Closed
ghost opened this issue Apr 27, 2022 · 4 comments
Closed

testing-library can't find i18next Trans texts #964

ghost opened this issue Apr 27, 2022 · 4 comments
Labels
duplicate This issue or pull request already exists

Comments

@ghost
Copy link

ghost commented Apr 27, 2022

Describe the bug

I'm using a <Trans/> component from react-i18next module to translate complex react elements (without mocking it)

It works well in React Native runtime:
image

  • But when I tried to find elements by text it doesn't work
  • When I used debug() text that I'm trying to find exists

Expected behavior

//...
const screen = render(<Text><Trans t={t}>My test</Trans></Text>)

screen.getByText('My test') // will work

Steps to Reproduce

git clone https://github.com/retyui/testing-library-and-i18next-issue
cd testing-library-and-i18next-issue
yarn
yarn test

Screenshots

Full code you find here https://github.com/retyui/testing-library-and-i18next-issue

      <Text>
        <Trans t={t}>My test</Trans>
      </Text>

      <Text>
        <Trans t={t}>
          Open <Text style={styles.link}>link</Text>
        </Trans>
      </Text>

      <Text>
        <Trans t={t} values={{name: 'callstack'}}>
          Hello <Text style={styles.bold}>{'{{name}}'}</Text>!
        </Trans>
      </Text>

image

Versions

  npmPackages:
    @testing-library/react-native: ^9.1.0 => 9.1.0 
    react: 17.0.2 => 17.0.2 
    react-native: 0.68.0 => 0.68.0 
    react-test-renderer: 17.0.2 => 17.0.2 
@pierrezimmermannbam
Copy link
Collaborator

Hi @davydspotnana , this issue is currently being adressed (see #937 for more information). You can refer to this comment #937 (comment) for a workaround by mocking the trans component. Hope this helps !

@AugustinLF
Copy link
Collaborator

Thanks the help @pierrezimmermannbam, will mark as duplicate.

@AugustinLF AugustinLF added the duplicate This issue or pull request already exists label May 3, 2022
@retyui
Copy link
Contributor

retyui commented May 4, 2022

@pierrezimmermannbam I am an author of two these issue ) I don think that this is duplicate (@AugustinLF please reopen it)

I don't want to mock a Trans component because it affect testing

With a mock I can't:

  1. testing pluralization
<Trans values={{cars: 2}}>{`{{cars}} car`}`</Trans> 
// getByText('2 cars') 
// getByText('1 car')
  1. Verify values
<Trans values={{ NON__CAR_NAME: 2 }}>{`{{cars}} car`}`</Trans> 
// getByText('2 cars') // not found

@AugustinLF
Copy link
Collaborator

@retyui The root cause is the same in both issues. RNTL can only detect text directly inside <Text> components. If you have any intermediate component (even if it doesn't appear in the renderer) RNTL will be confused and won't be able to match it.

We agree that mocking is not a long term solution, we're still trying to find a real fix, there's some progress in #967 but it's a non trivial issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants