|
| 1 | +const Repository = require("../../lib/modules/repository"); |
| 2 | +const { BranchNotFoundError } = require("../../lib/modules/errors"); |
| 3 | +const setupRepository = require("../../lib/setup-repository"); |
| 4 | + |
| 5 | +jest.mock("../../lib/modules/repository"); |
| 6 | + |
| 7 | +describe("Setup Repository", () => { |
| 8 | + const mockBranchName = "all-contributors/add-hertzg-tenshiAMD"; |
| 9 | + const mockContext = { |
| 10 | + log: { |
| 11 | + debug() {}, |
| 12 | + }, |
| 13 | + }; |
| 14 | + |
| 15 | + afterEach(() => jest.clearAllMocks()); |
| 16 | + |
| 17 | + it("should base off latest on first time request", async () => { |
| 18 | + Repository.prototype.getRef.mockReturnValue( |
| 19 | + Promise.reject(new BranchNotFoundError(mockBranchName)) |
| 20 | + ); |
| 21 | + |
| 22 | + await setupRepository({ branchName: mockBranchName, context: mockContext }); |
| 23 | + |
| 24 | + expect(Repository.prototype.getRef).toBeCalledWith(mockBranchName); |
| 25 | + expect(Repository.prototype.deleteBranch).not.toBeCalled(); |
| 26 | + expect(Repository.prototype.getPullRequestsBy).not.toBeCalled(); |
| 27 | + expect(Repository.prototype.setBaseBranch).not.toBeCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should delete existing dangling (already merged) branches then recreate new pr", async () => { |
| 31 | + Repository.prototype.getRef.mockReturnValue(Promise.resolve()); |
| 32 | + Repository.prototype.getPullRequestsBy.mockReturnValue( |
| 33 | + Promise.resolve({ data: [] }) |
| 34 | + ); |
| 35 | + |
| 36 | + await setupRepository({ branchName: mockBranchName, context: mockContext }); |
| 37 | + |
| 38 | + expect(Repository.prototype.getRef).toBeCalledWith(mockBranchName); |
| 39 | + expect(Repository.prototype.deleteBranch).toBeCalledTimes(1); |
| 40 | + expect(Repository.prototype.setBaseBranch).toBeCalledTimes(1); |
| 41 | + expect(Repository.prototype.getPullRequestsBy).toBeCalledTimes(1); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should use existing branch only if pr for it was not merged yet", async () => { |
| 45 | + Repository.prototype.getRef.mockReturnValue(Promise.resolve()); |
| 46 | + Repository.prototype.getPullRequestsBy.mockReturnValue( |
| 47 | + Promise.resolve({ data: [{}] }) |
| 48 | + ); |
| 49 | + |
| 50 | + await setupRepository({ branchName: mockBranchName, context: mockContext }); |
| 51 | + |
| 52 | + expect(Repository.prototype.getRef).toBeCalledWith(mockBranchName); |
| 53 | + expect(Repository.prototype.deleteBranch).not.toBeCalled(); |
| 54 | + expect(Repository.prototype.getPullRequestsBy).toBeCalledTimes(1); |
| 55 | + expect(Repository.prototype.setBaseBranch).toBeCalledTimes(1); |
| 56 | + }); |
| 57 | +}); |
0 commit comments