Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nitrosx committed Aug 23, 2024
1 parent 16045f0 commit 8d15458
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { ActionDataset } from "./datafiles-action.interfaces";
describe("1000: DatafilesActionComponent", () => {
let component: DatafilesActionComponent;
let fixture: ComponentFixture<DatafilesActionComponent>;
let htmlForm: HTMLFormElement;
let htmlInput: HTMLInputElement;

const actionsConfig = [
{
Expand Down Expand Up @@ -129,14 +131,20 @@ describe("1000: DatafilesActionComponent", () => {
id: "4ac45f3e-4d79-11ef-856c-6339dab93bee",
});

const browserWindowMock = {
document: {
write() {},
body: {
setAttribute() {},
},
},
} as unknown as Window;
// const browserWindowMock = {
// document: {
// write() {},
// body: {
// setAttribute() {},
// },
// },
// } as unknown as Window;

beforeAll(() => {
htmlForm = document.createElement("form");
(htmlForm as HTMLFormElement).submit = () => {};
htmlInput = document.createElement("input");
});

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand Down Expand Up @@ -494,9 +502,22 @@ describe("1000: DatafilesActionComponent", () => {
});
});

function getFakeElement(elementType: string): HTMLElement {
const element = new MockHtmlElement(elementType);
return element as unknown as HTMLElement;
function createFakeElement(elementType: string): HTMLElement {
//const element = new MockHtmlElement(elementType);
//return element as unknown as HTMLElement;
let element: HTMLElement = null;

switch (elementType) {
case "form":
element = htmlForm.cloneNode(true) as HTMLElement;
break;
case "input":
element = htmlInput.cloneNode(true) as HTMLElement;
break;
default:
element = null;
}
return element;
}

it("0400: Form submission should have all files when Download All is clicked", async () => {
Expand All @@ -505,8 +526,9 @@ describe("1000: DatafilesActionComponent", () => {
maxSizeType.higher,
selectedFilesType.none,
);
spyOn(document, "createElement").and.callFake(getFakeElement);
spyOn(window, "open").and.returnValue(browserWindowMock);

spyOn(document, "createElement").and.callFake(createFakeElement);
//spyOn(window, "open").and.returnValue(browserWindowMock);

component.perform_action();

Expand All @@ -526,13 +548,13 @@ describe("1000: DatafilesActionComponent", () => {
maxSizeType.higher,
selectedFilesType.none,
);
spyOn(document, "createElement").and.callFake(getFakeElement);
spyOn(window, "open").and.returnValue(browserWindowMock);
spyOn(document, "createElement").and.callFake(createFakeElement);
//spyOn(window, "open").and.returnValue(browserWindowMock);

component.perform_action();

expect(component.form.action).toEqual(
actionsConfig[actionSelectorType.download_all].url,
expect(component.form.action.replace(/\/$/, "")).toEqual(
actionsConfig[actionSelectorType.download_all].url.replace(/\/$/, ""),
);
});

Expand All @@ -542,8 +564,8 @@ describe("1000: DatafilesActionComponent", () => {
maxSizeType.higher,
selectedFilesType.none,
);
spyOn(document, "createElement").and.callFake(getFakeElement);
spyOn(window, "open").and.returnValue(browserWindowMock);
spyOn(document, "createElement").and.callFake(createFakeElement);
//spyOn(window, "open").and.returnValue(browserWindowMock);

component.perform_action();

Expand All @@ -566,8 +588,8 @@ describe("1000: DatafilesActionComponent", () => {
maxSizeType.higher,
selectedFile,
);
spyOn(document, "createElement").and.callFake(getFakeElement);
spyOn(window, "open").and.returnValue(browserWindowMock);
spyOn(document, "createElement").and.callFake(createFakeElement);
//spyOn(window, "open").and.returnValue(browserWindowMock);

component.perform_action();

Expand All @@ -592,8 +614,8 @@ describe("1000: DatafilesActionComponent", () => {
maxSizeType.higher,
selectedFilesType.none,
);
spyOn(document, "createElement").and.callFake(getFakeElement);
spyOn(window, "open").and.returnValue(browserWindowMock);
spyOn(document, "createElement").and.callFake(createFakeElement);
//spyOn(window, "open").and.returnValue(browserWindowMock);

component.perform_action();

Expand All @@ -613,13 +635,13 @@ describe("1000: DatafilesActionComponent", () => {
maxSizeType.higher,
selectedFilesType.none,
);
spyOn(document, "createElement").and.callFake(getFakeElement);
spyOn(window, "open").and.returnValue(browserWindowMock);
spyOn(document, "createElement").and.callFake(createFakeElement);
//spyOn(window, "open").and.returnValue(browserWindowMock);

component.perform_action();

expect(component.form.action).toEqual(
actionsConfig[actionSelectorType.notebook_all].url,
expect(component.form.action.replace(/\/$/, "")).toEqual(
actionsConfig[actionSelectorType.notebook_all].url.replace(/\/$/, ""),
);
});

Expand All @@ -630,8 +652,8 @@ describe("1000: DatafilesActionComponent", () => {
maxSizeType.higher,
selectedFile,
);
spyOn(document, "createElement").and.callFake(getFakeElement);
spyOn(window, "open").and.returnValue(browserWindowMock);
spyOn(document, "createElement").and.callFake(createFakeElement);
//spyOn(window, "open").and.returnValue(browserWindowMock);

component.perform_action();

Expand Down
38 changes: 20 additions & 18 deletions src/app/datasets/datafiles-actions/datafiles-action.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
visible = true;
use_mat_icon = false;
use_icon = false;
//disabled = false;
disabled_condition = "false";
selectedTotalFileSize = 0;
numberOfFileSelected = 0;

form: HTMLFormElement = null;

constructor(private userApi: UserApi) {
this.userApi.jwt().subscribe((jwt) => {
this.jwt = jwt.jwt;
Expand Down Expand Up @@ -90,10 +91,6 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
).length;
}

// compute_disabled() {
// this.disabled = eval(this.disabled_condition);
// }

get disabled() {
this.update_status();
this.prepare_disabled_condition();
Expand All @@ -118,21 +115,25 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
}

type_form() {
const form = document.createElement("form");
form.target = this.actionConfig.target || "_self";
form.method = this.actionConfig.method || "POST";
form.action = this.actionConfig.url;
form.style.display = "none";
if (this.form !== null) {
document.body.removeChild(this.form);
}

this.form = document.createElement("form");
this.form.target = this.actionConfig.target || "_self";
this.form.method = this.actionConfig.method || "POST";
this.form.action = this.actionConfig.url;
this.form.style.display = "none";

form.appendChild(
this.form.appendChild(
this.add_input("auth_token", this.userApi.getCurrentToken().id),
);

form.appendChild(this.add_input("jwt", this.jwt));
this.form.appendChild(this.add_input("jwt", this.jwt));

form.appendChild(this.add_input("dataset", this.actionDataset.pid));
this.form.appendChild(this.add_input("dataset", this.actionDataset.pid));

form.appendChild(
this.form.appendChild(
this.add_input("directory", this.actionDataset.sourceFolder),
);

Expand All @@ -142,14 +143,15 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
this.actionConfig.files === "all" ||
(this.actionConfig.files === "selected" && item.selected)
) {
form.appendChild(this.add_input("files[" + index + "]", item.path));
this.form.appendChild(
this.add_input("files[" + index + "]", item.path),
);
index = index + 1;
}
}

document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
document.body.appendChild(this.form);
this.form.submit();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input, OnInit } from "@angular/core";
import { Component, Input } from "@angular/core";
import { ActionConfig, ActionDataset } from "./datafiles-action.interfaces";
import { DataFiles_File } from "datasets/datafiles/datafiles.interfaces";
import { AppConfigService } from "app-config.service";
Expand Down

0 comments on commit 8d15458

Please sign in to comment.