Skip to content

Commit 9914e9f

Browse files
committed
docs: ✏️ bring back unit test examples
1 parent 42e8af0 commit 9914e9f

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { waitForAsync, TestBed } from '@angular/core/testing';
2+
import { By } from '@angular/platform-browser';
3+
4+
import { TRANSLOCO_SCOPE } from '@ngneat/transloco';
5+
6+
import { getTranslocoModule } from '../transloco-testing.module';
7+
8+
import { LazyComponent } from './lazy.component';
9+
10+
describe('LazyComponent', () => {
11+
beforeEach(waitForAsync(() => {
12+
TestBed.configureTestingModule({
13+
providers: [{ provide: TRANSLOCO_SCOPE, useValue: 'admin-page' }],
14+
imports: [getTranslocoModule()],
15+
declarations: [LazyComponent],
16+
}).compileComponents();
17+
}));
18+
19+
it('should get scoped title translation', function () {
20+
const fixture = TestBed.createComponent(LazyComponent);
21+
fixture.detectChanges();
22+
expect(
23+
fixture.debugElement.query(By.css('.admin-title')).nativeElement.innerText
24+
).toBe('Admin spanish');
25+
});
26+
27+
it('should get scoped translation with read', function () {
28+
const fixture = TestBed.createComponent(LazyComponent);
29+
fixture.detectChanges();
30+
expect(
31+
fixture.debugElement.query(By.css('.admin-read')).nativeElement.innerText
32+
).toBe('Admin read spanish');
33+
});
34+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { createComponentFactory, Spectator } from '@ngneat/spectator';
2+
3+
import { TranslocoService } from '@ngneat/transloco';
4+
5+
import { getTranslocoModule } from '../transloco-testing.module';
6+
7+
import { OnPushComponent } from './on-push.component';
8+
9+
describe('OnPushComponent', () => {
10+
let spectator: Spectator<OnPushComponent>;
11+
const createComponent = createComponentFactory({
12+
component: OnPushComponent,
13+
imports: [
14+
getTranslocoModule({
15+
translocoConfig: { reRenderOnLangChange: true },
16+
}),
17+
],
18+
});
19+
20+
beforeEach(() => (spectator = createComponent()));
21+
22+
it('should translate', () => {
23+
expect(spectator.query('.structural [data-cy=regular]')).toHaveText(
24+
'Regular: home spanish'
25+
);
26+
expect(spectator.query('.structural [data-cy=current-lang]')).toHaveText(
27+
'Current Lang: es'
28+
);
29+
expect(spectator.query('.pipe [data-cy=p-regular]')).toHaveText(
30+
'Regular: home spanish'
31+
);
32+
expect(spectator.query('.pipe [data-cy=p-regular]')).toHaveText(
33+
'Regular: home spanish'
34+
);
35+
const service = spectator.inject(TranslocoService);
36+
service.setActiveLang('en');
37+
spectator.detectChanges();
38+
expect(spectator.query('.structural [data-cy=regular]')).toHaveText(
39+
'Regular: home english'
40+
);
41+
expect(spectator.query('.structural [data-cy=current-lang]')).toHaveText(
42+
'Current Lang: en'
43+
);
44+
expect(spectator.query('.pipe [data-cy=p-regular]')).toHaveText(
45+
'Regular: home english'
46+
);
47+
});
48+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
TranslocoTestingModule,
3+
TranslocoTestingOptions,
4+
} from '@ngneat/transloco';
5+
6+
import en from '../assets/i18n/en.json';
7+
import es from '../assets/i18n/es.json';
8+
import admin from '../assets/i18n/admin-page/en.json';
9+
import adminSpanish from '../assets/i18n/admin-page/es.json';
10+
import lazy from '../assets/i18n/lazy-page/en.json';
11+
import lazySpanish from '../assets/i18n/lazy-page/es.json';
12+
13+
export function getTranslocoModule(options: TranslocoTestingOptions = {}) {
14+
const { langs, translocoConfig, ...rest } = options;
15+
return TranslocoTestingModule.forRoot({
16+
langs: {
17+
en,
18+
es,
19+
'admin-page/en': admin,
20+
'admin-page/es': adminSpanish,
21+
'lazy-page/en': lazy,
22+
'lazy-page/es': lazySpanish,
23+
...langs,
24+
},
25+
translocoConfig: {
26+
availableLangs: ['es', 'en'],
27+
defaultLang: 'es',
28+
...translocoConfig,
29+
},
30+
...rest,
31+
});
32+
}

docs/docs/unit-testing.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ describe('AppComponent', () => {
4545
});
4646
```
4747

48+
You can find an example [here](https://github.com/ngneat/transloco/blob/master/apps/transloco-playground/src/app/on-push/on-push.component.spec.ts).
49+
4850
If you need to test `scopes`, you should add them as `languages`, for example:
4951

5052
```ts {6,7} title="transloco-testing.module.ts"
@@ -66,6 +68,8 @@ export function getTranslocoModule(options: TranslocoTestingOptions = {}) {
6668
}
6769
```
6870

71+
You can find an example [here](https://github.com/ngneat/transloco/blob/master/apps/transloco-playground/src/app/lazy/lazy.component.spec.ts).
72+
6973
Note that in order to import JSON files, you need to configure the TypeScript compiler by adding the following properties in `tsconfig.json`:
7074

7175
```json

0 commit comments

Comments
 (0)