|
| 1 | +const mergify = require("../mergify"); |
1 | 2 | const { |
2 | 3 | MergifyCache, |
3 | 4 | findTimelineActions, |
|
7 | 8 | convertMergifyTimestamps, |
8 | 9 | isMergifyBotComment, |
9 | 10 | formatLocalTime, |
10 | | -} = require("../mergify"); |
| 11 | + buildPRListMergifyFilterButton, |
| 12 | + togglePrListFilter, |
| 13 | +} = mergify; |
11 | 14 | const { loadFixture, injectFixtureInDOM } = require("./utils"); |
12 | 15 |
|
13 | 16 | describe("MergifyCache", () => { |
@@ -336,6 +339,7 @@ describe("getMergifyConfigurationStatus", () => { |
336 | 339 | ); |
337 | 340 | }); |
338 | 341 |
|
| 342 | + |
339 | 343 | it("should not update cache if cached value matches search result", async () => { |
340 | 344 | const cache = new MergifyCache(); |
341 | 345 | cache.update("test-org", "test-repo", true); |
@@ -460,3 +464,125 @@ describe("convertMergifyTimestamps", () => { |
460 | 464 | expect(code.textContent).toBe("9999-99-99 99:99 UTC"); |
461 | 465 | }); |
462 | 466 | }); |
| 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 | +}); |
0 commit comments