Skip to content

Commit 3b36c1c

Browse files
committed
feat: added option to run HttpLoader with HttpBackend instead of HttpClient
1 parent 4fc81c8 commit 3b36c1c

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import {HttpBackend, HttpClient, provideHttpClient} from "@angular/common/http";
2+
import {HttpTestingController, provideHttpClientTesting} from "@angular/common/http/testing";
3+
import {TestBed} from "@angular/core/testing";
4+
import {TranslateLoader, provideTranslateService, TranslateService, Translation} from "@ngx-translate/core";
5+
import {TranslateHttpLoader} from "../public-api";
6+
7+
describe('TranslateLoader (HttpBackend)', () => {
8+
let translate: TranslateService;
9+
let http: HttpTestingController;
10+
11+
beforeEach(() => {
12+
TestBed.configureTestingModule({
13+
providers: [
14+
TranslateService,
15+
provideHttpClient(),
16+
provideHttpClientTesting(),
17+
/*
18+
provideTranslateService({
19+
loader: {
20+
provide: TranslateLoader,
21+
useFactory: (httpClient: HttpClient) => new TranslateHttpLoader(httpClient),
22+
deps: [HttpClient]
23+
}
24+
}
25+
)
26+
*/
27+
provideTranslateService({
28+
loader: {
29+
provide: TranslateLoader,
30+
useFactory: (backend: HttpBackend) => TranslateHttpLoader.withHttpBackend(backend),
31+
deps: [HttpBackend]
32+
}
33+
}
34+
)
35+
36+
]
37+
});
38+
translate = TestBed.inject(TranslateService);
39+
http = TestBed.inject(HttpTestingController);
40+
});
41+
42+
it('should be able to provide TranslateHttpLoader', () => {
43+
expect(TranslateHttpLoader).toBeDefined();
44+
expect(translate.currentLoader).toBeDefined();
45+
expect(translate.currentLoader instanceof TranslateHttpLoader).toBeTruthy();
46+
});
47+
48+
it('should be able to get translations', () => {
49+
translate.use('en');
50+
51+
// this will request the translation from the backend because we use a static files loader for TranslateService
52+
translate.get('TEST').subscribe((res: Translation) => {
53+
expect(res as string).toEqual('This is a test');
54+
});
55+
56+
// mock response after the xhr request, otherwise it will be undefined
57+
http.expectOne('/assets/i18n/en.json').flush({
58+
"TEST": "This is a test",
59+
"TEST2": "This is another test"
60+
});
61+
62+
// this will request the translation from downloaded translations without making a request to the backend
63+
translate.get('TEST2').subscribe((res: Translation) => {
64+
expect(res as string).toEqual('This is another test');
65+
});
66+
});
67+
68+
it('should be able to reload a lang', () => {
69+
translate.use('en');
70+
71+
// this will request the translation from the backend because we use a static files loader for TranslateService
72+
translate.get('TEST').subscribe((res: Translation) => {
73+
expect(res as string).toEqual('This is a test');
74+
75+
// reset the lang as if it was never initiated
76+
translate.reloadLang('en').subscribe(() => {
77+
expect(translate.instant('TEST') as string).toEqual('This is a test 2');
78+
});
79+
80+
http.expectOne('/assets/i18n/en.json').flush({"TEST": "This is a test 2"});
81+
});
82+
83+
// mock response after the xhr request, otherwise it will be undefined
84+
http.expectOne('/assets/i18n/en.json').flush({"TEST": "This is a test"});
85+
});
86+
87+
it('should be able to reset a lang', (done: DoneFn) => {
88+
translate.use('en');
89+
spyOn(http, 'expectOne').and.callThrough();
90+
91+
// this will request the translation from the backend because we use a static files loader for TranslateService
92+
translate.get('TEST').subscribe((res: Translation) => {
93+
expect(res as string).toEqual('This is a test');
94+
expect(http.expectOne).toHaveBeenCalledTimes(1);
95+
96+
// reset the lang as if it was never initiated
97+
translate.resetLang('en');
98+
99+
expect(translate.instant('TEST') as string).toEqual('TEST');
100+
101+
// use set timeout because no request is really made and we need to trigger zone to resolve the observable
102+
setTimeout(() => {
103+
translate.get('TEST').subscribe((res2: Translation) => {
104+
expect(res2 as string).toEqual('TEST'); // because the loader is "pristine" as if it was never called
105+
expect(http.expectOne).toHaveBeenCalledTimes(1);
106+
done();
107+
});
108+
}, 10);
109+
});
110+
111+
// mock response after the xhr request, otherwise it will be undefined
112+
http.expectOne('/assets/i18n/en.json').flush({"TEST": "This is a test"});
113+
});
114+
});

projects/http-loader/src/lib/http-loader.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {TestBed} from "@angular/core/testing";
44
import {TranslateLoader, provideTranslateService, TranslateService, Translation} from "@ngx-translate/core";
55
import {TranslateHttpLoader} from "../public-api";
66

7-
describe('TranslateLoader', () => {
7+
describe('TranslateLoader (HttpClient)', () => {
88
let translate: TranslateService;
99
let http: HttpTestingController;
1010

projects/http-loader/src/lib/http-loader.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import {TranslateLoader, TranslationObject} from "@ngx-translate/core";
2-
import {HttpClient} from "@angular/common/http";
2+
import {HttpBackend, HttpClient} from "@angular/common/http";
33
import {Inject, Injectable} from "@angular/core";
44
import {Observable} from 'rxjs';
55

66
@Injectable()
77
export class TranslateHttpLoader implements TranslateLoader {
8+
89
constructor(
910
private http: HttpClient,
1011
@Inject(String) public prefix = "/assets/i18n/",
1112
@Inject(String) public suffix = ".json"
1213
)
1314
{}
1415

16+
public static withHttpBackend(backend: HttpBackend): TranslateHttpLoader {
17+
return new TranslateHttpLoader(new HttpClient(backend));
18+
}
19+
1520
/**
1621
* Gets the translations from the server
1722
*/

0 commit comments

Comments
 (0)