Skip to content

Commit

Permalink
[DLT-1110] Update tests, add fixtures, update setup, lock in packages
Browse files Browse the repository at this point in the history
  • Loading branch information
AronPerez committed Jan 14, 2025
1 parent a1ee5cb commit d438fe1
Show file tree
Hide file tree
Showing 6 changed files with 446 additions and 271 deletions.
16 changes: 8 additions & 8 deletions web/package.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"sanitize-html": "^2.11.0"
},
"scripts": {
"test": "mocha --config .mocharc.cjs",
"test": "mocha --config .mocharc.cjs"
},
"repository": {
"type": "git",
Expand All @@ -36,14 +36,14 @@
},
"homepage": "https://github.com/ORNL/DataFed#readme",
"devDependencies": {
"chai": "^4",
"esm": "^3.2.25",
"jsdom": "^25.0.1",
"jsdom-global": "^3.0.2",
"mocha": "^10.8.2",
"chai": "5.1.2",
"esm": "3.2.25",
"jsdom": "26.0.0",
"jsdom-global": "3.0.2",
"mocha": "11.0.1",
"prettier": "3.4.2",
"pug": "^3.0.3",
"sinon": "^15.2.0"
"pug": "3.0.3",
"sinon": "19.0.2"
},
"type": "module"
}
76 changes: 43 additions & 33 deletions web/test/components/transfer/transfer-dialog-controller.test.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,80 @@
import { expect, sinon } from "../../setup.js";
import { createMockServices, setupJQueryMocks } from "../../fixtures/transfer-fixtures.js";
import { TransferDialogController } from "../../../static/components/transfer/transfer-dialog-controller.js";
import { TransferEndpointManager } from "../../../static/components/transfer/transfer-endpoint-manager.js";
import { TransferModel } from "../../../static/models/transfer-model.js";
import { TransferMode } from "../../../static/models/transfer-model.js";
import { TransferUIManager } from "../../../static/components/transfer/transfer-ui-manager.js";

describe("TransferDialogController", () => {
let controller;
let mockCallback;
let mockServices;
let sandbox;
let uiManagerStub;
let endpointManagerStub;
let modelStub;

const TEST_MODE = 1;
const TEST_MODE = TransferMode.TT_DATA_PUT;
const TEST_IDS = [{ id: 1 }, { id: 2 }];
const TEST_DIALOG_LABELS = { title: "Test Title", button: "Test Button" };

beforeEach(() => {
sandbox = sinon.createSandbox();
mockCallback = sandbox.stub();
mockServices = createMockServices();
setupJQueryMocks(sandbox);

modelStub = new TransferModel(TEST_MODE, TEST_IDS);
endpointManagerStub = new TransferEndpointManager();
uiManagerStub = new TransferUIManager();

sandbox.stub(modelStub);

uiManagerStub.createDialog = sandbox.stub();
uiManagerStub.initializeComponents = sandbox.stub();
uiManagerStub.attachMatchesHandler = sandbox.stub();
uiManagerStub.showDialog = sandbox.stub();
uiManagerStub.getDialogLabels = sandbox.stub().returns(TEST_DIALOG_LABELS);

endpointManagerStub.initialized = false;
controller = new TransferDialogController(TEST_MODE, TEST_IDS, mockCallback);

controller.uiManager = uiManagerStub;
controller.endpointManager = endpointManagerStub;
controller.model = modelStub;
controller = new TransferDialogController(TEST_MODE, TEST_IDS, mockCallback, mockServices);
});

afterEach(() => {
sandbox.restore();
});

describe("constructor", () => {
it("should initialize with correct parameters", () => {
const newController = new TransferDialogController(TEST_MODE, TEST_IDS, mockCallback);
it("should initialize with correct parameters and components", () => {
expect(controller.endpointManager).to.be.instanceOf(TransferEndpointManager);
expect(controller.uiManager).to.be.instanceOf(TransferUIManager);
expect(controller.ids).to.deep.equal(TEST_IDS);
expect(controller.callback).to.equal(mockCallback);

expect(newController.model).to.be.instanceOf(TransferModel);
expect(newController.endpointManager).to.be.instanceOf(TransferEndpointManager);
expect(newController.uiManager).to.be.instanceOf(TransferUIManager);
expect(newController.ids).to.deep.equal(TEST_IDS);
expect(newController.callback).to.equal(mockCallback);
expect(controller.endpointManager.api).to.equal(mockServices.api);
expect(controller.uiManager.api).to.equal(mockServices.api);
expect(controller.endpointManager.dialogs).to.equal(mockServices.dialogs);
expect(controller.uiManager.dialogs).to.equal(mockServices.dialogs);
});

it("should initialize with default services if none provided", () => {
const defaultController = new TransferDialogController(
TEST_MODE,
TEST_IDS,
mockCallback,
);
expect(defaultController.services).to.have.property("dialogs");
expect(defaultController.services).to.have.property("api");
});
});

describe("show", () => {
it("should successfully show the transfer dialog", async () => {
sandbox.stub(controller.uiManager, "initializeComponents");
sandbox.stub(controller.uiManager, "attachMatchesHandler");
sandbox.stub(controller.uiManager, "showDialog");

await controller.show();

expect(controller.uiManager.createDialog.called).to.be.true;
expect(controller.uiManager.initializeComponents.called).to.be.true;
expect(controller.uiManager.attachMatchesHandler.called).to.be.true;
expect(controller.uiManager.showDialog.called).to.be.true;
expect(controller.endpointManager.initialized).to.be.true;
expect(controller.uiManager.showDialog.called).to.be.true;
});

it("should handle errors gracefully", async () => {
sandbox
.stub(controller.uiManager, "initializeComponents")
.throws(new Error("Test error"));

await controller.show();

expect(
mockServices.dialogs.dlgAlert.calledWith("Error", "Failed to open transfer dialog"),
).to.be.true;
});
});
});
128 changes: 84 additions & 44 deletions web/test/components/transfer/transfer-endpoint-manager.test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
import { expect } from "chai";
import sinon from "sinon";
import { expect, sinon } from "../../setup.js";
import { createMockServices, setupJQueryMocks } from "../../fixtures/transfer-fixtures.js";
import { TransferEndpointManager } from "../../../static/components/transfer/transfer-endpoint-manager.js";

describe("TransferEndpointManager", () => {
let jQueryStub;
let manager;
let mockDialog;
let mockFrame;
let mockController;
let mockServices;
let sandbox;

beforeEach(() => {
mockFrame = $("<div>");
mockFrame.append('<textarea id="path"></textarea>');
mockFrame.append('<select id="matches"></select>');
mockDialog = {
sandbox = sinon.createSandbox();
mockServices = createMockServices();
jQueryStub = setupJQueryMocks(sandbox);

document.body.innerHTML = `
<div id="frame">
<textarea id="path"></textarea>
<select id="matches"></select>
</div>
`;

mockController = {
uiManager: {
frame: mockFrame,
updateEndpoint: sinon.stub(),
state: { endpointOk: false },
updateButtonStates: sinon.stub(),
createMatchesHtml: sinon.stub().returns("<option>Test</option>"),
},
};
mockServices = {
api: {
epAutocomplete: sinon.stub(),
epView: sinon.stub(),
},
dialogs: {
dlgAlert: sinon.stub(),
state: {
frame: $("#frame"),
endpointOk: false,
},
updateEndpoint: sandbox.stub().returnsThis(),
updateButtonStates: sandbox.stub().returnsThis(),
},
};

manager = new TransferEndpointManager(mockDialog, mockServices);
manager = new TransferEndpointManager(mockController, mockServices);
manager.initialized = true;
manager.controller = mockController;
});

afterEach(() => {
sinon.restore();
sandbox.restore();
});

describe("searchEndpoint", () => {
Expand All @@ -58,12 +60,21 @@ describe("TransferEndpointManager", () => {
mockServices.api.epView.callsFake((endpoint, callback) =>
callback(true, { code: "ERROR" }),
);
const searchAutocompleteSpy = sinon.spy(manager, "searchEndpointAutocomplete");
const searchAutocompleteSpy = sandbox.spy(manager, "searchEndpointAutocomplete");

manager.searchEndpoint("test-endpoint", "test-token");

expect(searchAutocompleteSpy.calledWith("test-endpoint", "test-token")).to.be.true;
});

it("should handle API errors", () => {
mockServices.api.epView.throws(new Error("API Error"));

manager.searchEndpoint("test-endpoint", "test-token");

expect(mockServices.dialogs.dlgAlert.calledWith("Globus Error", sinon.match.any)).to.be
.true;
});
});

describe("searchEndpointAutocomplete", () => {
Expand All @@ -81,10 +92,12 @@ describe("TransferEndpointManager", () => {
mockServices.api.epAutocomplete.callsFake((endpoint, callback) =>
callback(true, mockData),
);

manager.searchEndpointAutocomplete("test", "test-token");

expect(manager.endpointManagerList).to.deep.equal(mockData.DATA);
expect(mockServices.api.epAutocomplete.calledOnce).to.be.true;
expect(jQueryStub.html.called).to.be.true;
expect(jQueryStub.prop.calledWith("disabled", false)).to.be.true;
});

it("should handle no matches case", () => {
Expand All @@ -96,24 +109,20 @@ describe("TransferEndpointManager", () => {
manager.searchEndpointAutocomplete("test", "test-token");

expect(manager.endpointManagerList).to.be.null;
expect(jQueryStub.html.calledWith("<option disabled selected>No Matches</option>")).to
.be.true;
expect(jQueryStub.prop.calledWith("disabled", true)).to.be.true;
expect(consoleWarnStub.calledWith("No matches found")).to.be.true;
});
});

describe("getEndpointStatus", () => {
it('should return "active" for non-activated endpoint with expires_in = -1', () => {
const endpoint = { activated: false, expires_in: -1 };
expect(manager.getEndpointStatus(endpoint)).to.equal("active");
});
it("should handle error responses", () => {
mockServices.api.epAutocomplete.callsFake((endpoint, callback) =>
callback(true, { code: "ERROR", DATA: [] }),
);

it("should return hours remaining for activated endpoint", () => {
const endpoint = { activated: true, expires_in: 7200 };
expect(manager.getEndpointStatus(endpoint)).to.equal("2 hrs");
});
manager.searchEndpointAutocomplete("test", "test-token");

it('should return "inactive" for non-activated endpoint with expires_in != -1', () => {
const endpoint = { activated: false, expires_in: 0 };
expect(manager.getEndpointStatus(endpoint)).to.equal("inactive");
expect(mockServices.dialogs.dlgAlert.calledWith("Globus Error", "ERROR")).to.be.true;
});
});

Expand All @@ -123,16 +132,16 @@ describe("TransferEndpointManager", () => {
});

it("should process valid path input", () => {
$("#path", mockFrame).val("endpoint/path");
const searchEndpointSpy = sinon.spy(manager, "searchEndpoint");
jQueryStub.val.returns("endpoint/path");
const searchEndpointSpy = sandbox.spy(manager, "searchEndpoint");

manager.handlePathInput("test-token");

expect(searchEndpointSpy.calledWith("endpoint", "test-token")).to.be.true;
});

it("should handle empty path input", () => {
$("#path", mockFrame).val("");
jQueryStub.val.returns("");

manager.handlePathInput("test-token");

Expand All @@ -141,13 +150,44 @@ describe("TransferEndpointManager", () => {
});

it("should ignore stale requests", () => {
$("#path", mockFrame).val("endpoint/path");
jQueryStub.val.returns("endpoint/path");
manager.currentSearchToken = "different-token";
const searchEndpointSpy = sinon.spy(manager, "searchEndpoint");
const searchEndpointSpy = sandbox.spy(manager, "searchEndpoint");

manager.handlePathInput("test-token");

expect(searchEndpointSpy.called).to.be.false;
});

it("should handle uninitialized state", () => {
manager.initialized = false;
const handlePathInputSpy = sandbox.spy(manager, "handlePathInput");

manager.handlePathInput("test-token");

expect(handlePathInputSpy.calledOnce).to.be.true;
});
});

describe("updateMatchesList", () => {
it("should update matches list with endpoints", () => {
const endpoints = [
{ id: "1", name: "endpoint1" },
{ id: "2", name: "endpoint2" },
];

manager.updateMatchesList(endpoints);

expect(jQueryStub.html.called).to.be.true;
expect(jQueryStub.prop.calledWith("disabled", false)).to.be.true;
});

it("should handle empty endpoints list", () => {
manager.updateMatchesList([]);

expect(jQueryStub.html.calledWith("<option disabled selected>No Matches</option>")).to
.be.true;
expect(jQueryStub.prop.calledWith("disabled", true)).to.be.true;
});
});
});
Loading

0 comments on commit d438fe1

Please sign in to comment.