Skip to content

Commit 35e4703

Browse files
authored
chore: add coveralls coverage report (#42)
* chore: set read permission * chore: add istanbul vitest * chore: generate coverage lcov report file * test: add userBrowserNotification test
1 parent d69fac4 commit 35e4703

8 files changed

+69
-5
lines changed

Diff for: .github/workflows/_security-checks.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: Security Checks
22
on:
33
workflow_call:
4+
permissions:
5+
contents: read
46
jobs:
57
trivy:
68
name: Trivy

Diff for: .github/workflows/_static-checks.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: Static Checks
22
on:
33
workflow_call:
4+
permissions:
5+
contents: read
46
jobs:
57
lint:
68
name: ESLint Check

Diff for: .github/workflows/_unit-tests.yml

+5
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ jobs:
1515

1616
- name: Run tests
1717
run: npm run test:coverage
18+
19+
- name: Send coverage to Coveralls
20+
uses: coverallsapp/github-action@cfd0633edbd2411b532b808ba7a8b5e04f76d2c8 #v2.3.4
21+
with:
22+
github-token: ${{ secrets.GITHUB_TOKEN }}

Diff for: package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"@types/react": "^19.0.2",
5353
"@types/react-dom": "^19.0.2",
5454
"@vitejs/plugin-react-swc": "^3.5.0",
55-
"@vitest/coverage-istanbul": "^2.1.4",
55+
"@vitest/coverage-istanbul": "^2.1.8",
5656
"@vitest/expect": "^2.1.4",
5757
"@vitest/ui": "^2.1.4",
5858
"autoprefixer": "^10.4.20",

Diff for: src/hooks/__tests__/useBrowserNotification.test.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { renderHook } from "@testing-library/react";
2+
import { vi } from "vitest";
3+
import { useBrowserNotification } from "../useBrowserNotification";
4+
5+
describe("useBrowserNotification", () => {
6+
let mockRequestPermission: (
7+
deprecatedCallback?: NotificationPermissionCallback | undefined,
8+
) => Promise<NotificationPermission>;
9+
let mockNotification: ReturnType<typeof vi.fn>;
10+
11+
beforeAll(() => {
12+
mockRequestPermission = vi.fn();
13+
mockNotification = vi.fn();
14+
15+
global.Notification = vi.fn(
16+
(title: string, options?: NotificationOptions) => {
17+
mockNotification(title, options);
18+
return {};
19+
},
20+
) as unknown as typeof Notification;
21+
22+
Object.defineProperty(global.Notification, "permission", {
23+
value: "default",
24+
writable: true,
25+
});
26+
global.Notification.requestPermission = mockRequestPermission;
27+
});
28+
29+
it("should request the permission for notification", () => {
30+
renderHook(() => useBrowserNotification());
31+
expect(mockRequestPermission).toHaveBeenCalled();
32+
});
33+
34+
it("should send a notification if permission is granted", () => {
35+
Object.defineProperty(global.Notification, "permission", {
36+
value: "granted",
37+
});
38+
39+
const { result } = renderHook(() => useBrowserNotification());
40+
result.current.sendNotification("title", { body: "body" });
41+
42+
expect(mockNotification).toHaveBeenCalledWith("title", {
43+
body: "body",
44+
});
45+
});
46+
47+
it("should not send a notification if permission is denied", () => {
48+
Object.defineProperty(global.Notification, "permission", {
49+
value: "denied",
50+
});
51+
52+
const { result } = renderHook(() => useBrowserNotification());
53+
result.current.sendNotification("title", { body: "body" });
54+
55+
expect(mockNotification).not.toHaveBeenCalled();
56+
});
57+
});

Diff for: src/hooks/useBrowserNotification.ts

-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,5 @@ export function useBrowserNotification() {
1717
requestPermission();
1818
}, []);
1919

20-
21-
2220
return { sendNotification };
2321
}

Diff for: vitest.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default defineConfig({
5555
},
5656
enabled: false,
5757
provider: "istanbul",
58-
reporter: ["text", "json", "html", "json-summary", "lcov"],
58+
reporter: ["text", "lcov"],
5959
},
6060
},
6161
});

0 commit comments

Comments
 (0)