-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: ✏️ bring back unit test examples
- Loading branch information
1 parent
42e8af0
commit 9914e9f
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
apps/transloco-playground/src/app/lazy/lazy.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { waitForAsync, TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { TRANSLOCO_SCOPE } from '@ngneat/transloco'; | ||
|
||
import { getTranslocoModule } from '../transloco-testing.module'; | ||
|
||
import { LazyComponent } from './lazy.component'; | ||
|
||
describe('LazyComponent', () => { | ||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [{ provide: TRANSLOCO_SCOPE, useValue: 'admin-page' }], | ||
imports: [getTranslocoModule()], | ||
declarations: [LazyComponent], | ||
}).compileComponents(); | ||
})); | ||
|
||
it('should get scoped title translation', function () { | ||
const fixture = TestBed.createComponent(LazyComponent); | ||
fixture.detectChanges(); | ||
expect( | ||
fixture.debugElement.query(By.css('.admin-title')).nativeElement.innerText | ||
).toBe('Admin spanish'); | ||
}); | ||
|
||
it('should get scoped translation with read', function () { | ||
const fixture = TestBed.createComponent(LazyComponent); | ||
fixture.detectChanges(); | ||
expect( | ||
fixture.debugElement.query(By.css('.admin-read')).nativeElement.innerText | ||
).toBe('Admin read spanish'); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
apps/transloco-playground/src/app/on-push/on-push.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { createComponentFactory, Spectator } from '@ngneat/spectator'; | ||
|
||
import { TranslocoService } from '@ngneat/transloco'; | ||
|
||
import { getTranslocoModule } from '../transloco-testing.module'; | ||
|
||
import { OnPushComponent } from './on-push.component'; | ||
|
||
describe('OnPushComponent', () => { | ||
let spectator: Spectator<OnPushComponent>; | ||
const createComponent = createComponentFactory({ | ||
component: OnPushComponent, | ||
imports: [ | ||
getTranslocoModule({ | ||
translocoConfig: { reRenderOnLangChange: true }, | ||
}), | ||
], | ||
}); | ||
|
||
beforeEach(() => (spectator = createComponent())); | ||
|
||
it('should translate', () => { | ||
expect(spectator.query('.structural [data-cy=regular]')).toHaveText( | ||
'Regular: home spanish' | ||
); | ||
expect(spectator.query('.structural [data-cy=current-lang]')).toHaveText( | ||
'Current Lang: es' | ||
); | ||
expect(spectator.query('.pipe [data-cy=p-regular]')).toHaveText( | ||
'Regular: home spanish' | ||
); | ||
expect(spectator.query('.pipe [data-cy=p-regular]')).toHaveText( | ||
'Regular: home spanish' | ||
); | ||
const service = spectator.inject(TranslocoService); | ||
service.setActiveLang('en'); | ||
spectator.detectChanges(); | ||
expect(spectator.query('.structural [data-cy=regular]')).toHaveText( | ||
'Regular: home english' | ||
); | ||
expect(spectator.query('.structural [data-cy=current-lang]')).toHaveText( | ||
'Current Lang: en' | ||
); | ||
expect(spectator.query('.pipe [data-cy=p-regular]')).toHaveText( | ||
'Regular: home english' | ||
); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
apps/transloco-playground/src/app/transloco-testing.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
TranslocoTestingModule, | ||
TranslocoTestingOptions, | ||
} from '@ngneat/transloco'; | ||
|
||
import en from '../assets/i18n/en.json'; | ||
import es from '../assets/i18n/es.json'; | ||
import admin from '../assets/i18n/admin-page/en.json'; | ||
import adminSpanish from '../assets/i18n/admin-page/es.json'; | ||
import lazy from '../assets/i18n/lazy-page/en.json'; | ||
import lazySpanish from '../assets/i18n/lazy-page/es.json'; | ||
|
||
export function getTranslocoModule(options: TranslocoTestingOptions = {}) { | ||
const { langs, translocoConfig, ...rest } = options; | ||
return TranslocoTestingModule.forRoot({ | ||
langs: { | ||
en, | ||
es, | ||
'admin-page/en': admin, | ||
'admin-page/es': adminSpanish, | ||
'lazy-page/en': lazy, | ||
'lazy-page/es': lazySpanish, | ||
...langs, | ||
}, | ||
translocoConfig: { | ||
availableLangs: ['es', 'en'], | ||
defaultLang: 'es', | ||
...translocoConfig, | ||
}, | ||
...rest, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters