Skip to content

Commit

Permalink
Merge pull request #85 from tylergee/DE20872_code_review_suggestion_r…
Browse files Browse the repository at this point in the history
…emove_event_listener

De20872 code review suggestion remove event listener
  • Loading branch information
dlockhart authored Aug 25, 2016
2 parents efb4f69 + b1112be commit 1cbe0db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/plugins/user-activity-events/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ function throttle(fn) {
}

module.exports = function recordUserEvents(client) {

if (document.addEventListener) {
document.addEventListener('click', throttle(function() {
client.sendEvent('userIsActive');
}));
document.addEventListener('keydown', throttle(function() {
var listener = throttle(function userActivityListener() {
client.sendEvent('userIsActive');
}));
});
document.addEventListener('click', listener);
document.addEventListener('keydown', listener);

client.onClose(function removeUserActivityListeners() {
document.removeEventListener('click', listener);
document.removeEventListener('keydown', listener);
});
}
};
21 changes: 20 additions & 1 deletion test/plugins/user-activity-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ describe('user-activity-events', () => {
});

describe('user activity events client', () => {
var onClose;
var addEventListener;
var removeEventListener;
var sendEvent;
beforeEach(() => {
addEventListener = sinon.spy();
removeEventListener = sinon.spy();
onClose = sinon.spy();
sendEvent = sinon.spy();
global.document.addEventListener = addEventListener;
global.document.removeEventListener = removeEventListener;
client.sendEvent = sendEvent;
client.onClose = onClose;
});

it('should add an click event listener which triggers a userIsActiveEvent when called', () => {
Expand Down Expand Up @@ -70,19 +76,33 @@ describe('user-activity-events', () => {
sendEvent.should.have.callCount(1);
});

it('should add on-close handler', () => {
clientUserActivityEvents(client);
onClose.should.have.been.called;
});

it('should remove event listeners when client is closed ', () => {
clientUserActivityEvents(client);
onClose.args[0][0]();
removeEventListener.should.have.been.calledWith('click');
removeEventListener.should.have.been.calledWith('keydown');
});
});

describe('user activity events with fake-waiting for throttle buffer period', () => {
var clock;
var addEventListener;
var sendEvent;
var onClose;
beforeEach(function() {
var now = Date.now();
clock = sinon.useFakeTimers(now);
addEventListener = sinon.spy();
sendEvent = sinon.spy();
onClose = sinon.spy();
global.document.addEventListener = addEventListener;
client.sendEvent = sendEvent;
client.onClose = onClose;
});
afterEach(() => {
clock.restore();
Expand All @@ -97,7 +117,6 @@ describe('user-activity-events', () => {
clock.tick(10001);
sendEvent.should.have.callCount(2);
});

});
});

Expand Down

0 comments on commit 1cbe0db

Please sign in to comment.