Skip to content

Commit d53e398

Browse files
committed
test(http-loader): add tests for multipleHttpLoader
1 parent 9bd0951 commit d53e398

File tree

2 files changed

+173
-1
lines changed

2 files changed

+173
-1
lines changed

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

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,28 @@ describe("TranslateHttpLoader (HttpClient)", () => {
7474
expect(true).toBeTruthy(); // http.expectOne() is not detected by jasmine...
7575
});
7676

77+
it("uses multipleHttpLoader prefix", () => {
78+
prepareMulti({ resources: ["XXXX/", "YYYY/"] });
79+
translate.use("en");
80+
http.expectOne("XXXX/en.json").flush({});
81+
http.expectOne("YYYY/en.json").flush({});
82+
expect(true).toBeTruthy(); // http.expectOne() is not detected by jasmine...
83+
});
84+
7785
it("uses suffix", () => {
7886
prepareSingle({ suffix: ".XXXX" });
7987
translate.use("en");
8088
http.expectOne("/assets/i18n/en.XXXX").flush({});
8189
expect(true).toBeTruthy(); // http.expectOne() is not detected by jasmine...
8290
});
8391

92+
it("uses multipleHttpLoader suffix", () => {
93+
prepareMulti({ resources: [{ prefix: "/assets/i18n/", suffix: ".XXXX" }] });
94+
translate.use("en");
95+
http.expectOne("/assets/i18n/en.XXXX").flush({});
96+
expect(true).toBeTruthy(); // http.expectOne() is not detected by jasmine...
97+
});
98+
8499
it("uses cache buster", () => {
85100
prepareSingle({ enforceLoading: true });
86101
translate.use("en");
@@ -113,6 +128,28 @@ describe("TranslateHttpLoader (HttpClient)", () => {
113128
});
114129
});
115130

131+
it("should be able to get translations from multipleHttpLoader", () => {
132+
prepareMulti();
133+
134+
translate.use("en");
135+
136+
// this will request the translation from the backend because we use a static files loader for TranslateService
137+
translate.get("TEST").subscribe((res: Translation) => {
138+
expect(res as string).toEqual("This is a test");
139+
});
140+
141+
// mock response after the xhr request, otherwise it will be undefined
142+
http.expectOne("/assets/i18n/en.json").flush({
143+
TEST: "This is a test",
144+
TEST2: "This is another test",
145+
});
146+
147+
// this will request the translation from downloaded translations without making a request to the backend
148+
translate.get("TEST2").subscribe((res: Translation) => {
149+
expect(res as string).toEqual("This is another test");
150+
});
151+
});
152+
116153
it("should trigger MarkerInterceptor when loading translations", () => {
117154
prepareSingle();
118155

@@ -124,6 +161,17 @@ describe("TranslateHttpLoader (HttpClient)", () => {
124161
expect(req.request.headers.get("X-Test-Header")).toBe("marker");
125162
});
126163

164+
it("should trigger MarkerInterceptor when loading translations with multipleHttpLoader", () => {
165+
prepareMulti();
166+
167+
translate.use("en").subscribe();
168+
169+
const req = http.expectOne("/assets/i18n/en.json");
170+
req.flush({ HELLO: "Hello" });
171+
172+
expect(req.request.headers.get("X-Test-Header")).toBe("marker");
173+
});
174+
127175
it("should be able to reload a lang", () => {
128176
prepareSingle();
129177

@@ -145,6 +193,27 @@ describe("TranslateHttpLoader (HttpClient)", () => {
145193
http.expectOne("/assets/i18n/en.json").flush({ TEST: "This is a test" });
146194
});
147195

196+
it("should be able to reload a lang with multipleHttpLoader", () => {
197+
prepareMulti();
198+
199+
translate.use("en");
200+
201+
// this will request the translation from the backend because we use a static files loader for TranslateService
202+
translate.get("TEST").subscribe((res: Translation) => {
203+
expect(res as string).toEqual("This is a test");
204+
205+
// reset the lang as if it was never initiated
206+
translate.reloadLang("en").subscribe(() => {
207+
expect(translate.instant("TEST") as string).toEqual("This is a test 2");
208+
});
209+
210+
http.expectOne("/assets/i18n/en.json").flush({ TEST: "This is a test 2" });
211+
});
212+
213+
// mock response after the xhr request, otherwise it will be undefined
214+
http.expectOne("/assets/i18n/en.json").flush({ TEST: "This is a test" });
215+
});
216+
148217
it("should be able to reset a lang", (done: DoneFn) => {
149218
prepareSingle();
150219

@@ -174,4 +243,104 @@ describe("TranslateHttpLoader (HttpClient)", () => {
174243
// mock response after the xhr request, otherwise it will be undefined
175244
http.expectOne("/assets/i18n/en.json").flush({ TEST: "This is a test" });
176245
});
246+
247+
it("should be able to reset a lang from MultiHttpLoader", (done: DoneFn) => {
248+
prepareMulti();
249+
250+
translate.use("en");
251+
spyOn(http, "expectOne").and.callThrough();
252+
253+
// this will request the translation from the backend because we use a static files loader for TranslateService
254+
translate.get("TEST").subscribe((res: Translation) => {
255+
expect(res as string).toEqual("This is a test");
256+
expect(http.expectOne).toHaveBeenCalledTimes(1);
257+
258+
// reset the lang as if it was never initiated
259+
translate.resetLang("en");
260+
261+
expect(translate.instant("TEST") as string).toEqual("TEST");
262+
263+
// use set timeout because no request is really made and we need to trigger zone to resolve the observable
264+
setTimeout(() => {
265+
translate.get("TEST").subscribe((res2: Translation) => {
266+
expect(res2 as string).toEqual("TEST"); // because the loader is "pristine" as if it was never called
267+
expect(http.expectOne).toHaveBeenCalledTimes(1);
268+
done();
269+
});
270+
}, 10);
271+
});
272+
273+
// mock response after the xhr request, otherwise it will be undefined
274+
http.expectOne("/assets/i18n/en.json").flush({ TEST: "This is a test" });
275+
});
276+
277+
it("should merge translations from multiple resources", () => {
278+
prepareMulti({
279+
resources: ["/assets/i18n/", { prefix: "/custom/", suffix: ".lang.json" }],
280+
});
281+
translate.use("en").subscribe();
282+
http.expectOne("/assets/i18n/en.json").flush({ TEST: "A", ONLY1: "X" });
283+
http.expectOne("/custom/en.lang.json").flush({ TEST: "B", ONLY2: "Y" });
284+
// TEST should be overwritten by the second resource
285+
expect(translate.instant("TEST")).toBe("B");
286+
expect(translate.instant("ONLY1")).toBe("X");
287+
expect(translate.instant("ONLY2")).toBe("Y");
288+
});
289+
290+
it("should handle error in one resource and still merge others", () => {
291+
prepareMulti({
292+
resources: ["/assets/i18n/", { prefix: "/custom/", suffix: ".lang.json" }],
293+
});
294+
translate.use("en").subscribe();
295+
http.expectOne("/assets/i18n/en.json").flush({ TEST: "A" });
296+
http.expectOne("/custom/en.lang.json").flush(null, {
297+
status: 500,
298+
statusText: "Server Error",
299+
});
300+
expect(translate.instant("TEST")).toBe("A");
301+
});
302+
303+
it("should log error if showLog is true", () => {
304+
prepareMulti({
305+
resources: [
306+
"/assets/i18n/",
307+
{ prefix: "/custom/", suffix: ".lang.json", showLog: true },
308+
],
309+
showLog: true,
310+
});
311+
spyOn(console, "error");
312+
translate.use("en").subscribe();
313+
http.expectOne("/assets/i18n/en.json").flush({ TEST: "A" });
314+
http.expectOne("/custom/en.lang.json").flush(null, {
315+
status: 500,
316+
statusText: "Server Error",
317+
});
318+
expect(console.error).toHaveBeenCalled();
319+
});
320+
321+
it("should fallback to empty object if all resources fail", () => {
322+
prepareMulti({
323+
resources: ["/assets/i18n/", { prefix: "/custom/", suffix: ".lang.json" }],
324+
});
325+
translate.use("en").subscribe();
326+
http.expectOne("/assets/i18n/en.json").flush(null, {
327+
status: 500,
328+
statusText: "Server Error",
329+
});
330+
http.expectOne("/custom/en.lang.json").flush(null, {
331+
status: 500,
332+
statusText: "Server Error",
333+
});
334+
expect(translate.instant("ANY")).toBe("ANY");
335+
});
336+
337+
it("should support resource as string and object with suffix", () => {
338+
prepareMulti({
339+
resources: ["/assets/i18n/", { prefix: "/custom/", suffix: ".lang.json" }],
340+
});
341+
translate.use("en").subscribe();
342+
http.expectOne("/assets/i18n/en.json").flush({ TEST: "A" });
343+
http.expectOne("/custom/en.lang.json").flush({ TEST: "B" });
344+
expect(translate.instant("TEST")).toBe("B");
345+
});
177346
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ export function provideTranslateMultiHttpLoader(
108108
return [
109109
{
110110
provide: TRANSLATE_HTTP_LOADER_CONFIG,
111-
useValue: config,
111+
useValue: {
112+
resources: ["/assets/i18n/"],
113+
...config,
114+
},
112115
},
113116
{
114117
provide: TranslateLoader,

0 commit comments

Comments
 (0)