Skip to content

Commit 8984e88

Browse files
author
vakrilov
committed
test(http): Add tests for NsHttpBackEnd
1 parent 73aff9d commit 8984e88

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

nativescript-angular/http-client/http-client.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ global.__extends = cachedExtends;
99
import { NSFileSystem } from "../file-system/ns-file-system";
1010
import { NsHttpBackEnd } from "./ns-http-backend";
1111

12+
export { NsHttpBackEnd } from "./ns-http-backend";
13+
1214
@NgModule({
1315
providers: [
1416
NSFileSystem,
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// make sure you import mocha-config before @angular/core
2+
import { assert } from "./test-config";
3+
import { NSFileSystem } from "nativescript-angular/file-system/ns-file-system";
4+
import { NsHttpBackEnd } from "nativescript-angular/http-client";
5+
6+
import { XhrFactory, HttpRequest, HttpResponse, HttpErrorResponse } from "@angular/common/http";
7+
import { File } from "tns-core-modules/file-system";
8+
9+
class NSFileSystemMock implements NSFileSystem {
10+
public currentApp(): any {
11+
return { path: "/app/dir" };
12+
}
13+
14+
public fileFromPath(path: string): any {
15+
if (path === "/app/dir/data.json") {
16+
return {
17+
readText: () => { return Promise.resolve(` { "result": "success" } `); }
18+
};
19+
}
20+
throw new Error("Opening non-existing file");
21+
}
22+
23+
public fileExists(path: string): boolean {
24+
return path === "/app/dir/data.json";
25+
}
26+
}
27+
class XhrFactoryMock implements XhrFactory {
28+
build(): XMLHttpRequest {
29+
throw new Error("Hi, from XhrFactoryMock!");
30+
}
31+
}
32+
33+
describe("NsHttpBackEnd ", () => {
34+
let backend: NsHttpBackEnd;
35+
36+
before(() => {
37+
backend = new NsHttpBackEnd(new XhrFactoryMock(), new NSFileSystemMock());
38+
});
39+
40+
it("should work with local files prefixed with '~'", (done) => {
41+
const req = new HttpRequest("GET", "~/data.json");
42+
let nextCalled = false;
43+
backend.handle(req).subscribe(
44+
(response: HttpResponse<{ result: string }>) => {
45+
assert.equal(response.body.result, "success");
46+
nextCalled = true;
47+
}, (error) => {
48+
done(error);
49+
}, () => {
50+
assert.isTrue(nextCalled, "next callback should be called with result.");
51+
done();
52+
});
53+
});
54+
55+
it("should return 404 for non-existing local files prefixed with '~'", (done) => {
56+
const req = new HttpRequest("GET", "~/non/existing/file.json");
57+
backend.handle(req).subscribe(
58+
(response) => {
59+
assert.fail("next callback should not be called for non existing file.");
60+
}, (error: HttpErrorResponse) => {
61+
assert.equal(error.status, 404);
62+
done();
63+
}, () => {
64+
assert.fail("next callback should not be called for non existing file.");
65+
});
66+
});
67+
68+
it("should fallback to XHR backend when requesting remote files", (done) => {
69+
const req = new HttpRequest("GET", "https://nativescript.org/");
70+
backend.handle(req).subscribe(
71+
(response) => {
72+
assert.fail("next callback should not be called for non existing file.");
73+
}, (error: Error) => {
74+
assert.equal(error.message, "Hi, from XhrFactoryMock!");
75+
done();
76+
}, () => {
77+
assert.fail("next callback should not be called for non existing file.");
78+
});
79+
});
80+
});

0 commit comments

Comments
 (0)