Skip to content

Commit 90be548

Browse files
committed
feat: add tests to the PR filter buttons
1 parent 61a4924 commit 90be548

2 files changed

Lines changed: 136 additions & 2 deletions

File tree

src/__tests__/mergify.test.js

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const mergify = require("../mergify");
12
const {
23
MergifyCache,
34
findTimelineActions,
@@ -7,7 +8,9 @@ const {
78
convertMergifyTimestamps,
89
isMergifyBotComment,
910
formatLocalTime,
10-
} = require("../mergify");
11+
buildPRListMergifyFilterButton,
12+
togglePrListFilter,
13+
} = mergify;
1114
const { loadFixture, injectFixtureInDOM } = require("./utils");
1215

1316
describe("MergifyCache", () => {
@@ -336,6 +339,7 @@ describe("getMergifyConfigurationStatus", () => {
336339
);
337340
});
338341

342+
339343
it("should not update cache if cached value matches search result", async () => {
340344
const cache = new MergifyCache();
341345
cache.update("test-org", "test-repo", true);
@@ -460,3 +464,125 @@ describe("convertMergifyTimestamps", () => {
460464
expect(code.textContent).toBe("9999-99-99 99:99 UTC");
461465
});
462466
});
467+
468+
describe("togglePrListFilter", () => {
469+
beforeEach(() => {
470+
jest.spyOn(console, "error").mockImplementation(() => {});
471+
});
472+
473+
afterEach(() => {
474+
jest.restoreAllMocks();
475+
});
476+
477+
function setURL(path) {
478+
History.prototype.pushState.call(window.history, {}, "", path);
479+
}
480+
481+
function navigatedQuery() {
482+
return new URL(mergify._navigation).searchParams.get("q");
483+
}
484+
485+
it("should add -author:app/mergify, is:pr, and is:open when no query exists", () => {
486+
setURL("/org/repo/pulls");
487+
togglePrListFilter();
488+
const q = navigatedQuery();
489+
expect(q).toContain("-author:app/mergify");
490+
expect(q).toContain("is:pr");
491+
expect(q).toContain("is:open");
492+
});
493+
494+
it("should remove -author:app/mergify when already present", () => {
495+
setURL("/org/repo/pulls?q=is%3Apr+is%3Aopen+-author%3Aapp%2Fmergify");
496+
togglePrListFilter();
497+
expect(navigatedQuery()).not.toContain("-author:app/mergify");
498+
});
499+
500+
it("should not add is:open when is:open is already in the query", () => {
501+
setURL("/org/repo/pulls?q=is%3Aopen+label%3Abug");
502+
togglePrListFilter();
503+
const q = navigatedQuery();
504+
expect((q.match(/is:open/g) || []).length).toBe(1);
505+
expect(q).toContain("-author:app/mergify");
506+
});
507+
508+
it("should not add is:open when is:closed is already in the query", () => {
509+
setURL("/org/repo/pulls?q=is%3Aclosed");
510+
togglePrListFilter();
511+
const q = navigatedQuery();
512+
expect(q).not.toContain("is:open");
513+
expect(q).toContain("is:closed");
514+
expect(q).toContain("-author:app/mergify");
515+
});
516+
517+
it("should always add is:pr to the query", () => {
518+
setURL("/org/repo/pulls?q=label%3Abug");
519+
togglePrListFilter();
520+
expect(navigatedQuery()).toContain("is:pr");
521+
});
522+
523+
it("should preserve existing query terms when toggling on", () => {
524+
setURL("/org/repo/pulls?q=is%3Aopen+label%3Abug");
525+
togglePrListFilter();
526+
const q = navigatedQuery();
527+
expect(q).toContain("label:bug");
528+
expect(q).toContain("-author:app/mergify");
529+
});
530+
531+
it("should preserve existing query terms when toggling off", () => {
532+
setURL("/org/repo/pulls?q=is%3Aopen+label%3Abug+-author%3Aapp%2Fmergify");
533+
togglePrListFilter();
534+
const q = navigatedQuery();
535+
expect(q).toContain("label:bug");
536+
expect(q).not.toContain("-author:app/mergify");
537+
});
538+
});
539+
540+
describe("buildPRListMergifyFilterButton", () => {
541+
afterEach(() => {
542+
document.body.innerHTML = "";
543+
});
544+
545+
it("should create a button with the correct id", () => {
546+
History.prototype.pushState.call(
547+
window.history,
548+
{},
549+
"",
550+
"/org/repo/pulls",
551+
);
552+
const btn = buildPRListMergifyFilterButton();
553+
expect(btn.id).toBe("mergify-pr-list-filter");
554+
});
555+
556+
it("should set aria-pressed=false when filter is inactive", () => {
557+
History.prototype.pushState.call(
558+
window.history,
559+
{},
560+
"",
561+
"/org/repo/pulls",
562+
);
563+
const btn = buildPRListMergifyFilterButton();
564+
expect(btn.getAttribute("aria-pressed")).toBe("false");
565+
});
566+
567+
it("should set aria-pressed=true when filter is active", () => {
568+
History.prototype.pushState.call(
569+
window.history,
570+
{},
571+
"",
572+
"/org/repo/pulls?q=-author%3Aapp%2Fmergify",
573+
);
574+
const btn = buildPRListMergifyFilterButton();
575+
expect(btn.getAttribute("aria-pressed")).toBe("true");
576+
});
577+
578+
it("should wire onclick to togglePrListFilter", () => {
579+
History.prototype.pushState.call(
580+
window.history,
581+
{},
582+
"",
583+
"/org/repo/pulls",
584+
);
585+
const btn = buildPRListMergifyFilterButton();
586+
expect(btn.onclick).toBe(togglePrListFilter);
587+
});
588+
});

src/mergify.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ function postCommand(command) {
157157
button.click();
158158
}
159159

160+
let _navigation = window.location.href;
161+
162+
function navigate(href) {
163+
_navigation = href;
164+
window.location.href = href;
165+
}
166+
160167
function togglePrListFilter() {
161168
debug("Applying PR list filter");
162169
const url = new URL(window.location.href);
@@ -176,7 +183,7 @@ function togglePrListFilter() {
176183
}
177184
debug("New query components:", components);
178185
url.searchParams.set("q", Array.from(components).join(" "));
179-
window.location.href = url.href;
186+
navigate(url.href);
180187
}
181188

182189
function isPullRequestOpen() {
@@ -723,5 +730,6 @@ try {
723730
formatLocalTime,
724731
buildPRListMergifyFilterButton,
725732
togglePrListFilter,
733+
get _navigation() { return _navigation; },
726734
};
727735
} catch (_error) {}

0 commit comments

Comments
 (0)