1
1
import { Injector } from "@angular/core" ;
2
2
import { 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" ;
13
5
import { TranslateLoader , TranslateModule , TranslateService } from "@ngx-translate/core" ;
14
6
import { TranslateHttpLoader } from "../index" ;
15
7
16
- const mockBackendResponse = ( connection : MockConnection , response : string ) => {
17
- connection . mockRespond ( new Response ( new ResponseOptions ( { body : response } ) ) ) ;
18
- } ;
19
-
20
8
describe ( 'TranslateLoader' , ( ) => {
21
9
let injector : Injector ;
22
10
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 ;
25
12
26
13
beforeEach ( ( ) => {
27
14
TestBed . configureTestingModule ( {
28
15
imports : [
29
- HttpModule ,
16
+ HttpClientTestingModule ,
30
17
TranslateModule . forRoot ( {
31
18
loader : {
32
19
provide : TranslateLoader ,
33
- useFactory : ( http : Http ) => new TranslateHttpLoader ( http ) ,
34
- deps : [ Http ]
20
+ useFactory : ( http : HttpClient ) => new TranslateHttpLoader ( http ) ,
21
+ deps : [ HttpClient ]
35
22
}
36
23
} )
37
24
] ,
38
- providers : [
39
- { provide : XHRBackend , useClass : MockBackend } ,
40
- { provide : ConnectionBackend , useClass : MockBackend } ,
41
- { provide : RequestOptions , useClass : BaseRequestOptions }
42
- ]
25
+ providers : [ TranslateService ]
43
26
} ) ;
44
27
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 ) ;
49
30
} ) ;
50
31
51
32
afterEach ( ( ) => {
52
33
injector = undefined ;
53
34
translate = undefined ;
54
- backend = undefined ;
55
- connection = undefined ;
35
+ http = undefined ;
56
36
} ) ;
57
37
58
38
it ( 'should be able to provide TranslateHttpLoader' , ( ) => {
@@ -70,7 +50,7 @@ describe('TranslateLoader', () => {
70
50
} ) ;
71
51
72
52
// 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" } ) ;
74
54
75
55
// this will request the translation from downloaded translations without making a request to the backend
76
56
translate . get ( 'TEST2' ) . subscribe ( ( res : string ) => {
@@ -90,21 +70,21 @@ describe('TranslateLoader', () => {
90
70
expect ( translate . instant ( 'TEST' ) ) . toEqual ( 'This is a test 2' ) ;
91
71
} ) ;
92
72
93
- mockBackendResponse ( connection , ' {"TEST": "This is a test 2"}' ) ;
73
+ http . expectOne ( '/assets/i18n/en.json' ) . flush ( { "TEST" : "This is a test 2" } ) ;
94
74
} ) ;
95
75
96
76
// 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" } ) ;
98
78
} ) ;
99
79
100
80
it ( 'should be able to reset a lang' , ( done : Function ) => {
101
81
translate . use ( 'en' ) ;
102
- spyOn ( connection , 'mockRespond ' ) . and . callThrough ( ) ;
82
+ spyOn ( http , 'expectOne ' ) . and . callThrough ( ) ;
103
83
104
84
// this will request the translation from the backend because we use a static files loader for TranslateService
105
85
translate . get ( 'TEST' ) . subscribe ( ( res : string ) => {
106
86
expect ( res ) . toEqual ( 'This is a test' ) ;
107
- expect ( connection . mockRespond ) . toHaveBeenCalledTimes ( 1 ) ;
87
+ expect ( http . expectOne ) . toHaveBeenCalledTimes ( 1 ) ;
108
88
109
89
// reset the lang as if it was never initiated
110
90
translate . resetLang ( 'en' ) ;
@@ -115,13 +95,13 @@ describe('TranslateLoader', () => {
115
95
setTimeout ( ( ) => {
116
96
translate . get ( 'TEST' ) . subscribe ( ( res2 : string ) => {
117
97
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 ) ;
119
99
done ( ) ;
120
100
} ) ;
121
101
} , 10 ) ;
122
102
} ) ;
123
103
124
104
// 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" } ) ;
126
106
} ) ;
127
107
} ) ;
0 commit comments