Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit 65001ab

Browse files
authored
fix(TranslateHttpLoader): compatibility with Angular 4.3
BREAKING CHANGE: the loader is now only compatible with Angular 4.3+
2 parents d902488 + 8c4445d commit 65001ab

File tree

3 files changed

+31
-52
lines changed

3 files changed

+31
-52
lines changed

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@
3131
"typings": "index.d.ts",
3232
"peerDependencies": {
3333
"@ngx-translate/core": ">=6.0.0",
34-
"@angular/core": ">=2.0.0",
35-
"@angular/http": ">=2.0.0"
34+
"@angular/core": ">=4.3.1",
35+
"@angular/http": ">=4.3.1"
3636
},
3737
"devDependencies": {
38-
"@angular/animations": "4.1.3",
39-
"@angular/common": "4.1.3",
40-
"@angular/compiler": "4.1.3",
41-
"@angular/compiler-cli": "4.1.3",
42-
"@angular/core": "4.1.3",
43-
"@angular/http": "4.1.3",
38+
"@angular/animations": "4.3.1",
39+
"@angular/common": "4.3.1",
40+
"@angular/compiler": "4.3.1",
41+
"@angular/compiler-cli": "4.3.1",
42+
"@angular/core": "4.3.1",
43+
"@angular/http": "4.3.1",
4444
"@ngx-translate/core": "7.0.0",
45-
"@angular/platform-browser": "4.1.3",
46-
"@angular/platform-browser-dynamic": "4.1.3",
47-
"@angular/platform-server": "4.1.3",
45+
"@angular/platform-browser": "4.3.1",
46+
"@angular/platform-browser-dynamic": "4.3.1",
47+
"@angular/platform-server": "4.3.1",
4848
"@types/hammerjs": "2.0.34",
4949
"@types/jasmine": "2.5.51",
5050
"@types/node": "7.0.28",

src/http-loader.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import {Http, Response} from "@angular/http";
1+
import {HttpClient} from "@angular/common/http";
22
import {TranslateLoader} from "@ngx-translate/core";
33
import "rxjs/add/operator/map";
44

55
export class TranslateHttpLoader implements TranslateLoader {
6-
constructor(private http: Http, private prefix: string = "/assets/i18n/", private suffix: string = ".json") {}
6+
constructor(private http: HttpClient, private prefix: string = "/assets/i18n/", private suffix: string = ".json") {}
77

88
/**
99
* Gets the translations from the server
1010
* @param lang
1111
* @returns {any}
1212
*/
1313
public getTranslation(lang: string): any {
14-
return this.http.get(`${this.prefix}${lang}${this.suffix}`)
15-
.map((res: Response) => res.json());
14+
return this.http.get(`${this.prefix}${lang}${this.suffix}`);
1615
}
1716
}

tests/http-loader.spec.ts

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,38 @@
11
import {Injector} from "@angular/core";
22
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";
135
import {TranslateLoader, TranslateModule, TranslateService} from "@ngx-translate/core";
146
import {TranslateHttpLoader} from "../index";
157

16-
const mockBackendResponse = (connection: MockConnection, response: string) => {
17-
connection.mockRespond(new Response(new ResponseOptions({body: response})));
18-
};
19-
208
describe('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

Comments
 (0)