-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move client code into useClient hook, add tests
- Loading branch information
Showing
4 changed files
with
323 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { it, expect, describe, vi, beforeEach } from "vitest"; | ||
import { act, renderHook, waitFor } from "@testing-library/react"; | ||
import { Client } from "@xmtp/xmtp-js"; | ||
import { Wallet } from "ethers"; | ||
import type { PropsWithChildren } from "react"; | ||
import { useClient } from "@/hooks/useClient"; | ||
import { XMTPProvider } from "@/contexts/XMTPContext"; | ||
|
||
const processUnprocessedMessagesMock = vi.hoisted(() => vi.fn()); | ||
|
||
const TestWrapper: React.FC<PropsWithChildren & { client?: Client }> = ({ | ||
children, | ||
client, | ||
}) => <XMTPProvider client={client}>{children}</XMTPProvider>; | ||
|
||
vi.mock("@/helpers/caching/messages", async () => { | ||
const actual = await import("@/helpers/caching/messages"); | ||
return { | ||
...actual, | ||
processUnprocessedMessages: processUnprocessedMessagesMock, | ||
}; | ||
}); | ||
|
||
describe("useClient", () => { | ||
beforeEach(() => { | ||
processUnprocessedMessagesMock.mockReset(); | ||
}); | ||
|
||
it("should disconnect an active client", async () => { | ||
const disconnectClientMock = vi.fn(); | ||
const mockClient = { | ||
close: disconnectClientMock, | ||
}; | ||
const { result } = renderHook(() => useClient(), { | ||
wrapper: ({ children }) => ( | ||
<TestWrapper client={mockClient as unknown as Client}> | ||
{children} | ||
</TestWrapper> | ||
), | ||
}); | ||
|
||
expect(result.current.client).toBeDefined(); | ||
|
||
await act(async () => { | ||
await result.current.disconnect(); | ||
}); | ||
|
||
expect(disconnectClientMock).toHaveBeenCalledTimes(1); | ||
expect(result.current.client).toBeUndefined(); | ||
}); | ||
|
||
it("should not initialize a client if one is already active", async () => { | ||
const mockClient = { | ||
address: "testWalletAddress", | ||
}; | ||
const clientCreateSpy = vi.spyOn(Client, "create"); | ||
const testWallet = Wallet.createRandom(); | ||
|
||
const { result } = renderHook(() => useClient(), { | ||
wrapper: ({ children }) => ( | ||
<TestWrapper client={mockClient as unknown as Client}> | ||
{children} | ||
</TestWrapper> | ||
), | ||
}); | ||
|
||
await act(async () => { | ||
await result.current.initialize({ signer: testWallet }); | ||
}); | ||
|
||
expect(clientCreateSpy).not.toHaveBeenCalled(); | ||
|
||
await waitFor(() => { | ||
expect(processUnprocessedMessagesMock).toBeCalledTimes(1); | ||
}); | ||
}); | ||
|
||
it("should initialize a client if one is not active", async () => { | ||
const testWallet = Wallet.createRandom(); | ||
const mockClient = { | ||
address: "testWalletAddress", | ||
} as unknown as Client; | ||
const clientCreateSpy = vi | ||
.spyOn(Client, "create") | ||
.mockResolvedValue(mockClient); | ||
|
||
const { result } = renderHook(() => useClient(), { | ||
wrapper: ({ children }) => <TestWrapper>{children}</TestWrapper>, | ||
}); | ||
|
||
await act(async () => { | ||
await result.current.initialize({ signer: testWallet }); | ||
}); | ||
|
||
expect(clientCreateSpy).toHaveBeenCalledWith(testWallet, { | ||
codecs: [], | ||
privateKeyOverride: undefined, | ||
}); | ||
expect(result.current.client).toBe(mockClient); | ||
expect(result.current.signer).toBe(testWallet); | ||
|
||
await waitFor(() => { | ||
expect(processUnprocessedMessagesMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
it("should throw an error if client initialization fails", async () => { | ||
const testWallet = Wallet.createRandom(); | ||
const testError = new Error("testError"); | ||
vi.spyOn(Client, "create").mockRejectedValue(testError); | ||
const onErrorMock = vi.fn(); | ||
|
||
const { result } = renderHook(() => useClient(onErrorMock)); | ||
|
||
await act(async () => { | ||
await expect( | ||
result.current.initialize({ signer: testWallet }), | ||
).rejects.toThrow(testError); | ||
}); | ||
|
||
expect(onErrorMock).toBeCalledTimes(1); | ||
expect(onErrorMock).toHaveBeenCalledWith(testError); | ||
expect(result.current.client).toBeUndefined(); | ||
expect(result.current.signer).toBeUndefined(); | ||
expect(result.current.error).toEqual(testError); | ||
}); | ||
|
||
it("should should call the onError callback if processing unprocessed messages fails", async () => { | ||
const testWallet = Wallet.createRandom(); | ||
const testError = new Error("testError"); | ||
const mockClient = { | ||
address: "testWalletAddress", | ||
} as unknown as Client; | ||
const onErrorMock = vi.fn(); | ||
vi.spyOn(Client, "create").mockResolvedValue(mockClient); | ||
processUnprocessedMessagesMock.mockRejectedValue(testError); | ||
|
||
const { result } = renderHook(() => useClient(onErrorMock), { | ||
wrapper: ({ children }) => ( | ||
<TestWrapper client={mockClient as unknown as Client}> | ||
{children} | ||
</TestWrapper> | ||
), | ||
}); | ||
|
||
await act(async () => { | ||
await result.current.initialize({ signer: testWallet }); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(onErrorMock).toHaveBeenCalledTimes(1); | ||
expect(onErrorMock).toHaveBeenCalledWith(testError); | ||
expect(result.current.error).toBe(null); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.