Home > Mobile >  How to spy a "t" function in react-i18next?
How to spy a "t" function in react-i18next?

Time:11-12

I need to test this scenario:

  • in component:
t("some.key", { name: "Ash" })
  • in json:
{ "some": { "key": "Hi {{name}}" }

I would like to spy "t" function and get in the tests what parameters was passed and what was returned.

some like this:

expect(spyed).toHaveBeenCaledWith("some.key", { name: "Ash" })
expect(spyed).toHaveReturnedWith("Hi Ash")

CodePudding user response:

Usually it is a bad practice to make your assertions on implementation details.

i18next as a special mode, called cimode, which will make your t function to return the key itself. This allows your test to make the assertions on the outcome of your actions (check that the element contains the key value).

  • Related