-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathapp.component.spec.ts
73 lines (60 loc) · 2.51 KB
/
app.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {HttpClient} from "@angular/common/http";
import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing";
import {async, TestBed} from '@angular/core/testing';
import {TranslateLoader, TranslateModule, TranslateService} from "@ngx-translate/core";
import {AppComponent} from './app.component';
import {HttpLoaderFactory} from "./app.module";
const TRANSLATIONS_EN = require('../assets/i18n/en.json');
const TRANSLATIONS_FR = require('../assets/i18n/fr.json');
describe('AppComponent', () => {
let translate: TranslateService;
let http: HttpTestingController;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [
HttpClientTestingModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
providers: [TranslateService]
}).compileComponents();
translate = TestBed.get(TranslateService);
http = TestBed.get(HttpTestingController);
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it('should load translations', async(() => {
spyOn(translate, 'getBrowserLang').and.returnValue('en');
const fixture = TestBed.createComponent(AppComponent);
const compiled = fixture.debugElement.nativeElement;
// the DOM should be empty for now since the translations haven't been rendered yet
expect(compiled.querySelector('h2').textContent).toEqual('');
http.expectOne('/assets/i18n/en.json').flush(TRANSLATIONS_EN);
http.expectNone('/assets/i18n/fr.json');
// Finally, assert that there are no outstanding requests.
http.verify();
fixture.detectChanges();
// the content should be translated to english now
expect(compiled.querySelector('h2').textContent).toEqual(TRANSLATIONS_EN.HOME.TITLE);
translate.use('fr');
http.expectOne('/assets/i18n/fr.json').flush(TRANSLATIONS_FR);
// Finally, assert that there are no outstanding requests.
http.verify();
// the content has not changed yet
expect(compiled.querySelector('h2').textContent).toEqual(TRANSLATIONS_EN.HOME.TITLE);
fixture.detectChanges();
// the content should be translated to french now
expect(compiled.querySelector('h2').textContent).toEqual(TRANSLATIONS_FR.HOME.TITLE);
}));
});