Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,9 @@ jest.mock("@/services/api/common-api", () => ({

import * as ReviewDistributionPlanTableSubscriptionModule from "@/components/distribution-plan-tool/review-distribution-plan/table/ReviewDistributionPlanTableSubscription";
import { ApiIdentity } from "@/generated/models/ApiIdentity";
const { download, isSubscriptionsAdmin, SubscriptionConfirm } =
const { download, isSubscriptionsAdmin } =
ReviewDistributionPlanTableSubscriptionModule;

jest.mock("react-bootstrap", () => ({
__esModule: true,
Modal: Object.assign(
({ show, children }: any) => (show ? <div>{children}</div> : null),
{
Header: ({ children }: any) => <div>{children}</div>,
Title: ({ children }: any) => <div>{children}</div>,
Body: ({ children }: any) => <div>{children}</div>,
Footer: ({ children }: any) => <div>{children}</div>,
}
),
Button: (p: any) => <button {...p}>{p.children}</button>,
Col: (p: any) => <div>{p.children}</div>,
Container: (p: any) => <div>{p.children}</div>,
Row: (p: any) => <div>{p.children}</div>,
}));

describe("ReviewDistributionPlanTableSubscription utilities", () => {
it("checks subscriptions admin wallets", () => {
const profile: Partial<ApiIdentity> = { wallets: [{ wallet: "0xabc" }] };
Expand Down Expand Up @@ -70,50 +53,6 @@ jest.mock("@/components/distribution-plan-tool/common/CircleLoader", () => ({
default: () => <div data-testid="circle-loader">Loading...</div>,
}));

test("SubscriptionConfirm extracts token id from plan name", () => {
render(
<SubscriptionConfirm
title="t"
plan={{ id: "1", name: "Meme 123 drop" } as any}
show={true}
handleClose={jest.fn()}
onConfirm={jest.fn()}
/>
);
const input = screen.getByRole("spinbutton") as HTMLInputElement;
expect(input.value).toBe("123");
});

test("SubscriptionConfirm displays token id as text when confirmedTokenId is provided", () => {
render(
<SubscriptionConfirm
title="t"
plan={{ id: "1", name: "Meme 123 drop" } as any}
show={true}
handleClose={jest.fn()}
confirmedTokenId="456"
onConfirm={jest.fn()}
/>
);
expect(screen.queryByRole("spinbutton")).not.toBeInTheDocument();
expect(screen.getByText("456")).toBeInTheDocument();
});

test("SubscriptionConfirm shows input when confirmedTokenId is not provided", () => {
render(
<SubscriptionConfirm
title="t"
plan={{ id: "1", name: "Meme 123 drop" } as any}
show={true}
handleClose={jest.fn()}
onConfirm={jest.fn()}
/>
);
const input = screen.getByRole("spinbutton") as HTMLInputElement;
expect(input).toBeInTheDocument();
expect(input.value).toBe("123");
});

describe("SubscriptionLinks", () => {
const mockSetToast = jest.fn();
const mockAuthCtx = {
Expand All @@ -124,7 +63,11 @@ describe("SubscriptionLinks", () => {
};

const mockPlan = { id: "plan1", name: "Meme 123 drop" } as any;
const mockDistCtx = {
const mockDistCtxWithTokenId = {
confirmedTokenId: "123",
} as any;

const mockDistCtxNoTokenId = {
confirmedTokenId: null,
} as any;

Expand All @@ -146,7 +89,7 @@ describe("SubscriptionLinks", () => {
jest.restoreAllMocks();
});

it("renders Public Subscriptions button for public phase", () => {
it("renders Public Subscriptions button for public phase when token is confirmed", () => {
const publicPhase = {
id: "phase1",
name: "public",
Expand All @@ -155,7 +98,7 @@ describe("SubscriptionLinks", () => {
} as any;

render(
<DistributionPlanToolContext.Provider value={mockDistCtx}>
<DistributionPlanToolContext.Provider value={mockDistCtxWithTokenId}>
<AuthContext.Provider value={mockAuthCtx as any}>
<SubscriptionLinks plan={mockPlan} phase={publicPhase} />
</AuthContext.Provider>
Expand All @@ -165,33 +108,26 @@ describe("SubscriptionLinks", () => {
expect(screen.getByText("Public Subscriptions")).toBeInTheDocument();
});

it("shows confirm modal when Public Subscriptions button is clicked", async () => {
const user = userEvent.setup();
it("does not render when token is not confirmed", () => {
const publicPhase = {
id: "phase1",
name: "public",
type: ReviewDistributionPlanTableItemType.PHASE,
phaseId: PUBLIC_SUBSCRIPTIONS_PHASE_ID,
} as any;

render(
<DistributionPlanToolContext.Provider value={mockDistCtx}>
const { container } = render(
<DistributionPlanToolContext.Provider value={mockDistCtxNoTokenId}>
<AuthContext.Provider value={mockAuthCtx as any}>
<SubscriptionLinks plan={mockPlan} phase={publicPhase} />
</AuthContext.Provider>
</DistributionPlanToolContext.Provider>
);

await user.click(screen.getByText("Public Subscriptions"));

await waitFor(() => {
expect(
screen.getByText("Confirm Download Public Subscriptions Info")
).toBeInTheDocument();
});
expect(container.innerHTML).toBe("");
});

it("calls download and shows toast when Public Subscriptions is confirmed", async () => {
it("calls download directly when button is clicked", async () => {
const user = userEvent.setup();

jest
Expand All @@ -215,7 +151,7 @@ describe("SubscriptionLinks", () => {
} as any;

render(
<DistributionPlanToolContext.Provider value={mockDistCtx}>
<DistributionPlanToolContext.Provider value={mockDistCtxWithTokenId}>
<AuthContext.Provider value={mockAuthCtx as any}>
<SubscriptionLinks plan={mockPlan} phase={publicPhase} />
</AuthContext.Provider>
Expand All @@ -224,14 +160,6 @@ describe("SubscriptionLinks", () => {

await user.click(screen.getByText("Public Subscriptions"));

await waitFor(() => {
expect(
screen.getByText("Confirm Download Public Subscriptions Info")
).toBeInTheDocument();
});

await user.click(screen.getByText("Looks good"));

await waitFor(() => {
expect(mockSetToast).toHaveBeenCalledWith({
type: "success",
Expand All @@ -256,7 +184,7 @@ describe("SubscriptionLinks", () => {
} as any;

const { container } = render(
<DistributionPlanToolContext.Provider value={mockDistCtx}>
<DistributionPlanToolContext.Provider value={mockDistCtxWithTokenId}>
<AuthContext.Provider value={nonAdminCtx as any}>
<SubscriptionLinks plan={mockPlan} phase={publicPhase} />
</AuthContext.Provider>
Expand All @@ -266,11 +194,27 @@ describe("SubscriptionLinks", () => {
expect(container.innerHTML).toBe("");
});

it("displays token id as text when confirmedTokenId is provided in context", async () => {
it("shows loading state during download", async () => {
const user = userEvent.setup();
const distCtxWithTokenId = {
confirmedTokenId: "789",
} as any;

jest
.spyOn(
ReviewDistributionPlanTableSubscriptionModule,
"isSubscriptionsAdmin"
)
.mockReturnValue(true);

let resolveDownload: (value: { success: boolean; message: string }) => void;
const downloadPromise = new Promise<{ success: boolean; message: string }>(
(resolve) => {
resolveDownload = resolve;
}
);

jest
.spyOn(ReviewDistributionPlanTableSubscriptionModule, "download")
.mockReturnValue(downloadPromise);

const publicPhase = {
id: "phase1",
name: "public",
Expand All @@ -279,7 +223,7 @@ describe("SubscriptionLinks", () => {
} as any;

render(
<DistributionPlanToolContext.Provider value={distCtxWithTokenId}>
<DistributionPlanToolContext.Provider value={mockDistCtxWithTokenId}>
<AuthContext.Provider value={mockAuthCtx as any}>
<SubscriptionLinks plan={mockPlan} phase={publicPhase} />
</AuthContext.Provider>
Expand All @@ -289,12 +233,13 @@ describe("SubscriptionLinks", () => {
await user.click(screen.getByText("Public Subscriptions"));

await waitFor(() => {
expect(
screen.getByText("Confirm Download Public Subscriptions Info")
).toBeInTheDocument();
expect(screen.getByText("Downloading")).toBeInTheDocument();
});

expect(screen.queryByRole("spinbutton")).not.toBeInTheDocument();
expect(screen.getByText("789")).toBeInTheDocument();
resolveDownload!({ success: true, message: "Done" });

await waitFor(() => {
expect(screen.getByText("Public Subscriptions")).toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import { useMemo, useState } from "react";
jest.mock(
"@/components/distribution-plan-tool/review-distribution-plan/table/ReviewDistributionPlanTableSubscription",
() => ({
SubscriptionConfirm: (props: any) =>
props.show ? (
<button
data-testid={props.title}
onClick={() => props.onConfirm?.("c", "t")}
/>
) : null,
download: jest.fn(async () => ({ success: true, message: "ok" })),
isSubscriptionsAdmin: jest.fn(),
})
Expand Down Expand Up @@ -54,7 +47,7 @@ jest.mock(
<div data-testid="Confirm Token ID">
<button
data-testid="confirm-token-id-button"
onClick={() => props.onConfirm("contract", "123")}>
onClick={() => props.onConfirm("123")}>
Confirm
</button>
</div>
Expand Down Expand Up @@ -138,7 +131,7 @@ test("displays contract and token id after confirmation", async () => {
});
});

test("shows confirm modals when buttons clicked", async () => {
test("shows upload modals when upload buttons clicked", async () => {
const user = userEvent.setup();
(isSubscriptionsAdmin as jest.Mock).mockReturnValue(true);
render(<TestWrapper />);
Expand All @@ -155,14 +148,7 @@ test("shows confirm modals when buttons clicked", async () => {
screen.getByTestId("Upload Distribution Photos")
).toBeInTheDocument();
});
await user.click(screen.getByText("Reset Subscriptions"));
await waitFor(() => {
expect(screen.getByTestId("Reset Subscriptions")).toBeInTheDocument();
});
await user.click(screen.getByText("Finalize Distribution"));
await waitFor(() => {
expect(screen.getByTestId("Finalize Distribution")).toBeInTheDocument();
});

await user.click(screen.getByText("Upload Automatic Airdrops"));
await waitFor(() => {
expect(screen.getByTestId("Automatic Airdrops")).toBeInTheDocument();
Expand All @@ -187,7 +173,7 @@ test("change token id button opens confirm modal", async () => {
});
});

test("resetSubscriptions posts data and shows toast", async () => {
test("resetSubscriptions posts data and shows toast directly", async () => {
const user = userEvent.setup();
(isSubscriptionsAdmin as jest.Mock).mockReturnValue(true);
const commonApiPost = jest.fn().mockResolvedValue({});
Expand All @@ -208,13 +194,54 @@ test("resetSubscriptions posts data and shows toast", async () => {
});

await user.click(screen.getByText("Reset Subscriptions"));

await waitFor(() => {
expect(commonApiPost).toHaveBeenCalledWith(
expect.objectContaining({
endpoint: expect.stringContaining("/reset"),
})
);
expect(authCtx.setToast).toHaveBeenCalledWith({
type: "success",
message: "Subscriptions reset successfully.",
});
});
});

test("finalizeDistribution posts data and shows toast directly", async () => {
const user = userEvent.setup();
(isSubscriptionsAdmin as jest.Mock).mockReturnValue(true);
const commonApiPost = jest
.fn()
.mockResolvedValue({ success: true, message: "Normalized" });
jest
.spyOn(require("@/services/api/common-api"), "commonApiPost")
.mockImplementation(commonApiPost);

jest
.spyOn(require("@/services/api/common-api"), "commonApiFetch")
.mockResolvedValue({ photos_count: 0, is_normalized: false });

render(<TestWrapper />);

await user.click(screen.getByTestId("confirm-token-id-button"));

await waitFor(() => {
expect(screen.getByTestId("Reset Subscriptions")).toBeInTheDocument();
expect(screen.getByText("Finalize Distribution")).toBeInTheDocument();
});
await user.click(screen.getByTestId("Reset Subscriptions"));

await user.click(screen.getByText("Finalize Distribution"));

await waitFor(() => {
expect(commonApiPost).toHaveBeenCalled();
expect(authCtx.setToast).toHaveBeenCalled();
expect(commonApiPost).toHaveBeenCalledWith(
expect.objectContaining({
endpoint: expect.stringContaining("/normalize"),
})
);
expect(authCtx.setToast).toHaveBeenCalledWith({
type: "success",
message: "Normalized",
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("ConfirmTokenIdModal", () => {
);
const confirmButton = screen.getByText("Confirm");
await user.click(confirmButton);
expect(mockOnConfirm).toHaveBeenCalledWith(expect.any(String), "123");
expect(mockOnConfirm).toHaveBeenCalledWith("123");
});

it("calls onConfirm when Enter key is pressed with valid token id", async () => {
Expand All @@ -65,7 +65,7 @@ describe("ConfirmTokenIdModal", () => {
const input = screen.getByRole("spinbutton");
input.focus();
await user.keyboard("{Enter}");
expect(mockOnConfirm).toHaveBeenCalledWith(expect.any(String), "123");
expect(mockOnConfirm).toHaveBeenCalledWith("123");
});

it("does not call onConfirm when Enter key is pressed with invalid token id", async () => {
Expand Down
Loading