Skip to content

Commit 60357ae

Browse files
committed
test: add setUserId test
1 parent 0824fdd commit 60357ae

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/amplitude-client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,7 @@ AmplitudeClient.prototype.logEvent = function logEvent(
14351435
* @param {Amplitude~eventCallback} opt_error_callback - (optional) a callback function to run after the event logging
14361436
* fails. The failure can be from the request being malformed or from a network failure
14371437
* Note: the server response code and response body from the event upload are passed to the callback function.
1438+
* @param {boolean} outOfSession - (optional) if out of the sessioin or not
14381439
* @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15});
14391440
*/
14401441
AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(
@@ -1443,7 +1444,7 @@ AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(
14431444
timestamp,
14441445
opt_callback,
14451446
opt_error_callback,
1446-
outOfSession,
1447+
outOfSession = false,
14471448
) {
14481449
if (this._shouldDeferCall()) {
14491450
return this._q.push(['logEventWithTimestamp'].concat(Array.prototype.slice.call(arguments, 0)));

test/amplitude-client.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,6 +2840,18 @@ describe('AmplitudeClient', function () {
28402840
});
28412841
});
28422842

2843+
describe('logEvent with outOfSession', function () {
2844+
this.beforeEach(function () {
2845+
reset();
2846+
});
2847+
2848+
it('should reset the sessionId', function () {
2849+
amplitude.init(apiKey);
2850+
amplitude.logEvent('Event Type', null, null, null, true);
2851+
assert.equal(amplitude._sessionId, -1);
2852+
});
2853+
});
2854+
28432855
describe('setEventUploadThreshold', function () {
28442856
beforeEach(function () {
28452857
reset();
@@ -4387,4 +4399,53 @@ describe('AmplitudeClient', function () {
43874399
assert.equal(amplitude._unsentEvents.length, 0);
43884400
});
43894401
});
4402+
4403+
describe('setUserId', function () {
4404+
let clock, startTime;
4405+
beforeEach(function () {
4406+
reset();
4407+
startTime = Date.now();
4408+
clock = sinon.useFakeTimers(startTime);
4409+
amplitude.init(apiKey);
4410+
});
4411+
4412+
it('should not renew the session id with invalid startNewSession input', function () {
4413+
var amplitude = new AmplitudeClient();
4414+
// set up initial session
4415+
var sessionId = 1000;
4416+
clock.tick(sessionId);
4417+
amplitude.init(apiKey);
4418+
assert.equal(amplitude.getSessionId(), startTime);
4419+
4420+
amplitude.setUserId('test user', 'invalid startNewSession');
4421+
assert.notEqual(amplitude.getSessionId(), new Date().getTime());
4422+
assert.notEqual(amplitude.options.userId, 'test user');
4423+
});
4424+
4425+
it('should renew the session id with current timestemp', function () {
4426+
var amplitude = new AmplitudeClient();
4427+
// set up initial session
4428+
var sessionId = 1000;
4429+
clock.tick(sessionId);
4430+
amplitude.init(apiKey);
4431+
assert.equal(amplitude.getSessionId(), startTime);
4432+
4433+
amplitude.setUserId('test user', true);
4434+
assert.equal(amplitude.getSessionId(), new Date().getTime());
4435+
assert.equal(amplitude.options.userId, 'test user');
4436+
});
4437+
4438+
it('should continue the old session', function () {
4439+
var amplitude = new AmplitudeClient();
4440+
// set up initial session
4441+
var sessionId = 1000;
4442+
clock.tick(sessionId);
4443+
amplitude.init(apiKey);
4444+
assert.equal(amplitude.getSessionId(), startTime);
4445+
4446+
amplitude.setUserId('test user');
4447+
assert.equal(amplitude.getSessionId(), startTime);
4448+
assert.equal(amplitude.options.userId, 'test user');
4449+
});
4450+
});
43904451
});

0 commit comments

Comments
 (0)