Skip to content

Commit ec3c048

Browse files
committed
fixed tests
1 parent e9e3f61 commit ec3c048

File tree

1 file changed

+128
-129
lines changed

1 file changed

+128
-129
lines changed

test/amplitude.js

Lines changed: 128 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ describe('Amplitude', function() {
3333
sessionStorage.clear();
3434
cookie.remove(amplitude.options.cookieName);
3535
cookie.remove(amplitude.options.cookieName + keySuffix);
36-
cookie.remove(amplitude.options.cookieName + '_1');
37-
cookie.remove(amplitude.options.cookieName + '_2');
36+
cookie.remove(amplitude.options.cookieName + '_app1');
37+
cookie.remove(amplitude.options.cookieName + '_app2');
3838
cookie.reset();
3939
}
4040

@@ -75,133 +75,132 @@ describe('Amplitude', function() {
7575
assert.equal(amplitude.getInstance('app').options.apiKey, 1);
7676
});
7777

78-
// it('instances should have separate event queues and settings', function() {
79-
// var app1 = amplitude.getInstance('app1');
80-
// app1.init(1);
81-
// var app2 = amplitude.getInstance('app2');
82-
// app2.init(2);
83-
84-
// assert.notEqual(app1.options.deviceId, app2.options.deviceId);
85-
86-
// var identify = new Identify().set('key', 'value');
87-
// app1.identify(identify);
88-
// app2.logEvent('app2 event');
89-
90-
// assert.lengthOf(app1._unsentEvents, 0);
91-
// assert.lengthOf(app1._unsentIdentifys, 1);
92-
// assert.lengthOf(app2._unsentEvents, 1);
93-
// assert.lengthOf(app2._unsentIdentifys, 0);
94-
95-
// assert.deepEqual(app1._unsentEvents, []);
96-
// assert.deepEqual(app1._unsentIdentifys[0].user_properties, {'$set':{'key':'value'}});
97-
// assert.deepEqual(app2._unsentEvents[0].event_type, 'app2 event');
98-
// assert.deepEqual(app2._unsentIdentifys, []);
99-
100-
// assert.equal(app1._eventId, 0);
101-
// assert.equal(app1._identifyId, 1);
102-
// assert.equal(app1._sequenceNumber, 1);
103-
// assert.equal(app2._eventId, 1);
104-
// assert.equal(app2._identifyId, 0);
105-
// assert.equal(app2._sequenceNumber, 1);
106-
107-
// // verify separate localstorages
108-
// assert.equal(localStorage.getItem('amplitude_unsent_1'), JSON.stringify([]));
109-
// assert.deepEqual(
110-
// JSON.parse(localStorage.getItem('amplitude_unsent_identify_1'))[0].user_properties, {'$set':{'key':'value'}}
111-
// );
112-
// assert.equal(
113-
// JSON.parse(localStorage.getItem('amplitude_unsent_2'))[0].event_type, 'app2 event'
114-
// );
115-
// assert.equal(localStorage.getItem('amplitude_unsent_identify_2'), JSON.stringify([]));
116-
117-
// // verify separate apiKeys in server requests
118-
// assert.lengthOf(server.requests, 2);
119-
// assert.equal(JSON.parse(querystring.parse(server.requests[0].requestBody).client), 1);
120-
// assert.equal(JSON.parse(querystring.parse(server.requests[1].requestBody).client), 2);
121-
122-
// // verify separate cookie data
123-
// var cookieData1 = cookie.get(app1.options.cookieName + '_1');
124-
// assert.equal(cookieData1.deviceId, app1.options.deviceId);
125-
126-
// var cookieData2 = cookie.get(app2.options.cookieName + '_2');
127-
// assert.equal(cookieData2.deviceId, app2.options.deviceId);
128-
// });
129-
130-
// it('instances should share same historical deviceId and userId by default', function() {
131-
// var now = new Date().getTime();
132-
133-
// var cookieData = {
134-
// deviceId: 'test_device_id',
135-
// userId: 'test_user_id',
136-
// optOut: false,
137-
// sessionId: now,
138-
// lastEventTime: now,
139-
// eventId: 50,
140-
// identifyId: 60,
141-
// sequenceNumber: 70
142-
// }
143-
// cookie.set(amplitude.options.cookieName, cookieData);
144-
145-
// var app1 = amplitude.getInstance('app1');
146-
// app1.init(apiKey);
147-
// assert.equal(app1.options.deviceId, 'test_device_id');
148-
// assert.equal(app1.options.userId, 'test_user_id');
149-
// assert.isFalse(app1.options.optOut);
150-
// assert.equal(app1._sessionId, now);
151-
// assert.isTrue(app1._lastEventTime >= now);
152-
// assert.equal(app1._eventId, 50);
153-
// assert.equal(app1._identifyId, 60);
154-
// assert.equal(app1._sequenceNumber, 70);
155-
156-
// var app2 = amplitude.getInstance('app2');
157-
// app2.init(apiKey);
158-
// assert.equal(app2.options.deviceId, 'test_device_id');
159-
// assert.equal(app2.options.userId, 'test_user_id');
160-
// assert.isFalse(app2.options.optOut);
161-
// assert.equal(app2._sessionId, now);
162-
// assert.isTrue(app2._lastEventTime >= now);
163-
// assert.equal(app2._eventId, 50);
164-
// assert.equal(app2._identifyId, 60);
165-
// assert.equal(app2._sequenceNumber, 70);
166-
// });
167-
168-
// it('instances should not load historical cookie data if newBlankInstance is true', function() {
169-
// var now = new Date().getTime();
170-
171-
// var cookieData = {
172-
// deviceId: 'test_device_id',
173-
// userId: 'test_user_id',
174-
// optOut: false,
175-
// sessionId: now-500,
176-
// lastEventTime: now-500,
177-
// eventId: 50,
178-
// identifyId: 60,
179-
// sequenceNumber: 70
180-
// }
181-
// cookie.set(amplitude.options.cookieName, cookieData);
182-
183-
// var app1 = amplitude.getInstance('app1');
184-
// app1.init(1);
185-
// assert.equal(app1.options.deviceId, 'test_device_id');
186-
// assert.equal(app1.options.userId, 'test_user_id');
187-
// assert.isFalse(app1.options.optOut);
188-
// assert.equal(app1._sessionId, now-500);
189-
// assert.isTrue(app1._lastEventTime >= now);
190-
// assert.equal(app1._eventId, 50);
191-
// assert.equal(app1._identifyId, 60);
192-
// assert.equal(app1._sequenceNumber, 70);
193-
194-
// var app2 = amplitude.getInstance('app2');
195-
// app2.init(2, null, {newBlankInstance: true});
196-
// assert.notEqual(app2.options.deviceId, 'test_device_id');
197-
// assert.isNull(app2.options.userId);
198-
// assert.isFalse(app2.options.optOut);
199-
// assert.isTrue(app2._sessionId >= now);
200-
// assert.isTrue(app2._lastEventTime >= now);
201-
// assert.equal(app2._eventId, 0);
202-
// assert.equal(app2._identifyId, 0);
203-
// assert.equal(app2._sequenceNumber, 0);
204-
// });
78+
it('instances should have separate event queues and settings', function() {
79+
amplitude.init(apiKey, null, {batchEvents: true, eventUploadThreshold: 2});
80+
var app1 = amplitude.getInstance('app1');
81+
app1.init(1);
82+
var app2 = amplitude.getInstance('app2');
83+
app2.init(2);
84+
85+
assert.notEqual(amplitude.options.deviceId, app1.options.deviceId);
86+
assert.notEqual(amplitude.options.deviceId, app2.options.deviceId);
87+
assert.notEqual(app1.options.deviceId, app2.options.deviceId);
88+
89+
amplitude.logEvent('amplitude event');
90+
amplitude.logEvent('amplitude event2');
91+
var identify = new Identify().set('key', 'value');
92+
app1.identify(identify);
93+
app2.logEvent('app2 event');
94+
95+
assert.lengthOf(amplitude.getInstance()._unsentEvents, 2);
96+
assert.lengthOf(amplitude.getInstance()._unsentIdentifys, 0);
97+
98+
assert.lengthOf(app1._unsentEvents, 0);
99+
assert.lengthOf(app1._unsentIdentifys, 1);
100+
assert.lengthOf(app2._unsentEvents, 1);
101+
assert.lengthOf(app2._unsentIdentifys, 0);
102+
103+
assert.deepEqual(amplitude.getInstance()._unsentEvents[0].event_type, 'amplitude event');
104+
assert.deepEqual(amplitude.getInstance()._unsentEvents[1].event_type, 'amplitude event2');
105+
assert.deepEqual(amplitude.getInstance()._unsentIdentifys, []);
106+
assert.deepEqual(app1._unsentEvents, []);
107+
assert.deepEqual(app1._unsentIdentifys[0].user_properties, {'$set':{'key':'value'}});
108+
assert.deepEqual(app2._unsentEvents[0].event_type, 'app2 event');
109+
assert.deepEqual(app2._unsentIdentifys, []);
110+
111+
assert.equal(amplitude.getInstance()._eventId, 2);
112+
assert.equal(amplitude.getInstance()._identifyId, 0);
113+
assert.equal(amplitude.getInstance()._sequenceNumber, 2);
114+
assert.equal(app1._eventId, 0);
115+
assert.equal(app1._identifyId, 1);
116+
assert.equal(app1._sequenceNumber, 1);
117+
assert.equal(app2._eventId, 1);
118+
assert.equal(app2._identifyId, 0);
119+
assert.equal(app2._sequenceNumber, 1);
120+
121+
// verify separate localstorages
122+
assert.deepEqual(
123+
JSON.parse(localStorage.getItem('amplitude_unsent'))[0].event_type, 'amplitude event'
124+
);
125+
assert.deepEqual(
126+
JSON.parse(localStorage.getItem('amplitude_unsent'))[1].event_type, 'amplitude event2'
127+
);
128+
assert.equal(localStorage.getItem('amplitude_unsent_identify'), JSON.stringify([]));
129+
assert.equal(localStorage.getItem('amplitude_unsent_app1'), JSON.stringify([]));
130+
assert.deepEqual(
131+
JSON.parse(localStorage.getItem('amplitude_unsent_identify_app1'))[0].user_properties, {'$set':{'key':'value'}}
132+
);
133+
assert.equal(
134+
JSON.parse(localStorage.getItem('amplitude_unsent_app2'))[0].event_type, 'app2 event'
135+
);
136+
assert.equal(localStorage.getItem('amplitude_unsent_identify_app2'), JSON.stringify([]));
137+
138+
// verify separate apiKeys in server requests
139+
assert.lengthOf(server.requests, 3);
140+
assert.equal(JSON.parse(querystring.parse(server.requests[1].requestBody).client), 1);
141+
assert.equal(JSON.parse(querystring.parse(server.requests[2].requestBody).client), 2);
142+
143+
// verify separate cookie data
144+
var cookieData = cookie.get(amplitude.options.cookieName);
145+
assert.equal(cookieData.deviceId, amplitude.options.deviceId);
146+
147+
var cookieData1 = cookie.get(app1.options.cookieName + '_app1');
148+
assert.equal(cookieData1.deviceId, app1.options.deviceId);
149+
150+
var cookieData2 = cookie.get(app2.options.cookieName + '_app2');
151+
assert.equal(cookieData2.deviceId, app2.options.deviceId);
152+
});
153+
154+
it('new instances should not load historical cookie data', function() {
155+
var now = new Date().getTime();
156+
157+
var cookieData = {
158+
deviceId: 'test_device_id',
159+
userId: 'test_user_id',
160+
optOut: true,
161+
sessionId: now-500,
162+
lastEventTime: now-500,
163+
eventId: 50,
164+
identifyId: 60,
165+
sequenceNumber: 70
166+
}
167+
cookie.set(amplitude.options.cookieName, cookieData);
168+
169+
// default instance loads from existing cookie
170+
var app = amplitude.getInstance();
171+
app.init(apiKey);
172+
assert.equal(app.options.deviceId, 'test_device_id');
173+
assert.equal(app.options.userId, 'test_user_id');
174+
assert.isTrue(app.options.optOut);
175+
assert.equal(app._sessionId, now-500);
176+
assert.isTrue(app._lastEventTime >= now);
177+
assert.equal(app._eventId, 50);
178+
assert.equal(app._identifyId, 60);
179+
assert.equal(app._sequenceNumber, 70);
180+
181+
var app1 = amplitude.getInstance('app1');
182+
app1.init(1);
183+
assert.notEqual(app1.options.deviceId, 'test_device_id');
184+
assert.isNull(app1.options.userId);
185+
assert.isFalse(app1.options.optOut);
186+
console.log(app1._sessionId);
187+
assert.isTrue(app1._sessionId >= now);
188+
assert.isTrue(app1._lastEventTime >= now);
189+
assert.equal(app1._eventId, 0);
190+
assert.equal(app1._identifyId, 0);
191+
assert.equal(app1._sequenceNumber, 0);
192+
193+
var app2 = amplitude.getInstance('app2');
194+
app2.init(2);
195+
assert.notEqual(app2.options.deviceId, 'test_device_id');
196+
assert.isNull(app2.options.userId);
197+
assert.isFalse(app2.options.optOut);
198+
assert.isTrue(app2._sessionId >= now);
199+
assert.isTrue(app2._lastEventTime >= now);
200+
assert.equal(app2._eventId, 0);
201+
assert.equal(app2._identifyId, 0);
202+
assert.equal(app2._sequenceNumber, 0);
203+
});
205204
});
206205

207206
describe('init', function() {

0 commit comments

Comments
 (0)