Skip to content

Commit

Permalink
Merge pull request #1740 from skaut/type-cleanup
Browse files Browse the repository at this point in the history
Cleaned up optional properties and test mocks
  • Loading branch information
marekdedic authored Sep 12, 2024
2 parents 02325b1 + dd02d98 commit 8028319
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 698 deletions.
31 changes: 11 additions & 20 deletions __tests__/backend/doGet.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
Expand Down
24 changes: 8 additions & 16 deletions __tests__/backend/listFolders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
1 change: 0 additions & 1 deletion __tests__/backend/listSharedDrives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ test("listSharedDrives works correctly", () => {
];
const rawResponse = {
items: response,
nextPageToken: undefined,
};
const driveServiceMock = mockedSafeDriveService();
driveServiceMock.Drives.list.mockReturnValueOnce(rawResponse);
Expand Down
179 changes: 34 additions & 145 deletions __tests__/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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_();

Expand Down Expand Up @@ -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_();

Expand Down Expand Up @@ -154,25 +133,10 @@ test("list works", () => {
],
};

const list = jest
.fn<
(
fileId: string,
optionalArgs?: {
fields?: string;
maxResults?: number;
pageToken?: string | undefined;
},
) => 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_();

Expand Down Expand Up @@ -223,25 +187,10 @@ test("list works with optional arguments", () => {
],
};

const list = jest
.fn<
(
fileId: string,
optionalArgs?: {
fields?: string;
maxResults?: number;
pageToken?: string | undefined;
},
) => 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_();

Expand Down Expand Up @@ -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 | undefined;
},
) => 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_();

Expand All @@ -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 | undefined;
},
) => 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_();

Expand Down Expand Up @@ -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 | undefined;
},
) => 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_();

Expand Down Expand Up @@ -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 | undefined;
},
) => 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_();

Expand Down
Loading

0 comments on commit 8028319

Please sign in to comment.