11import { Injector } from "@angular/core" ;
22import { getTestBed , TestBed } from "@angular/core/testing" ;
3- import {
4- BaseRequestOptions ,
5- ConnectionBackend ,
6- Http ,
7- HttpModule ,
8- RequestOptions ,
9- Response ,
10- ResponseOptions , XHRBackend
11- } from "@angular/http" ;
12- import { MockBackend , MockConnection } from "@angular/http/testing" ;
3+ import { HttpClient } from "@angular/common/http" ;
4+ import { HttpClientTestingModule , HttpTestingController } from "@angular/common/http/testing" ;
135import { TranslateLoader , TranslateModule , TranslateService } from "@ngx-translate/core" ;
146import { TranslateHttpLoader } from "../index" ;
157
16- const mockBackendResponse = ( connection : MockConnection , response : string ) => {
17- connection . mockRespond ( new Response ( new ResponseOptions ( { body : response } ) ) ) ;
18- } ;
19-
208describe ( 'TranslateLoader' , ( ) => {
219 let injector : Injector ;
2210 let translate : TranslateService ;
23- let backend : any ;
24- let connection : MockConnection ; // this will be set when a new connection is emitted from the backend.
11+ let http : HttpTestingController ;
2512
2613 beforeEach ( ( ) => {
2714 TestBed . configureTestingModule ( {
2815 imports : [
29- HttpModule ,
16+ HttpClientTestingModule ,
3017 TranslateModule . forRoot ( {
3118 loader : {
3219 provide : TranslateLoader ,
33- useFactory : ( http : Http ) => new TranslateHttpLoader ( http ) ,
34- deps : [ Http ]
20+ useFactory : ( http : HttpClient ) => new TranslateHttpLoader ( http ) ,
21+ deps : [ HttpClient ]
3522 }
3623 } )
3724 ] ,
38- providers : [
39- { provide : XHRBackend , useClass : MockBackend } ,
40- { provide : ConnectionBackend , useClass : MockBackend } ,
41- { provide : RequestOptions , useClass : BaseRequestOptions }
42- ]
25+ providers : [ TranslateService ]
4326 } ) ;
4427 injector = getTestBed ( ) ;
45- translate = injector . get ( TranslateService ) ;
46- backend = injector . get ( XHRBackend ) ;
47- // sets the connection when someone tries to access the backend with an xhr request
48- backend . connections . subscribe ( ( c : MockConnection ) => connection = c ) ;
28+ translate = TestBed . get ( TranslateService ) ;
29+ http = TestBed . get ( HttpTestingController ) ;
4930 } ) ;
5031
5132 afterEach ( ( ) => {
5233 injector = undefined ;
5334 translate = undefined ;
54- backend = undefined ;
55- connection = undefined ;
35+ http = undefined ;
5636 } ) ;
5737
5838 it ( 'should be able to provide TranslateHttpLoader' , ( ) => {
@@ -70,7 +50,7 @@ describe('TranslateLoader', () => {
7050 } ) ;
7151
7252 // mock response after the xhr request, otherwise it will be undefined
73- mockBackendResponse ( connection , ' {"TEST": "This is a test", "TEST2": "This is another test"}' ) ;
53+ http . expectOne ( '/assets/i18n/en.json' ) . flush ( { "TEST" : "This is a test" , "TEST2" : "This is another test" } ) ;
7454
7555 // this will request the translation from downloaded translations without making a request to the backend
7656 translate . get ( 'TEST2' ) . subscribe ( ( res : string ) => {
@@ -90,21 +70,21 @@ describe('TranslateLoader', () => {
9070 expect ( translate . instant ( 'TEST' ) ) . toEqual ( 'This is a test 2' ) ;
9171 } ) ;
9272
93- mockBackendResponse ( connection , ' {"TEST": "This is a test 2"}' ) ;
73+ http . expectOne ( '/assets/i18n/en.json' ) . flush ( { "TEST" : "This is a test 2" } ) ;
9474 } ) ;
9575
9676 // mock response after the xhr request, otherwise it will be undefined
97- mockBackendResponse ( connection , ' {"TEST": "This is a test"}' ) ;
77+ http . expectOne ( '/assets/i18n/en.json' ) . flush ( { "TEST" : "This is a test" } ) ;
9878 } ) ;
9979
10080 it ( 'should be able to reset a lang' , ( done : Function ) => {
10181 translate . use ( 'en' ) ;
102- spyOn ( connection , 'mockRespond ' ) . and . callThrough ( ) ;
82+ spyOn ( http , 'expectOne ' ) . and . callThrough ( ) ;
10383
10484 // this will request the translation from the backend because we use a static files loader for TranslateService
10585 translate . get ( 'TEST' ) . subscribe ( ( res : string ) => {
10686 expect ( res ) . toEqual ( 'This is a test' ) ;
107- expect ( connection . mockRespond ) . toHaveBeenCalledTimes ( 1 ) ;
87+ expect ( http . expectOne ) . toHaveBeenCalledTimes ( 1 ) ;
10888
10989 // reset the lang as if it was never initiated
11090 translate . resetLang ( 'en' ) ;
@@ -115,13 +95,13 @@ describe('TranslateLoader', () => {
11595 setTimeout ( ( ) => {
11696 translate . get ( 'TEST' ) . subscribe ( ( res2 : string ) => {
11797 expect ( res2 ) . toEqual ( 'TEST' ) ; // because the loader is "pristine" as if it was never called
118- expect ( connection . mockRespond ) . toHaveBeenCalledTimes ( 1 ) ;
98+ expect ( http . expectOne ) . toHaveBeenCalledTimes ( 1 ) ;
11999 done ( ) ;
120100 } ) ;
121101 } , 10 ) ;
122102 } ) ;
123103
124104 // mock response after the xhr request, otherwise it will be undefined
125- mockBackendResponse ( connection , ' {"TEST": "This is a test"}' ) ;
105+ http . expectOne ( '/assets/i18n/en.json' ) . flush ( { "TEST" : "This is a test" } ) ;
126106 } ) ;
127107} ) ;
0 commit comments