@@ -18,7 +18,7 @@ describe('AmplitudeClient', function() {
1818 var server ;
1919
2020 beforeEach ( function ( ) {
21- amplitude = new Amplitude ( ) ;
21+ amplitude = new AmplitudeClient ( ) ;
2222 server = sinon . fakeServer . create ( ) ;
2323 } ) ;
2424
@@ -34,6 +34,8 @@ describe('AmplitudeClient', function() {
3434 localStorage . clear ( ) ;
3535 sessionStorage . clear ( ) ;
3636 cookie . remove ( amplitude . options . cookieName ) ;
37+ cookie . remove ( amplitude . options . cookieName + keySuffix ) ;
38+ cookie . remove ( amplitude . options . cookieName + '_new_app' ) ;
3739 cookie . reset ( ) ;
3840 }
3941
@@ -46,6 +48,11 @@ describe('AmplitudeClient', function() {
4648 reset ( ) ;
4749 } ) ;
4850
51+ it ( 'should make instanceName case-insensitive' , function ( ) {
52+ assert . equal ( new AmplitudeClient ( 'APP3' ) . _instanceName , 'app3' ) ;
53+ assert . equal ( new AmplitudeClient ( '$DEFAULT_INSTANCE' ) . _instanceName , '$default_instance' ) ;
54+ } ) ;
55+
4956 it ( 'fails on invalid apiKeys' , function ( ) {
5057 amplitude . init ( null ) ;
5158 assert . equal ( amplitude . options . apiKey , undefined ) ;
@@ -124,7 +131,7 @@ describe('AmplitudeClient', function() {
124131 assert . equal ( counter , 1 ) ;
125132 } ) ;
126133
127- it ( 'should migrate deviceId, userId, optOut from localStorage to cookie' , function ( ) {
134+ it ( 'should migrate deviceId, userId, optOut from localStorage to cookie on default instance ' , function ( ) {
128135 var deviceId = 'test_device_id' ;
129136 var userId = 'test_user_id' ;
130137
@@ -144,6 +151,27 @@ describe('AmplitudeClient', function() {
144151 assert . isTrue ( cookieData . optOut ) ;
145152 } ) ;
146153
154+ it ( 'should not migrate any cookie or LS data for non-default instances' , function ( ) {
155+ var deviceId = 'testDeviceId' ;
156+ var userId = 'test_user_id' ;
157+
158+ assert . isNull ( cookie . get ( amplitude . options . cookieName ) ) ;
159+ localStorage . setItem ( 'amplitude_deviceId' + keySuffix , deviceId ) ;
160+ localStorage . setItem ( 'amplitude_userId' + keySuffix , userId ) ;
161+ localStorage . setItem ( 'amplitude_optOut' + keySuffix , true ) ;
162+
163+ var amplitude2 = new AmplitudeClient ( 'new_app' ) ;
164+ amplitude2 . init ( apiKey ) ;
165+ assert . notEqual ( amplitude . options . deviceId , deviceId ) ;
166+ assert . isNull ( amplitude2 . options . userId ) ;
167+ assert . isFalse ( amplitude2 . options . optOut ) ;
168+
169+ var cookieData = cookie . get ( amplitude . options . cookieName + '_new_app' ) ;
170+ assert . equal ( cookieData . deviceId , amplitude2 . options . deviceId ) ;
171+ assert . isNull ( cookieData . userId ) ;
172+ assert . isFalse ( cookieData . optOut ) ;
173+ } ) ;
174+
147175 it ( 'should migrate session and event info from localStorage to cookie' , function ( ) {
148176 var now = new Date ( ) . getTime ( ) ;
149177
@@ -241,7 +269,7 @@ describe('AmplitudeClient', function() {
241269
242270 localStorage . clear ( ) ;
243271 sinon . stub ( CookieStorage . prototype , '_cookiesEnabled' ) . returns ( false ) ;
244- var amplitude2 = new Amplitude ( ) ;
272+ var amplitude2 = new AmplitudeClient ( ) ;
245273 CookieStorage . prototype . _cookiesEnabled . restore ( ) ;
246274 amplitude2 . init ( apiKey , userId , { 'deviceId' : deviceId } ) ;
247275 clock . restore ( ) ;
@@ -267,7 +295,7 @@ describe('AmplitudeClient', function() {
267295 var eventIdKey = 'amplitude_lastEventId' ;
268296 var identifyIdKey = 'amplitude_lastIdentifyId' ;
269297 var sequenceNumberKey = 'amplitude_lastSequenceNumber' ;
270- var amplitude2 = new Amplitude ( ) ;
298+ var amplitude2 = new AmplitudeClient ( ) ;
271299
272300 var clock = sinon . useFakeTimers ( ) ;
273301 clock . tick ( 1000 ) ;
@@ -310,7 +338,7 @@ describe('AmplitudeClient', function() {
310338 var eventIdKey = 'amplitude_lastEventId' ;
311339 var identifyIdKey = 'amplitude_lastIdentifyId' ;
312340 var sequenceNumberKey = 'amplitude_lastSequenceNumber' ;
313- var amplitude2 = new Amplitude ( ) ;
341+ var amplitude2 = new AmplitudeClient ( ) ;
314342
315343 var cookieData = {
316344 deviceId : 'test_device_id' ,
@@ -341,7 +369,7 @@ describe('AmplitudeClient', function() {
341369 assert . equal ( amplitude2 . _sequenceNumber , 70 ) ;
342370 } ) ;
343371
344- it ( 'should load saved events from localStorage' , function ( ) {
372+ it ( 'should load saved events from localStorage for default instance ' , function ( ) {
345373 var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
346374 '"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
347375 ',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
@@ -383,7 +411,7 @@ describe('AmplitudeClient', function() {
383411 '47a0-8918-b4530ce51f89","library":{"name":"amplitude-js","version":"2.9.0"},"sequence_number":5}]'
384412 localStorage . setItem ( 'amplitude_unsent' , existingEvents ) ;
385413
386- var amplitude2 = new Amplitude ( ) ;
414+ var amplitude2 = new AmplitudeClient ( '$default_instance' ) ;
387415 amplitude2 . init ( apiKey , null , { batchEvents : true } ) ;
388416
389417 var expected = {
@@ -412,7 +440,7 @@ describe('AmplitudeClient', function() {
412440 '47a0-8918-b4530ce51f89","library":{"name":"amplitude-js","version":"2.9.0"},"sequence_number":5}]'
413441 localStorage . setItem ( 'amplitude_unsent_identify' , existingEvents ) ;
414442
415- var amplitude2 = new Amplitude ( ) ;
443+ var amplitude2 = new AmplitudeClient ( ) ;
416444 amplitude2 . init ( apiKey , null , { batchEvents : true } ) ;
417445
418446 var expected = {
@@ -444,7 +472,7 @@ describe('AmplitudeClient', function() {
444472 localStorage . setItem ( 'amplitude_unsent' , existingEvent ) ;
445473 localStorage . setItem ( 'amplitude_unsent_identify' , existingIdentify ) ;
446474
447- var amplitude2 = new Amplitude ( ) ;
475+ var amplitude2 = new AmplitudeClient ( ) ;
448476 amplitude2 . init ( apiKey , null , { batchEvents : true , eventUploadThreshold : 2 } ) ;
449477 server . respondWith ( 'success' ) ;
450478 server . respond ( ) ;
@@ -480,7 +508,7 @@ describe('AmplitudeClient', function() {
480508 '47a0-8918-b4530ce51f89","library":{"name":"amplitude-js","version":"2.9.0"},"sequence_number":5}]' ;
481509 localStorage . setItem ( 'amplitude_unsent' , existingEvents ) ;
482510
483- var amplitude2 = new Amplitude ( ) ;
511+ var amplitude2 = new AmplitudeClient ( ) ;
484512 amplitude2 . init ( apiKey , null , {
485513 batchEvents : true
486514 } ) ;
0 commit comments