|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { describe, it, expect, vi, beforeEach, beforeAll } from "vitest"; |
| 5 | + |
| 6 | +beforeAll(() => { |
| 7 | + if (typeof CSS === "undefined" || !CSS.escape) { |
| 8 | + // @ts-ignore |
| 9 | + globalThis.CSS = { |
| 10 | + escape(value) { |
| 11 | + const str = String(value); |
| 12 | + const length = str.length; |
| 13 | + let result = ""; |
| 14 | + for (let i = 0; i < length; i++) { |
| 15 | + const code = str.charCodeAt(i); |
| 16 | + if (code === 0x0000) { |
| 17 | + result += "\uFFFD"; |
| 18 | + continue; |
| 19 | + } |
| 20 | + if ( |
| 21 | + (code >= 0x0001 && code <= 0x001f) || |
| 22 | + code === 0x007f || |
| 23 | + (i === 0 && code >= 0x0030 && code <= 0x0039) || |
| 24 | + (i === 1 && code >= 0x0030 && code <= 0x0039 && str.charCodeAt(0) === 0x002d) |
| 25 | + ) { |
| 26 | + result += `\\${code.toString(16)} `; |
| 27 | + continue; |
| 28 | + } |
| 29 | + if ( |
| 30 | + code >= 0x80 || |
| 31 | + code === 0x2d || |
| 32 | + code === 0x5f || |
| 33 | + (code >= 0x30 && code <= 0x39) || |
| 34 | + (code >= 0x41 && code <= 0x5a) || |
| 35 | + (code >= 0x61 && code <= 0x7a) |
| 36 | + ) { |
| 37 | + result += str[i]; |
| 38 | + } else { |
| 39 | + result += `\\${str[i]}`; |
| 40 | + } |
| 41 | + } |
| 42 | + return result; |
| 43 | + }, |
| 44 | + }; |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +vi.mock("../src/lib/constants.js", () => ({ |
| 49 | + GITHUB_SITE_BASE: "https://github.com", |
| 50 | + ANIMATION_DURATION: { FADE_OUT: 0, ERROR_BACKGROUND_FADE: 0 }, |
| 51 | + NOTIFICATION_TYPES: { RELEASE: "Release" }, |
| 52 | + MESSAGE_TYPES: { |
| 53 | + MARK_AS_READ: "markAsRead", |
| 54 | + OPEN_NOTIFICATION: "openNotification", |
| 55 | + }, |
| 56 | + TIME_CONVERSION: { MS_PER_MINUTE: 60000 }, |
| 57 | +})); |
| 58 | + |
| 59 | +vi.mock("../src/lib/format-utils.js", () => ({ |
| 60 | + formatReason: vi.fn((reason) => reason), |
| 61 | + getNotificationStatus: vi.fn(() => "Status"), |
| 62 | +})); |
| 63 | + |
| 64 | +vi.mock("../src/lib/icons.js", () => ({ |
| 65 | + getIconSVGElement: vi.fn(() => { |
| 66 | + const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); |
| 67 | + return svg; |
| 68 | + }), |
| 69 | +})); |
| 70 | + |
| 71 | +const sendMessage = vi.fn(); |
| 72 | +const onMarkRepoAsRead = vi.fn(); |
| 73 | + |
| 74 | +const { initRenderer, renderNotifications, clearNotificationCache } = |
| 75 | + await import("../src/popup/notification-renderer.js"); |
| 76 | + |
| 77 | +function makeNotif(id) { |
| 78 | + return { |
| 79 | + id: String(id), |
| 80 | + title: `Notification ${id}`, |
| 81 | + reason: "mention", |
| 82 | + updated_at: new Date().toISOString(), |
| 83 | + icon: "issue", |
| 84 | + type: "Issue", |
| 85 | + url: "https://github.com/owner/repo/issues/1", |
| 86 | + repository: { |
| 87 | + full_name: "owner/repo", |
| 88 | + html_url: "https://github.com/owner/repo", |
| 89 | + }, |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +describe("notification repo header links", () => { |
| 94 | + let notificationsList; |
| 95 | + let emptyState; |
| 96 | + let markAllBtn; |
| 97 | + |
| 98 | + beforeEach(() => { |
| 99 | + vi.clearAllMocks(); |
| 100 | + clearNotificationCache(); |
| 101 | + |
| 102 | + document.body.innerHTML = ` |
| 103 | + <ul id="notifications-list"></ul> |
| 104 | + <div id="empty-state" hidden></div> |
| 105 | + <button id="mark-all-btn"></button> |
| 106 | + `; |
| 107 | + |
| 108 | + notificationsList = document.getElementById("notifications-list"); |
| 109 | + emptyState = document.getElementById("empty-state"); |
| 110 | + markAllBtn = document.getElementById("mark-all-btn"); |
| 111 | + |
| 112 | + initRenderer({ |
| 113 | + notificationsList, |
| 114 | + emptyState, |
| 115 | + markAllBtn, |
| 116 | + sendMessage, |
| 117 | + getShowHoverCards: () => false, |
| 118 | + onUserAction: vi.fn(), |
| 119 | + onMarkRepoAsRead, |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it("renders separate homepage and notifications links for each repo header", () => { |
| 124 | + renderNotifications([makeNotif(1)]); |
| 125 | + |
| 126 | + const repoHeader = notificationsList.querySelector(".repo-group-header"); |
| 127 | + const repoHomeLink = repoHeader.querySelector(".repo-home-link"); |
| 128 | + const repoNotificationsLink = repoHeader.querySelector(".repo-notifications-link"); |
| 129 | + |
| 130 | + expect(repoHeader.tagName).toBe("DIV"); |
| 131 | + expect(repoHomeLink.getAttribute("href")).toBe("https://github.com/owner/repo"); |
| 132 | + expect(repoNotificationsLink.getAttribute("href")).toBe( |
| 133 | + "https://github.com/notifications?query=repo%3Aowner%2Frepo", |
| 134 | + ); |
| 135 | + expect(repoNotificationsLink.textContent).toBe("owner/repo"); |
| 136 | + }); |
| 137 | + |
| 138 | + it("keeps mark repo as read independent from repo links", () => { |
| 139 | + renderNotifications([makeNotif(1)]); |
| 140 | + |
| 141 | + const markReadBtn = notificationsList.querySelector(".repo-mark-read-btn"); |
| 142 | + markReadBtn.click(); |
| 143 | + |
| 144 | + expect(onMarkRepoAsRead).toHaveBeenCalledWith("owner/repo"); |
| 145 | + expect(sendMessage).not.toHaveBeenCalled(); |
| 146 | + }); |
| 147 | +}); |
0 commit comments