Skip to content

Commit 58b6624

Browse files
committed
test(transloco-service): fix pr
1 parent 5224b0f commit 58b6624

File tree

3 files changed

+20
-31
lines changed

3 files changed

+20
-31
lines changed

projects/transloco/src/lib/tests/transloco.mocks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ export function load(lang: string): any {
3939
return timer(1000).pipe(map(() => langs[lang])) as any;
4040
}
4141

42-
export function runLoader() {
43-
tick(1001);
42+
export function runLoader(times = 1) {
43+
tick(times * 1001);
4444
}

projects/transloco/src/lib/tests/transloco.service.spec.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import createSpy = jasmine.createSpy;
55
import { fakeAsync } from '@angular/core/testing';
66
import en from '../../../../../src/assets/langs/en.json';
77
import { TRANSLOCO_MISSING_HANDLER, TranslocoMissingHandler } from '../transloco-missing-handler';
8-
import { of, timer } from 'rxjs';
8+
import { of, throwError, timer } from 'rxjs';
99
import { catchError, map } from 'rxjs/operators';
1010

1111
describe('TranslocoService', () => {
@@ -33,31 +33,27 @@ describe('TranslocoService', () => {
3333
})
3434
);
3535
};
36-
it('should retry loading if initial loading failed', fakeAsync(() => {
36+
37+
it('should return the default lang if the load fails', fakeAsync(() => {
38+
loadLang();
3739
(spectator.service as any).loader = createSpy().and.callFake(failLoad(1));
38-
spectator.service.load('en').subscribe();
39-
runLoader();
40-
expect((spectator.service as any).loader).toHaveBeenCalledTimes(2);
41-
/* We need to run the loader again since it's retrying */
42-
runLoader();
40+
spectator.service.load('es').subscribe();
41+
runLoader(2);
42+
/* Once since en is in cache */
43+
expect((spectator.service as any).loader).toHaveBeenCalledTimes(1);
4344
}));
4445

45-
it('should stop retrying to load when reaching 3 tries', fakeAsync(() => {
46+
it('should stop retrying to load the default lang when reaching 3 tries', fakeAsync(() => {
4647
const spy = createSpy().and.returnValue(of());
4748
(spectator.service as any).loader = createSpy().and.callFake(failLoad(5));
4849
const loader = (spectator.service as any).loader;
4950
spectator.service
5051
.load('en')
5152
.pipe(catchError(spy))
5253
.subscribe();
53-
runLoader();
54-
expect(loader).toHaveBeenCalledTimes(2);
55-
runLoader();
56-
expect(loader).toHaveBeenCalledTimes(3);
57-
runLoader();
58-
expect(loader).toHaveBeenCalledTimes(3);
59-
runLoader();
60-
expect(loader).toHaveBeenCalledTimes(3);
54+
/* 4 times - first try + 3 retries */
55+
runLoader(4);
56+
expect(loader).toHaveBeenCalledTimes(1);
6157
const expectedMsg = 'Unable to load the default translation file (en), reached maximum retries';
6258
const givenMsg = (spy.calls.argsFor(0)[0] as any).message;
6359
expect(givenMsg).toEqual(expectedMsg);

projects/transloco/src/lib/transloco.service.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Inject, Injectable } from '@angular/core';
2-
import { BehaviorSubject, from, Observable, Subject } from 'rxjs';
3-
import { catchError, distinctUntilChanged, map, shareReplay, tap } from 'rxjs/operators';
2+
import { BehaviorSubject, from, Observable, Subject, throwError } from 'rxjs';
3+
import { catchError, distinctUntilChanged, map, retry, shareReplay, tap } from 'rxjs/operators';
44
import { Lang, TRANSLOCO_LOADER, TranslocoLoader } from './transloco.loader';
55
import { TRANSLOCO_PARSER, TranslocoParser } from './transloco.parser';
66
import { HashMap } from './types';
@@ -23,8 +23,6 @@ export class TranslocoService {
2323
private translationLoaded = new Subject<{ lang: string }>();
2424
translationLoaded$ = this.translationLoaded.asObservable();
2525

26-
private loadRetries = 0;
27-
2826
constructor(
2927
@Inject(TRANSLOCO_LOADER) private loader: TranslocoLoader,
3028
@Inject(TRANSLOCO_PARSER) private parser: TranslocoParser,
@@ -64,22 +62,17 @@ export class TranslocoService {
6462
* @internal
6563
*/
6664
load(lang: string): Observable<Lang> {
67-
if (this.loadRetries > 2) {
68-
this.loadRetries = 0;
69-
throw new Error(`Unable to load the default translation file (${lang}), reached maximum retries`);
70-
}
71-
7265
if (this.cache.has(lang) === false) {
7366
const load$ = from(this.loader(lang)).pipe(
67+
retry(3),
7468
catchError(() => {
75-
if (this.cache.has(this.defaultLang)) {
76-
this.cache.delete(this.defaultLang);
69+
if (lang === this.defaultLang) {
70+
const errMsg = `Unable to load the default translation file (${lang}), reached maximum retries`;
71+
throw new Error(errMsg);
7772
}
78-
this.loadRetries++;
7973
return this.load(this.defaultLang);
8074
}),
8175
tap(value => {
82-
this.loadRetries = 0;
8376
this.langs.set(lang, value);
8477
this.translationLoaded.next({ lang });
8578
}),

0 commit comments

Comments
 (0)