-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseHash.test.ts
More file actions
44 lines (33 loc) · 1.2 KB
/
useHash.test.ts
File metadata and controls
44 lines (33 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { renderHook, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { useHash } from "./useHash";
describe("useHash", () => {
beforeEach(() => {
window.location.hash = "#hash";
});
test("should get url hash", async () => {
const { result } = renderHook(() => useHash());
expect(result.current.hash).toBe("#hash");
window.location.hash = "#changedHash";
await waitFor(() => expect(result.current.hash).toBe("#changedHash"));
});
test("should remove event listener when unmount", () => {
const addEventListenerMock = vi.spyOn(window, "addEventListener");
const removeEventListenerMock = vi.spyOn(window, "removeEventListener");
const { unmount } = renderHook(() => useHash());
expect(addEventListenerMock).toBeCalledWith(
"hashchange",
expect.any(Function),
);
unmount();
expect(removeEventListenerMock).toBeCalledWith(
"hashchange",
expect.any(Function),
);
});
test("should update url hash", () => {
const { result } = renderHook(() => useHash());
result.current.updateHash("#updatedHash");
expect(window.location.hash).toBe("#updatedHash");
});
});