Skip to content

Commit 4f83ae5

Browse files
committed
feat(tests): add test for JSON-download datafile action
1 parent 0be5951 commit 4f83ae5

File tree

1 file changed

+110
-3
lines changed

1 file changed

+110
-3
lines changed

src/app/datasets/datafiles-actions/datafiles-action.component.spec.ts

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("1000: DatafilesActionComponent", () => {
2929
const actionsConfig = [
3030
{
3131
id: "eed8efec-4354-11ef-a3b5-d75573a5d37f",
32-
order: 4,
32+
order: 0,
3333
label: "Download All",
3434
files: "all",
3535
mat_icon: "download",
@@ -40,7 +40,7 @@ describe("1000: DatafilesActionComponent", () => {
4040
},
4141
{
4242
id: "3072fafc-4363-11ef-b9f9-ebf568222d26",
43-
order: 3,
43+
order: 1,
4444
label: "Download Selected",
4545
files: "selected",
4646
mat_icon: "download",
@@ -61,14 +61,54 @@ describe("1000: DatafilesActionComponent", () => {
6161
},
6262
{
6363
id: "fa3ce6ee-482d-11ef-95e9-ff2c80dd50bd",
64-
order: 1,
64+
order: 3,
65+
label: "Notebook Selected",
66+
files: "selected",
67+
icon: "/assets/icons/jupyter_logo.png",
68+
url: "https://notebook.scicat.org",
69+
target: "_blank",
70+
enabled: "#Selected",
71+
authorization: ["#datasetAccess", "#datasetPublic"],
72+
},
73+
{
74+
id: "fa3ce6ee-482d-11ef-95e9-ff2c80ddnews",
75+
order: 4,
76+
label: "Notebook Selected",
77+
files: "selected",
78+
type: "json-download",
79+
icon: "/assets/icons/jupyter_logo.png",
80+
url: "https://notebook.scicat.org",
81+
target: "_blank",
82+
enabled: "#Selected",
83+
authorization: ["#datasetAccess", "#datasetPublic"],
84+
filename: "{{ uuid }}.ipynb",
85+
},
86+
{
87+
id: "test-test-test-test-1",
88+
order: 5,
89+
label: "Notebook Selected",
90+
files: "selected",
91+
type: "json-download",
92+
icon: "/assets/icons/jupyter_logo.png",
93+
url: "https://notebook.scicat.org",
94+
target: "_blank",
95+
enabled: "#Selected",
96+
authorization: ["#datasetAccess", "#datasetPublic"],
97+
payload: "",
98+
filename: "{{ uuid }}.ipynb",
99+
},
100+
{
101+
id: "test-test-test-test-2",
102+
order: 6,
65103
label: "Notebook Selected",
66104
files: "selected",
105+
type: "json-download",
67106
icon: "/assets/icons/jupyter_logo.png",
68107
url: "https://notebook.scicat.org",
69108
target: "_blank",
70109
enabled: "#Selected",
71110
authorization: ["#datasetAccess", "#datasetPublic"],
111+
filename: "{{ uuid }}.ipynb",
72112
},
73113
];
74114

@@ -121,6 +161,8 @@ describe("1000: DatafilesActionComponent", () => {
121161
download_selected = 1,
122162
notebook_all = 2,
123163
notebook_selected = 3,
164+
json_download_with_payload = 4,
165+
json_download_without_payload = 5,
124166
}
125167

126168
const usersControllerGetUserJWTV3 = () => ({
@@ -732,4 +774,69 @@ describe("1000: DatafilesActionComponent", () => {
732774
actionsConfig[actionSelectorType.notebook_selected].label,
733775
);
734776
});
777+
778+
it("0600: JSON-download action should fetch with customized payload fields when provided", async () => {
779+
selectTestCase(
780+
actionSelectorType.json_download_with_payload,
781+
maxSizeType.higher,
782+
selectedFilesType.none,
783+
);
784+
actionsConfig[actionSelectorType.json_download_with_payload].payload =
785+
'{"test_id":"test-id","parameters":{"testField1":"{{ datasetPid }}","testField2":"{{ sourceFolder }}","files": {{ filesPath }},"jwt":"{{ jwt }}","scicat_url":"https://staging.scicat.ess.url","file_server_url":"sftserver2.esss.dk","file_server_port":"22"}}';
786+
component.jwt = "TEST_JWT";
787+
spyOn(window, "fetch").and.returnValue(
788+
Promise.resolve(
789+
new Response(new Blob(), {
790+
status: 200,
791+
statusText: "OK",
792+
headers: { "Content-Type": "application/json" },
793+
}),
794+
),
795+
);
796+
component.perform_action();
797+
798+
const [url, opts] = (window.fetch as jasmine.Spy).calls.mostRecent().args;
799+
expect(url).toBe(
800+
actionsConfig[actionSelectorType.json_download_with_payload].url,
801+
);
802+
expect(opts.method).toBe("POST");
803+
expect(opts.headers["Content-Type"]).toBe("application/json");
804+
805+
const body = JSON.parse(opts.body);
806+
expect(body.test_id).toBe("test-id");
807+
expect(body.parameters.jwt).toBe("TEST_JWT");
808+
expect(body.parameters.testField1).toBe(actionDataset.pid);
809+
expect(body.parameters.testField2).toBe(actionDataset.sourceFolder);
810+
});
811+
812+
it("0610: JSON-download action should fetch with default fields if payload is not provided", async () => {
813+
selectTestCase(
814+
actionSelectorType.json_download_without_payload,
815+
maxSizeType.higher,
816+
selectedFilesType.none,
817+
);
818+
component.jwt = "TEST_JWT2";
819+
spyOn(window, "fetch").and.returnValue(
820+
Promise.resolve(
821+
new Response(new Blob(), {
822+
status: 200,
823+
statusText: "OK",
824+
headers: { "Content-Type": "application/json" },
825+
}),
826+
),
827+
);
828+
component.perform_action();
829+
830+
const [url, opts] = (window.fetch as jasmine.Spy).calls.mostRecent().args;
831+
expect(url).toBe(
832+
actionsConfig[actionSelectorType.json_download_without_payload].url,
833+
);
834+
expect(opts.method).toBe("POST");
835+
expect(opts.headers["Content-Type"]).toBe("application/json");
836+
837+
const body = JSON.parse(opts.body);
838+
expect(body.jwt).toBe("TEST_JWT2");
839+
expect(body.dataset).toBe(actionDataset.pid);
840+
expect(body.directory).toBe(actionDataset.sourceFolder);
841+
});
735842
});

0 commit comments

Comments
 (0)