Skip to content

Commit 40f5424

Browse files
committed
refactor(index.ts): updates index to use instance an of a GitHub class
- Updates `fetchRecentlyClosedPullRequests` in `index` to use the new version of `GitHub` that requires creating an instance that is passed in the auth token. - Updates `fetchRecentlyClosedPullRequests` to require an `auth_token` is passed in - Updates tests for `fetchRecentlyClosedPullRequests` to use different jest mocking functionality and mock out the implementation of `GitHub`. Now, instead of passing in a fake module that matches `GitHub` we use magic jest mocking to create a mocked GitHub import which lets us mock the constructor. BREAKING CHANGE: Updates the function `fetchRecentlyClosedPullRequests` exported from `index.ts` to now take in the auth token. This allows for more flexibilty in that the auth token no longer **has** to be fetched from an environment variable named `GIT
1 parent 3f4298f commit 40f5424

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

src/github.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ interface IGitHubGraphqlArgs {
7373
}
7474

7575
export class GitHub {
76-
token: string;
76+
private token: string;
7777

7878
constructor({ authToken }: { authToken: string }) {
7979
this.token = authToken;

src/index.test.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ import { fetchRecentlyClosedPullRequests } from "./index";
44
// Reset Date back to the real date
55
afterEach(() => MockDate.reset());
66

7-
class FakeService {
8-
public static async recentlyClosedPullRequests(): Promise<any> {
9-
return {};
10-
}
11-
public static __searchQueryString(): string {
12-
return "";
13-
}
14-
}
15-
16-
const fakeApiCall = (FakeService.recentlyClosedPullRequests = jest.fn());
17-
187
test("dates default to the last week", () => {
198
MockDate.set("1/8/2019");
209

21-
fetchRecentlyClosedPullRequests({ organization: "my-org" }, FakeService);
10+
jest.mock("./github");
11+
const MockedGitHub = require("./github").GitHub;
12+
13+
const fakeApiCall = jest.fn().mockImplementation();
14+
MockedGitHub.mockImplementation(() => {
15+
return {
16+
recentlyClosedPullRequests: fakeApiCall
17+
};
18+
});
19+
20+
fetchRecentlyClosedPullRequests(
21+
{ organization: "my-org" },
22+
"fake-token",
23+
MockedGitHub
24+
);
2225

2326
expect(fakeApiCall).toHaveBeenCalledWith({
2427
endDate: new Date("1/7/2019"),

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ interface IRequestParams {
77

88
export function fetchRecentlyClosedPullRequests(
99
requestParams: IRequestParams,
10-
service = GitHub
10+
authToken: string,
11+
Service = GitHub
1112
) {
1213
const dates = {
1314
endDate: subDays(new Date(), 1),
@@ -21,5 +22,5 @@ export function fetchRecentlyClosedPullRequests(
2122
} that were closed between ${dates.startDate} and ${dates.endDate}`
2223
);
2324

24-
return service.recentlyClosedPullRequests(args);
25+
return new Service({ authToken }).recentlyClosedPullRequests(args);
2526
}

0 commit comments

Comments
 (0)