Skip to content

Commit

Permalink
Always mocking whole Drive in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Dec 29, 2024
1 parent 367b4e3 commit 79da88e
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 230 deletions.
16 changes: 2 additions & 14 deletions __tests__/backend/utils/SafeDriveService.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import { expect, test } from "@jest/globals";

import { SafeDriveService_ } from "../../../src/backend/utils/SafeDriveService";
import {
mockedCommentsCollection,
mockedDrive,
mockedDrivesCollection,
mockedFilesCollection,
mockedRepliesCollection,
} from "../../test-utils/gas-stubs";
import { mockedDrive } from "../../test-utils/gas-stubs";

test("SafeDriveService constructs correctly", () => {
global.Drive = {
...mockedDrive(),
Comments: mockedCommentsCollection(),
Drives: mockedDrivesCollection(),
Files: mockedFilesCollection(),
Replies: mockedRepliesCollection(),
};
global.Drive = mockedDrive();

expect(() => {
new SafeDriveService_();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ import { expect, test } from "@jest/globals";
import { mocked } from "jest-mock";

import { SafeCommentsCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeCommentsCollection";
import {
mockedCommentsCollection,
mockedDrive,
} from "../../../test-utils/gas-stubs";
import { mockedDrive } from "../../../test-utils/gas-stubs";

test("SafeCommentsCollection constructs correctly", () => {
global.Drive = {
...mockedDrive(),
Comments: mockedCommentsCollection(),
};
global.Drive = mockedDrive();

expect(() => {
new SafeCommentsCollection_();
Expand All @@ -37,7 +31,7 @@ test("create works", () => {
],
};

global.Drive.Comments = mockedCommentsCollection();
global.Drive = mockedDrive();
const create = mocked(global.Drive.Comments).create.mockReturnValueOnce(
comment,
);
Expand Down Expand Up @@ -69,7 +63,7 @@ test("create throws an error on an invalid comment", () => {
],
};

global.Drive.Comments = mockedCommentsCollection();
global.Drive = mockedDrive();
const create = mocked(global.Drive.Comments).create.mockReturnValueOnce(
comment,
);
Expand Down Expand Up @@ -123,7 +117,7 @@ test("list works", () => {
],
};

global.Drive.Comments = mockedCommentsCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Comments).list.mockReturnValueOnce(
commentList,
);
Expand Down Expand Up @@ -177,7 +171,7 @@ test("list works with optional arguments", () => {
],
};

global.Drive.Comments = mockedCommentsCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Comments).list.mockReturnValueOnce(
commentList,
);
Expand Down Expand Up @@ -238,7 +232,7 @@ test("list throws an error on an invalid comment", () => {
],
};

global.Drive.Comments = mockedCommentsCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Comments).list.mockReturnValueOnce(
commentList,
);
Expand All @@ -257,7 +251,7 @@ test("list throws an error on an invalid comment list", () => {
nextPageToken: "TOKEN",
};

global.Drive.Comments = mockedCommentsCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Comments).list.mockReturnValueOnce(
commentList,
);
Expand Down Expand Up @@ -302,7 +296,7 @@ test("list throws an error on missing replies", () => {
],
};

global.Drive.Comments = mockedCommentsCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Comments).list.mockReturnValueOnce(
commentList,
);
Expand Down Expand Up @@ -355,7 +349,7 @@ test("list throws an error on an invalid reply", () => {
],
};

global.Drive.Comments = mockedCommentsCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Comments).list.mockReturnValueOnce(
commentList,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ import { expect, test } from "@jest/globals";
import { mocked } from "jest-mock";

import { SafeDrivesCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeDrivesCollection";
import {
mockedDrive,
mockedDrivesCollection,
} from "../../../test-utils/gas-stubs";
import { mockedDrive } from "../../../test-utils/gas-stubs";

test("SafeDrivesCollection constructs correctly", () => {
global.Drive = {
...mockedDrive(),
Drives: mockedDrivesCollection(),
};
global.Drive = mockedDrive();

expect(() => {
new SafeDrivesCollection_();
Expand All @@ -32,7 +26,7 @@ test("list works", () => {
],
};

global.Drive.Drives = mockedDrivesCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Drives).list.mockReturnValueOnce(driveList);

const drivesCollection = new SafeDrivesCollection_();
Expand All @@ -57,7 +51,7 @@ test("list works with optional parameters", () => {
],
};

global.Drive.Drives = mockedDrivesCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Drives).list.mockReturnValueOnce(driveList);

const drivesCollection = new SafeDrivesCollection_();
Expand Down Expand Up @@ -98,7 +92,7 @@ test("list works with selective fields", () => {
],
};

global.Drive.Drives = mockedDrivesCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Drives)
.list.mockReturnValueOnce(driveList1)
.mockReturnValueOnce(driveList2);
Expand Down Expand Up @@ -131,7 +125,7 @@ test("list throws an error on an invalid drive", () => {
],
};

global.Drive.Drives = mockedDrivesCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Drives).list.mockReturnValueOnce(driveList);

const drivesCollection = new SafeDrivesCollection_();
Expand All @@ -143,7 +137,7 @@ test("list throws an error on an invalid drive", () => {
});

test("list throws an error on an invalid drive list", () => {
global.Drive.Drives = mockedDrivesCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Drives).list.mockReturnValueOnce({});

const drivesCollection = new SafeDrivesCollection_();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ import { expect, test } from "@jest/globals";
import { mocked } from "jest-mock";

import { SafeFilesCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeFilesCollection";
import {
mockedDrive,
mockedFilesCollection,
} from "../../../test-utils/gas-stubs";
import { mockedDrive } from "../../../test-utils/gas-stubs";

test("SafeFilesCollection constructs correctly", () => {
global.Drive = {
...mockedDrive(),
Files: mockedFilesCollection(),
};
global.Drive = mockedDrive();

expect(() => {
new SafeFilesCollection_();
Expand All @@ -29,7 +23,7 @@ test("copy works", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const copy = mocked(global.Drive.Files).copy.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -53,7 +47,7 @@ test("copy works with optional arguments", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const copy = mocked(global.Drive.Files).copy.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -76,7 +70,7 @@ test("copy works with selective fields", () => {
mimeType: "text/plain",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const copy = mocked(global.Drive.Files).copy.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand Down Expand Up @@ -109,7 +103,7 @@ test("copy throws and error on invalid file", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const copy = mocked(global.Drive.Files).copy.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -133,7 +127,7 @@ test("get works", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const get = mocked(global.Drive.Files).get.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -156,7 +150,7 @@ test("get works with optional arguments", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const get = mocked(global.Drive.Files).get.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -174,7 +168,7 @@ test("get works with selective fields", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const get = mocked(global.Drive.Files).get.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -196,7 +190,7 @@ test("get throws an error on invalid file", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const get = mocked(global.Drive.Files).get.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -219,7 +213,7 @@ test("create works", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const create = mocked(global.Drive.Files).create.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -243,7 +237,7 @@ test("create works with optional arguments", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const create = mocked(global.Drive.Files).create.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -266,7 +260,7 @@ test("create works with selective fields", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const create = mocked(global.Drive.Files).create.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand Down Expand Up @@ -296,7 +290,7 @@ test("create throws an error on invalid file", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const create = mocked(global.Drive.Files).create.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand Down Expand Up @@ -333,7 +327,7 @@ test("list works", () => {
],
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Files).list.mockReturnValueOnce(fileList);

const filesCollection = new SafeFilesCollection_();
Expand Down Expand Up @@ -368,7 +362,7 @@ test("list works with optional arguments", () => {
],
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Files).list.mockReturnValueOnce(fileList);

const filesCollection = new SafeFilesCollection_();
Expand Down Expand Up @@ -405,7 +399,7 @@ test("list works with selective fields", () => {
],
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Files).list.mockReturnValueOnce(fileList);

const filesCollection = new SafeFilesCollection_();
Expand Down Expand Up @@ -444,7 +438,7 @@ test("list throws an error on invalid file", () => {
],
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Files).list.mockReturnValueOnce(fileList);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -458,7 +452,7 @@ test("list throws an error on invalid file", () => {
test("list throws an error on invalid file list", () => {
const fileList = {};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const list = mocked(global.Drive.Files).list.mockReturnValueOnce(fileList);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -470,7 +464,7 @@ test("list throws an error on invalid file list", () => {
});

test("remove works", () => {
global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const remove = mocked(global.Drive.Files).remove.mockImplementationOnce(
// eslint-disable-next-line @typescript-eslint/no-empty-function -- Implementation needed so the function is bound to local this
() => {},
Expand All @@ -495,7 +489,7 @@ test("update works", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const update = mocked(global.Drive.Files).update.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -520,7 +514,7 @@ test("update works with optional arguments", () => {
name: "FILE_TITLE",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const update = mocked(global.Drive.Files).update.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand Down Expand Up @@ -549,7 +543,7 @@ test("update works with selective fields", () => {
},
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const update = mocked(global.Drive.Files).update.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand All @@ -574,7 +568,7 @@ test("update throws an error on invalid file", () => {
id: "FILE_ID",
};

global.Drive.Files = mockedFilesCollection();
global.Drive = mockedDrive();
const update = mocked(global.Drive.Files).update.mockReturnValueOnce(file);

const filesCollection = new SafeFilesCollection_();
Expand Down
Loading

0 comments on commit 79da88e

Please sign in to comment.