@@ -13,6 +13,7 @@ describe('ConstructorioID', function () {
1313 'generate_client_id' ,
1414 'get_local_object' ,
1515 'set_local_object' ,
16+ 'delete_local_object' ,
1617 'generate_session_id'
1718 ] ;
1819 expect ( actualKeys ) . to . eql ( expectedKeys ) ;
@@ -97,26 +98,38 @@ describe('ConstructorioID', function () {
9798 expect ( session . client_id ) . to . match ( / ( \w | d | - ) { 36 } / ) ;
9899 } ) ;
99100
100- it ( 'should read the client id from local storage if storage location is set to local' , function ( ) {
101- window . localStorage . setItem ( 'dummyname' , 'dummyid' ) ;
102- var session = new ConstructorioID ( { local_name_client_id : 'dummyname' , client_id_storage_location : 'local' } ) ;
103- expect ( session . client_id ) . to . equal ( 'dummyid' ) ;
104- expect ( window . localStorage . getItem ( 'dummyname' ) ) . to . equal ( 'dummyid' ) ;
105- } ) ;
101+ describe ( 'when the storage location is set to local' , function ( ) {
102+ it ( 'should read the client id from local storage' , function ( ) {
103+ window . localStorage . setItem ( 'dummyname' , 'dummyid' ) ;
104+ var session = new ConstructorioID ( { local_name_client_id : 'dummyname' , client_id_storage_location : 'local' } ) ;
105+ expect ( session . client_id ) . to . equal ( 'dummyid' ) ;
106+ expect ( window . localStorage . getItem ( 'dummyname' ) ) . to . equal ( 'dummyid' ) ;
107+ } ) ;
106108
107- it ( 'should read the client id from the default local storage name and storage location is set to local ' , function ( ) {
108- window . localStorage . setItem ( '_constructorio_search_client_id' , 'bummyid' ) ;
109- var session = new ConstructorioID ( { client_id_storage_location : 'local' } ) ;
110- expect ( session . client_id ) . to . equal ( 'bummyid' ) ;
111- expect ( window . localStorage . getItem ( '_constructorio_search_client_id' ) ) . to . equal ( 'bummyid' ) ;
112- } ) ;
109+ it ( 'should read the client id from the default local storage name' , function ( ) {
110+ window . localStorage . setItem ( '_constructorio_search_client_id' , 'bummyid' ) ;
111+ var session = new ConstructorioID ( { client_id_storage_location : 'local' } ) ;
112+ expect ( session . client_id ) . to . equal ( 'bummyid' ) ;
113+ expect ( window . localStorage . getItem ( '_constructorio_search_client_id' ) ) . to . equal ( 'bummyid' ) ;
114+ } ) ;
113115
114- it ( 'should set the client id if missing and storage location is set to local' , function ( ) {
115- var session = new ConstructorioID ( { cookie_name_client_id : 'missingname' , client_id_storage_location : 'local' } ) ;
116- expect ( session . client_id ) . to . be . a . string ;
117- expect ( session . client_id ) . to . match ( / ( \w | d | - ) { 36 } / ) ;
116+ it ( 'should read the client id from cookies if it does not exist in local storage and remove it from the cookies' , function ( ) {
117+ document . cookie = 'ConstructorioID_client_id=chummyid; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/' ;
118+ var session = new ConstructorioID ( { client_id_storage_location : 'local' } ) ;
119+ var cookieData = JSON . parse ( helper . getCookie ( 'ConstructorioID_client_id' ) ) ;
120+ expect ( session . client_id ) . to . equal ( 'chummyid' ) ;
121+ expect ( window . localStorage . getItem ( '_constructorio_search_client_id' ) ) . to . equal ( 'chummyid' ) ;
122+ expect ( cookieData ) . to . equal ( null ) ;
123+ } ) ;
124+
125+ it ( 'should set the client id if missing from both local storage and cookies' , function ( ) {
126+ var session = new ConstructorioID ( { client_id_storage_location : 'local' } ) ;
127+ expect ( session . client_id ) . to . be . a . string ;
128+ expect ( session . client_id ) . to . match ( / ( \w | d | - ) { 36 } / ) ;
129+ } ) ;
118130 } ) ;
119131
132+
120133 it ( 'should read the session id from local storage data' , function ( ) {
121134 window . localStorage . setItem ( '_constructorio_search_session' , JSON . stringify ( {
122135 sessionId : 42 ,
@@ -183,17 +196,39 @@ describe('ConstructorioID', function () {
183196 expect ( session . session_id ) . to . equal ( 1 ) ;
184197 } ) ;
185198
186- it ( 'should read the session id from cookie if storage location is set to cookie' , function ( ) {
187- document . cookie = `ConstructorioID_session={"sessionId":42,"lastTime":${ Date . now ( ) } }; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/` ;
188- var session = new ConstructorioID ( { session_id_storage_location : 'cookie' } ) ;
189- expect ( session . session_id ) . to . be . a ( 'number' ) ;
190- expect ( session . session_id ) . to . equal ( 42 ) ;
191- } ) ;
199+ describe ( 'when the storage location is set to cookie' , function ( ) {
200+ it ( 'should read the session id from cookie' , function ( ) {
201+ document . cookie = `ConstructorioID_session={"sessionId":42,"lastTime":${ Date . now ( ) } }; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/` ;
202+ var session = new ConstructorioID ( { session_id_storage_location : 'cookie' } ) ;
203+ expect ( session . session_id ) . to . be . a ( 'number' ) ;
204+ expect ( session . session_id ) . to . equal ( 42 ) ;
205+ } ) ;
192206
193- it ( 'should set the session id to 1 if there is no cookie and storage location is set to cookie' , function ( ) {
194- var session = new ConstructorioID ( { session_id_storage_location : 'cookie' } ) ;
195- expect ( session . session_id ) . to . be . a ( 'number' ) ;
196- expect ( session . session_id ) . to . equal ( 1 ) ;
207+ it ( 'should read the session id from the default cookie name' , function ( ) {
208+ document . cookie = `ConstructorioID_session={"sessionId":42,"lastTime":${ Date . now ( ) } }; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/` ;
209+ var session = new ConstructorioID ( { session_id_storage_location : 'cookie' } ) ;
210+ var cookieData = JSON . parse ( helper . getCookie ( 'ConstructorioID_session' ) ) ;
211+ expect ( session . session_id ) . to . equal ( 42 ) ;
212+ expect ( cookieData . sessionId ) . to . equal ( 42 ) ;
213+ } ) ;
214+
215+ it ( 'should read the session id from local storage if there is no cookie and remove it from local storage' , function ( ) {
216+ window . localStorage . setItem ( '_constructorio_search_session' , JSON . stringify ( {
217+ sessionId : '11' ,
218+ lastTime : Date . now ( )
219+ } ) ) ;
220+ var session = new ConstructorioID ( { session_id_storage_location : 'cookie' } ) ;
221+ var cookieData = JSON . parse ( helper . getCookie ( 'ConstructorioID_session' ) ) ;
222+ expect ( window . localStorage . getItem ( '_constructorio_search_session' ) ) . to . equal ( null ) ;
223+ expect ( session . session_id ) . to . equal ( 11 ) ;
224+ expect ( cookieData . sessionId ) . to . equal ( 11 ) ;
225+ } ) ;
226+
227+ it ( 'should set the session id to 1 if missing from both local storage and cookies' , function ( ) {
228+ var session = new ConstructorioID ( { session_id_storage_location : 'cookie' } ) ;
229+ expect ( session . session_id ) . to . be . a ( 'number' ) ;
230+ expect ( session . session_id ) . to . equal ( 1 ) ;
231+ } ) ;
197232 } ) ;
198233
199234 it ( 'should set the session id if cookie data can be parsed into a number' , function ( ) {
0 commit comments