Skip to content

Commit

Permalink
Bug fixes and sorting issue with loadFromFlattenedJson
Browse files Browse the repository at this point in the history
Also added several new test cases, most significantly some cases that test the commit functionality
  • Loading branch information
jcputney committed Aug 17, 2020
1 parent 8c781ac commit 7f1d995
Show file tree
Hide file tree
Showing 10 changed files with 429 additions and 48 deletions.
41 changes: 29 additions & 12 deletions dist/scorm-again.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/scorm-again.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/scorm-again.min.js

Large diffs are not rendered by default.

150 changes: 150 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scorm-again",
"version": "1.4.1",
"version": "1.4.2",
"description": "A modern SCORM JavaScript run-time library for AICC, SCORM 1.2, and SCORM 2004",
"main": "dist/scorm-again.min.js",
"directories": {
Expand Down Expand Up @@ -38,7 +38,9 @@
"mocha": "^8.1.1",
"mocha-junit-reporter": "^2.0.0",
"mochawesome": "^6.1.1",
"nyc": "^15.1.0"
"nyc": "^15.1.0",
"fetch-pretender": "^1.5.0",
"sinon": "^9.0.3"
},
"scripts": {
"test": "./node_modules/.bin/mocha --require @babel/register --bdd --recursive --reporter list",
Expand Down
33 changes: 25 additions & 8 deletions src/BaseAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default class BaseAPI {
result = {};
if (xhr.status === 200) {
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} else {
result.result = global_constants.SCORM_FALSE;
result.errorCode = 101;
Expand Down Expand Up @@ -869,7 +870,22 @@ export default class BaseAPI {

let c_match;
if (a_match !== null && (c_match = c.match(a_pattern)) !== null) {
return Number(a_match[2]) - Number(c_match[2]);
const a_num = Number(a_match[2]);
const c_num = Number(c_match[2]);
if (a_num === c_num) {
if (a_match[3] === 'id') {
return -1;
} else if (a_match[3] === 'type') {
if (c_match[3] === 'id') {
return 1;
} else {
return -1;
}
} else {
return 1;
}
}
return a_num - c_num;
}

return null;
Expand Down Expand Up @@ -989,6 +1005,7 @@ export default class BaseAPI {
* @return {object}
*/
processHttpRequest(url: String, params, immediate = false) {
const api = this;
const process = function(url, params, settings, error_codes) {
const genericError = {
'result': global_constants.SCORM_FALSE,
Expand Down Expand Up @@ -1049,6 +1066,12 @@ export default class BaseAPI {
return genericError;
}

if (result.errorCode === 0) {
api.processListeners('CommitSuccess');
} else {
api.processListeners('CommitError');
}

return result;
};

Expand All @@ -1066,13 +1089,7 @@ export default class BaseAPI {
errorCode: 0,
};
} else {
const result = process(url, params, this.settings, this.error_codes);
if (result.errorCode === 0) {
this.processListeners('CommitSuccess');
} else {
this.processListeners('CommitError');
}
return result;
return process(url, params, this.settings, this.error_codes);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/constants/regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const scorm2004 = {
CMISStatus: '^(passed|failed|unknown)$',
CMIExit: '^(time-out|suspend|logout|normal)$',
CMIType: '^(true-false|choice|fill-in|long-fill-in|matching|performance|sequencing|likert|numeric|other)$',
CMIResult: '^(correct|wrong|unanticipated|neutral|-?([0-9]{1,4})(\\.[0-9]{1,18})?)$',
CMIResult: '^(correct|incorrect|unanticipated|neutral|-?([0-9]{1,4})(\\.[0-9]{1,18})?)$',
NAVEvent: '^(previous|continue|exit|exitAll|abandon|abandonAll|suspendAll|\{target=\\S{0,200}[a-zA-Z0-9]\}choice|jump)$', // eslint-disable-line
NAVBoolean: '^(unknown|true|false$)',
NAVTarget: '^(previous|continue|choice.{target=\\S{0,200}[a-zA-Z0-9]})$',
Expand Down
74 changes: 73 additions & 1 deletion test/Scorm12API.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {expect} from 'chai';
import {describe, it} from 'mocha';
import {after, before, describe, it} from 'mocha';
import Scorm12API from '../src/Scorm12API';
import * as h from './api_helpers';
import ErrorCodes from '../src/constants/error_codes';
import {scorm12_values} from './field_values';
import * as sinon from 'sinon';
import Pretender from 'fetch-pretender';

const scorm12_error_codes = ErrorCodes.scorm12;

let clock;
const api = (settings = {}) => {
const API = new Scorm12API(settings);
API.apiLogLevel = 1;
Expand All @@ -19,6 +22,24 @@ const apiInitialized = (settings = {}) => {
};

describe('SCORM 1.2 API Tests', () => {
before(() => {
clock = sinon.useFakeTimers();

const server = new Pretender(() => {
});
server.post('/scorm12', () => {
return [200, {'Content-Type': 'application/json'}, '{}'];
}, false);

server.post('/scorm12/error', () => {
return [500, {'Content-Type': 'application/json'}, '{}'];
}, false);
});

after(() => {
clock.restore();
});

describe('LMSSetValue()', () => {
h.checkValidValues({
api: apiInitialized(),
Expand Down Expand Up @@ -393,4 +414,55 @@ describe('SCORM 1.2 API Tests', () => {
expect(scorm12API.cmi.core.lesson_status).to.equal('passed');
});
});

describe('Event Handlers', () => {
it('Should handle SetValue.cmi.core.student_name event',
() => {
const scorm12API = apiInitialized();
const callback = sinon.spy();
scorm12API.on('LMSSetValue.cmi.core.student_name', callback);
scorm12API.lmsSetValue('cmi.core.student_name', '@jcputney');
expect(callback.called).to.be.true;
});
it('Should handle SetValue.cmi.* event',
() => {
const scorm12API = apiInitialized();
const callback = sinon.spy();
scorm12API.on('LMSSetValue.cmi.*', callback);
scorm12API.lmsSetValue('cmi.core.student_name', '@jcputney');
expect(callback.called).to.be.true;
});
it('Should handle CommitSuccess event',
() => {
const scorm12API = api({
lmsCommitUrl: '/scorm12',
autocommit: true,
autocommitSeconds: 1,
});
scorm12API.lmsInitialize();

const callback = sinon.spy();
scorm12API.on('CommitSuccess', callback);

scorm12API.lmsSetValue('cmi.core.session_time', '00:01:00');
clock.tick(2000);
expect(callback.called).to.be.true;
});
it('Should handle CommitError event',
() => {
const scorm12API = api({
lmsCommitUrl: '/scorm12/error',
autocommit: true,
autocommitSeconds: 1,
});
scorm12API.lmsInitialize();

const callback = sinon.spy();
scorm12API.on('CommitError', callback);

scorm12API.lmsSetValue('cmi.core.session_time', '00:01:00');
clock.tick(2000);
expect(callback.called).to.be.true;
});
});
});
Loading

0 comments on commit 7f1d995

Please sign in to comment.