1+ import { Http , BaseRequestOptions , Response , ResponseOptions , Headers } from '@angular/http' ;
2+ import { MockBackend } from '@angular/http/testing' ;
3+ import { provide } from '@angular/core' ;
4+ import { inject , addProviders } from '@angular/core/testing' ;
5+
6+ import { Angular2TokenService } from './' ;
7+
8+ describe ( 'Angular2TokenService' , ( ) => {
9+
10+ // Init common test data
11+ let tokenType = 'Bearer' ;
12+ let uid = 'test@test.com' ;
13+ let accessToken = 'fJypB1ugmWHJfW6CELNfug' ;
14+ let client = '5dayGs4hWTi4eKwSifu_mg' ;
15+ let expiry = '1472108318' ;
16+
17+ let emptyHeaders = new Headers ( {
18+ 'content-Type' : 'application/json'
19+ } ) ;
20+ let tokenHeaders = new Headers ( {
21+ 'content-Type' : 'application/json' ,
22+ 'token-type' : tokenType ,
23+ 'uid' : uid ,
24+ 'access-token' : accessToken ,
25+ 'client' : client ,
26+ 'expiry' : expiry
27+ } ) ;
28+
29+ beforeEach ( ( ) => {
30+ // Inject HTTP and Angular2TokenService
31+ addProviders ( [
32+ BaseRequestOptions ,
33+ MockBackend ,
34+ provide ( Http , {
35+ useFactory :
36+ function ( backend , defaultOptions ) {
37+ return new Http ( backend , defaultOptions ) ;
38+ } ,
39+ deps : [ MockBackend , BaseRequestOptions ]
40+ } ) ,
41+ Angular2TokenService
42+ ] ) ;
43+
44+ // Fake Local Storage
45+ var store = { } ;
46+
47+ spyOn ( localStorage , 'getItem' ) . and . callFake ( ( key : string ) : String => {
48+ return store [ key ] || null ;
49+ } ) ;
50+ spyOn ( localStorage , 'removeItem' ) . and . callFake ( ( key : string ) : void => {
51+ delete store [ key ] ;
52+ } ) ;
53+ spyOn ( localStorage , 'setItem' ) . and . callFake ( ( key : string , value : string ) : string => {
54+ return store [ key ] = < string > value ;
55+ } ) ;
56+ spyOn ( localStorage , 'clear' ) . and . callFake ( ( ) => {
57+ store = { } ;
58+ } ) ;
59+ } ) ;
60+
61+ it ( 'logIn method should send data' , inject ( [ Angular2TokenService , MockBackend ] , ( tokenService , mockBackend ) => {
62+
63+ let logInData = {
64+ email : 'test@test.de' ,
65+ password : 'password'
66+ }
67+
68+ mockBackend . connections . subscribe (
69+ c => expect ( c . request . getBody ( ) ) . toEqual ( JSON . stringify ( logInData ) )
70+ ) ;
71+
72+ tokenService . init ( ) ;
73+ tokenService . logIn ( logInData . email , logInData . password ) ;
74+ } ) ) ;
75+
76+ it ( 'logIn method should receive headers and set local storage' , inject ( [ Angular2TokenService , MockBackend ] , ( tokenService , mockBackend ) => {
77+
78+ let logInData = {
79+ email : 'test@test.de' ,
80+ password : 'password'
81+ }
82+
83+ mockBackend . connections . subscribe (
84+ c => c . mockRespond ( new Response (
85+ new ResponseOptions ( {
86+ headers : tokenHeaders
87+ } )
88+ ) )
89+ ) ;
90+
91+ tokenService . init ( ) ;
92+ tokenService . logIn ( logInData . email , logInData . password ) ;
93+
94+ expect ( localStorage . getItem ( 'accessToken' ) ) . toEqual ( accessToken ) ;
95+ expect ( localStorage . getItem ( 'client' ) ) . toEqual ( client ) ;
96+ expect ( localStorage . getItem ( 'expiry' ) ) . toEqual ( expiry ) ;
97+ expect ( localStorage . getItem ( 'tokenType' ) ) . toEqual ( tokenType ) ;
98+ expect ( localStorage . getItem ( 'uid' ) ) . toEqual ( uid ) ;
99+ } ) ) ;
100+
101+ } ) ;
0 commit comments