Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Question: What is the recommended best practice for unit testing? #695

Closed
RyanClementsHax opened this issue Sep 24, 2021 · 1 comment
Closed

Comments

@RyanClementsHax
Copy link

RyanClementsHax commented Sep 24, 2021

Hello! What is the recommended way to set up unit tests with components that use next-translate?

I currently don't see a straightforward way of doing this, so I concocted my own solution (albiet imperfect). Right now I'm creating a custom render function when using testing-library/react. In that, I use I18nProvider then pass in any translations I think it might need, but this can get unwieldy since this would require me to know all the translations my tests will need upfront.

// testUtils.tsx
import React from 'react'
import { render } from '@testing-library/react'
import I18nProvider from 'next-translate/I18nProvider'
import index from 'locales/en/index.json'

const Providers: React.FC = ({ children }) => (
  <I18nProvider lang="en" namespaces={{ index }}>
    {children}
  </I18nProvider>
)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const customRender: typeof render = ((ui: any, options: any) =>
  render(ui, { wrapper: Providers, ...options })) as unknown as typeof render


export * from '@testing-library/react'

export { customRender as render }

In the test, I'm trying to use getT to get the translation being used, but I get the following error:
image
Is there a recomended way of going about this?
Thanks

Edit: turns out this is also an open question on stack overflow

@aralroca
Copy link
Owner

You can fix It adding the config json on global.i18nConfig, config.loadLocaleFrom is not working because config is get from this helper https://github.com/vinissimus/next-translate/blob/b1f0f409508d1a53a11018229a6789b56be76dc6/src/getConfig.tsx#L3-L7

Repository owner locked and limited conversation to collaborators Sep 24, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants