Skip to content

Commit bec424c

Browse files
author
Daniel Jih
committed
updated index to copy all instances, and added snippet tests
1 parent 178a3da commit bec424c

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

amplitude.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,16 @@
9696
var Amplitude = require('./amplitude');
9797

9898
var old = window.amplitude || {};
99-
var instance = new Amplitude();
100-
instance._q = old._q || [];
99+
var newInstance = new Amplitude();
100+
newInstance._q = old._q || [];
101+
for (var instance in old._iq) { // migrate eat instance's queue
102+
if (old._iq.hasOwnProperty(instance)) {
103+
newInstance.getInstance(instance)._q = old._iq[instance]._q || [];
104+
}
105+
}
101106

102107
// export the instance
103-
module.exports = instance;
108+
module.exports = newInstance;
104109

105110
}, {"./amplitude":2}],
106111
2: [function(require, module, exports) {

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
var Amplitude = require('./amplitude');
44

55
var old = window.amplitude || {};
6-
var instance = new Amplitude();
7-
instance._q = old._q || [];
6+
var newInstance = new Amplitude();
7+
newInstance._q = old._q || [];
8+
for (var instance in old._iq) { // migrate eat instance's queue
9+
if (old._iq.hasOwnProperty(instance)) {
10+
newInstance.getInstance(instance)._q = old._iq[instance]._q || [];
11+
}
12+
}
813

914
// export the instance
10-
module.exports = instance;
15+
module.exports = newInstance;

test/snippet-tests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,24 @@ describe('Snippet', function() {
3232
assert.deepEqual(revenue._q[1], ['setQuantity', 5]);
3333
assert.deepEqual(revenue._q[2], ['setPrice', 10.99]);
3434
});
35+
36+
it('amplitude object should proxy instance functions', function() {
37+
amplitude.getInstance(null).init('API_KEY');
38+
amplitude.getInstance('$DEFAULT_instance').logEvent('Click');
39+
amplitude.getInstance('').clearUserProperties();
40+
amplitude.getInstance('INSTANCE1').init('API_KEY1');
41+
amplitude.getInstance('instanCE2').init('API_KEY2');
42+
amplitude.getInstance('instaNce2').logEvent('Event');
43+
44+
assert.deepEqual(Object.keys(amplitude._iq), ['$default_instance', 'instance1', 'instance2']);
45+
assert.lengthOf(amplitude._iq['$default_instance']._q, 3);
46+
assert.deepEqual(amplitude._iq['$default_instance']._q[0], ['init', 'API_KEY']);
47+
assert.deepEqual(amplitude._iq['$default_instance']._q[1], ['logEvent', 'Click']);
48+
assert.deepEqual(amplitude._iq['$default_instance']._q[2], ['clearUserProperties']);
49+
assert.lengthOf(amplitude._iq['instance1']._q, 1);
50+
assert.deepEqual(amplitude._iq['instance1']._q[0], ['init', 'API_KEY1']);
51+
assert.lengthOf(amplitude._iq['instance2']._q, 2);
52+
assert.deepEqual(amplitude._iq['instance2']._q[0], ['init', 'API_KEY2']);
53+
assert.deepEqual(amplitude._iq['instance2']._q[1], ['logEvent', 'Event']);
54+
});
3555
});

0 commit comments

Comments
 (0)