Skip to content

Commit

Permalink
Fix console.error in upload-queue.test.js.
Browse files Browse the repository at this point in the history
I don't think the test meant to issue the error so I fixed the call and I instrumented the queue to ensure errors don't happen in any of the tests.
  • Loading branch information
jmchilton committed Nov 19, 2024
1 parent 6463f86 commit 27d534f
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions client/src/utils/upload-queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,74 @@ function StubFile(name = null, size = 0, mode = "local") {
return { name, size, mode };
}

function instrumentedUploadQueue(options = {}) {
const uploadQueue = new UploadQueue(options);
uploadQueue.encountedErrors = false;
uploadQueue.opts.error = function (d, m) {
uploadQueue.encountedErrors = true;
};
return uploadQueue;
}

describe("UploadQueue", () => {
test("a queue is initialized to correct state", () => {
const q = new UploadQueue({ foo: 1 });
const q = instrumentedUploadQueue({ foo: 1 });
expect(q.size).toEqual(0);
expect(q.isRunning).toBe(false);
expect(q.opts.foo).toEqual(1); // passed as options
expect(q.opts.multiple).toBe(true); // default value
expect(q.encountedErrors).toBeFalsy();
});

test("resetting the queue removes all files from it", () => {
const q = new UploadQueue();
const q = instrumentedUploadQueue();
q.add([StubFile("a"), StubFile("b")]);
expect(q.size).toEqual(2);
q.reset();
expect(q.size).toEqual(0);
expect(q.encountedErrors).toBeFalsy();
});

test("calling configure updates options", () => {
const q = new UploadQueue({ foo: 1 });
const q = instrumentedUploadQueue({ foo: 1 });
expect(q.opts.foo).toEqual(1);
expect(q.opts.bar).toBeUndefined();
q.configure({ bar: 2 }); // overwrite bar
expect(q.opts.foo).toEqual(1); // value unchangee
expect(q.opts.bar).toEqual(2); // value overwritten
expect(q.encountedErrors).toBeFalsy();
});

test("calling start sets isRunning to true", () => {
const q = new UploadQueue();
const q = instrumentedUploadQueue();
q._process = jest.fn(); // mock this, otherwise it'll reset isRunning after it's done.
expect(q.isRunning).toBe(false);
q.start();
expect(q.isRunning).toBe(true);
expect(q.encountedErrors).toBeFalsy();
});

test("calling start is a noop if queue is running", () => {
const q = new UploadQueue();
const q = instrumentedUploadQueue();
const mockedProcess = jest.fn();
q._process = mockedProcess();
q.isRunning = true;
q.start();
expect(mockedProcess.mock.calls.length === 0); // function was not called
expect(q.encountedErrors).toBeFalsy();
});

test("calling start processes all files in queue", () => {
const fileEntries = {};
const q = new UploadQueue({
const q = instrumentedUploadQueue({
get: (index) => fileEntries[index],
announce: (index, file) => {
fileEntries[index] = {
fileMode: file.mode,
fileName: file.name,
fileSize: file.size,
fileContent: "fileContent",
targetHistoryId: "mockhistoryid",
};
},
});
Expand All @@ -71,20 +86,21 @@ describe("UploadQueue", () => {
q.add([StubFile("a"), StubFile("b")]);
q.start();
expect(q.size).toEqual(0);
expect(q.encountedErrors).toBeFalsy();
expect(spy.mock.calls.length).toEqual(3); // called for 2, 1, 0 files.
spy.mockRestore(); // not necessary, but safer, in case we later modify implementation.
});

test("calling stop sets isPaused to true", () => {
const q = new UploadQueue();
const q = instrumentedUploadQueue();
q.start();
expect(q.isPaused).toBe(false);
q.stop();
expect(q.isPaused).toBe(true);
});

test("adding files increases the queue size by the number of files", () => {
const q = new UploadQueue();
const q = instrumentedUploadQueue();
expect(q.size).toEqual(0);
q.add([StubFile("a"), StubFile("b")]);
expect(q.nextIndex).toEqual(2);
Expand All @@ -94,14 +110,14 @@ describe("UploadQueue", () => {
});

test("adding files increases the next index by the number of files", () => {
const q = new UploadQueue();
const q = instrumentedUploadQueue();
expect(q.nextIndex).toEqual(0);
q.add([StubFile("a"), StubFile("b")]);
expect(q.nextIndex).toEqual(2);
});

test("duplicate files are not added to the queue, unless the mode is set to 'new'", () => {
const q = new UploadQueue();
const q = instrumentedUploadQueue();
const file1 = StubFile("a", 1);
const file2 = StubFile("a", 1);
const file3 = StubFile("a", 1, "new");
Expand All @@ -115,7 +131,7 @@ describe("UploadQueue", () => {

test("adding a file calls opts.announce with correct arguments", () => {
const mockAnnounce = jest.fn();
const q = new UploadQueue({ announce: mockAnnounce });
const q = instrumentedUploadQueue({ announce: mockAnnounce });
const file = StubFile("a");
expect(mockAnnounce.mock.calls.length).toBe(0);
q.add([file]);
Expand All @@ -126,7 +142,7 @@ describe("UploadQueue", () => {

test("removing a file reduces the queue size by 1", () => {
const fileEntries = {};
const q = new UploadQueue({
const q = instrumentedUploadQueue({
announce: (index, file) => {
fileEntries[index] = file;
},
Expand All @@ -138,7 +154,7 @@ describe("UploadQueue", () => {
});

test("removing a file by index out of sequence is allowed", () => {
const q = new UploadQueue();
const q = instrumentedUploadQueue();
const file1 = StubFile("a");
const file2 = StubFile("b");
const file3 = StubFile("c");
Expand All @@ -149,10 +165,11 @@ describe("UploadQueue", () => {
expect(q.queue.get("0")).toBe(file1);
expect(q.queue.get("1")).toBeUndefined();
expect(q.queue.get("2")).toBe(file3);
expect(q.encountedErrors).toBeFalsy();
});

test("removing a file via _processIndex, obeys FIFO protocol", () => {
const q = new UploadQueue();
const q = instrumentedUploadQueue();
q.add([StubFile("a"), StubFile("b")]);
let nextIndex = q._processIndex();
expect(nextIndex).toEqual("0");
Expand All @@ -161,11 +178,12 @@ describe("UploadQueue", () => {
expect(nextIndex).toEqual("1");
q.remove(nextIndex);
expect(q._processIndex()).toBeUndefined();
expect(q.encountedErrors).toBeFalsy();
});

test("remote file batch", () => {
const fileEntries = {};
const q = new UploadQueue({
const q = instrumentedUploadQueue({
historyId: "historyId",
announce: (index, file) => {
fileEntries[index] = {
Expand Down Expand Up @@ -227,5 +245,6 @@ describe("UploadQueue", () => {
},
],
});
expect(q.encountedErrors).toBeFalsy();
});
});

0 comments on commit 27d534f

Please sign in to comment.