Skip to content

Commit

Permalink
adjust jests for upload functions made async
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedhamidawan committed Dec 11, 2024
1 parent 9d43341 commit ca6c38d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
32 changes: 21 additions & 11 deletions client/src/utils/upload-payload.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { isUrl, uploadPayload } from "./upload-payload.js";

// confirm dialog to ask user if they want to auto rename a potential Galaxy file
jest.mock("@/composables/confirmDialog", () => ({
useConfirmDialog: jest.fn(() => ({
// we assume here the user always wants to auto rename the Galaxy file
confirm: jest.fn(() => Promise.resolve(true)),
})),
}));

describe("uploadPayload", () => {
test("basic validation", () => {
expect(() => uploadPayload([], "historyId")).toThrow("No valid items provided.");
expect(() => uploadPayload([{}], "historyId")).toThrow("Content not available.");
expect(() => uploadPayload([{ fileContent: "fileContent" }], "historyId")).toThrow(
test("basic validation", async () => {
await expect(uploadPayload([], "historyId")).rejects.toThrow("No valid items provided.");
await expect(uploadPayload([{}], "historyId")).rejects.toThrow("Content not available.");
await expect(uploadPayload([{ fileContent: "fileContent" }], "historyId")).rejects.toThrow(
"Unknown file mode: undefined."
);
expect(() =>
await expect(
uploadPayload(
[
{
Expand All @@ -23,7 +31,7 @@ describe("uploadPayload", () => {
],
"historyId"
)
).toThrow("Invalid url: xyz://test.me.1");
).rejects.toThrow("Invalid url: xyz://test.me.1");
});

test("url detection", () => {
Expand All @@ -32,8 +40,8 @@ describe("uploadPayload", () => {
expect(isUrl("http://")).toBeTruthy();
});

test("regular payload", () => {
const p = uploadPayload(
test("regular payload", async () => {
const p = await uploadPayload(
[
{ fileContent: " fileContent ", fileMode: "new", fileName: "1" },
{
Expand Down Expand Up @@ -136,6 +144,7 @@ describe("uploadPayload", () => {
to_posix_lines: false,
url: "http://test.me",
},
// confirmDialog for auto renaming, assume user wants to auto rename
{
dbkey: "dbKey5",
deferred: true,
Expand All @@ -151,8 +160,8 @@ describe("uploadPayload", () => {
});
});

test("composite payload", () => {
const p = uploadPayload(
test("composite payload", async () => {
const p = await uploadPayload(
[
{ fileContent: "fileContent", fileMode: "new", fileName: "1" },
{
Expand Down Expand Up @@ -211,11 +220,12 @@ describe("uploadPayload", () => {
src: "files",
to_posix_lines: true,
},
// in composite mode, we do not have a confirmDialog for auto renaming
{
dbkey: "dbKey2",
deferred: true,
ext: "extension2",
name: "PreviousGalaxyFile",
name: "Galaxy2-[PreviousGalaxyFile].bed",
space_to_tab: true,
src: "files",
to_posix_lines: true,
Expand Down
8 changes: 6 additions & 2 deletions client/src/utils/upload-queue.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import flushPromises from "flush-promises";

import { sendPayload } from "@/utils/upload-submit.js";

import { UploadQueue } from "./upload-queue.js";
Expand Down Expand Up @@ -66,7 +68,7 @@ describe("UploadQueue", () => {
expect(q.encountedErrors).toBeFalsy();
});

test("calling start processes all files in queue", () => {
test("calling start processes all files in queue", async () => {
const fileEntries = {};
const q = instrumentedUploadQueue({
get: (index) => fileEntries[index],
Expand All @@ -85,6 +87,7 @@ describe("UploadQueue", () => {
q._processSubmit = mockedSubmit;
q.add([StubFile("a"), StubFile("b")]);
q.start();
await flushPromises();
expect(q.size).toEqual(0);
expect(q.encountedErrors).toBeFalsy();
expect(spy.mock.calls.length).toEqual(3); // called for 2, 1, 0 files.
Expand Down Expand Up @@ -181,7 +184,7 @@ describe("UploadQueue", () => {
expect(q.encountedErrors).toBeFalsy();
});

test("remote file batch", () => {
test("remote file batch", async () => {
const fileEntries = {};
const q = instrumentedUploadQueue({
historyId: "historyId",
Expand All @@ -203,6 +206,7 @@ describe("UploadQueue", () => {
q.add([StubFile("a"), StubFile("b"), StubFile("c")]);
expect(q.size).toEqual(3);
q.start();
await flushPromises();
expect(sendPayload.mock.calls[0][0]).toEqual({
auto_decompress: true,
files: [],
Expand Down

0 comments on commit ca6c38d

Please sign in to comment.