Skip to content

Commit 8a95dcd

Browse files
authored
documents keyFromSelector (#237)
* GITBOOK-1: No subject * GITBOOK-2: No subject * GITBOOK-3: No subject * GITBOOK-4: No subject * GITBOOK-5: No subject
1 parent 8d7eef2 commit 8a95dcd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎overview/typescript.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ We provide a few options that can improve TypeScript for `i18next`. All options
124124
| interpolationSuffix | '\}}' | Suffix for interpolation |
125125
| strictKeyChecks | false | Flag that enables strict key checking even if a `defaultValue` has been provided. This ensures all calls of `t` function don't accidentally use implicitly missing keys. |
126126

127+
## Testing
128+
129+
### Mocking the selector function
130+
131+
For testing purposes, you might find yourself wanting to mock the `t` function.
132+
133+
`i18next` exports a function called `keyFromSelector` to make this easier. Here's an example of how you can use it:
134+
135+
```typescript
136+
import { keyFromSelector } from "i18next";
137+
138+
const mockT = (selector: ($: Record<string, any>) => any) => keyFromSelector(selector);
139+
140+
const mockTranslation = mockT($ => $.abc.def);
141+
142+
console.log(mockTranslation); // => "abc.def"
143+
```
144+
127145
## Troubleshooting
128146

129147
### Intellisense not working

0 commit comments

Comments
 (0)