From cac21ebc202010079d8a2449087b55183f98029c Mon Sep 17 00:00:00 2001 From: Will Barnett Date: Tue, 5 Mar 2024 12:37:33 +0000 Subject: [PATCH] Added ActivityManager tests for getting activity and improved the naming of tests. --- platform/test/resources/TestActivityFiles.js | 6 +- platform/test/resources/TestToolFiles.js | 46 ++++++++++++ platform/test/spec/testActivityManagerSpec.js | 70 +++++++++++++++++-- 3 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 platform/test/resources/TestToolFiles.js diff --git a/platform/test/resources/TestActivityFiles.js b/platform/test/resources/TestActivityFiles.js index dd85beb..92f6c38 100644 --- a/platform/test/resources/TestActivityFiles.js +++ b/platform/test/resources/TestActivityFiles.js @@ -15,12 +15,14 @@ export const ACTIVITY_2PANELS_1ACTION = `{ { "id": "panel-1", "name": "Panel 1", - "ref": "x" + "ref": "paneldef-t1", + "file": "file1.ext" }, { "id": "panel-2", "name": "Panel 2", - "ref": "x" + "ref": "paneldef-t1", + "file": "file2.ext" } ], "actions": [ diff --git a/platform/test/resources/TestToolFiles.js b/platform/test/resources/TestToolFiles.js new file mode 100644 index 0000000..f70bcc9 --- /dev/null +++ b/platform/test/resources/TestToolFiles.js @@ -0,0 +1,46 @@ +export const TOOL_1PANELDEF_1FUNCTION = `{ + "tool": + { + "id": "tool-1", + "name": "Tool 1", + "version": "0.0.0", + "author": "Test", + "homepage": "test://t.url", + "functions": [ + { + "id": "function-1", + "name": "Function 1", + "parameters": [ {"name":"parameter1", "type":"type1"}, + {"name":"paremeter2", "type":"type2"}, + {"name":"language", "type":"string"} ], + + "returnType": "text", + "path": "test://tool1.url" + } + ], + "panelDefs": [ + { + "id": "paneldef-t1", + "name": "Panel Definition Tool 1", + "panelclass": "ProgramPanel", + "icon": "icon", + "language": "test", + "buttons" : [ + { + "id": "action-button", + "icon": "run", + "actionfunction": "function-1", + "hint": "Run the program" + }, + { + "id": "help-button", + "icon": "info", + "url": "test://t1.url/help", + "hint": "Tool 1 Help" + } + ] + } + ] + } +} +` \ No newline at end of file diff --git a/platform/test/spec/testActivityManagerSpec.js b/platform/test/spec/testActivityManagerSpec.js index 55b03b3..37cd6cc 100644 --- a/platform/test/spec/testActivityManagerSpec.js +++ b/platform/test/spec/testActivityManagerSpec.js @@ -4,8 +4,9 @@ import {ActivityManager} from "../../src/ActivityManager.js"; import { FileHandler } from "../../src/FileHandler.js"; import { ActivityConfigValidator } from "../../src/ActivityConfigValidator.js"; import { utility } from "../../src/Utility.js"; -import { ACTIVITY_2PANELS_1ACTION } from "../resources/TestActivityFiles.js" -import { configObjectEquals, configToString } from "../resources/TestUtility.js" +import { ACTIVITY_2PANELS_1ACTION } from "../resources/TestActivityFiles.js"; +import { TOOL_1PANELDEF_1FUNCTION } from "../resources/TestToolFiles.js"; +import { configObjectEquals, configToString } from "../resources/TestUtility.js"; describe("ActivityManager", () => { @@ -157,6 +158,7 @@ describe("ActivityManager", () => { const ACTIVITY_ID = "activity-1"; const PANEL1_ID = "panel-1"; const PANEL2_ID = "panel-2"; + const PANEL_DEINITION_ID = "paneldef-t1"; it("current activity activityId property is set to the activity id", () => { expect(am.activityId).toEqual(ACTIVITY_ID); @@ -175,34 +177,88 @@ describe("ActivityManager", () => { expect(am.activities[ACTIVITY_ID]).toBeInstanceOf(Object); }) - it("the activities property's value object has required keys", () => { - const EXPECTED_KEYS = ["actions", "icon", "id", "layout", "panels", "title", "tools"]; + it("the activities property is an object that has required keys", () => { + const REQUIRED_KEYS = ["actions", "icon", "id", "layout", "panels", "title", "tools"]; const activityObject = am.activities[ACTIVITY_ID]; let parsedActivityKeys = Object.keys(activityObject); - expect(parsedActivityKeys).toHaveSize(EXPECTED_KEYS.length) - for(const k of EXPECTED_KEYS){ + expect(parsedActivityKeys).toHaveSize(REQUIRED_KEYS.length) + for(const k of REQUIRED_KEYS){ if ( !parsedActivityKeys.find( (n) => (n == k) ) ){ fail("Expected key '" + k + "' not found in activity object."); } } }) - it("the activities property's value object action panels are resolved", () => { + it("the activities actions property panel references have been resolved", () => { const expectedActivity = JSON.parse(activityFile).activities[0]; const expectedPanelSource = expectedActivity.panels.find((p) => p.id===PANEL1_ID); const expectedPanelOutput = expectedActivity.panels.find((p) => p.id===PANEL2_ID); const action = am.activities[ACTIVITY_ID].actions[0]; + expect(action.source).toBeInstanceOf(Object); if (!configObjectEquals( action.source, expectedPanelSource )) { fail("Expected action's source panel'" + configToString(action.source) + "' to equal'" + configToString(expectedPanelSource) + "'"); } + expect(action.output).toBeInstanceOf(Object); if (!configObjectEquals( action.output, expectedPanelOutput )){ fail("Expected action's source panel'" + configToString(action.output) + "' to equal'" + configToString(expectedPanelOutput) + "'" ); } }) + + it("the panel definitions the current activity references are unresolved", () => { + expect (am.activities[ACTIVITY_ID].panels[0].ref).toEqual(PANEL_DEINITION_ID); + expect (am.activities[ACTIVITY_ID].panels[1].ref).toEqual(PANEL_DEINITION_ID); + }) + }) + + + describe("getting current activity", () => { + const ACTIVITY_URL="http://a.url"; + const QUERY="?activities=" + ACTIVITY_URL; + let fileh; + let refPanelDef; + let am; + const activityFile = ACTIVITY_2PANELS_1ACTION; + const toolConfig = JSON.parse(TOOL_1PANELDEF_1FUNCTION).tool; + const panelDef = toolConfig.panelDefs[0]; + + beforeEach( () => { + // Setup + fileh = new FileHandler("test://th.url"); + refPanelDef = () => { + return panelDef; + }; + + spyOn( utility, "getWindowLocationSearch").and.returnValue(QUERY); + spyOn(FileHandler.prototype, "fetchFile").and.returnValue({ content: activityFile }); + spyOn(ActivityConfigValidator.prototype, "validateConfigFile").and.returnValue([]); + spyOn(ActivityManager.prototype, "appendTopLevelActivityMenuItem"); // Uses document.getElementById + spyOn(ActivityManager.prototype, "fetchFile").and.returnValue({content: "Test Content", sha: "a123"}); + + // Call the target object + am = new ActivityManager(refPanelDef , fileh); + am.initializeActivities(); + am.getSelectedActivity(); + }) + + // Check the expected results + const ACTIVITY_ID = "activity-1"; + + it("the panel definitions for the current activity references are resolved", () => { + expect(am.activities[ACTIVITY_ID].panels[0].ref).toBeInstanceOf(Object); + expect(am.activities[ACTIVITY_ID].panels[0].ref).toEqual(toolConfig.panelDefs[0]) + + expect(am.activities[ACTIVITY_ID].panels[1].ref).toBeInstanceOf(Object); + expect(am.activities[ACTIVITY_ID].panels[1].ref).toEqual(toolConfig.panelDefs[0]); + }) + + it("the activity files are fetched using FileHandler", () => { + expect(am.fetchFile.calls.allArgs()).toEqual( [["file1.ext"], ["file2.ext"] ]); + }) + }) })