|
| 1 | +describe('Localstorage', function() { |
| 2 | + var Amplitude = require('../src/amplitude.js'); |
| 3 | + var localStorage = require('../src/localstorage.js'); |
| 4 | + // var cookieStorage = require('../src/localstorage-cookie.js'); |
| 5 | + var Cookie = require('../src/cookie.js'); |
| 6 | + |
| 7 | + var apiKey = '000000'; |
| 8 | + var cookieKey = 'amplitude_id'; |
| 9 | + var userId = 'test_user_id'; |
| 10 | + var deviceId = 'test_device_id'; |
| 11 | + var amplitude; |
| 12 | + |
| 13 | + var localStorageKeys = [ |
| 14 | + 'amplitude_lastEventId', |
| 15 | + 'amplitude_lastIdentifyId', |
| 16 | + 'amplitude_lastSequenceNumber', |
| 17 | + 'amplitude_lastEventTime', |
| 18 | + 'amplitude_sessionId', |
| 19 | + 'amplitude_unsent', |
| 20 | + 'amplitude_unsent_identify', |
| 21 | + 'amplitude_deviceId', |
| 22 | + 'amplitude_userId', |
| 23 | + 'amplitude_optOut', |
| 24 | + ]; |
| 25 | + |
| 26 | + beforeEach(function() { |
| 27 | + amplitude = new Amplitude(); |
| 28 | + amplitude.options.apiKey = apiKey; |
| 29 | + |
| 30 | + for (var i = 0; i < localStorageKeys.length; i++) { |
| 31 | + var key = localStorageKeys[i]; |
| 32 | + if (localStorage.getItem(key)) localStorage.removeItem(key); |
| 33 | + key = key + '_' + apiKey; |
| 34 | + if (localStorage.getItem(key)) localStorage.removeItem(key); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(function() { |
| 39 | + }); |
| 40 | + |
| 41 | + describe('upgrade', function() { |
| 42 | + |
| 43 | + it('should migrate cookie values to localstorage', function() { |
| 44 | + Cookie.set(cookieKey, { |
| 45 | + deviceId: deviceId, |
| 46 | + userId: userId, |
| 47 | + optOut: true |
| 48 | + }); |
| 49 | + |
| 50 | + amplitude._upgradeStoredData(); |
| 51 | + |
| 52 | + assert.equal(amplitude.getLocalStorage('DEVICE_ID'), deviceId); |
| 53 | + assert.equal(amplitude.getLocalStorage('USER_ID'), userId); |
| 54 | + assert.equal(amplitude.getLocalStorage('OPT_OUT'), 'true'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should update local storage keys and append with apiKey', function() { |
| 58 | + var lastEventId = 'amplitude_lastEventId'; |
| 59 | + var lastIdentifyId = 'amplitude_lastIdentifyId' |
| 60 | + var lastSequenceNumber = 'amplitude_lastSequenceNumber'; |
| 61 | + var lastEventTime = 'amplitude_lastEventTime'; |
| 62 | + var sessionId = 'amplitude_sessionId'; |
| 63 | + var unsentEvents = 'amplitude_unsent'; |
| 64 | + var unsentIdentifys = 'amplitude_unsent_identify'; |
| 65 | + |
| 66 | + localStorage.setItem(lastEventId, 1000); |
| 67 | + localStorage.setItem(lastIdentifyId, 2000); |
| 68 | + localStorage.setItem(lastSequenceNumber, 3000); |
| 69 | + localStorage.setItem(lastEventTime, 4000); |
| 70 | + localStorage.setItem(sessionId, 4000); |
| 71 | + localStorage.setItem(unsentEvents, 'unsent_events_string'); |
| 72 | + localStorage.setItem(unsentIdentifys, 'unsent_identifys_string'); |
| 73 | + |
| 74 | + amplitude._upgradeStoredData(); |
| 75 | + |
| 76 | + assert.equal(amplitude.getLocalStorage('LAST_EVENT_ID'), '1000'); |
| 77 | + assert.equal(amplitude.getLocalStorage('LAST_IDENTIFY_ID'), '2000'); |
| 78 | + assert.equal(amplitude.getLocalStorage('LAST_SEQUENCE_NUMBER'), '3000'); |
| 79 | + assert.equal(amplitude.getLocalStorage('LAST_EVENT_TIME'), '4000'); |
| 80 | + assert.equal(amplitude.getLocalStorage('SESSION_ID'), '4000'); |
| 81 | + assert.equal(amplitude.getLocalStorage('UNSENT_EVENTS'), 'unsent_events_string'); |
| 82 | + assert.equal(amplitude.getLocalStorage('UNSENT_IDENTIFYS'), 'unsent_identifys_string'); |
| 83 | + |
| 84 | + // assert new keys |
| 85 | + assert.equal(localStorage.getItem(lastEventId + '_' + apiKey), '1000'); |
| 86 | + assert.equal(localStorage.getItem(lastIdentifyId + '_' + apiKey), '2000'); |
| 87 | + assert.equal(localStorage.getItem(lastSequenceNumber + '_' + apiKey), '3000'); |
| 88 | + assert.equal(localStorage.getItem(lastEventTime + '_' + apiKey), '4000'); |
| 89 | + assert.equal(localStorage.getItem(sessionId + '_' + apiKey), '4000'); |
| 90 | + assert.equal(localStorage.getItem(unsentEvents + '_' + apiKey), 'unsent_events_string'); |
| 91 | + assert.equal(localStorage.getItem(unsentIdentifys + '_' + apiKey), 'unsent_identifys_string'); |
| 92 | + |
| 93 | + // assert old keys removed |
| 94 | + assert.isNull(localStorage.getItem(lastEventId)); |
| 95 | + assert.isNull(localStorage.getItem(lastIdentifyId)); |
| 96 | + assert.isNull(localStorage.getItem(lastSequenceNumber)); |
| 97 | + assert.isNull(localStorage.getItem(lastEventTime)); |
| 98 | + assert.isNull(localStorage.getItem(sessionId)); |
| 99 | + assert.isNull(localStorage.getItem(unsentEvents)); |
| 100 | + assert.isNull(localStorage.getItem(unsentIdentifys)); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should upgrade and set options correctly', function() { |
| 104 | + var curTime = new Date().getTime(); |
| 105 | + |
| 106 | + Cookie.set(cookieKey, { |
| 107 | + deviceId: deviceId, |
| 108 | + userId: userId, |
| 109 | + optOut: true |
| 110 | + }); |
| 111 | + |
| 112 | + localStorage.setItem('amplitude_lastEventId', 1000); |
| 113 | + localStorage.setItem('amplitude_lastIdentifyId', 2000); |
| 114 | + localStorage.setItem('amplitude_lastSequenceNumber', 3000); |
| 115 | + localStorage.setItem('amplitude_lastEventTime', curTime); |
| 116 | + localStorage.setItem('amplitude_sessionId', curTime); |
| 117 | + localStorage.setItem('amplitude_unsent', '{"event_type": "test", "event_id": 1}'); |
| 118 | + localStorage.setItem('amplitude_unsent_identify', '{"event_type": "$identify", "event_id": 2}'); |
| 119 | + |
| 120 | + amplitude.init(apiKey); |
| 121 | + |
| 122 | + assert.equal(amplitude._eventId, 1000); |
| 123 | + assert.equal(amplitude._identifyId, 2000); |
| 124 | + assert.equal(amplitude._sequenceNumber, 3000); |
| 125 | + assert.isTrue(amplitude._lastEventTime >= curTime); |
| 126 | + assert.equal(amplitude._sessionId, curTime); |
| 127 | + assert.deepEqual(amplitude._unsentEvents, {'event_type': 'test', 'event_id': 1}); |
| 128 | + assert.deepEqual(amplitude._unsentIdentifys, {'event_type': '$identify', 'event_id': 2}); |
| 129 | + |
| 130 | + assert.equal(amplitude.options.deviceId, deviceId); |
| 131 | + assert.equal(amplitude.options.userId, userId); |
| 132 | + assert.isTrue(amplitude.options.optOut); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should set options correctly with the new local storage keys', function() { |
| 136 | + var curTime = new Date().getTime(); |
| 137 | + |
| 138 | + localStorage.setItem('amplitude_lastEventId' + '_' + apiKey, 1000); |
| 139 | + localStorage.setItem('amplitude_lastIdentifyId' + '_' + apiKey, 2000); |
| 140 | + localStorage.setItem('amplitude_lastSequenceNumber' + '_' + apiKey, 3000); |
| 141 | + localStorage.setItem('amplitude_lastEventTime' + '_' + apiKey, curTime) |
| 142 | + localStorage.setItem('amplitude_sessionId' + '_' + apiKey, curTime); |
| 143 | + localStorage.setItem('amplitude_unsent' + '_' + apiKey, '{"event_type": "test", "event_id": 1}'); |
| 144 | + localStorage.setItem('amplitude_unsent_identify' + '_' + apiKey, '{"event_type": "$identify", "event_id": 2}'); |
| 145 | + localStorage.setItem('amplitude_deviceId' + '_' + apiKey, deviceId); |
| 146 | + localStorage.setItem('amplitude_userId' + '_' + apiKey, userId); |
| 147 | + localStorage.setItem('amplitude_optOut' + '_' + apiKey, true); |
| 148 | + |
| 149 | + amplitude.init(apiKey); |
| 150 | + |
| 151 | + assert.equal(amplitude._eventId, 1000); |
| 152 | + assert.equal(amplitude._identifyId, 2000); |
| 153 | + assert.equal(amplitude._sequenceNumber, 3000); |
| 154 | + assert.isTrue(amplitude._lastEventTime >= curTime); |
| 155 | + assert.equal(amplitude._sessionId, curTime); |
| 156 | + assert.deepEqual(amplitude._unsentEvents, {'event_type': 'test', 'event_id': 1}); |
| 157 | + assert.deepEqual(amplitude._unsentIdentifys, {'event_type': '$identify', 'event_id': 2}); |
| 158 | + |
| 159 | + assert.equal(amplitude.options.deviceId, deviceId); |
| 160 | + assert.equal(amplitude.options.userId, userId); |
| 161 | + assert.isTrue(amplitude.options.optOut); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should not override new options', function() { |
| 165 | + Cookie.set(cookieKey, { |
| 166 | + deviceId: deviceId, |
| 167 | + userId: userId, |
| 168 | + optOut: true |
| 169 | + }); |
| 170 | + |
| 171 | + amplitude.init(apiKey, 'NEW_USER_ID', { |
| 172 | + deviceId: 'NEW_DEVICE_ID' |
| 173 | + }); |
| 174 | + amplitude.setOptOut(false); |
| 175 | + |
| 176 | + assert.equal(amplitude.options.deviceId, 'NEW_DEVICE_ID'); |
| 177 | + assert.equal(amplitude.options.userId, 'NEW_USER_ID'); |
| 178 | + assert.isFalse(amplitude.options.optOut); |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments