Skip to content

Commit 3bac8c9

Browse files
authored
test(react): useBlock hook (#745)
1 parent 3d27311 commit 3bac8c9

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { bestBlockAtom, finalizedBlockAtom, useBlock } from "./use-block.js";
2+
import { internal_useChainId } from "./use-chain-id.js";
3+
import { useConfig } from "./use-config.js";
4+
import { usePausableAtomValue } from "./use-pausable-atom-value.js";
5+
import { renderHook } from "@testing-library/react";
6+
import { beforeEach, expect, it, vi } from "vitest";
7+
8+
vi.mock("./use-config.js");
9+
vi.mock("./use-chain-id.js");
10+
vi.mock("./use-pausable-atom-value.js");
11+
12+
const mockConfig = { chains: {} };
13+
const mockChainId = "test-chain";
14+
const mockAtomValue = { hash: "0x123", number: 100 };
15+
16+
beforeEach(() => {
17+
vi.clearAllMocks();
18+
vi.mocked(useConfig).mockReturnValue(mockConfig);
19+
vi.mocked(internal_useChainId).mockReturnValue(mockChainId);
20+
vi.mocked(usePausableAtomValue).mockReturnValue(mockAtomValue);
21+
});
22+
23+
it("should use finalized block atom by default", () => {
24+
renderHook(() => useBlock());
25+
26+
expect(useConfig).toHaveBeenCalled();
27+
expect(internal_useChainId).toHaveBeenCalledWith(undefined);
28+
expect(usePausableAtomValue).toHaveBeenCalledWith(
29+
finalizedBlockAtom(mockConfig, mockChainId),
30+
);
31+
});
32+
33+
it('should use finalized block atom when tag is "finalized"', () => {
34+
renderHook(() => useBlock("finalized"));
35+
36+
expect(usePausableAtomValue).toHaveBeenCalledWith(
37+
finalizedBlockAtom(mockConfig, mockChainId),
38+
);
39+
});
40+
41+
it('should use best block atom when tag is "best"', () => {
42+
renderHook(() => useBlock("best"));
43+
44+
expect(usePausableAtomValue).toHaveBeenCalledWith(
45+
bestBlockAtom(mockConfig, mockChainId),
46+
);
47+
});
48+
49+
it("should pass options to internal_useChainId", () => {
50+
const options = { chainId: "custom-chain" };
51+
renderHook(() => useBlock("finalized", options));
52+
53+
expect(internal_useChainId).toHaveBeenCalledWith(options);
54+
});
55+
56+
it("should return the value from usePausableAtomValue", () => {
57+
const { result } = renderHook(() => useBlock());
58+
59+
expect(result.current).toBe(mockAtomValue);
60+
});

0 commit comments

Comments
 (0)