Skip to content

Commit c529df5

Browse files
committed
chores(__tests__): hardened testing, validate hook cleaner function
1 parent 20fbda3 commit c529df5

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

__tests__/useMetamask.test.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react"
2-
import { act, render } from "@testing-library/react"
2+
import { act, render, RenderResult } from "@testing-library/react"
33

44
import { UseMatamaskAPI } from "../lib/shared"
55
import { useMetamask } from "../lib/index"
@@ -9,14 +9,14 @@ import { exposeMetamask, waitForUseEffect } from "../__utils__"
99

1010
function ExposeHook({
1111
onRender = (_: UseMatamaskAPI) => {},
12-
onMetamask = undefined,
12+
onMetamask = undefined as any,
1313
}) {
1414
onRender(useMetamask(onMetamask))
1515
return null
1616
}
1717

1818
describe("useMetamask", () => {
19-
let node
19+
let node: RenderResult
2020
afterEach(() => {
2121
if (node && node.unmount) {
2222
node.unmount()
@@ -73,4 +73,20 @@ describe("useMetamask", () => {
7373
})
7474
expect(api.isConnected).toBeFalsy()
7575
})
76+
77+
it("should expose metamask at hook mount & execute cleanerFn", async () => {
78+
const exposedMetamask = exposeMetamask({
79+
request: jest.fn().mockResolvedValue(ACCOUNTS),
80+
})
81+
const mockOnRender = jest.fn()
82+
const mockCleanerFn = jest.fn()
83+
const mockOnMetamask = jest.fn(() => mockCleanerFn)
84+
node = render(
85+
<ExposeHook onRender={mockOnRender} onMetamask={mockOnMetamask} />
86+
)
87+
await waitForUseEffect(node)
88+
expect(mockOnMetamask).toBeCalledWith(exposedMetamask)
89+
node.unmount() // Remove component to trigger hookCleaner
90+
expect(mockCleanerFn).toHaveBeenCalledTimes(1)
91+
})
7692
})

0 commit comments

Comments
 (0)