|
| 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 | +}); |
0 commit comments