Skip to content

Commit ca569c6

Browse files
authored
test: add coverage for auto merge feature (#196)
1 parent 8700220 commit ca569c6

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

e2e/e2e.spec.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,8 @@ describe("e2e tests", () => {
468468
);
469469

470470
// Pull request was created with the corrects params
471-
expect(fakeGitHub.pullRequestHandler).toHaveBeenCalledTimes(1);
472-
const request = fakeGitHub.pullRequestHandler.mock
471+
expect(fakeGitHub.createPullRequestHandler).toHaveBeenCalledTimes(1);
472+
const request = fakeGitHub.createPullRequestHandler.mock
473473
.calls[0][0] as CompletedRequest;
474474
expect(request.path).toEqual(
475475
expect.stringMatching(/bazelbuild\/bazel-central-registry/)
@@ -491,6 +491,22 @@ describe("e2e tests", () => {
491491
`https://github.com/${testOrg}/${repo}/releases/tag/${tag}`
492492
)
493493
);
494+
495+
// Pull request was updated to enable auto merge
496+
expect(fakeGitHub.updatePullRequestHandler).toHaveBeenCalledTimes(1);
497+
const updateRequest = fakeGitHub.updatePullRequestHandler.mock
498+
.calls[0][0] as CompletedRequest;
499+
expect(updateRequest.path).toEqual(
500+
expect.stringMatching(
501+
/repos\/bazelbuild\/bazel-central-registry\/pulls\/\d+/
502+
)
503+
);
504+
const updateBody = (await updateRequest.body.getJson()) as any;
505+
expect(updateBody).toEqual(
506+
expect.objectContaining({
507+
allow_auto_merge: true,
508+
})
509+
);
494510
});
495511

496512
test("happy path with multiple modules", async () => {
@@ -536,8 +552,8 @@ describe("e2e tests", () => {
536552
expect(messages.length).toEqual(0);
537553

538554
// One pull requests was created with the corrects params
539-
expect(fakeGitHub.pullRequestHandler).toHaveBeenCalledTimes(1);
540-
let request = fakeGitHub.pullRequestHandler.mock
555+
expect(fakeGitHub.createPullRequestHandler).toHaveBeenCalledTimes(1);
556+
let request = fakeGitHub.createPullRequestHandler.mock
541557
.calls[0][0] as CompletedRequest;
542558
expect(request.path).toEqual(
543559
expect.stringMatching(/bazelbuild\/bazel-central-registry/)
@@ -644,8 +660,8 @@ describe("e2e tests", () => {
644660
expect(response.status).toEqual(200);
645661

646662
// Pull request points to releaser's BCR fork
647-
expect(fakeGitHub.pullRequestHandler).toHaveBeenCalled();
648-
const request = fakeGitHub.pullRequestHandler.mock
663+
expect(fakeGitHub.createPullRequestHandler).toHaveBeenCalled();
664+
const request = fakeGitHub.createPullRequestHandler.mock
649665
.calls[0][0] as CompletedRequest;
650666
const body = (await request.body.getJson()) as any;
651667
expect(body).toEqual(

e2e/stubs/fake-github.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { User } from "@octokit/webhooks-types";
22
import { randomUUID } from "crypto";
33
import * as mockttp from "mockttp";
4+
import { randomInt } from "node:crypto";
45
import url from "node:url";
56
import { StubbedServer } from "./stubbed-server";
67

@@ -18,7 +19,8 @@ export class FakeGitHub implements StubbedServer {
1819
{ owner: string; repo: string; sourceOwner?: string; sourceRepo?: string }
1920
>();
2021

21-
public readonly pullRequestHandler = jest.fn();
22+
public readonly createPullRequestHandler = jest.fn();
23+
public readonly updatePullRequestHandler = jest.fn();
2224
public readonly installationTokenHandler = jest.fn();
2325

2426
public constructor() {
@@ -38,14 +40,16 @@ export class FakeGitHub implements StubbedServer {
3840
this.setupGetOwnedReposHandler(),
3941
this.setupGetRepoHandler(),
4042
this.setupCreatePullHandler(),
43+
this.setupUpdatePullHandler(),
4144
this.setupAppHandler(),
4245
]);
4346
}
4447

4548
public async reset(): Promise<void> {
4649
this.server.reset();
4750
this.clearMockedData();
48-
this.pullRequestHandler.mockReset();
51+
this.createPullRequestHandler.mockReset();
52+
this.updatePullRequestHandler.mockReset();
4953
this.installationTokenHandler.mockReset();
5054
await this.setupHandlers();
5155
}
@@ -262,16 +266,31 @@ export class FakeGitHub implements StubbedServer {
262266
}
263267

264268
private async setupCreatePullHandler(): Promise<void> {
265-
this.pullRequestHandler.mockImplementation((request) => {
269+
this.createPullRequestHandler.mockImplementation((request) => {
266270
return {
267271
statusCode: 201,
268-
body: "{}",
272+
json: {
273+
number: randomInt(100),
274+
},
269275
};
270276
});
271277

272278
await this.server
273279
.forPost("/repos/bazelbuild/bazel-central-registry/pulls")
274-
.thenCallback((request) => this.pullRequestHandler(request));
280+
.thenCallback((request) => this.createPullRequestHandler(request));
281+
}
282+
283+
private async setupUpdatePullHandler(): Promise<void> {
284+
this.updatePullRequestHandler.mockImplementation((request) => {
285+
return {
286+
statusCode: 200,
287+
json: {},
288+
};
289+
});
290+
291+
await this.server
292+
.forPatch(/\/repos\/bazelbuild\/bazel-central-registry\/pulls\/\d+$/)
293+
.thenCallback((request) => this.updatePullRequestHandler(request));
275294
}
276295

277296
private async setupAppHandler(): Promise<void> {

0 commit comments

Comments
 (0)