Skip to content

Commit

Permalink
fix: async on unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dandheedge committed Mar 26, 2024
1 parent c466a5d commit 569534b
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions apps/web/test/components/inputs/voucherExecution.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { afterEach, describe, it } from "vitest";
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import {
useCartesiDAppExecuteVoucher,
useCartesiDAppWasVoucherExecuted,
} from "@cartesi/rollups-wagmi";
import {
cleanup,
fireEvent,
render,
screen,
waitFor,
} from "@testing-library/react";
import { afterEach, describe, it } from "vitest";
import { useAccount, useWaitForTransaction } from "wagmi";
import { withMantineTheme } from "../../utils/WithMantineTheme";
import VoucherExecution from "../../../src/components/inputs/voucherExecution";
import { Voucher } from "../../../src/graphql/rollups/types";
import { withMantineTheme } from "../../utils/WithMantineTheme";

vi.mock("@cartesi/rollups-wagmi");
vi.mock("@mantine/notifications", async () => {
Expand Down Expand Up @@ -119,7 +125,7 @@ describe("VoucherExecution component", () => {
expect(button.hasAttribute("disabled")).toBe(true);
});

it("should display enabled button when voucher is not yet executed", () => {
it("should display enabled button when voucher is not yet executed", async () => {
useCartesiDAppWasVoucherExecutedMock.mockReturnValue({
data: false,
isLoading: false,
Expand All @@ -131,10 +137,15 @@ describe("VoucherExecution component", () => {
const button = screen
.getByText("Execute")
.closest("button") as HTMLButtonElement;
expect(button.hasAttribute("disabled")).toBe(false);
await waitFor(
() => expect(button.hasAttribute("disabled")).toBe(false),
{
timeout: 500,
},
);
});

it("should display tooltip when voucher is pending", () => {
it("should display tooltip when voucher is pending", async () => {
useCartesiDAppWasVoucherExecutedMock.mockReturnValue({
data: false,
isLoading: false,
Expand All @@ -156,12 +167,18 @@ describe("VoucherExecution component", () => {
expect(button.hasAttribute("disabled")).toBe(true);

fireEvent.mouseEnter(button);
expect(
screen.getByText("Voucher proof is pending"),
).toBeInTheDocument();
await waitFor(
() =>
expect(
screen.getByText("Voucher proof is pending"),
).toBeInTheDocument(),
{
timeout: 500,
},
);
});

it("should display tooltip when wallet is not connected and voucher is not executed", () => {
it("should display tooltip when wallet is not connected and voucher is not executed", async () => {
useCartesiDAppWasVoucherExecutedMock.mockReturnValue({
data: false,
isLoading: false,
Expand All @@ -178,9 +195,17 @@ describe("VoucherExecution component", () => {
expect(button.hasAttribute("disabled")).toBe(true);

fireEvent.mouseEnter(button);
expect(
screen.getByText("Connect your wallet to execute the voucher"),
).toBeInTheDocument();
await waitFor(
() =>
expect(
screen.getByText(
"Connect your wallet to execute the voucher",
),
).toBeInTheDocument(),
{
timeout: 500,
},
);
});

it("should execute voucher when button is clicked", () => {
Expand Down

0 comments on commit 569534b

Please sign in to comment.