Skip to content

Commit d49729e

Browse files
committed
test: update mocks and add tests for new shared exports
- Add classifyError tests in format-utils.test.js - Add buildProfileUrl tests in url-builder.test.js - Add CONCURRENCY and classifyError to service-worker test mocks - Add GITHUB_SITE_BASE to notification-renderer test mock
1 parent 670ff7c commit d49729e

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

tests/format-utils.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
formatState,
66
getNotificationStatus,
77
escapeHtml,
8+
classifyError,
89
} from "../src/lib/format-utils.js";
910

1011
describe("formatReason", () => {
@@ -138,3 +139,28 @@ describe("escapeHtml", () => {
138139
expect(escapeHtml(true)).toBe("true");
139140
});
140141
});
142+
143+
describe("classifyError", () => {
144+
it('should classify rate limit errors as "rate-limited"', () => {
145+
expect(classifyError(new Error("Rate limited until 12:00"))).toBe("rate-limited");
146+
});
147+
148+
it('should classify timeout errors as "timeout"', () => {
149+
expect(classifyError(new Error("Request timeout after 30s"))).toBe("timeout");
150+
});
151+
152+
it('should classify network errors as "offline"', () => {
153+
expect(classifyError(new Error("NetworkError when attempting to fetch"))).toBe("offline");
154+
expect(classifyError(new Error("Failed to fetch"))).toBe("offline");
155+
});
156+
157+
it('should classify other errors as "unknown"', () => {
158+
expect(classifyError(new Error("Something went wrong"))).toBe("unknown");
159+
});
160+
161+
it("should handle null/undefined errors", () => {
162+
expect(classifyError(null)).toBe("unknown");
163+
expect(classifyError(undefined)).toBe("unknown");
164+
expect(classifyError({})).toBe("unknown");
165+
});
166+
});

tests/notification-renderer.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ beforeAll(() => {
4848

4949
// Mock dependencies before importing
5050
vi.mock("../src/lib/constants.js", () => ({
51+
GITHUB_SITE_BASE: "https://github.com",
5152
ANIMATION_DURATION: {
5253
FADE_OUT: 200,
5354
ERROR_BACKGROUND_FADE: 1000,

tests/service-worker.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,23 @@ vi.mock("../src/lib/constants.js", () => ({
134134
Release: "release",
135135
CheckSuite: "actions",
136136
},
137+
CONCURRENCY: {
138+
PRIORITY: 5,
139+
BACKGROUND: 3,
140+
VISIBLE_COUNT: 10,
141+
},
137142
}));
138143

139144
// Mock format-utils
140145
vi.mock("../src/lib/format-utils.js", () => ({
141146
formatReason: vi.fn((reason) => reason || "Unknown"),
147+
classifyError: vi.fn((error) => {
148+
const msg = error?.message || "";
149+
if (msg.includes("Rate limited")) return "rate-limited";
150+
if (msg.includes("timeout")) return "timeout";
151+
if (msg.includes("NetworkError") || msg.includes("Failed to fetch")) return "offline";
152+
return "unknown";
153+
}),
142154
}));
143155

144156
// Mock url-builder

tests/url-builder.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
import { buildNotificationUrl } from "../src/lib/url-builder.js";
2+
import { buildNotificationUrl, buildProfileUrl } from "../src/lib/url-builder.js";
33

44
const GITHUB_BASE = "https://github.com";
55

@@ -182,3 +182,19 @@ describe("buildNotificationUrl", () => {
182182
});
183183
});
184184
});
185+
186+
describe("buildProfileUrl", () => {
187+
it("should build URL from login", () => {
188+
expect(buildProfileUrl("octocat")).toBe("https://github.com/octocat");
189+
});
190+
191+
it("should encode special characters in login", () => {
192+
expect(buildProfileUrl("user name")).toBe("https://github.com/user%20name");
193+
});
194+
195+
it("should return null for falsy login", () => {
196+
expect(buildProfileUrl(null)).toBeNull();
197+
expect(buildProfileUrl(undefined)).toBeNull();
198+
expect(buildProfileUrl("")).toBeNull();
199+
});
200+
});

0 commit comments

Comments
 (0)