Skip to content

Commit

Permalink
Merge pull request #36427 from ruben-rebelo/ts-migration/githubutilstest
Browse files Browse the repository at this point in the history
[TS migration] Migrate GithubUtilsTest to typescript
  • Loading branch information
yuwenmemon authored Feb 26, 2024
2 parents 87eeb99 + 960ebb7 commit 4ee3b90
Showing 1 changed file with 105 additions and 34 deletions.
139 changes: 105 additions & 34 deletions tests/unit/GithubUtilsTest.js → tests/unit/GithubUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,69 @@
/* eslint-disable @typescript-eslint/naming-convention */

/**
* @jest-environment node
*/
import * as core from '@actions/core';
import type {Writable} from 'type-fest';
import GithubUtils from '../../.github/libs/GithubUtils';

const mockGetInput = jest.fn();
const mockListIssues = jest.fn();

type DeployBlockers = {
url: string;
number: number;
isResolved: boolean;
};

type PullRequest = {
url: string;
number: number;
isVerified: boolean;
};

type Label = {
id: number;
number?: number;
isVerified?: boolean;
node_id: string;
url: string;
name: string;
color: string;
default: boolean;
description: string;
};

type ExpectedReponse = {
PRList: PullRequest[];
labels: Label[];
tag: string;
title: string;
url: string;
number: number;
deployBlockers: DeployBlockers[];
internalQAPRList: string[];
isTimingDashboardChecked: boolean;
isFirebaseChecked: boolean;
isGHStatusChecked: boolean;
};

type Issue = {
url: string;
title: string;
labels: Label[];
body: string;
};

type ObjectMethodData<T> = {
data: T;
};

const asMutable = <T>(value: T): Writable<T> => value as Writable<T>;

beforeAll(() => {
// Mock core module
core.getInput = mockGetInput;
asMutable(core).getInput = mockGetInput;

// Mock octokit module
const moctokit = {
Expand All @@ -26,8 +80,10 @@ beforeAll(() => {
listForRepo: mockListIssues,
},
},
paginate: jest.fn().mockImplementation((objectMethod) => objectMethod().then(({data}) => data)),
paginate: jest.fn().mockImplementation(<T>(objectMethod: () => Promise<ObjectMethodData<T>>) => objectMethod().then(({data}) => data)),
};

// @ts-expect-error TODO: Remove this once GithubUtils (https://github.com/Expensify/App/issues/25382) is migrated to TypeScript.
GithubUtils.internalOctokit = moctokit;
});

Expand All @@ -38,7 +94,7 @@ afterEach(() => {

describe('GithubUtils', () => {
describe('getStagingDeployCash', () => {
const baseIssue = {
const baseIssue: Issue = {
url: 'https://api.github.com/repos/Andrew-Test-Org/Public-Test-Repo/issues/29',
title: 'Andrew Test Issue',
labels: [
Expand All @@ -60,7 +116,7 @@ describe('GithubUtils', () => {
issueWithDeployBlockers.body +=
'\r\n**Deploy Blockers:**\r\n- [ ] https://github.com/Expensify/App/issues/1\r\n- [x] https://github.com/Expensify/App/issues/2\r\n- [ ] https://github.com/Expensify/App/pull/1234\r\n';

const baseExpectedResponse = {
const baseExpectedResponse: ExpectedReponse = {
PRList: [
{
url: 'https://github.com/Expensify/App/pull/21',
Expand Down Expand Up @@ -119,13 +175,13 @@ describe('GithubUtils', () => {
];

test('Test finding an open issue with no PRs successfully', () => {
const bareIssue = {
const bareIssue: Issue = {
...baseIssue,
// eslint-disable-next-line max-len
body: '**Release Version:** `1.0.1-47`\r\n**Compare Changes:** https://github.com/Expensify/App/compare/production...staging\r\n\r\ncc @Expensify/applauseleads\n',
};

const bareExpectedResponse = {
const bareExpectedResponse: ExpectedReponse = {
...baseExpectedResponse,
PRList: [],
};
Expand Down Expand Up @@ -193,7 +249,9 @@ describe('GithubUtils', () => {
['https://github.com/Expensify/Expensify/issues/156481'],
['https://docs.google.com/document/d/1mMFh-m1seOES48r3zNqcvfuTvr3qOAsY6n5rP4ejdXE/edit?ts=602420d2#'],
])('getPullRequestNumberFromURL("%s")', (input) => {
expect(() => GithubUtils.getPullRequestNumberFromURL(input)).toThrow(new Error(`Provided URL ${input} is not a Github Pull Request!`));
expect(() => {
GithubUtils.getPullRequestNumberFromURL(input);
}).toThrow(new Error(`Provided URL ${input} is not a Github Pull Request!`));
});
});
});
Expand All @@ -218,7 +276,9 @@ describe('GithubUtils', () => {
['https://github.com/Expensify/Expensify/pull/156481'],
['https://docs.google.com/document/d/1mMFh-m1seOES48r3zNqcvfuTvr3qOAsY6n5rP4ejdXE/edit?ts=602420d2#'],
])('getIssueNumberFromURL("%s")', (input) => {
expect(() => GithubUtils.getIssueNumberFromURL(input)).toThrow(new Error(`Provided URL ${input} is not a Github Issue!`));
expect(() => {
GithubUtils.getIssueNumberFromURL(input);
}).toThrow(new Error(`Provided URL ${input} is not a Github Issue!`));
});
});
});
Expand Down Expand Up @@ -247,7 +307,9 @@ describe('GithubUtils', () => {
test.each([['https://www.google.com/'], ['https://docs.google.com/document/d/1mMFh-m1seOES48r3zNqcvfuTvr3qOAsY6n5rP4ejdXE/edit?ts=602420d2#']])(
'getIssueOrPullRequestNumberFromURL("%s")',
(input) => {
expect(() => GithubUtils.getIssueOrPullRequestNumberFromURL(input)).toThrow(new Error(`Provided URL ${input} is not a valid Github Issue or Pull Request!`));
expect(() => {
GithubUtils.getIssueOrPullRequestNumberFromURL(input);
}).toThrow(new Error(`Provided URL ${input} is not a valid Github Issue or Pull Request!`));
},
);
});
Expand Down Expand Up @@ -350,12 +412,13 @@ describe('GithubUtils', () => {
list: jest.fn().mockResolvedValue({data: mockPRs}),
},
},
paginate: jest.fn().mockImplementation((objectMethod) => objectMethod().then(({data}) => data)),
paginate: jest.fn().mockImplementation(<T>(objectMethod: () => Promise<ObjectMethodData<T>>) => objectMethod().then(({data}) => data)),
}),
}));

const octokit = mockGithub().getOctokit();
const githubUtils = class extends GithubUtils {};
// @ts-expect-error TODO: Remove this once GithubUtils (https://github.com/Expensify/App/issues/25382) is migrated to TypeScript.
githubUtils.internalOctokit = octokit;
const tag = '1.0.2-12';
const basePRList = [
Expand Down Expand Up @@ -404,8 +467,8 @@ describe('GithubUtils', () => {
`${lineBreak}${closedCheckbox}${basePRList[5]}` +
`${lineBreak}`;

test('Test no verified PRs', () =>
githubUtils.generateStagingDeployCashBody(tag, basePRList).then((issueBody) => {
test('Test no verified PRs', () => {
githubUtils.generateStagingDeployCashBody(tag, basePRList).then((issueBody: string) => {
expect(issueBody).toBe(
`${baseExpectedOutput}` +
`${openCheckbox}${basePRList[2]}` +
Expand All @@ -419,10 +482,11 @@ describe('GithubUtils', () => {
`${lineBreak}${openCheckbox}${ghVerification}` +
`${lineBreakDouble}${ccApplauseLeads}`,
);
}));
});
});

test('Test some verified PRs', () =>
githubUtils.generateStagingDeployCashBody(tag, basePRList, [basePRList[0]]).then((issueBody) => {
test('Test some verified PRs', () => {
githubUtils.generateStagingDeployCashBody(tag, basePRList, [basePRList[0]]).then((issueBody: string) => {
expect(issueBody).toBe(
`${baseExpectedOutput}` +
`${openCheckbox}${basePRList[2]}` +
Expand All @@ -436,10 +500,11 @@ describe('GithubUtils', () => {
`${lineBreak}${openCheckbox}${ghVerification}` +
`${lineBreakDouble}${ccApplauseLeads}`,
);
}));
});
});

test('Test all verified PRs', () =>
githubUtils.generateStagingDeployCashBody(tag, basePRList, basePRList).then((issueBody) => {
test('Test all verified PRs', () => {
githubUtils.generateStagingDeployCashBody(tag, basePRList, basePRList).then((issueBody: string) => {
expect(issueBody).toBe(
`${allVerifiedExpectedOutput}` +
`${lineBreak}${deployerVerificationsHeader}` +
Expand All @@ -448,10 +513,11 @@ describe('GithubUtils', () => {
`${lineBreak}${openCheckbox}${ghVerification}` +
`${lineBreakDouble}${ccApplauseLeads}`,
);
}));
});
});

test('Test no resolved deploy blockers', () =>
githubUtils.generateStagingDeployCashBody(tag, basePRList, basePRList, baseDeployBlockerList).then((issueBody) => {
test('Test no resolved deploy blockers', () => {
githubUtils.generateStagingDeployCashBody(tag, basePRList, basePRList, baseDeployBlockerList).then((issueBody: string) => {
expect(issueBody).toBe(
`${allVerifiedExpectedOutput}` +
`${lineBreak}${deployBlockerHeader}` +
Expand All @@ -463,10 +529,11 @@ describe('GithubUtils', () => {
`${lineBreak}${openCheckbox}${ghVerification}${lineBreak}` +
`${lineBreak}${ccApplauseLeads}`,
);
}));
});
});

test('Test some resolved deploy blockers', () =>
githubUtils.generateStagingDeployCashBody(tag, basePRList, basePRList, baseDeployBlockerList, [baseDeployBlockerList[0]]).then((issueBody) => {
test('Test some resolved deploy blockers', () => {
githubUtils.generateStagingDeployCashBody(tag, basePRList, basePRList, baseDeployBlockerList, [baseDeployBlockerList[0]]).then((issueBody: string) => {
expect(issueBody).toBe(
`${allVerifiedExpectedOutput}` +
`${lineBreak}${deployBlockerHeader}` +
Expand All @@ -478,10 +545,11 @@ describe('GithubUtils', () => {
`${lineBreak}${openCheckbox}${ghVerification}` +
`${lineBreakDouble}${ccApplauseLeads}`,
);
}));
});
});

test('Test all resolved deploy blockers', () =>
githubUtils.generateStagingDeployCashBody(tag, basePRList, basePRList, baseDeployBlockerList, baseDeployBlockerList).then((issueBody) => {
test('Test all resolved deploy blockers', () => {
githubUtils.generateStagingDeployCashBody(tag, basePRList, basePRList, baseDeployBlockerList, baseDeployBlockerList).then((issueBody: string) => {
expect(issueBody).toBe(
`${baseExpectedOutput}` +
`${closedCheckbox}${basePRList[2]}` +
Expand All @@ -498,10 +566,11 @@ describe('GithubUtils', () => {
`${lineBreak}${openCheckbox}${ghVerification}` +
`${lineBreakDouble}${ccApplauseLeads}`,
);
}));
});
});

test('Test internalQA PRs', () =>
githubUtils.generateStagingDeployCashBody(tag, [...basePRList, ...internalQAPRList]).then((issueBody) => {
test('Test internalQA PRs', () => {
githubUtils.generateStagingDeployCashBody(tag, [...basePRList, ...internalQAPRList]).then((issueBody: string) => {
expect(issueBody).toBe(
`${baseExpectedOutput}` +
`${openCheckbox}${basePRList[2]}` +
Expand All @@ -518,10 +587,11 @@ describe('GithubUtils', () => {
`${lineBreak}${openCheckbox}${ghVerification}` +
`${lineBreakDouble}${ccApplauseLeads}`,
);
}));
});
});

test('Test some verified internalQA PRs', () =>
githubUtils.generateStagingDeployCashBody(tag, [...basePRList, ...internalQAPRList], [], [], [], [internalQAPRList[0]]).then((issueBody) => {
test('Test some verified internalQA PRs', () => {
githubUtils.generateStagingDeployCashBody(tag, [...basePRList, ...internalQAPRList], [], [], [], [internalQAPRList[0]]).then((issueBody: string) => {
expect(issueBody).toBe(
`${baseExpectedOutput}` +
`${openCheckbox}${basePRList[2]}` +
Expand All @@ -538,7 +608,8 @@ describe('GithubUtils', () => {
`${lineBreak}${openCheckbox}${ghVerification}` +
`${lineBreakDouble}${ccApplauseLeads}`,
);
}));
});
});
});

describe('getPullRequestURLFromNumber', () => {
Expand Down

0 comments on commit 4ee3b90

Please sign in to comment.