diff --git a/client/src/utils/upload-queue.test.js b/client/src/utils/upload-queue.test.js index 3e5a72588132..ce38d572ea30 100644 --- a/client/src/utils/upload-queue.test.js +++ b/client/src/utils/upload-queue.test.js @@ -9,52 +9,66 @@ 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] = { @@ -62,6 +76,7 @@ describe("UploadQueue", () => { fileName: file.name, fileSize: file.size, fileContent: "fileContent", + targetHistoryId: "mockhistoryid", }; }, }); @@ -71,12 +86,13 @@ 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(); @@ -84,7 +100,7 @@ describe("UploadQueue", () => { }); 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); @@ -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"); @@ -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]); @@ -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; }, @@ -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"); @@ -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"); @@ -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] = { @@ -227,5 +245,6 @@ describe("UploadQueue", () => { }, ], }); + expect(q.encountedErrors).toBeFalsy(); }); });