Skip to content

Commit

Permalink
Moved notification functions to PlaygroundUtility for use by ErrorHan…
Browse files Browse the repository at this point in the history
…dler
  • Loading branch information
barnettwilliam committed Apr 10, 2024
1 parent 603acd3 commit bbabad1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 143 deletions.
35 changes: 8 additions & 27 deletions platform/src/EducationPlatformApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class EducationPlatformApp {
constructor() {
this.outputType = "text";
this.outputLanguage = "text";
this.errorHandler = new ErrorHandler(this.errorNotification.bind(this));
this.errorHandler = new ErrorHandler();
this.preloader = new Preloader();
this.panels = [];
}
Expand Down Expand Up @@ -459,7 +459,7 @@ class EducationPlatformApp {

if (response.editorUrl) {
// Language workbench
this.longNotification("Building editor");
PlaygroundUtility.longNotification("Building editor");
this.checkEditorReady( response.editorStatusUrl, response.editorUrl, action.source.editorPanel, action.source.editorActivity, outputConsole);


Expand Down Expand Up @@ -601,7 +601,7 @@ class EducationPlatformApp {

this.handleResponseActionFunction(action , actionResultPromise);

this.longNotification("Executing program");
PlaygroundUtility.longNotification("Executing program");
}
}

Expand All @@ -613,26 +613,7 @@ class EducationPlatformApp {
this.toggle(parentElement.id);
}
}


notification(title, message, cls="light"){
const crossIcon = "<div class=\"default-icon-cross\" style=\"float:right\"></div>"
Metro.notify.create(crossIcon + "<b>" + title + "</b>" + "<br>" + message + "<br>", null, {keepOpen: true, cls: cls, width: 300});
}

longNotification(title, cls="light") {
this.notification(title + "...", "This may take a few seconds to complete if the back end is not warmed up.", cls);
}

successNotification(message, cls="light") {
this.notification("Success:", message, cls);
}

errorNotification(message) {
console.log("ERROR: " + message);
this.notification("Error:", message, "bg-red fg-white");
}



toggle(elementId, onEmpty) {
var element = document.getElementById(elementId);
Expand Down Expand Up @@ -711,7 +692,7 @@ class EducationPlatformApp {
}

Promise.all(fileStorePromises).then( () => {
this.successNotification("The activity panel contents have been saved.");
PlaygroundUtility.successNotification("The activity panel contents have been saved.");

}).catch( (err) => {
this.errorHandler.notify("An error occurred while trying to save the panel contents.", err);
Expand Down Expand Up @@ -743,7 +724,7 @@ class EducationPlatformApp {
sessionStorage.removeItem(editorPanelId);
this.activityManager.setActivityVisibility(editorActivityId, false);
Metro.notify.killAll();
this.notification("Build Failed", result.error, "ribbed-lightAmber");
PlaygroundUtility.notification("Build Failed", result.error, "ribbed-lightAmber");

} else if (!result.editorReady){
await new Promise(resolve => setTimeout(resolve, 2000));
Expand All @@ -755,12 +736,12 @@ class EducationPlatformApp {
sessionStorage.setItem( editorPanelId , editorInstanceUrl );
this.activityManager.setActivityVisibility(editorActivityId, true);
Metro.notify.killAll();
this.successNotification("Building complete.");
PlaygroundUtility.successNotification("Building complete.");
}

} else {
console.log("ERROR: The editor response could not be checked: " + statusUrl);
this.errorNotification("Failed to start the editor.");
PlaygroundUtility.errorNotification("Failed to start the editor.");
}
}
}
Expand Down
13 changes: 3 additions & 10 deletions platform/src/ErrorHandler.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { EducationPlatformError } from "./EducationPlatformError";

import { PlaygroundUtility } from "./PlaygroundUtility";
class ErrorHandler {

displayError;

/**
* @param {Function} notifier - The function to use for displaying error messages.
*/
constructor(notifier){

this.displayError = notifier;
constructor(){

window.onerror = (event, source, lineno, colno, err) => {

Expand Down Expand Up @@ -47,7 +40,7 @@ class ErrorHandler {
}
}

this.displayError(displayMessage);
PlaygroundUtility.errorNotification(displayMessage);
}

}
Expand Down
20 changes: 19 additions & 1 deletion platform/src/PlaygroundUtility.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

/*global Metro -- Metro is externally imported*/

class PlaygroundUtility {

Expand All @@ -23,6 +23,24 @@ class PlaygroundUtility {
document.getElementById("feedback-url").href = url;
}

static notification(title, message, cls="light"){
const crossIcon = "<div class=\"default-icon-cross\" style=\"float:right\"></div>"
Metro.notify.create(crossIcon + "<b>" + title + "</b>" + "<br>" + message + "<br>", null, {keepOpen: true, cls: cls, width: 300});
}

static longNotification(title, cls="light") {
this.notification(title + "...", "This may take a few seconds to complete if the back end is not warmed up.", cls);
}

static successNotification(message, cls="light") {
this.notification("Success:", message, cls);
}

static errorNotification(message) {
console.log("ERROR: " + message);
this.notification("Error:", message, "bg-red fg-white");
}

}

export { PlaygroundUtility };
3 changes: 2 additions & 1 deletion platform/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

/*global TOKEN_SERVER_URL -- is set by environment variable*/
import { EducationPlatformApp } from "./EducationPlatformApp.js";
import { PlaygroundUtility } from "./PlaygroundUtility.js";

const TOKEN_HANDLER_URL = TOKEN_SERVER_URL || "http://127.0.0.1:10000";
let urlParameters = new URLSearchParams(window.location.search);
Expand All @@ -19,5 +20,5 @@ window.savePanelContents = platform.savePanelContents.bind(platform);
window.toggle = platform.toggle.bind(platform);
window.togglePanelById = platform.togglePanelById.bind(platform);
//window.renderDiagram = renderDiagram;
window.longNotification = platform.longNotification.bind(platform);
window.longNotification = PlaygroundUtility.longNotification;
window.getPanelTitle = platform.getPanelTitle.bind(platform);
99 changes: 6 additions & 93 deletions platform/test/spec/testEducationPlatformAppSpec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*global describe, it, expect, spyOn, beforeEach, afterEach, expectAsync -- functions provided by Jasmine */
/*global describe, it, expect, spyOn, beforeEach, expectAsync -- functions provided by Jasmine */
/*global jasmine -- object provided by Jasmine */
/*global $ -- jquery is externally imported*/

export var TOKEN_SERVER_URL = "test://ts.url";
import {EducationPlatformApp} from "../../src/EducationPlatformApp.js";
import { ActionFunction } from "../../src/ActionFunction.js";
import { Panel } from "../../src/Panel.js";
import { ErrorHandler } from "../../src/ErrorHandler.js";
import "jasmine-ajax";
import { PlaygroundUtility } from "../../src/PlaygroundUtility.js";

describe("EducationPlatformApp", () => {

Expand Down Expand Up @@ -77,8 +77,8 @@ describe("EducationPlatformApp", () => {
spyOn(EducationPlatformApp.prototype, "handleResponseActionFunction");

// platform - notifications
spyOn(EducationPlatformApp.prototype, "longNotification");
spyOn(EducationPlatformApp.prototype, "errorNotification");
spyOn(PlaygroundUtility, "longNotification");
spyOn(PlaygroundUtility, "errorNotification");
spyOn(ErrorHandler.prototype, "notify");
})

Expand Down Expand Up @@ -120,8 +120,8 @@ describe("EducationPlatformApp", () => {
platform.runAction(PANEL_ID, BUTTON_ID);

// Check the expected results
expect(platform.longNotification).toHaveBeenCalledWith(jasmine.stringMatching('(E|e)xecuting'));
expect(platform.errorNotification).not.toHaveBeenCalled();
expect(PlaygroundUtility.longNotification).toHaveBeenCalledWith(jasmine.stringMatching('(E|e)xecuting'));
expect(PlaygroundUtility.errorNotification).not.toHaveBeenCalled();
})

it("raises an error when an action does not exist for a given panel using ErrorHandler notify", () => {
Expand Down Expand Up @@ -169,91 +169,4 @@ describe("EducationPlatformApp", () => {
})
})
})


describe("notification()", () => {
let platform;

const NOTIFICATION_TITLE = "ABC123";
const NOTIFICATION_MESSAGE = "DEF456";

beforeEach(()=>{
// Setup
platform = new EducationPlatformApp();

})

it("displays a message with given title and text", () => {

// Call the target object
platform.notification(NOTIFICATION_TITLE, NOTIFICATION_MESSAGE);

// Check the expected results
const documentMessages = $(".notify-message");

expect(documentMessages).toHaveSize(1);

expect(documentMessages.text()).toContain(NOTIFICATION_TITLE);
expect(documentMessages.text()).toContain(NOTIFICATION_MESSAGE);
})

afterEach( () => {
$(".notify-message").remove();
})
})

describe("longNotification()", () => {

const NOTIFICATION_TEXT = "ABC123";
const NOTIFICATION_MESSAGE = "may take a few seconds to complete";

it("calls notification() with the given text", () => {
// Setup
spyOn(EducationPlatformApp.prototype, "notification");
const platform = new EducationPlatformApp();

// Call the target object
platform.longNotification(NOTIFICATION_TEXT);

// Check the expected results
expect(platform.notification).toHaveBeenCalledWith(jasmine.stringMatching(NOTIFICATION_TEXT), jasmine.stringMatching(NOTIFICATION_MESSAGE), jasmine.anything());
})
})

describe("successNotification()", () => {

const NOTIFICATION_TEXT = "ABC123";
const NOTIFICATION_TITLE = "Success";

it("calls notification with the given text", () => {
// Setup
spyOn(EducationPlatformApp.prototype, "notification");
const platform = new EducationPlatformApp();

// Call the target object
platform.successNotification(NOTIFICATION_TEXT);

// Check the expected results
expect(platform.notification).toHaveBeenCalledWith(jasmine.stringMatching(NOTIFICATION_TITLE), jasmine.stringMatching(NOTIFICATION_TEXT), jasmine.anything());
})
})

describe("errorNotification()", () => {

const NOTIFICATION_TEXT = "ABC123";
const NOTIFICATION_TITLE = "Error";

it("calls notification with the given text", () => {
// Setup
spyOn(EducationPlatformApp.prototype, "notification");
const platform = new EducationPlatformApp();

// Call the target object
platform.errorNotification(NOTIFICATION_TEXT);

// Check the expected results
expect(platform.notification).toHaveBeenCalledWith(jasmine.stringMatching(NOTIFICATION_TITLE), jasmine.stringMatching(NOTIFICATION_TEXT), jasmine.anything());
})
})

})
15 changes: 4 additions & 11 deletions platform/test/spec/testErrorHandlerSpec.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
/*global describe, it, beforeEach, expect -- functions provided by Jasmine */
/*global describe, it, beforeEach, expect, spyOn -- functions provided by Jasmine */
/*global jasmine -- object provided by Jasmine */

import { EducationPlatformError } from "../../src/EducationPlatformError.js";
import {ErrorHandler} from "../../src/ErrorHandler.js"
import { PlaygroundUtility } from "../../src/PlaygroundUtility.js";

describe("ErrorHandler", () => {

// Setup
let notifierSpy;

beforeEach(()=>{
notifierSpy = jasmine.createSpy("notifier");
notifierSpy = spyOn(PlaygroundUtility,"errorNotification");
})

describe("constructor()", () => {
it("initialises displayError with the given notifier function", () =>{
// Call the target object
const eh = new ErrorHandler(notifierSpy);

// Check the expected results
expect(eh.displayError).toBe(notifierSpy);
});

it("initialises window.onError with function that calls notify", () =>{
const error = new EducationPlatformError("Error Information");

Expand All @@ -35,7 +28,7 @@ describe("ErrorHandler", () => {
})

describe("notify()", () => {
it("calls notifier function with error information", () =>{
it("calls errorNotification() with error information", () =>{

const MESSAGE = "Test Message";
const error = new EducationPlatformError("Error Information");
Expand Down

0 comments on commit bbabad1

Please sign in to comment.