Skip to content

Commit

Permalink
Adding default value for loadFromJSON for issue #767
Browse files Browse the repository at this point in the history
  • Loading branch information
jcputney committed Oct 29, 2024
1 parent 0a5fa75 commit 757fc13
Show file tree
Hide file tree
Showing 35 changed files with 135 additions and 27 deletions.
1 change: 1 addition & 0 deletions dist/aicc.js

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

2 changes: 1 addition & 1 deletion dist/aicc.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aicc.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aicc.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/esm/aicc.js

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

2 changes: 1 addition & 1 deletion dist/esm/aicc.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/aicc.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/aicc.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/esm/scorm-again.js

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

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/esm/scorm12.js

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

2 changes: 1 addition & 1 deletion dist/esm/scorm12.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/scorm12.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/scorm12.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/esm/scorm2004.js

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

2 changes: 1 addition & 1 deletion dist/esm/scorm2004.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/scorm2004.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/scorm2004.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/scorm-again.js

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

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/scorm12.js

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

2 changes: 1 addition & 1 deletion dist/scorm12.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/scorm12.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/scorm12.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/scorm2004.js

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

2 changes: 1 addition & 1 deletion dist/scorm2004.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/scorm2004.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/scorm2004.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scorm-again",
"version": "2.4.0",
"version": "2.4.1",
"description": "A modern SCORM JavaScript run-time library for AICC, SCORM 1.2, and SCORM 2004",
"main": "dist/scorm-again.js",
"types": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/BaseAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ export default abstract class BaseAPI implements IBaseAPI {
* @param {RefObject} json
* @param {string} CMIElement
*/
loadFromJSON(json: RefObject, CMIElement: string) {
loadFromJSON(json: RefObject, CMIElement: string = "") {
if (!this.isNotInitialized()) {
console.error(
"loadFromJSON can only be called before the call to lmsInitialize.",
Expand Down
102 changes: 101 additions & 1 deletion test/Scorm12API.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const api = (settings?: Settings, startingData: RefObject = {}) => {
};
const apiInitialized = (settings?: Settings, startingData: RefObject = {}) => {
const API = api(settings);
API.loadFromJSON(startingData ? startingData : {}, "");
API.loadFromJSON(startingData ? startingData : {});
API.lmsInitialize();
return API;
};
Expand Down Expand Up @@ -51,6 +51,106 @@ describe("SCORM 1.2 API Tests", () => {
clock.restore();
});

describe("loadFromJSON()", () => {
it("should load JSON data into the CMI object", () => {
const scorm12API = api();
const jsonData = {
cmi: {
core: {
student_id: "student_1",
student_name: "John Doe",
lesson_status: "incomplete",
},
},
};

scorm12API.loadFromJSON(jsonData);
scorm12API.lmsInitialize();

expect(scorm12API.cmi.core.student_id).toEqual("student_1");
expect(scorm12API.cmi.core.student_name).toEqual("John Doe");
expect(scorm12API.cmi.core.lesson_status).toEqual("incomplete");
});

it("should load nested JSON data into the CMI object", () => {
const scorm12API = api();
const jsonData = {
cmi: {
objectives: {
"0": {
id: "obj_1",
score: {
raw: "85",
min: "0",
max: "100",
},
},
},
},
};

scorm12API.loadFromJSON(jsonData);

expect(scorm12API.cmi.objectives.childArray[0].id).toEqual("obj_1");
expect(scorm12API.cmi.objectives.childArray[0].score.raw).toEqual("85");
expect(scorm12API.cmi.objectives.childArray[0].score.min).toEqual("0");
expect(scorm12API.cmi.objectives.childArray[0].score.max).toEqual("100");
});

it("should load nested cmi JSON data into the CMI object", () => {
const scorm12API = api();
const jsonData = {
objectives: {
"0": {
id: "obj_1",
score: {
raw: "85",
min: "0",
max: "100",
},
},
},
};

scorm12API.loadFromJSON(jsonData, "cmi");

expect(scorm12API.cmi.objectives.childArray[0].id).toEqual("obj_1");
expect(scorm12API.cmi.objectives.childArray[0].score.raw).toEqual("85");
expect(scorm12API.cmi.objectives.childArray[0].score.min).toEqual("0");
expect(scorm12API.cmi.objectives.childArray[0].score.max).toEqual("100");
});

it("should handle empty JSON data", () => {
const scorm12API = api();
const jsonData = {};

scorm12API.loadFromJSON(jsonData);

expect(scorm12API.cmi.core.student_id).toBeFalsy();
expect(scorm12API.cmi.core.student_name).toBeFalsy();
expect(scorm12API.cmi.core.lesson_status).toBe("not attempted");
});

it("should not load data if API is initialized", () => {
const scorm12API = apiInitialized();
const jsonData = {
cmi: {
core: {
student_id: "student_1",
student_name: "John Doe",
lesson_status: "incomplete",
},
},
};

scorm12API.loadFromJSON(jsonData);

expect(scorm12API.cmi.core.student_id).toBeFalsy();
expect(scorm12API.cmi.core.student_name).toBeFalsy();
expect(scorm12API.cmi.core.lesson_status).toBe("not attempted");
});
});

describe("LMSSetValue()", () => {
h.checkValidValues({
api: apiInitialized(),
Expand Down

0 comments on commit 757fc13

Please sign in to comment.