Skip to content

Commit 08db253

Browse files
authored
fix: 🐛 service not initialized in testing module (#382)
* fix: 🐛 service not initialized in testing module
1 parent 174f178 commit 08db253

File tree

12 files changed

+14295
-43
lines changed

12 files changed

+14295
-43
lines changed

docs/docs/additional-functionality.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ import { translate } from '@ngneat/transloco';
5959
translate('hello');
6060
```
6161

62+
:::important
63+
Note that this function is a proxy to the [TranslocoService.translate](./translation-api#translate) method.
64+
It will not work if the service didn't initialize.
65+
In order to safely use this method, you are responsible for ensuring that the translation files have been successfully loaded by the time it's called.
66+
:::
67+
6268
### `getBrowserLang()`
6369

6470
Returns the language code name from the browser, e.g. `"en"`

docs/docs/faq.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,18 @@ Using a structural directive is the recommended approach. It’s DRY and efficie
3333
Moreover, the t function is `memoized`, It means that given the same key, it will return the result directly from the cache.
3434

3535
Furthermore, when using on push change detection strategy (which is recommended) the change detection cycles should greatly reduce.
36+
37+
### Why values don't get translated when using the `translate` in unit tests?
38+
As stated beneath the function, even in tests, it's you responsibility to make sure the translation are loaded
39+
before calling it.
40+
41+
You can make sure you translations are loaded before your test executes by simply setting the `preloadLangs` to `true` in the options passed to
42+
`TranslocoTestingModule.forRoot`.
43+
44+
### Why does querying an element inside the `*transloco` directive using `@ViewChild()` not working?
45+
The transloco structural directive has an async operation in its internals which is fetching the translations.
46+
47+
Because of that, the contents of the directive won't be available until the translations are fetched, so you can't access
48+
the element in the `ngOnInit` nor the `ngAfterViewInit`.
49+
50+
The simplest way you can gain access to the element on render is by making the `ViewChild` a setter.

docs/docs/unit-testing.mdx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ When running specs, we want to have the languages available immediately, in a sy
88
We recommend to be DRY and create a module factory function that we can use in each spec, For example:
99
```ts title="transloco-module.spec.ts"
1010

11-
import { TranslocoTestingModule, TranslocoConfig } from '@ngneat/transloco';
11+
import { TranslocoTestingModule, TranslocoTestingOptions } from '@ngneat/transloco';
1212
import en from '../assets/i18n/en.json';
1313
import es from '../assets/i18n/es.json';
1414

15-
export function getTranslocoModule(config: Partial<TranslocoConfig> = {}) {
16-
return TranslocoTestingModule.withLangs(
17-
{ en, es },
18-
{
15+
export function getTranslocoModule(options: TranslocoTestingOptions = {}) {
16+
return TranslocoTestingModule.forRoot({
17+
langs: { en, es },
18+
translocoConfig: {
1919
availableLangs: ['en', 'es'],
2020
defaultLang: 'en',
21-
...config
22-
}
23-
);
21+
},
22+
...options
23+
});
2424
}
2525
```
2626

@@ -49,20 +49,20 @@ You can find an example [here](https://github.com/ngneat/transloco/blob/master/s
4949
If you need to test `scopes`, you should add them as `languages`, for example:
5050

5151
```ts {6,7} title="transloco-module.spec.ts"
52-
export function getTranslocoModule(config: Partial<TranslocoConfig> = {}) {
53-
return TranslocoTestingModule.withLangs(
54-
{
55-
en,
52+
export function getTranslocoModule(options: TranslocoTestingOptions = {}) {
53+
return TranslocoTestingModule.forRoot({
54+
langs: {
55+
en,
5656
es,
5757
'admin-page/en': admin,
58-
'admin-page/es': adminSpanish
58+
'admin-page/es': adminSpanish,
5959
},
60-
{
60+
translocoConfig: {
6161
availableLangs: ['en', 'es'],
6262
defaultLang: 'en',
63-
...config
64-
}
65-
);
63+
},
64+
...options
65+
});
6666
}
6767
```
6868

0 commit comments

Comments
 (0)