Adding ability to deregister event listeners

This commit is contained in:
Jonathan Putney
2020-12-14 12:17:43 -05:00
parent cae17ee511
commit 28fe544ef5
8 changed files with 309 additions and 19 deletions

View File

@@ -448,6 +448,62 @@ describe('SCORM 1.2 API Tests', () => {
clock.tick(2000);
expect(callback.called).to.be.true;
});
it('Should clear all event listeners for CommitSuccess',
() => {
const scorm12API = api({
lmsCommitUrl: '/scorm12',
autocommit: true,
autocommitSeconds: 1,
});
scorm12API.lmsInitialize();
const callback = sinon.spy();
const callback2 = sinon.spy();
scorm12API.on('CommitSuccess', callback);
scorm12API.on('CommitSuccess', callback2);
scorm12API.lmsSetValue('cmi.core.session_time', '00:01:00');
clock.tick(2000);
expect(callback.calledOnce).to.be.true;
expect(callback2.calledOnce).to.be.true;
scorm12API.clear('CommitSuccess');
scorm12API.lmsSetValue('cmi.core.session_time', '00:01:00');
clock.tick(2000);
expect(callback.calledTwice).to.be.false;
expect(callback2.calledTwice).to.be.false;
});
it('Should clear only the specific event listener for CommitSuccess',
() => {
const scorm12API = api({
lmsCommitUrl: '/scorm12',
autocommit: true,
autocommitSeconds: 1,
});
scorm12API.lmsInitialize();
const callback = sinon.spy(() => 1);
const callback2 = sinon.spy(() => 2);
const callback3 = sinon.spy(() => 3);
scorm12API.on('CommitSuccess', callback);
scorm12API.on('CommitSuccess', callback2);
scorm12API.on('LMSSetValue', callback3);
scorm12API.lmsSetValue('cmi.core.session_time', '00:01:00');
clock.tick(2000);
expect(callback.calledOnce).to.be.true;
expect(callback2.calledOnce).to.be.true;
expect(callback3.calledOnce).to.be.true;
scorm12API.off('CommitSuccess', callback);
scorm12API.lmsSetValue('cmi.core.session_time', '00:01:00');
clock.tick(2000);
expect(callback.calledTwice).to.be.false; // removed callback should not be called a second time
expect(callback2.calledTwice).to.be.true;
expect(callback3.calledTwice).to.be.true;
});
it('Should handle CommitError event',
() => {
const scorm12API = api({

View File

@@ -38,6 +38,10 @@ describe('SCORM 2004 API Tests', () => {
server.post('/scorm2004/error', () => {
return [500, {'Content-Type': 'application/json'}, '{}'];
}, false);
server.unhandledRequest = function(verb, path, request) {
// do nothing
};
});
after(() => {
@@ -645,6 +649,62 @@ describe('SCORM 2004 API Tests', () => {
clock.tick(2000);
expect(callback.called).to.be.true;
});
it('Should clear all event listeners for CommitSuccess',
() => {
const scorm2004API = api({
lmsCommitUrl: '/scorm2004',
autocommit: true,
autocommitSeconds: 1,
});
scorm2004API.lmsInitialize();
const callback = sinon.spy();
const callback2 = sinon.spy();
scorm2004API.on('CommitSuccess', callback);
scorm2004API.on('CommitSuccess', callback2);
scorm2004API.lmsSetValue('cmi.session_time', 'PT1M0S');
clock.tick(2000);
expect(callback.calledOnce).to.be.true;
expect(callback2.calledOnce).to.be.true;
scorm2004API.clear('CommitSuccess');
scorm2004API.lmsSetValue('cmi.session_time', 'PT1M0S');
clock.tick(2000);
expect(callback.calledTwice).to.be.false;
expect(callback2.calledTwice).to.be.false;
});
it('Should clear only the specific event listener for CommitSuccess',
() => {
const scorm2004API = api({
lmsCommitUrl: '/scorm2004',
autocommit: true,
autocommitSeconds: 1,
});
scorm2004API.lmsInitialize();
const callback = sinon.spy(() => 1);
const callback2 = sinon.spy(() => 2);
const callback3 = sinon.spy(() => 3);
scorm2004API.on('CommitSuccess', callback);
scorm2004API.on('CommitSuccess', callback2);
scorm2004API.on('SetValue', callback3);
scorm2004API.lmsSetValue('cmi.session_time', 'PT1M0S');
clock.tick(2000);
expect(callback.calledOnce).to.be.true;
expect(callback2.calledOnce).to.be.true;
expect(callback3.calledOnce).to.be.true;
scorm2004API.off('CommitSuccess', callback);
scorm2004API.lmsSetValue('cmi.session_time', 'PT1M0S');
clock.tick(2000);
expect(callback.calledTwice).to.be.false; // removed callback should not be called a second time
expect(callback2.calledTwice).to.be.true;
expect(callback3.calledTwice).to.be.true;
});
it('Should handle CommitError event',
() => {
const scorm2004API = api({
@@ -657,6 +717,22 @@ describe('SCORM 2004 API Tests', () => {
const callback = sinon.spy();
scorm2004API.on('CommitError', callback);
scorm2004API.lmsSetValue('cmi.session_time', 'PT1M0S');
clock.tick(2000);
expect(callback.called).to.be.true;
});
it('Should handle CommitError event when offline',
() => {
const scorm2004API = api({
lmsCommitUrl: '/scorm2004/does_not_exist',
autocommit: true,
autocommitSeconds: 1,
});
scorm2004API.lmsInitialize();
const callback = sinon.spy();
scorm2004API.on('CommitError', callback);
scorm2004API.lmsSetValue('cmi.session_time', 'PT1M0S');
clock.tick(2000);
expect(callback.called).to.be.true;