From 41e86c31ae5262c5eb9f079fe3cac22540d4465e Mon Sep 17 00:00:00 2001 From: Will Barnett Date: Thu, 7 Mar 2024 09:48:08 +0000 Subject: [PATCH] Added tests for config validators --- .../spec/testActivityConfigValidatorSpec.js | 84 +++++++++++++++++++ .../test/spec/testToolConfigValidatorSpec.js | 84 +++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 platform/test/spec/testActivityConfigValidatorSpec.js create mode 100644 platform/test/spec/testToolConfigValidatorSpec.js diff --git a/platform/test/spec/testActivityConfigValidatorSpec.js b/platform/test/spec/testActivityConfigValidatorSpec.js new file mode 100644 index 0000000..42e45ea --- /dev/null +++ b/platform/test/spec/testActivityConfigValidatorSpec.js @@ -0,0 +1,84 @@ +/*global describe, it, expect, beforeEach -- functions provided by Jasmine */ +import { ActivityConfigValidator } from "../../src/ActivityConfigValidator.js" +import { ACTIVITY_2PANELS_1ACTION } from "../resources/TestActivityFiles.js"; +import { ConfigValidationError } from "../../src/ConfigValidationError.js" + + +const EXPECTED_FILE_TYPE = "ActivityConfig"; + +describe("ActivityConfigValidator", () => { + + describe("constructor", () => { + it("can be created", () => { + // Call the target object + let acv = new ActivityConfigValidator(); + + // Check the expected results + expect(acv).toBeInstanceOf(ActivityConfigValidator); + }) + }) + + describe("validate tool configration", () => { + let acv; + let activityConfig; + + //Setup + beforeEach( () => { + activityConfig = JSON.parse(ACTIVITY_2PANELS_1ACTION); + acv = new ActivityConfigValidator(); + }) + + it("reports no errors for a valid config", () => { + // Call the target object + let errors = acv.validateConfigFile(activityConfig); + + // Check the expected results + expect(errors).toHaveSize(0); + }) + + it("returns errors that are ConfigValidationError instances",() => { + delete activityConfig.activities[0].id; + + // Call the target object + let errors = acv.validateConfigFile(activityConfig); + let e = errors[0]; + + // Check the expected results + expect(e).toBeInstanceOf(ConfigValidationError); + expect(e.fileType).toEqual(EXPECTED_FILE_TYPE); + }) + + it("returns an error if the config has no id key", () => { + delete activityConfig.activities[0].id; + + // Call the target object + let errors = acv.validateConfigFile(activityConfig); + let e = errors[0]; + + // Check the expected results + expect(errors).toHaveSize(1); + checkErrorPopulated(e); + }) + + it("returns an error if a panel in config has no id key", () => { + delete activityConfig.activities[0].panels[1].id; + + // Call the target object + let errors = acv.validateConfigFile(activityConfig); + let e = errors[0]; + + // Check the expected results + expect(errors).toHaveSize(1); + checkErrorPopulated(e); + }) + }) +}) + +function checkErrorPopulated(error){ + const MIN_LENGTH = 3; + expect(error).toBeInstanceOf(ConfigValidationError); + expect(error.fileType).toEqual(EXPECTED_FILE_TYPE); + expect(error.location.length).toBeGreaterThanOrEqual(MIN_LENGTH); + expect(error.category.length).toBeGreaterThanOrEqual(MIN_LENGTH); + expect(error.message.length).toBeGreaterThanOrEqual(MIN_LENGTH); +} \ No newline at end of file diff --git a/platform/test/spec/testToolConfigValidatorSpec.js b/platform/test/spec/testToolConfigValidatorSpec.js new file mode 100644 index 0000000..d206c69 --- /dev/null +++ b/platform/test/spec/testToolConfigValidatorSpec.js @@ -0,0 +1,84 @@ +/*global describe, it, expect, beforeEach -- functions provided by Jasmine */ +import { ToolConfigValidator } from "../../src/ToolConfigValidator" +import { TOOL_1PANELDEF_1FUNCTION } from "../resources/TestToolFiles.js"; +import { ConfigValidationError } from "../../src/ConfigValidationError.js" + + +const EXPECTED_FILE_TYPE = "ToolConfig"; + +describe("ToolConfigValidator", () => { + + describe("constructor", () => { + it("can be created", () => { + // Call the target object + let tcv = new ToolConfigValidator(); + + // Check the expected results + expect(tcv).toBeInstanceOf(ToolConfigValidator); + }) + }) + + describe("validate tool configration", () => { + let tcv; + let toolConfig; + + //Setup + beforeEach( () => { + toolConfig = JSON.parse(TOOL_1PANELDEF_1FUNCTION); + tcv = new ToolConfigValidator(); + }) + + it("reports no errors for a valid config", () => { + // Call the target object + let errors = tcv.validateConfigFile(toolConfig); + + // Check the expected results + expect(errors).toHaveSize(0); + }) + + it("returns errors that are ConfigValidationError instances",() => { + delete toolConfig.tool.id; + + // Call the target object + let errors = tcv.validateConfigFile(toolConfig); + let e = errors[0]; + + // Check the expected results + expect(e).toBeInstanceOf(ConfigValidationError); + expect(e.fileType).toEqual(EXPECTED_FILE_TYPE); + }) + + it("returns an error if the config has no id key", () => { + delete toolConfig.tool.id; + + // Call the target object + let errors = tcv.validateConfigFile(toolConfig); + let e = errors[0]; + + // Check the expected results + expect(errors).toHaveSize(1); + checkErrorPopulated(e); + }) + + it("returns an error if a panel definition in the config has no id key", () => { + delete toolConfig.tool.panelDefs[0].id; + + // Call the target object + let errors = tcv.validateConfigFile(toolConfig); + let e = errors[0]; + + // Check the expected results + expect(errors).toHaveSize(1); + checkErrorPopulated(e); + }) + }) +}) + +function checkErrorPopulated(error){ + const MIN_LENGTH = 3; + expect(error).toBeInstanceOf(ConfigValidationError); + expect(error.fileType).toEqual(EXPECTED_FILE_TYPE); + expect(error.location.length).toBeGreaterThanOrEqual(MIN_LENGTH); + expect(error.category.length).toBeGreaterThanOrEqual(MIN_LENGTH); + expect(error.message.length).toBeGreaterThanOrEqual(MIN_LENGTH); +} \ No newline at end of file