From dcfd6f3db016941c6c2c6eddfc04faef92cf7fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Wed, 11 Sep 2024 21:52:25 +0300 Subject: [PATCH 1/2] Unified optional properties with undefined --- __tests__/backend/listSharedDrives.test.ts | 1 - .../SafeDriveService/SafeCommentsCollection.test.ts | 12 ++++++------ __tests__/backend/utils/paginationHelper.test.ts | 2 -- __tests__/test-utils/SafeDriveService-stub.ts | 2 +- .../utils/SafeDriveService/SafeDrivesCollection.ts | 4 ++-- src/backend/utils/paginationHelper.ts | 5 ++++- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/__tests__/backend/listSharedDrives.test.ts b/__tests__/backend/listSharedDrives.test.ts index b1be7dbf..5b1de8ca 100644 --- a/__tests__/backend/listSharedDrives.test.ts +++ b/__tests__/backend/listSharedDrives.test.ts @@ -27,7 +27,6 @@ test("listSharedDrives works correctly", () => { ]; const rawResponse = { items: response, - nextPageToken: undefined, }; const driveServiceMock = mockedSafeDriveService(); driveServiceMock.Drives.list.mockReturnValueOnce(rawResponse); diff --git a/__tests__/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts b/__tests__/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts index e719074d..82db7ed3 100644 --- a/__tests__/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts +++ b/__tests__/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts @@ -161,7 +161,7 @@ test("list works", () => { optionalArgs?: { fields?: string; maxResults?: number; - pageToken?: string | undefined; + pageToken?: string; }, ) => GoogleAppsScript.Drive.Schema.CommentList >() @@ -230,7 +230,7 @@ test("list works with optional arguments", () => { optionalArgs?: { fields?: string; maxResults?: number; - pageToken?: string | undefined; + pageToken?: string; }, ) => GoogleAppsScript.Drive.Schema.CommentList >() @@ -306,7 +306,7 @@ test("list throws an error on an invalid comment", () => { optionalArgs?: { fields?: string; maxResults?: number; - pageToken?: string | undefined; + pageToken?: string; }, ) => GoogleAppsScript.Drive.Schema.CommentList >() @@ -340,7 +340,7 @@ test("list throws an error on an invalid comment list", () => { optionalArgs?: { fields?: string; maxResults?: number; - pageToken?: string | undefined; + pageToken?: string; }, ) => GoogleAppsScript.Drive.Schema.CommentList >() @@ -400,7 +400,7 @@ test("list throws an error on missing replies", () => { optionalArgs?: { fields?: string; maxResults?: number; - pageToken?: string | undefined; + pageToken?: string; }, ) => GoogleAppsScript.Drive.Schema.CommentList >() @@ -468,7 +468,7 @@ test("list throws an error on an invalid reply", () => { optionalArgs?: { fields?: string; maxResults?: number; - pageToken?: string | undefined; + pageToken?: string; }, ) => GoogleAppsScript.Drive.Schema.CommentList >() diff --git a/__tests__/backend/utils/paginationHelper.test.ts b/__tests__/backend/utils/paginationHelper.test.ts index 4b67bac1..1656f2be 100644 --- a/__tests__/backend/utils/paginationHelper.test.ts +++ b/__tests__/backend/utils/paginationHelper.test.ts @@ -9,7 +9,6 @@ test("paginationHelper works correctly", () => { } const rawResponse = { a: "b", - nextPageToken: undefined, }; const request = jest @@ -44,7 +43,6 @@ test("paginationHelper works correctly with multiple pages", () => { }; const rawResponse3 = { a: "c", - nextPageToken: undefined, }; const request = jest diff --git a/__tests__/test-utils/SafeDriveService-stub.ts b/__tests__/test-utils/SafeDriveService-stub.ts index fc093bcf..552b6e6d 100644 --- a/__tests__/test-utils/SafeDriveService-stub.ts +++ b/__tests__/test-utils/SafeDriveService-stub.ts @@ -77,7 +77,7 @@ export function mockedSafeDriveService< optionalArgs?: { includeItemsFromAllDrives?: boolean; maxResults?: number; - pageToken?: string | undefined; + pageToken?: string; q?: string; supportsAllDrives?: boolean; }, diff --git a/src/backend/utils/SafeDriveService/SafeDrivesCollection.ts b/src/backend/utils/SafeDriveService/SafeDrivesCollection.ts index 560a3010..89cd8447 100644 --- a/src/backend/utils/SafeDriveService/SafeDrivesCollection.ts +++ b/src/backend/utils/SafeDriveService/SafeDrivesCollection.ts @@ -10,7 +10,7 @@ export interface SafeDrive { export interface SafeDriveList> { items: Array>; - nextPageToken?: string | undefined; + nextPageToken?: string; } const safeDriveKeys: DeepKeyof = { @@ -63,7 +63,7 @@ export class SafeDrivesCollection_ { optionalArgs: { maxResults?: number; orderBy?: string; - pageToken?: string; + pageToken?: string | undefined; } = {}, ): SafeDriveList { const ret = this.unsafeDrives.list({ diff --git a/src/backend/utils/paginationHelper.ts b/src/backend/utils/paginationHelper.ts index 326e3ffd..135fc62d 100644 --- a/src/backend/utils/paginationHelper.ts +++ b/src/backend/utils/paginationHelper.ts @@ -1,4 +1,7 @@ -export function paginationHelper_( +export function paginationHelper_< + T extends { nextPageToken?: string | undefined }, + U, +>( request: (pageToken: string | undefined) => T, transform: (response: T) => Array, ): Array { From dd02d98859c159057931a673bc8c93a5ec20f81d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Thu, 12 Sep 2024 00:46:06 +0300 Subject: [PATCH 2/2] Removed redundant function mocks --- __tests__/backend/doGet.test.ts | 31 +- __tests__/backend/listFolders.test.ts | 24 +- .../SafeCommentsCollection.test.ts | 179 ++----- .../SafeDrivesCollection.test.ts | 99 +--- .../SafeFilesCollection.test.ts | 473 ++---------------- 5 files changed, 115 insertions(+), 691 deletions(-) diff --git a/__tests__/backend/doGet.test.ts b/__tests__/backend/doGet.test.ts index 31dd8fb3..f6031bde 100644 --- a/__tests__/backend/doGet.test.ts +++ b/__tests__/backend/doGet.test.ts @@ -1,4 +1,5 @@ -import { expect, jest, test } from "@jest/globals"; +import { expect, test } from "@jest/globals"; +import { mocked } from "jest-mock"; import { doGet } from "../../src/backend/doGet"; import { @@ -9,25 +10,15 @@ import { test("doGet works correctly", () => { const outputWithTitle = mockedHtmlOutput(); - const setTitle = jest - .fn<(title: string) => GoogleAppsScript.HTML.HtmlOutput>() - .mockReturnValueOnce(outputWithTitle); - const outputWithoutTitle = { - ...mockedHtmlOutput(), - setTitle, - }; - const createTemplateFromFile = jest - .fn<(filename: string) => GoogleAppsScript.HTML.HtmlTemplate>() - .mockReturnValueOnce({ - ...mockedHtmlTemplate(), - evaluate: jest - .fn<() => GoogleAppsScript.HTML.HtmlOutput>() - .mockReturnValueOnce(outputWithoutTitle), - }); - global.HtmlService = { - ...mockedHtmlService(), - createTemplateFromFile, - }; + const outputWithoutTitle = mockedHtmlOutput(); + const setTitle = + mocked(outputWithoutTitle).setTitle.mockReturnValueOnce(outputWithTitle); + global.HtmlService = mockedHtmlService(); + const htmlTemplate = mockedHtmlTemplate(); + mocked(htmlTemplate).evaluate.mockReturnValueOnce(outputWithoutTitle); + const createTemplateFromFile = mocked( + global.HtmlService, + ).createTemplateFromFile.mockReturnValueOnce(htmlTemplate); expect(doGet()).toBe(outputWithTitle); expect(createTemplateFromFile.mock.calls).toHaveLength(1); diff --git a/__tests__/backend/listFolders.test.ts b/__tests__/backend/listFolders.test.ts index db2b485f..4d8cbc8a 100644 --- a/__tests__/backend/listFolders.test.ts +++ b/__tests__/backend/listFolders.test.ts @@ -35,10 +35,8 @@ test("listFolders works correctly", () => { driveServiceMock.Files.list.mockReturnValueOnce(rawResponse); mocked(SafeDriveService_).mockReturnValueOnce(driveServiceMock); - global.Session = { - ...mockedSession(), - getActiveUserLocale: jest.fn<() => string>().mockReturnValueOnce("en"), - }; + global.Session = mockedSession(); + mocked(global.Session).getActiveUserLocale.mockReturnValueOnce("en"); expect(listFolders("ID_PARENT")).toStrictEqual({ response: [ @@ -93,10 +91,8 @@ test("listFolders works correctly with shortcuts", () => { driveServiceMock.Files.list.mockReturnValueOnce(rawResponse); mocked(SafeDriveService_).mockReturnValueOnce(driveServiceMock); - global.Session = { - ...mockedSession(), - getActiveUserLocale: jest.fn<() => string>().mockReturnValueOnce("en"), - }; + global.Session = mockedSession(); + mocked(global.Session).getActiveUserLocale.mockReturnValueOnce("en"); expect(listFolders("ID_PARENT")).toStrictEqual({ response: [ @@ -131,10 +127,8 @@ test("listFolders handles invalid parameters gracefully", () => { }); mocked(SafeDriveService_).mockReturnValueOnce(driveServiceMock); - global.Session = { - ...mockedSession(), - getActiveUserLocale: jest.fn<() => string>().mockReturnValueOnce("en"), - }; + global.Session = mockedSession(); + mocked(global.Session).getActiveUserLocale.mockReturnValueOnce("en"); expect(listFolders(42)).toStrictEqual({ status: "error", @@ -158,10 +152,8 @@ test("listFolders handles errors in Google Drive API gracefully", () => { }); mocked(SafeDriveService_).mockReturnValueOnce(driveServiceMock); - global.Session = { - ...mockedSession(), - getActiveUserLocale: jest.fn<() => string>().mockReturnValueOnce("en"), - }; + global.Session = mockedSession(); + mocked(global.Session).getActiveUserLocale.mockReturnValueOnce("en"); expect(listFolders("ID_PARENT")).toStrictEqual({ status: "error", diff --git a/__tests__/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts b/__tests__/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts index 82db7ed3..beb837e7 100644 --- a/__tests__/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts +++ b/__tests__/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts @@ -1,4 +1,5 @@ -import { expect, jest, test } from "@jest/globals"; +import { expect, test } from "@jest/globals"; +import { mocked } from "jest-mock"; import { SafeCommentsCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeCommentsCollection"; import { @@ -46,21 +47,10 @@ test("insert works", () => { ], }; - const insert = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.Comment, - fileId: string, - ) => GoogleAppsScript.Drive.Schema.Comment - >() - .mockReturnValueOnce(comment); - global.Drive = { - ...mockedDrive(), - Comments: { - ...mockedCommentsCollection(), - insert, - }, - }; + global.Drive.Comments = mockedCommentsCollection(); + const insert = mocked(global.Drive.Comments).insert.mockReturnValueOnce( + comment, + ); const commentsCollection = new SafeCommentsCollection_(); @@ -89,21 +79,10 @@ test("insert throws an error on an invalid comment", () => { ], }; - const insert = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.Comment, - fileId: string, - ) => GoogleAppsScript.Drive.Schema.Comment - >() - .mockReturnValueOnce(comment); - global.Drive = { - ...mockedDrive(), - Comments: { - ...mockedCommentsCollection(), - insert, - }, - }; + global.Drive.Comments = mockedCommentsCollection(); + const insert = mocked(global.Drive.Comments).insert.mockReturnValueOnce( + comment, + ); const commentsCollection = new SafeCommentsCollection_(); @@ -154,25 +133,10 @@ test("list works", () => { ], }; - const list = jest - .fn< - ( - fileId: string, - optionalArgs?: { - fields?: string; - maxResults?: number; - pageToken?: string; - }, - ) => GoogleAppsScript.Drive.Schema.CommentList - >() - .mockReturnValueOnce(commentList); - global.Drive = { - ...mockedDrive(), - Comments: { - ...mockedCommentsCollection(), - list, - }, - }; + global.Drive.Comments = mockedCommentsCollection(); + const list = mocked(global.Drive.Comments).list.mockReturnValueOnce( + commentList, + ); const commentsCollection = new SafeCommentsCollection_(); @@ -223,25 +187,10 @@ test("list works with optional arguments", () => { ], }; - const list = jest - .fn< - ( - fileId: string, - optionalArgs?: { - fields?: string; - maxResults?: number; - pageToken?: string; - }, - ) => GoogleAppsScript.Drive.Schema.CommentList - >() - .mockReturnValueOnce(commentList); - global.Drive = { - ...mockedDrive(), - Comments: { - ...mockedCommentsCollection(), - list, - }, - }; + global.Drive.Comments = mockedCommentsCollection(); + const list = mocked(global.Drive.Comments).list.mockReturnValueOnce( + commentList, + ); const commentsCollection = new SafeCommentsCollection_(); @@ -299,25 +248,10 @@ test("list throws an error on an invalid comment", () => { ], }; - const list = jest - .fn< - ( - fileId: string, - optionalArgs?: { - fields?: string; - maxResults?: number; - pageToken?: string; - }, - ) => GoogleAppsScript.Drive.Schema.CommentList - >() - .mockReturnValueOnce(commentList); - global.Drive = { - ...mockedDrive(), - Comments: { - ...mockedCommentsCollection(), - list, - }, - }; + global.Drive.Comments = mockedCommentsCollection(); + const list = mocked(global.Drive.Comments).list.mockReturnValueOnce( + commentList, + ); const commentsCollection = new SafeCommentsCollection_(); @@ -333,25 +267,10 @@ test("list throws an error on an invalid comment list", () => { nextPageToken: "TOKEN", }; - const list = jest - .fn< - ( - fileId: string, - optionalArgs?: { - fields?: string; - maxResults?: number; - pageToken?: string; - }, - ) => GoogleAppsScript.Drive.Schema.CommentList - >() - .mockReturnValueOnce(commentList); - global.Drive = { - ...mockedDrive(), - Comments: { - ...mockedCommentsCollection(), - list, - }, - }; + global.Drive.Comments = mockedCommentsCollection(); + const list = mocked(global.Drive.Comments).list.mockReturnValueOnce( + commentList, + ); const commentsCollection = new SafeCommentsCollection_(); @@ -393,25 +312,10 @@ test("list throws an error on missing replies", () => { ], }; - const list = jest - .fn< - ( - fileId: string, - optionalArgs?: { - fields?: string; - maxResults?: number; - pageToken?: string; - }, - ) => GoogleAppsScript.Drive.Schema.CommentList - >() - .mockReturnValueOnce(commentList); - global.Drive = { - ...mockedDrive(), - Comments: { - ...mockedCommentsCollection(), - list, - }, - }; + global.Drive.Comments = mockedCommentsCollection(); + const list = mocked(global.Drive.Comments).list.mockReturnValueOnce( + commentList, + ); const commentsCollection = new SafeCommentsCollection_(); @@ -461,25 +365,10 @@ test("list throws an error on an invalid reply", () => { ], }; - const list = jest - .fn< - ( - fileId: string, - optionalArgs?: { - fields?: string; - maxResults?: number; - pageToken?: string; - }, - ) => GoogleAppsScript.Drive.Schema.CommentList - >() - .mockReturnValueOnce(commentList); - global.Drive = { - ...mockedDrive(), - Comments: { - ...mockedCommentsCollection(), - list, - }, - }; + global.Drive.Comments = mockedCommentsCollection(); + const list = mocked(global.Drive.Comments).list.mockReturnValueOnce( + commentList, + ); const commentsCollection = new SafeCommentsCollection_(); diff --git a/__tests__/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts b/__tests__/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts index 0cd9a650..d6691c4f 100644 --- a/__tests__/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts +++ b/__tests__/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts @@ -1,4 +1,5 @@ -import { expect, jest, test } from "@jest/globals"; +import { expect, test } from "@jest/globals"; +import { mocked } from "jest-mock"; import { SafeDrivesCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeDrivesCollection"; import { @@ -41,23 +42,8 @@ test("list works", () => { ], }; - const list = jest - .fn< - (optionalArgs?: { - fields?: string; - maxResults?: number; - orderBy?: string; - pageToken?: string; - }) => GoogleAppsScript.Drive.Schema.DriveList - >() - .mockReturnValueOnce(driveList); - global.Drive = { - ...mockedDrive(), - Drives: { - ...mockedDrivesCollection(), - list, - }, - }; + global.Drive.Drives = mockedDrivesCollection(); + const list = mocked(global.Drive.Drives).list.mockReturnValueOnce(driveList); const drivesCollection = new SafeDrivesCollection_(); @@ -81,23 +67,8 @@ test("list works with optional parameters", () => { ], }; - const list = jest - .fn< - (optionalArgs?: { - fields?: string; - maxResults?: number; - orderBy?: string; - pageToken?: string; - }) => GoogleAppsScript.Drive.Schema.DriveList - >() - .mockReturnValueOnce(driveList); - global.Drive = { - ...mockedDrive(), - Drives: { - ...mockedDrivesCollection(), - list, - }, - }; + global.Drive.Drives = mockedDrivesCollection(); + const list = mocked(global.Drive.Drives).list.mockReturnValueOnce(driveList); const drivesCollection = new SafeDrivesCollection_(); @@ -137,24 +108,10 @@ test("list works with selective fields", () => { ], }; - const list = jest - .fn< - (optionalArgs?: { - fields?: string; - maxResults?: number; - orderBy?: string; - pageToken?: string; - }) => GoogleAppsScript.Drive.Schema.DriveList - >() - .mockReturnValueOnce(driveList1) + global.Drive.Drives = mockedDrivesCollection(); + const list = mocked(global.Drive.Drives) + .list.mockReturnValueOnce(driveList1) .mockReturnValueOnce(driveList2); - global.Drive = { - ...mockedDrive(), - Drives: { - ...mockedDrivesCollection(), - list, - }, - }; const drivesCollection = new SafeDrivesCollection_(); @@ -184,23 +141,8 @@ test("list throws an error on an invalid drive", () => { ], }; - const list = jest - .fn< - (optionalArgs?: { - fields?: string; - maxResults?: number; - orderBy?: string; - pageToken?: string; - }) => GoogleAppsScript.Drive.Schema.DriveList - >() - .mockReturnValueOnce(driveList); - global.Drive = { - ...mockedDrive(), - Drives: { - ...mockedDrivesCollection(), - list, - }, - }; + global.Drive.Drives = mockedDrivesCollection(); + const list = mocked(global.Drive.Drives).list.mockReturnValueOnce(driveList); const drivesCollection = new SafeDrivesCollection_(); @@ -211,23 +153,8 @@ test("list throws an error on an invalid drive", () => { }); test("list throws an error on an invalid drive list", () => { - const list = jest - .fn< - (optionalArgs?: { - fields?: string; - maxResults?: number; - orderBy?: string; - pageToken?: string; - }) => GoogleAppsScript.Drive.Schema.DriveList - >() - .mockReturnValueOnce({}); - global.Drive = { - ...mockedDrive(), - Drives: { - ...mockedDrivesCollection(), - list, - }, - }; + global.Drive.Drives = mockedDrivesCollection(); + const list = mocked(global.Drive.Drives).list.mockReturnValueOnce({}); const drivesCollection = new SafeDrivesCollection_(); diff --git a/__tests__/backend/utils/SafeDriveService/SafeFilesCollection.test.ts b/__tests__/backend/utils/SafeDriveService/SafeFilesCollection.test.ts index 13c5abc4..3464e3cd 100644 --- a/__tests__/backend/utils/SafeDriveService/SafeFilesCollection.test.ts +++ b/__tests__/backend/utils/SafeDriveService/SafeFilesCollection.test.ts @@ -1,4 +1,5 @@ -import { expect, jest, test } from "@jest/globals"; +import { expect, test } from "@jest/globals"; +import { mocked } from "jest-mock"; import { SafeFilesCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeFilesCollection"; import { @@ -40,25 +41,8 @@ test("copy works", () => { }, }; - const copy = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - fileId: string, - optionalArgs?: { - fields?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - copy, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const copy = mocked(global.Drive.Files).copy.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -83,25 +67,8 @@ test("copy works with optional arguments", () => { }, }; - const copy = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - fileId: string, - optionalArgs?: { - fields?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - copy, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const copy = mocked(global.Drive.Files).copy.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -123,25 +90,8 @@ test("copy works with selective fields", () => { mimeType: "text/plain", }; - const copy = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - fileId: string, - optionalArgs?: { - fields?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - copy, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const copy = mocked(global.Drive.Files).copy.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -173,25 +123,8 @@ test("copy throws and error on invalid file", () => { }, }; - const copy = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - fileId: string, - optionalArgs?: { - fields?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - copy, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const copy = mocked(global.Drive.Files).copy.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -216,24 +149,8 @@ test("get works", () => { }, }; - const get = jest - .fn< - ( - fileId: string, - optionalArgs?: { - alt?: string; - fields?: string; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - get, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const get = mocked(global.Drive.Files).get.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -257,24 +174,8 @@ test("get works with optional arguments", () => { }, }; - const get = jest - .fn< - ( - fileId: string, - optionalArgs?: { - alt?: string; - fields?: string; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - get, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const get = mocked(global.Drive.Files).get.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -291,24 +192,8 @@ test("get works with selective fields", () => { title: "FILE_TITLE", }; - const get = jest - .fn< - ( - fileId: string, - optionalArgs?: { - alt?: string; - fields?: string; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - get, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const get = mocked(global.Drive.Files).get.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -331,24 +216,8 @@ test("get throws an error on invalid file", () => { }, }; - const get = jest - .fn< - ( - fileId: string, - optionalArgs?: { - alt?: string; - fields?: string; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - get, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const get = mocked(global.Drive.Files).get.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -372,26 +241,8 @@ test("insert works", () => { }, }; - const insert = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required by the Drive API - mediaData?: any, - optionalArgs?: { - fields?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - insert, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const insert = mocked(global.Drive.Files).insert.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -416,26 +267,8 @@ test("insert works with optional arguments", () => { }, }; - const insert = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required by the Drive API - mediaData?: any, - optionalArgs?: { - fields?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - insert, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const insert = mocked(global.Drive.Files).insert.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -457,26 +290,8 @@ test("insert works with selective fields", () => { }, }; - const insert = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required by the Drive API - mediaData?: any, - optionalArgs?: { - fields?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - insert, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const insert = mocked(global.Drive.Files).insert.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -506,26 +321,8 @@ test("insert throws an error on invalid file", () => { userPermission: {}, }; - const insert = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required by the Drive API - mediaData?: any, - optionalArgs?: { - fields?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - insert, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const insert = mocked(global.Drive.Files).insert.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -565,25 +362,8 @@ test("list works", () => { ], }; - const list = jest - .fn< - (optionalArgs?: { - fields?: string; - includeItemsFromAllDrives?: boolean; - maxResults?: number; - pageToken?: string; - q?: string; - supportsAllDrives?: boolean; - }) => GoogleAppsScript.Drive.Schema.FileList - >() - .mockReturnValueOnce(fileList); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - list, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const list = mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); const filesCollection = new SafeFilesCollection_(); @@ -621,25 +401,8 @@ test("list works with optional arguments", () => { ], }; - const list = jest - .fn< - (optionalArgs?: { - fields?: string; - includeItemsFromAllDrives?: boolean; - maxResults?: number; - pageToken?: string; - q?: string; - supportsAllDrives?: boolean; - }) => GoogleAppsScript.Drive.Schema.FileList - >() - .mockReturnValueOnce(fileList); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - list, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const list = mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); const filesCollection = new SafeFilesCollection_(); @@ -675,25 +438,8 @@ test("list works with selective fields", () => { ], }; - const list = jest - .fn< - (optionalArgs?: { - fields?: string; - includeItemsFromAllDrives?: boolean; - maxResults?: number; - pageToken?: string; - q?: string; - supportsAllDrives?: boolean; - }) => GoogleAppsScript.Drive.Schema.FileList - >() - .mockReturnValueOnce(fileList); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - list, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const list = mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); const filesCollection = new SafeFilesCollection_(); @@ -733,25 +479,8 @@ test("list throws an error on invalid file", () => { ], }; - const list = jest - .fn< - (optionalArgs?: { - fields?: string; - includeItemsFromAllDrives?: boolean; - maxResults?: number; - pageToken?: string; - q?: string; - supportsAllDrives?: boolean; - }) => GoogleAppsScript.Drive.Schema.FileList - >() - .mockReturnValueOnce(fileList); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - list, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const list = mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); const filesCollection = new SafeFilesCollection_(); @@ -764,25 +493,8 @@ test("list throws an error on invalid file", () => { test("list throws an error on invalid file list", () => { const fileList = {}; - const list = jest - .fn< - (optionalArgs?: { - fields?: string; - includeItemsFromAllDrives?: boolean; - maxResults?: number; - pageToken?: string; - q?: string; - supportsAllDrives?: boolean; - }) => GoogleAppsScript.Drive.Schema.FileList - >() - .mockReturnValueOnce(fileList); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - list, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const list = mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); const filesCollection = new SafeFilesCollection_(); @@ -793,14 +505,11 @@ test("list throws an error on invalid file list", () => { }); test("remove works", () => { - const remove = jest.fn<(fileId: string) => void>(); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - remove, - }, - }; + global.Drive.Files = mockedFilesCollection(); + 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 + () => {}, + ); const filesCollection = new SafeFilesCollection_(); @@ -823,29 +532,8 @@ test("update works", () => { }, }; - const update = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - fileId: string, - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required by the Drive API - mediaData?: any, - optionalArgs?: { - addParents?: string; - fields?: string; - removeParents?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - update, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const update = mocked(global.Drive.Files).update.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -871,29 +559,8 @@ test("update works with optional arguments", () => { }, }; - const update = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - fileId: string, - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required by the Drive API - mediaData?: any, - optionalArgs?: { - addParents?: string; - fields?: string; - removeParents?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - update, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const update = mocked(global.Drive.Files).update.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -921,29 +588,8 @@ test("update works with selective fields", () => { }, }; - const update = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - fileId: string, - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required by the Drive API - mediaData?: any, - optionalArgs?: { - addParents?: string; - fields?: string; - removeParents?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - update, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const update = mocked(global.Drive.Files).update.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -967,29 +613,8 @@ test("update throws an error on invalid file", () => { id: "FILE_ID", }; - const update = jest - .fn< - ( - resource: GoogleAppsScript.Drive.Schema.File, - fileId: string, - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required by the Drive API - mediaData?: any, - optionalArgs?: { - addParents?: string; - fields?: string; - removeParents?: string; - supportsAllDrives?: boolean; - }, - ) => GoogleAppsScript.Drive.Schema.File - >() - .mockReturnValueOnce(file); - global.Drive = { - ...mockedDrive(), - Files: { - ...mockedFilesCollection(), - update, - }, - }; + global.Drive.Files = mockedFilesCollection(); + const update = mocked(global.Drive.Files).update.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_();