Skip to content

Commit

Permalink
Added tests for config validators
Browse files Browse the repository at this point in the history
  • Loading branch information
barnettwilliam committed Mar 7, 2024
1 parent cc2f19f commit 41e86c3
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
84 changes: 84 additions & 0 deletions platform/test/spec/testActivityConfigValidatorSpec.js
Original file line number Diff line number Diff line change
@@ -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);
}
84 changes: 84 additions & 0 deletions platform/test/spec/testToolConfigValidatorSpec.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 41e86c3

Please sign in to comment.