Skip to content

Commit 5c6de8e

Browse files
test: add comprehensive test suite for useLocalStorageCooldown hook (#31)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com>
1 parent 02ccbf1 commit 5c6de8e

7 files changed

Lines changed: 209 additions & 4 deletions

File tree

bun-test-setup.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { mock } from 'bun:test';
2+
import { GlobalWindow } from 'happy-dom';
3+
4+
const win = new GlobalWindow();
5+
global.window = win as any;
6+
global.document = win.document as any;
7+
global.navigator = win.navigator as any;
8+
global.localStorage = win.localStorage as any;
9+
global.addEventListener = win.addEventListener.bind(win) as any;
10+
global.removeEventListener = win.removeEventListener.bind(win) as any;
11+
global.dispatchEvent = win.dispatchEvent.bind(win) as any;
12+
global.StorageEvent = win.StorageEvent as any;
13+
14+
// Mock dependencies that cause side effects or fail on static assets imports
15+
mock.module('@/services/api', () => ({
16+
api: {},
17+
}));
18+
mock.module('@/services/request', () => ({
19+
getToken: () => '',
20+
default: {},
21+
}));

bun.lock

Lines changed: 45 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bunfig.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[test]
2+
preload = ["./bun-test-setup.ts"]

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@
3232
"devDependencies": {
3333
"@biomejs/biome": "2.4.11",
3434
"@tailwindcss/postcss": "^4.2.2",
35+
"@testing-library/react": "^16.3.2",
36+
"@types/bun": "^1.3.12",
3537
"@types/git-url-parse": "^16.0.2",
3638
"@types/node": "^25.6.0",
3739
"@types/react": "^19",
3840
"@types/react-dom": "^19",
3941
"@types/react-router-dom": "^5.3.3",
4042
"@typescript/native-preview": "^7.0.0-dev.20260412.1",
43+
"happy-dom": "^20.9.0",
4144
"mitata": "^1.0.34",
4245
"tailwindcss": "^4.2.2",
4346
"typescript": "^6.0.2"

src/globals.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ declare module 'bun:test' {
2828
export function test(name: string, fn: TestHandler): void;
2929
export function expect<T>(actual: T): {
3030
toBe(expected: unknown): void;
31+
toBeNull(): void;
32+
};
33+
export function beforeEach(fn: () => void | Promise<void>): void;
34+
export function afterEach(fn: () => void | Promise<void>): void;
35+
export function setSystemTime(time: Date | number | null): void;
36+
export const mock: {
37+
module(path: string, factory: () => any): void;
3138
};
3239
}
3340

src/pages/realtime-metrics.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ const attributeOptions = [
8686
];
8787

8888
export const Component = () => {
89-
const [searchParams, setSearchParams] = useSearchParams({ attribute: 'hash' });
89+
const [searchParams, setSearchParams] = useSearchParams({
90+
attribute: 'hash',
91+
});
9092
const [dateRange, setDateRange] = useState<[Dayjs, Dayjs]>([
9193
dayjs().subtract(24, 'hour'),
9294
dayjs(),

0 commit comments

Comments
 (0)