-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhttp-client-ns-backend.spec.ts
More file actions
95 lines (86 loc) · 2.94 KB
/
http-client-ns-backend.spec.ts
File metadata and controls
95 lines (86 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// make sure you import mocha-config before @angular/core
import { NSFileSystem } from '@nativescript/angular';
import { NsHttpBackEnd } from '@nativescript/angular';
import { EnvironmentInjector, createEnvironmentInjector, runInInjectionContext } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { XhrFactory } from '@angular/common';
class NSFileSystemMock implements NSFileSystem {
public currentApp(): any {
return { path: '/app/dir' };
}
public fileFromPath(path: string): any {
if (path === '/app/dir/data.json') {
return {
readText: () => {
return Promise.resolve(` { "result": "success" } `);
},
};
}
throw new Error('Opening non-existing file');
}
public fileExists(path: string): boolean {
return path === '/app/dir/data.json';
}
}
class XhrFactoryMock implements XhrFactory {
build(): XMLHttpRequest {
throw new Error('Hi, from XhrFactoryMock!');
}
}
describe('NsHttpBackEnd ', () => {
let backend: NsHttpBackEnd;
beforeEach(() => {
TestBed.configureTestingModule({});
const parentInjector = TestBed.inject(EnvironmentInjector);
const injector = createEnvironmentInjector([], parentInjector);
backend = runInInjectionContext(injector, () => new NsHttpBackEnd(new XhrFactoryMock(), new NSFileSystemMock()));
});
it("should work with local files prefixed with '~'", (done) => {
const req = new HttpRequest('GET', '~/data.json');
let nextCalled = false;
backend.handle(req).subscribe(
(response: HttpResponse<{ result: string }>) => {
expect(response.body.result).toEqual('success');
nextCalled = true;
},
(error) => {
fail(error);
},
() => {
expect(nextCalled).withContext('next callback should be called with result.').toBe(true);
done();
}
);
});
it("should return 404 for non-existing local files prefixed with '~'", (done) => {
const req = new HttpRequest('GET', '~/non/existing/file.json');
backend.handle(req).subscribe(
(response) => {
fail('next callback should not be called for non existing file.');
},
(error: HttpErrorResponse) => {
expect(error.status).toBe(404);
done();
},
() => {
fail('next callback should not be called for non existing file.');
}
);
});
it('should fallback to XHR backend when requesting remote files', (done) => {
const req = new HttpRequest('GET', 'https://nativescript.org/');
backend.handle(req).subscribe(
(response) => {
fail('next callback should not be called for non existing file.');
},
(error: Error) => {
expect(error.message).toEqual('Hi, from XhrFactoryMock!');
done();
},
() => {
fail('next callback should not be called for non existing file.');
}
);
});
});