Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Aug 28, 2023
1 parent 376b353 commit ba60b7d
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions packages/react-sdk/src/hooks/useResendMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import { ContentTypeText } from "@xmtp/xmtp-js";
import { useResendMessage } from "@/hooks/useResendMessage";
import type { CachedMessageWithId } from "@/helpers/caching/messages";

const resendMessageMock = vi.hoisted(() => vi.fn());
const resendMock = vi.hoisted(() => vi.fn());

vi.mock("@/hooks/useMessage", async () => {
const actual = await import("@/hooks/useMessage");
return {
useMessage: () => ({
...actual.useMessage,
resendMessage: resendMessageMock,
resend: resendMock,
}),
};
});

describe("useResendMessage", () => {
beforeEach(() => {
resendMessageMock.mockReset();
resendMock.mockReset();
});

it("should resend a previously failed message", async () => {
resendMessageMock.mockResolvedValueOnce({ id: 1 });
resendMock.mockResolvedValueOnce({ id: 1 });
const onSuccessMock = vi.fn();

const testMessage = {
Expand All @@ -47,19 +47,19 @@ describe("useResendMessage", () => {
);

await act(async () => {
const sentMessage = await result.current.resendMessage(testMessage);
const sentMessage = await result.current.resend(testMessage);
expect(sentMessage).toEqual({ id: 1 });
expect(onSuccessMock).toHaveBeenCalledTimes(1);
expect(onSuccessMock).toHaveBeenCalledWith(sentMessage);
});

expect(resendMessageMock).toHaveBeenCalledTimes(1);
expect(resendMessageMock).toHaveBeenCalledWith(testMessage);
expect(resendMock).toHaveBeenCalledTimes(1);
expect(resendMock).toHaveBeenCalledWith(testMessage);
});

it("should have an error when resending fails", async () => {
const testError = new Error("testError");
resendMessageMock.mockRejectedValueOnce(testError);
resendMock.mockRejectedValueOnce(testError);
const onErrorMock = vi.fn();

const testMessage = {
Expand All @@ -85,11 +85,11 @@ describe("useResendMessage", () => {

await act(async () => {
try {
await result.current.resendMessage(testMessage);
await result.current.resend(testMessage);
} catch (e) {
expect(e).toEqual(testError);
} finally {
expect(resendMessageMock).toHaveBeenCalledTimes(1);
expect(resendMock).toHaveBeenCalledTimes(1);
expect(onErrorMock).toHaveBeenCalledTimes(1);
expect(onErrorMock).toHaveBeenCalledWith(testError);
}
Expand Down

0 comments on commit ba60b7d

Please sign in to comment.