Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions src/__tests__/mergify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,84 @@ describe("isPullRequestOpen", () => {
document.body.innerHTML = "";
});

it("should get opened pull request status", () => {
it("should detect open PR via legacy span.State (fixture)", () => {
injectFixtureInDOM("github_pr_opened");

const status = isPullRequestOpen();

expect(status).toBe(true);
});

it("should get opened pull request status", () => {
it("should detect merged PR via legacy span.State (fixture)", () => {
injectFixtureInDOM("github_pr_merged");

const status = isPullRequestOpen();

expect(status).toBe(false);
});

it("should detect open PR via data-status=pullOpened attribute", () => {
document.body.innerHTML = '<span data-status="pullOpened">Open</span>';

expect(isPullRequestOpen()).toBe(true);
});

it("should detect draft PR via data-status=draft attribute", () => {
document.body.innerHTML = '<span data-status="draft">Draft</span>';

expect(isPullRequestOpen()).toBe(true);
});

it("should detect closed PR via data-status=pullClosed attribute", () => {
document.body.innerHTML =
'<span data-status="pullClosed">Closed</span>';

expect(isPullRequestOpen()).toBe(false);
});

it("should detect merged PR via data-status=pullMerged attribute", () => {
document.body.innerHTML =
'<span data-status="pullMerged">Merged</span>';

expect(isPullRequestOpen()).toBe(false);
});

it("should prefer data-status over legacy span.State", () => {
document.body.innerHTML =
'<span data-status="pullOpened">Open</span>' +
'<span class="State" title="Status: Closed">Closed</span>';

expect(isPullRequestOpen()).toBe(true);
});

it("should detect closed PR via legacy span.State when no data-status", () => {
document.body.innerHTML =
'<span class="State" title="Status: Closed">Closed</span>';

expect(isPullRequestOpen()).toBe(false);
});

it("should assume open when no status element is found", () => {
document.body.innerHTML = "<div>No status here</div>";

expect(isPullRequestOpen()).toBe(true);
});

it("should assume open when legacy span.State has no parseable title", () => {
const consoleSpy = jest
.spyOn(console, "warn")
.mockImplementation(() => {});

document.body.innerHTML =
'<span class="State" title="Malformed">Badge</span>';

expect(isPullRequestOpen()).toBe(true);
expect(consoleSpy).toHaveBeenCalledWith(
"Can't find pull request status",
);

consoleSpy.mockRestore();
});
});

describe("isMergifyEnabledOnTheRepo caching behavior", () => {
Expand Down
3 changes: 3 additions & 0 deletions src/mergify.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ function postCommand(command) {
function isPullRequestOpen() {
const opened = document.querySelector("span[data-status=pullOpened]");
const draft = document.querySelector("span[data-status=draft]");
const closed = document.querySelector("span[data-status=pullClosed]");
const merged = document.querySelector("span[data-status=pullMerged]");
if (opened || draft) return true;
if (closed || merged) return false;

const oldStatusBadge = document.querySelector("span.State");
if (oldStatusBadge) {
Expand Down