Skip to content

Commit

Permalink
Merge pull request #1732 from skaut/no-non-null-assertion
Browse files Browse the repository at this point in the history
No non-null assertions
  • Loading branch information
marekdedic authored Sep 11, 2024
2 parents dfb5735 + 55cd25e commit 02325b1
Show file tree
Hide file tree
Showing 35 changed files with 4,034 additions and 1,472 deletions.
180 changes: 78 additions & 102 deletions __tests__/backend/listFolders.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { expect, jest, test } from "@jest/globals";
import { mocked } from "jest-mock";

import { listFolders } from "../../src/backend/listFolders";
import {
mockedDrive,
mockedFilesCollection,
mockedSession,
} from "../test-utils/gas-stubs";
import { SafeDriveService_ } from "../../src/backend/utils/SafeDriveService";
import { mockedSession } from "../test-utils/gas-stubs";
import { mockedSafeDriveService } from "../test-utils/SafeDriveService-stub";

/* eslint-disable @typescript-eslint/naming-convention -- Properties are mock classes */
jest.mock<{ SafeDriveService_: jest.Mock }>(
"../../src/backend/utils/SafeDriveService",
() => ({
SafeDriveService_: jest.fn(),
}),
);
/* eslint-enable */

test("listFolders works correctly", () => {
interface ListFilesOptions {
fields?: string;
includeItemsFromAllDrives?: boolean;
maxResults?: number;
pageToken?: string;
Expand All @@ -24,20 +31,9 @@ test("listFolders works correctly", () => {
],
nextPageToken: undefined,
};
const list = jest
.fn<
(
optionalArgs?: ListFilesOptions,
) => GoogleAppsScript.Drive.Schema.FileList
>()
.mockReturnValueOnce(rawResponse);
global.Drive = {
...mockedDrive(),
Files: {
...mockedFilesCollection(),
list,
},
};
const driveServiceMock = mockedSafeDriveService();
driveServiceMock.Files.list.mockReturnValueOnce(rawResponse);
mocked(SafeDriveService_).mockReturnValueOnce(driveServiceMock);

global.Session = {
...mockedSession(),
Expand All @@ -51,20 +47,27 @@ test("listFolders works correctly", () => {
],
status: "success",
});
expect(list.mock.calls).toHaveLength(1);
expect(list.mock.calls[0][0]).toBeDefined();
expect(list.mock.calls[0][0]!.q).toContain("ID_PARENT");
expect(list.mock.calls[0][0]!.includeItemsFromAllDrives).toBe(true);
expect(list.mock.calls[0][0]!.supportsAllDrives).toBe(true);
expect(list.mock.calls[0][0]!.pageToken).toBeUndefined();
expect(driveServiceMock.Files.list.mock.calls).toHaveLength(1);
expect(driveServiceMock.Files.list.mock.calls[0][1]).toBeDefined();
expect(
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions).q,
).toContain("ID_PARENT");
expect(
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions)
.includeItemsFromAllDrives,
).toBe(true);
expect(
list.mock.calls[0][0]!.fields!.split(",").map((s) => s.trim()),
).toContain("nextPageToken");
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions)
.supportsAllDrives,
).toBe(true);
expect(
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions)
.pageToken,
).toBeUndefined();
});

test("listFolders works correctly with shortcuts", () => {
interface ListFilesOptions {
fields?: string;
includeItemsFromAllDrives?: boolean;
maxResults?: number;
pageToken?: string;
Expand All @@ -86,20 +89,9 @@ test("listFolders works correctly with shortcuts", () => {
],
nextPageToken: undefined,
};
const list = jest
.fn<
(
optionalArgs?: ListFilesOptions,
) => GoogleAppsScript.Drive.Schema.FileList
>()
.mockReturnValueOnce(rawResponse);
global.Drive = {
...mockedDrive(),
Files: {
...mockedFilesCollection(),
list,
},
};
const driveServiceMock = mockedSafeDriveService();
driveServiceMock.Files.list.mockReturnValueOnce(rawResponse);
mocked(SafeDriveService_).mockReturnValueOnce(driveServiceMock);

global.Session = {
...mockedSession(),
Expand All @@ -113,43 +105,31 @@ test("listFolders works correctly with shortcuts", () => {
],
status: "success",
});
expect(list.mock.calls).toHaveLength(1);
expect(list.mock.calls[0][0]).toBeDefined();
expect(list.mock.calls[0][0]!.q).toContain("ID_PARENT");
expect(list.mock.calls[0][0]!.includeItemsFromAllDrives).toBe(true);
expect(list.mock.calls[0][0]!.supportsAllDrives).toBe(true);
expect(list.mock.calls[0][0]!.pageToken).toBeUndefined();
expect(driveServiceMock.Files.list.mock.calls).toHaveLength(1);
expect(driveServiceMock.Files.list.mock.calls[0][1]).toBeDefined();
expect(
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions).q,
).toContain("ID_PARENT");
expect(
list.mock.calls[0][0]!.fields!.split(",").map((s) => s.trim()),
).toContain("nextPageToken");
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions)
.includeItemsFromAllDrives,
).toBe(true);
expect(
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions)
.supportsAllDrives,
).toBe(true);
expect(
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions)
.pageToken,
).toBeUndefined();
});

test("listFolders handles invalid parameters gracefully", () => {
interface ListFilesOptions {
fields?: string;
includeItemsFromAllDrives?: boolean;
maxResults?: number;
pageToken?: string;
q?: string;
supportsAllDrives?: boolean;
}

const list = jest
.fn<
(
optionalArgs?: ListFilesOptions,
) => GoogleAppsScript.Drive.Schema.FileList
>()
.mockImplementationOnce(() => {
throw new Error();
});
global.Drive = {
...mockedDrive(),
Files: {
...mockedFilesCollection(),
list,
},
};
const driveServiceMock = mockedSafeDriveService();
driveServiceMock.Files.list.mockImplementationOnce(() => {
throw new Error();
});
mocked(SafeDriveService_).mockReturnValueOnce(driveServiceMock);

global.Session = {
...mockedSession(),
Expand All @@ -160,35 +140,23 @@ test("listFolders handles invalid parameters gracefully", () => {
status: "error",
type: "invalidParameter",
});
expect(list.mock.calls).toHaveLength(0);
expect(driveServiceMock.Files.list.mock.calls).toHaveLength(0);
});

test("listFolders handles errors in Google Drive API gracefully", () => {
interface ListFilesOptions {
fields?: string;
includeItemsFromAllDrives?: boolean;
maxResults?: number;
pageToken?: string;
q?: string;
supportsAllDrives?: boolean;
}

const list = jest
.fn<
(
optionalArgs?: ListFilesOptions,
) => GoogleAppsScript.Drive.Schema.FileList
>()
.mockImplementationOnce(() => {
throw new Error();
});
global.Drive = {
...mockedDrive(),
Files: {
...mockedFilesCollection(),
list,
},
};
const driveServiceMock = mockedSafeDriveService();
driveServiceMock.Files.list.mockImplementationOnce(() => {
throw new Error();
});
mocked(SafeDriveService_).mockReturnValueOnce(driveServiceMock);

global.Session = {
...mockedSession(),
Expand All @@ -199,13 +167,21 @@ test("listFolders handles errors in Google Drive API gracefully", () => {
status: "error",
type: "DriveAPIError",
});
expect(list.mock.calls).toHaveLength(1);
expect(list.mock.calls[0][0]).toBeDefined();
expect(list.mock.calls[0][0]!.q).toContain("ID_PARENT");
expect(list.mock.calls[0][0]!.includeItemsFromAllDrives).toBe(true);
expect(list.mock.calls[0][0]!.supportsAllDrives).toBe(true);
expect(list.mock.calls[0][0]!.pageToken).toBeUndefined();
expect(driveServiceMock.Files.list.mock.calls).toHaveLength(1);
expect(driveServiceMock.Files.list.mock.calls[0][1]).toBeDefined();
expect(
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions).q,
).toContain("ID_PARENT");
expect(
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions)
.includeItemsFromAllDrives,
).toBe(true);
expect(
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions)
.supportsAllDrives,
).toBe(true);
expect(
list.mock.calls[0][0]!.fields!.split(",").map((s) => s.trim()),
).toContain("nextPageToken");
(driveServiceMock.Files.list.mock.calls[0][1] as ListFilesOptions)
.pageToken,
).toBeUndefined();
});
87 changes: 42 additions & 45 deletions __tests__/backend/listSharedDrives.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { expect, jest, test } from "@jest/globals";
import { mocked } from "jest-mock";

import { listSharedDrives } from "../../src/backend/listSharedDrives";
import { mockedDrive, mockedDrivesCollection } from "../test-utils/gas-stubs";
import { SafeDriveService_ } from "../../src/backend/utils/SafeDriveService";
import { mockedSafeDriveService } from "../test-utils/SafeDriveService-stub";

/* eslint-disable @typescript-eslint/naming-convention -- Properties are mock classes */
jest.mock<{ SafeDriveService_: jest.Mock }>(
"../../src/backend/utils/SafeDriveService",
() => ({
SafeDriveService_: jest.fn(),
}),
);
/* eslint-enable */

test("listSharedDrives works correctly", () => {
interface ListDrivesOptions {
fields?: string;
maxResults?: number;
orderBy?: string;
pageToken?: string;
Expand All @@ -19,65 +29,52 @@ test("listSharedDrives works correctly", () => {
items: response,
nextPageToken: undefined,
};
const list = jest
.fn<
(
optionalArgs?: ListDrivesOptions,
) => GoogleAppsScript.Drive.Schema.DriveList
>()
.mockReturnValueOnce(rawResponse);
global.Drive = {
...mockedDrive(),
Drives: {
...mockedDrivesCollection(),
list,
},
};
const driveServiceMock = mockedSafeDriveService();
driveServiceMock.Drives.list.mockReturnValueOnce(rawResponse);
mocked(SafeDriveService_).mockReturnValueOnce(driveServiceMock);

expect(listSharedDrives()).toStrictEqual({ response, status: "success" });
expect(list.mock.calls).toHaveLength(1);
expect(list.mock.calls[0][0]).toBeDefined();
expect(list.mock.calls[0][0]!.pageToken).toBeUndefined();
expect(list.mock.calls[0][0]!.orderBy).toBe("name");

expect(mocked(SafeDriveService_).mock.calls).toHaveLength(1);
expect(driveServiceMock.Drives.list.mock.calls).toHaveLength(1);
expect(driveServiceMock.Drives.list.mock.calls[0][1]).toBeDefined();
expect(
(driveServiceMock.Drives.list.mock.calls[0][1] as ListDrivesOptions)
.pageToken,
).toBeUndefined();
expect(
list.mock.calls[0][0]!.fields!.split(",").map((s) => s.trim()),
).toContain("nextPageToken");
(driveServiceMock.Drives.list.mock.calls[0][1] as ListDrivesOptions)
.orderBy,
).toBe("name");
});

test("listSharedDrives handles Drive API error gracefully", () => {
interface ListDrivesOptions {
fields?: string;
maxResults?: number;
orderBy?: string;
pageToken?: string;
}

const list = jest
.fn<
(
optionalArgs?: ListDrivesOptions,
) => GoogleAppsScript.Drive.Schema.DriveList
>()
.mockImplementationOnce(() => {
throw new Error();
});
global.Drive = {
...mockedDrive(),
Drives: {
...mockedDrivesCollection(),
list,
},
};
const driveServiceMock = mockedSafeDriveService();
driveServiceMock.Drives.list.mockImplementationOnce(() => {
throw new Error();
});
mocked(SafeDriveService_).mockReturnValueOnce(driveServiceMock);

expect(listSharedDrives()).toStrictEqual({
status: "error",
type: "DriveAPIError",
});
expect(list.mock.calls).toHaveLength(1);
expect(list.mock.calls[0][0]).toBeDefined();
expect(list.mock.calls[0][0]!.pageToken).toBeUndefined();
expect(list.mock.calls[0][0]!.orderBy).toBe("name");

expect(mocked(SafeDriveService_).mock.calls).toHaveLength(1);
expect(driveServiceMock.Drives.list.mock.calls).toHaveLength(1);
expect(driveServiceMock.Drives.list.mock.calls[0][1]).toBeDefined();
expect(
(driveServiceMock.Drives.list.mock.calls[0][1] as ListDrivesOptions)
.pageToken,
).toBeUndefined();
expect(
list.mock.calls[0][0]!.fields!.split(",").map((s) => s.trim()),
).toContain("nextPageToken");
(driveServiceMock.Drives.list.mock.calls[0][1] as ListDrivesOptions)
.orderBy,
).toBe("name");
});
Loading

0 comments on commit 02325b1

Please sign in to comment.