Skip to content
Open
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
7 changes: 5 additions & 2 deletions apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@
"postcss": "8.5.23",
"storybook": "9.1.21",
"tailwindcss": "3.4.17",
"typescript": "5.8.3"
"typescript": "5.8.3",
"vitest": "4.1.11"
},
"name": "thirdweb-dashboard",
"private": true,
Expand All @@ -125,7 +126,9 @@
"preinstall": "npx only-allow pnpm",
"start": "next start",
"storybook": "storybook dev -p 6006",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"test:unit": "turbo run test --filter=thirdweb-dashboard",
"test": "vitest run"
},
"version": "3.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { type ComponentProps, createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { CheckoutWidget } from "thirdweb/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { reportPaymentLinkBuySuccessful } from "@/analytics/report";
import { PayPageWidget } from "./PayPageWidget.client";

vi.mock("next-themes", () => ({ useTheme: () => ({ theme: "light" }) }));
vi.mock("thirdweb/react", async (importOriginal) => ({
...(await importOriginal<typeof import("thirdweb/react")>()),
AutoConnect: () => null,
CheckoutWidget: vi.fn(() => null),
}));
vi.mock("@/analytics/report", () => ({
reportPaymentLinkBuyFailed: vi.fn(),
reportPaymentLinkBuySuccessful: vi.fn(),
}));
vi.mock("@/hooks/chains/v5-adapter", () => ({
useV5DashboardChain: () => ({ id: 1 }),
}));

const open = vi.fn();

beforeEach(() => {
vi.stubGlobal("window", { open });
});

afterEach(() => {
vi.clearAllMocks();
vi.unstubAllGlobals();
});

function completeCheckout(redirectUri: string | undefined): void {
renderToStaticMarkup(
createElement(PayPageWidget, {
chainId: 1,
clientId: "test-client-id",
purchaseData: undefined,
recipientAddress: "0x0000000000000000000000000000000000000001",
redirectUri,
token: {
address: "0x0000000000000000000000000000000000000002",
decimals: 18,
name: "Test token",
symbol: "TEST",
},
}),
);
const props = vi.mocked(CheckoutWidget).mock.calls[0]?.[0] as
| ComponentProps<typeof CheckoutWidget>
| undefined;
expect(props?.onSuccess).toBeTypeOf("function");
// This handler does not consume the SDK's payment result.
const onSuccess = props?.onSuccess as (() => void) | undefined;
onSuccess?.();
}

describe("payment success redirect", () => {
it.each([
[
"https://merchant.example/complete?order=1#receipt",
"https://merchant.example/complete?order=1#receipt",
],
["http://localhost:3000/done", "http://localhost:3000/done"],
["HTTPS://MERCHANT.EXAMPLE", "https://merchant.example/"],
])(
"opens %s without giving the destination an opener",
(input, normalized) => {
completeCheckout(input);
expect(open.mock.calls).toEqual([
[normalized, "_blank", "noopener,noreferrer"],
]);
expect(reportPaymentLinkBuySuccessful).toHaveBeenCalledOnce();
},
);

it.each([
undefined,
"",
"not a URL",
"/relative/path",
"//merchant.example/done",
"https://",
"javascript:alert(1)",
" JaVaScRiPt:alert(1)",
"data:text/html,hello",
"file:///tmp/receipt.html",
"ftp://merchant.example/receipt",
"blob:https://merchant.example/id",
"mailto:merchant@example.com",
])(
"ignores invalid or non-HTTP destination %s without breaking checkout",
(input) => {
expect(() => completeCheckout(input)).not.toThrow();
expect(open).not.toHaveBeenCalled();
expect(reportPaymentLinkBuySuccessful).toHaveBeenCalledOnce();
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,14 @@ export function PayPageWidget({
reportPaymentLinkBuySuccessful();

if (!redirectUri) return;
const url = new URL(redirectUri);
return window.open(url.toString());
let url: URL;
try {
url = new URL(redirectUri);
} catch {
return;
}
if (url.protocol !== "http:" && url.protocol !== "https:") return;
return window.open(url.toString(), "_blank", "noopener,noreferrer");
}}
onError={(error) => {
reportPaymentLinkBuyFailed({
Expand Down
16 changes: 16 additions & 0 deletions apps/dashboard/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { fileURLToPath } from "node:url";
import { defineConfig } from "vitest/config";

export default defineConfig({
esbuild: { jsx: "automatic" },
resolve: {
alias: {
"@": fileURLToPath(new URL("./src/@", import.meta.url)),
app: fileURLToPath(new URL("./src/app", import.meta.url)),
},
},
test: {
environment: "node",
include: ["src/**/*.test.ts"],
},
});
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"$schema": "https://turborepo.org/schema.json",
"globalEnv": ["CI", "TW_SECRET_KEY", "TW_CLIENT_ID"],
"tasks": {
"thirdweb-dashboard#test": {
"dependsOn": ["^build"],
"outputs": []
},
"bench": {
"cache": false,
"dependsOn": []
Expand Down
Loading