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 Jan 15, 2025
1 parent 53165cc commit bd3b5fa
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 214 deletions.
316 changes: 160 additions & 156 deletions tests/backend/test-utils/gas-stubs.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,6 @@
import { vi } from "vitest";

export function mockedCommentsCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.CommentsCollection {
return {
create:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment,
fileId: string,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment
>(),
get: vi.fn<
(
fileId: string,
commentId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment
>(),
list: vi.fn<
(
fileId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.CommentList
>(),
remove: vi.fn<(fileId: string, commentId: string) => void>(),
update:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment,
fileId: string,
commentId: string,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment
>(),
};
}
/* eslint-disable @typescript-eslint/no-explicit-any -- These are stubs for external functions */

export function mockedDrive(): GoogleAppsScript.Drive {
return {
Expand Down Expand Up @@ -149,129 +117,6 @@ export function mockedDrive(): GoogleAppsScript.Drive {
};
}

export function mockedDrivesCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.DrivesCollection {
return {
create:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive,
requestId: string,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive
>(),
get: vi.fn<
(
driveId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive
>(),
hide: vi.fn<
(driveId: string) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive
>(),
list: vi.fn<
(
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.DriveList
>(),
remove:
vi.fn<(driveId: string, optionalArgs?: Record<string, any>) => void>(),
unhide:
vi.fn<
(driveId: string) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive
>(),
update:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive,
driveId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive
>(),
};
}

export function mockedFilesCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.FilesCollection {
return {
copy: vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File,
fileId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File
>(),
create:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File,
mediaData?: GoogleAppsScript.Base.Blob,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File
>(),
download:
vi.fn<
(
fileId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Operation
>(),
emptyTrash: vi.fn<(optionalArgs?: Record<string, any>) => void>(),
export: vi.fn<(fileId: string, mimeType: string) => void>(),
generateIds:
vi.fn<
(
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.GeneratedIds
>(),
get: vi.fn() as {
(
fileId: string,
optionalArgs?: Record<string, any> & { alt: "media" },
): string;
(
fileId: string,
optionalArgs?: Record<string, any>,
): GoogleAppsScript.Drive_v3.Drive.V3.Schema.File;
},
list: vi.fn<
(
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.FileList
>(),
listLabels:
vi.fn<
(
fileId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.LabelList
>(),
modifyLabels:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.ModifyLabelsRequest,
fileId: string,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.ModifyLabelsResponse
>(),
remove:
vi.fn<(fileId: string, optionalArgs?: Record<string, any>) => void>(),
update:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File,
fileId: string,
mediaData?: GoogleAppsScript.Base.Blob,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File
>(),
watch:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Channel,
fileId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Channel
>(),
};
}

export function mockedHtmlOutput(): GoogleAppsScript.HTML.HtmlOutput {
return {
addMetaTag:
Expand Down Expand Up @@ -602,6 +447,163 @@ function mockedChannelsCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collecti
};
}

function mockedCommentsCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.CommentsCollection {
return {
create:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment,
fileId: string,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment
>(),
get: vi.fn<
(
fileId: string,
commentId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment
>(),
list: vi.fn<
(
fileId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.CommentList
>(),
remove: vi.fn<(fileId: string, commentId: string) => void>(),
update:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment,
fileId: string,
commentId: string,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment
>(),
};
}

function mockedDrivesCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.DrivesCollection {
return {
create:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive,
requestId: string,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive
>(),
get: vi.fn<
(
driveId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive
>(),
hide: vi.fn<
(driveId: string) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive
>(),
list: vi.fn<
(
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.DriveList
>(),
remove:
vi.fn<(driveId: string, optionalArgs?: Record<string, any>) => void>(),
unhide:
vi.fn<
(driveId: string) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive
>(),
update:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive,
driveId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive
>(),
};
}

function mockedFilesCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.FilesCollection {
return {
copy: vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File,
fileId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File
>(),
create:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File,
mediaData?: GoogleAppsScript.Base.Blob,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File
>(),
download:
vi.fn<
(
fileId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Operation
>(),
emptyTrash: vi.fn<(optionalArgs?: Record<string, any>) => void>(),
export: vi.fn<(fileId: string, mimeType: string) => void>(),
generateIds:
vi.fn<
(
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.GeneratedIds
>(),
get: vi.fn() as {
(
fileId: string,
optionalArgs?: Record<string, any> & { alt: "media" },
): string;
(
fileId: string,
optionalArgs?: Record<string, any>,
): GoogleAppsScript.Drive_v3.Drive.V3.Schema.File;
},
list: vi.fn<
(
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.FileList
>(),
listLabels:
vi.fn<
(
fileId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.LabelList
>(),
modifyLabels:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.ModifyLabelsRequest,
fileId: string,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.ModifyLabelsResponse
>(),
remove:
vi.fn<(fileId: string, optionalArgs?: Record<string, any>) => void>(),
update:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File,
fileId: string,
mediaData?: GoogleAppsScript.Base.Blob,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File
>(),
watch:
vi.fn<
(
resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Channel,
fileId: string,
optionalArgs?: Record<string, any>,
) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Channel
>(),
};
}

function mockedOperationCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.OperationCollection {
return {
cancel: vi.fn<(name: string) => void>(),
Expand Down Expand Up @@ -723,3 +725,5 @@ function mockedTeamdrivesCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collec
>(),
};
}

/* eslint-enable */
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 "vitest";

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
@@ -1,16 +1,10 @@
import { expect, test, vi } from "vitest";

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 Down
Loading

0 comments on commit bd3b5fa

Please sign in to comment.