Skip to content

Commit

Permalink
Merge pull request #188 from mdenet/fix/unittests-toolmanager
Browse files Browse the repository at this point in the history
Unit tests for ToolManager
  • Loading branch information
barnettwilliam authored Mar 6, 2024
2 parents 863a27f + 0f8cd9c commit cc2f19f
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 12 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions platform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"copyfiles": "^2.4.1",
"eslint": "^8.56.0",
"jasmine": "^4.5.0",
"jasmine-ajax": "^4.0.0",
"karma": "^6.4.1",
"karma-chrome-launcher": "^3.1.1",
"karma-coverage": "^2.2.1",
Expand Down
2 changes: 1 addition & 1 deletion platform/src/ToolsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class ToolManager {
/**
* Find a panel definition by id
* @param {string} panelDefId
* @returns {PanelDefinition|null} the found panel definition
* @returns {Object|null} the found panel definition
*/
getPanelDefinition(panelDefId) {

Expand Down
183 changes: 172 additions & 11 deletions platform/test/spec/testToolManagerSpec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,179 @@
/*global describe, it, expect, spyOn, beforeEach, afterEach -- functions provided by Jasmine */
/*global jasmine -- object provided by Jasmine */

import {ToolManager} from "../../src/ToolsManager.js"

import { ToolManager } from "../../src/ToolsManager.js";
import { TOOL_1PANELDEF_1FUNCTION } from "../resources/TestToolFiles.js";
import "jasmine-ajax"

describe("ToolManager", () => {

describe("constructor", () => {
it("can be created", () => {

var tm = new ToolManager(["test-url"]);

var tmType = typeof tm;

expect(tmType).toBe("object");

})

// Call the target object
let tm = new ToolManager();

// Check the expected results
expect(tm).toBeInstanceOf(ToolManager);
})
})

describe("initialisation", () => {
// Setup
const TOOL_URLS= [ "test://t1.url/tool-config.json",
"test://t2.url/tool-config.json",
"test://t3.url/tool-config.json" ];
let tm;

beforeEach( () => {
jasmine.Ajax.install();

spyOn(XMLHttpRequest.prototype, "open").and.callThrough();
spyOn(XMLHttpRequest.prototype, "send").and.callThrough();

tm = new ToolManager();
})

afterEach(function() {
jasmine.Ajax.uninstall();
});

it("sets tools URLs correctly", () => {
// Call the target object
tm.setToolsUrls(TOOL_URLS);

// Check the expected results
expect(tm.toolsUrls).toHaveSize(TOOL_URLS.length);

for (const i in TOOL_URLS){
expect(tm.toolsUrls[i].url).toEqual(TOOL_URLS[i]);
}
})

it("fetches the tool configuration from the remote given by its URL", () => {
// Call the target object
tm.setToolsUrls(TOOL_URLS);

// Check the expected results
expect(XMLHttpRequest.prototype.open).toHaveBeenCalledWith("GET", TOOL_URLS[0],false);
})

it("parses and stores the tool configuration", () => {
jasmine.Ajax.stubRequest('test://t1.url/tool-config.json').andReturn({
"responseText": TOOL_1PANELDEF_1FUNCTION,
"status": 200
});

// Call the target object
tm.setToolsUrls(TOOL_URLS);

// Check the expected results
const TOOL1_ID= "tool-1"

expect(tm.tools[TOOL1_ID]).toBeInstanceOf(Object);
const EXPECTED_TOOL_KEYS = ["id", "name","version", "author", "homepage", "functions", "panelDefs"];

const storedToolKeys = Object.keys(tm.tools[TOOL1_ID]);
for (let key of EXPECTED_TOOL_KEYS){
expect( storedToolKeys.find(n => n===key ) ).toEqual(key);
}
})
})

describe("panel definitions", () => {
// Setup
const TOOL_URLS= [ "test://t1.url/tool-config.json" ];
const PANEL_DEF_ID = "paneldef-t1";
let tm;

beforeEach( () => {
jasmine.Ajax.install();

spyOn(XMLHttpRequest.prototype, "open").and.callThrough();
spyOn(XMLHttpRequest.prototype, "send").and.callThrough();

tm = new ToolManager();
})

afterEach(function() {
jasmine.Ajax.uninstall();
});

it ("returns the correct object for an existing panel definition id", () => {
jasmine.Ajax.stubRequest('test://t1.url/tool-config.json').andReturn({
"responseText": TOOL_1PANELDEF_1FUNCTION,
"status": 200
});
tm.setToolsUrls(TOOL_URLS);

// Call the target object
const foundPanelDef= tm.getPanelDefinition(PANEL_DEF_ID);

// Check the expected results
const toolConfig = JSON.parse(TOOL_1PANELDEF_1FUNCTION);
const expectedPanelDef = toolConfig.tool.panelDefs[0];

expect(foundPanelDef).toBeInstanceOf(Object);
expect(foundPanelDef).toEqual(expectedPanelDef);
})

it ("returns null for a panel definition id that does not exist", () => {
jasmine.Ajax.stubRequest('test://t1.url/tool-config.json').andReturn({
"responseText": TOOL_1PANELDEF_1FUNCTION,
"status": 200
});
tm.setToolsUrls(TOOL_URLS);

// Call the target object
const foundPanelDef= tm.getPanelDefinition("X");

// Check the expected results
expect(foundPanelDef).toEqual(null)
})

})

describe("tool grammar imports", () => {
// Setup
const TOOL_BASE_URL = "test://t1.url";
const TOOL_URL= TOOL_BASE_URL + "/tool-config.json";
const TOOL_LANGUAGE_NAME = "test";
let tm;

beforeEach( () => {
jasmine.Ajax.install();

spyOn(XMLHttpRequest.prototype, "open").and.callThrough();
spyOn(XMLHttpRequest.prototype, "send").and.callThrough();

tm = new ToolManager();

jasmine.Ajax.stubRequest(TOOL_URL).andReturn({
"responseText": TOOL_1PANELDEF_1FUNCTION,
"status": 200
});

tm.setToolsUrls([TOOL_URL]);
})

afterEach(function() {
jasmine.Ajax.uninstall();
});

it ("correctly loads the URL of the ace grammar for a defined tool", () => {
// Call the target object
const grammarImports = tm.getToolsGrammarImports();

// Check the expected results
expect(grammarImports[0].url).toEqual( TOOL_BASE_URL + "/highlighting.js");
})


it ("correctly loads the ace grammar module for a defined tool", () => {
// Call the target object
const grammarImports = tm.getToolsGrammarImports();

// Check the expected results
expect(grammarImports[0].module).toEqual( "ace/mode/"+ TOOL_LANGUAGE_NAME);
})
})
})

0 comments on commit cc2f19f

Please sign in to comment.