Handling non-string values being passed to SetValue/LMSSetValue
This commit is contained in:
@@ -169,6 +169,9 @@ export default class BaseAPI {
|
||||
checkTerminated: boolean,
|
||||
CMIElement,
|
||||
value) {
|
||||
if (value !== undefined) {
|
||||
value = String(value);
|
||||
}
|
||||
let returnValue = global_constants.SCORM_FALSE;
|
||||
|
||||
if (this.checkState(checkTerminated, this.#error_codes.STORE_BEFORE_INIT,
|
||||
@@ -181,6 +184,7 @@ export default class BaseAPI {
|
||||
this.lastErrorCode = e.errorCode;
|
||||
returnValue = global_constants.SCORM_FALSE;
|
||||
} else {
|
||||
console.error(e.getMessage());
|
||||
this.throwSCORMError(this.#error_codes.GENERAL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +132,12 @@ export const scorm12_values = {
|
||||
validScoreRange: [
|
||||
'1',
|
||||
'50.25',
|
||||
'70',
|
||||
'100',
|
||||
1,
|
||||
50.25,
|
||||
70,
|
||||
100,
|
||||
],
|
||||
invalidScoreRange: [
|
||||
'invalid',
|
||||
|
||||
@@ -3,6 +3,7 @@ import {describe, it} from 'mocha';
|
||||
import Scorm12API from '../src/Scorm12API';
|
||||
import * as h from './api_helpers';
|
||||
import {scorm12_error_codes} from '../src/constants/error_codes';
|
||||
import {scorm12_values} from '../src/constants/field_values';
|
||||
|
||||
const api = (settings = {}) => {
|
||||
const API = new Scorm12API(settings);
|
||||
@@ -16,6 +17,27 @@ const apiInitialized = (settings = {}) => {
|
||||
};
|
||||
|
||||
describe('SCORM 1.2 API Tests', () => {
|
||||
describe('LMSSetValue()', () => {
|
||||
h.checkValidValues({
|
||||
api: apiInitialized(),
|
||||
fieldName: 'cmi.core.score.raw',
|
||||
validValues: scorm12_values.validScoreRange,
|
||||
invalidValues: scorm12_values.invalidScoreRange,
|
||||
});
|
||||
h.checkValidValues({
|
||||
api: apiInitialized(),
|
||||
fieldName: 'cmi.core.score.min',
|
||||
validValues: scorm12_values.validScoreRange,
|
||||
invalidValues: scorm12_values.invalidScoreRange,
|
||||
});
|
||||
h.checkValidValues({
|
||||
api: apiInitialized(),
|
||||
fieldName: 'cmi.core.score.max',
|
||||
validValues: scorm12_values.validScoreRange,
|
||||
invalidValues: scorm12_values.invalidScoreRange,
|
||||
});
|
||||
});
|
||||
|
||||
describe('setCMIValue()', () => {
|
||||
describe('Invalid Sets - Should Always Fail', () => {
|
||||
h.checkSetCMIValue({
|
||||
@@ -280,7 +302,7 @@ describe('SCORM 1.2 API Tests', () => {
|
||||
scorm12API.cmi.core.session_time = '23:59:59';
|
||||
const cmiExport = scorm12API.renderCommitCMI(true);
|
||||
expect(
|
||||
cmiExport.cmi.core.total_time
|
||||
cmiExport.cmi.core.total_time,
|
||||
).to.equal('36:34:55');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@ import {describe, it} from 'mocha';
|
||||
import * as h from './api_helpers';
|
||||
import {scorm2004_error_codes} from '../src/constants/error_codes';
|
||||
import Scorm2004API from '../src/Scorm2004API';
|
||||
import {scorm2004_values} from '../src/constants/field_values';
|
||||
import {scorm12_values, scorm2004_values} from '../src/constants/field_values';
|
||||
|
||||
const api = () => {
|
||||
const API = new Scorm2004API();
|
||||
@@ -17,6 +17,33 @@ const apiInitialized = () => {
|
||||
};
|
||||
|
||||
describe('SCORM 2004 API Tests', () => {
|
||||
describe('SetValue()', () => {
|
||||
h.checkValidValues({
|
||||
api: apiInitialized(),
|
||||
fieldName: 'cmi.score.scaled',
|
||||
validValues: scorm2004_values.validScaledRange,
|
||||
invalidValues: scorm2004_values.invalidScaledRange,
|
||||
});
|
||||
h.checkValidValues({
|
||||
api: apiInitialized(),
|
||||
fieldName: 'cmi.score.raw',
|
||||
validValues: scorm2004_values.validScoreRange,
|
||||
invalidValues: scorm2004_values.invalidScoreRange,
|
||||
});
|
||||
h.checkValidValues({
|
||||
api: apiInitialized(),
|
||||
fieldName: 'cmi.score.min',
|
||||
validValues: scorm2004_values.validScoreRange,
|
||||
invalidValues: scorm2004_values.invalidScoreRange,
|
||||
});
|
||||
h.checkValidValues({
|
||||
api: apiInitialized(),
|
||||
fieldName: 'cmi.score.max',
|
||||
validValues: scorm2004_values.validScoreRange,
|
||||
invalidValues: scorm2004_values.invalidScoreRange,
|
||||
});
|
||||
});
|
||||
|
||||
describe('setCMIValue()', () => {
|
||||
describe('Invalid Sets - Should Always Fail', () => {
|
||||
h.checkSetCMIValue({
|
||||
|
||||
@@ -1,6 +1,36 @@
|
||||
import {describe, it} from 'mocha';
|
||||
import {expect} from 'chai';
|
||||
|
||||
export const checkValidValues = (
|
||||
{
|
||||
api,
|
||||
fieldName,
|
||||
validValues,
|
||||
invalidValues,
|
||||
}) => {
|
||||
describe(`Field: ${fieldName}`, () => {
|
||||
for (const idx in validValues) {
|
||||
if ({}.hasOwnProperty.call(validValues, idx)) {
|
||||
it(`Should successfully write '${validValues[idx]}' to ${fieldName}`,
|
||||
() => {
|
||||
expect(api.lmsSetValue(fieldName, validValues[idx])).
|
||||
to.equal('true');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const idx in invalidValues) {
|
||||
if ({}.hasOwnProperty.call(invalidValues, idx)) {
|
||||
it(`Should fail to write '${invalidValues[idx]}' to ${fieldName}`,
|
||||
() => {
|
||||
expect(api.lmsSetValue(fieldName, invalidValues[idx])).
|
||||
to.equal('false');
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const checkLMSSetValue = (
|
||||
{
|
||||
api,
|
||||
|
||||
Reference in New Issue
Block a user