-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.spec.ts
77 lines (57 loc) · 2.26 KB
/
device.spec.ts
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
import {TestBed, inject, fakeAsync, tick} from "@angular/core/testing";
import {MockBackend, MockConnection} from "@angular/http/testing";
import {BaseRequestOptions, Http, HttpModule, ResponseOptions, Response, RequestMethod} from "@angular/http";
import {TESTHEADERS, TESTURL, TESTCONFIG} from "./helpers";
import {SelfbitsDevice} from "../src/services/device";
import {SELFBITS_CONFIG} from "../src/utils/tokens";
declare var window:any;
describe('device.ts',()=> {
beforeEach(()=> {
TestBed.configureTestingModule({
providers: [
MockBackend,
BaseRequestOptions,
{
provide: Http, useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions]
},
{ provide: SELFBITS_CONFIG, useValue:TESTCONFIG},
SelfbitsDevice
],
imports: [
HttpModule
]
});
window.cordova=true;
window.device=true;
});
it('should load with config data injected',inject([SelfbitsDevice],(device:any)=>{
expect(device.config).toEqual(TESTCONFIG);
expect(device.headers).toEqual(TESTHEADERS);
expect(device.baseUrl).toEqual(TESTURL);
}));
it('checkForToken() should append Authorization header ', inject([SelfbitsDevice],(device:any)=>{
window.localStorage.setItem('token','httpTestToken');
expect(device.headers.has('Authorization')).toBeFalsy();
device.checkForToken();
expect(device.headers.get('Authorization')).toEqual('Bearer httpTestToken');
window.localStorage.removeItem('token');
}));
it('sync() should transmite user device data', inject([SelfbitsDevice, MockBackend], fakeAsync((device:SelfbitsDevice, backend:MockBackend)=>{
let checkTokenSpy = spyOn(device, 'checkForToken');
backend.connections.subscribe((connection:MockConnection)=>{
expect(connection.request.method).toEqual(RequestMethod.Post);
expect(connection.request.url).toEqual(`${TESTURL}/api/v1/user/device`);
let response = new ResponseOptions({body:JSON.stringify({data:'deviceTestData'})});
connection.mockRespond(new Response(response));
});
let response:any;
device.sync().subscribe(res => {
response = res.json();
});
tick();
expect(response['data']).toEqual('deviceTestData');
expect(checkTokenSpy).toHaveBeenCalled();
})));
});