Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): preserve translated button labels during checkout (#15612)

Nicholas Tindled2daa4d3a884
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/SubscriptionStep.trial.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/SubscriptionStep.trial.test.tsx
index e31aed2b4f..4f1e3f2400 100644
--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/SubscriptionStep.trial.test.tsx
+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/SubscriptionStep.trial.test.tsx
@@ -25,6 +25,7 @@ import { http, HttpResponse } from "msw";
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { useOnboardingWizardStore } from "../../store";
import { SubscriptionStep } from "../SubscriptionStep/SubscriptionStep";
+import { translateText } from "@/tests/integrations/translation";
const posthog = vi.hoisted(() => ({ capture: vi.fn() }));
vi.mock("@posthog/react", () => ({
@@ -77,6 +78,128 @@ function mockStatus(response = eligible()) {
server.use(getGetTrialsGetTrialStatusMockHandler200(response));
}
+test.each(["paid", "trial"])(
+ "keeps a translated %s checkout usable while the request is pending",
+ async (mode) => {
+ mockStatus(
+ mode === "trial"
+ ? eligible()
+ : trialResponse({
+ eligible: false,
+ active: false,
+ status: null,
+ offer: null,
+ }),
+ );
+ let finishCheckout = () => {};
+ const pending = new Promise<void>((resolve) => {
+ finishCheckout = resolve;
+ });
+ const requested = vi.fn();
+ server.use(
+ http.post(
+ `*/api/credits/${mode === "paid" ? "subscription" : "trial"}`,
+ async ({ request }) => {
+ requested(await request.json());
+ await pending;
+ return HttpResponse.json({
+ url: "https://checkout.stripe.com/translated",
+ });
+ },
+ ),
+ );
+ render(<SubscriptionStep />);
+ const label = mode === "paid" ? "Get Pro" : "Start 7-day trial";
+ const button = await screen.findByRole("button", { name: label });
+ const translatedLabel =
+ mode === "paid" ? "احصل على برو" : "ابدأ الفترة التجريبية";
+ const translation = translateText(button, label, translatedLabel);
+
+ try {
+ fireEvent.click(button);
+ await waitFor(() => expect(requested).toHaveBeenCalledOnce());
+ expect(screen.getByRole("button", { name: translatedLabel })).toBe(
+ button,
+ );
+ expect(button.contains(translation)).toBe(true);
+ expect(button.hasAttribute("disabled")).toBe(true);
+ expect(requested).toHaveBeenCalledWith(
+ mode === "paid"
+ ? expect.objectContaining({ tier: "PRO", billing_cycle: "monthly" })
+ : { offer_token: trialOffer.token, return_to: "onboarding" },
+ );
+ } finally {
+ finishCheckout();
+ }
+
+ await waitFor(() => {
+ if (mode === "paid")
+ expect(checkoutLocation.href).toBe(
+ "https://checkout.stripe.com/translated",
+ );
+ else
+ expect(checkoutLocation.assign).toHaveBeenCalledWith(
+ "https://checkout.stripe.com/translated",
+ );
+ });
+ },
+);
+
+test.each(["paid", "trial"])(
+ "can retry a translated %s checkout after a failed request",
+ async (mode) => {
+ mockStatus(
+ mode === "trial"
+ ? eligible()
+ : trialResponse({
+ eligible: false,
+ active: false,
+ status: null,
+ offer: null,
+ }),
+ );
+ let attempts = 0;
+ server.use(
+ http.post(
+ `*/api/credits/${mode === "paid" ? "subscription" : "trial"}`,
+ () => {
+ attempts++;
+ return attempts === 1
+ ? HttpResponse.json({ detail: "Please try again" }, { status: 503 })
+ : HttpResponse.json({ url: "https://checkout.stripe.com/retried" });
+ },
+ ),
+ );
+ render(<SubscriptionStep />);
+ const label = mode === "paid" ? "Get Pro" : "Start 7-day trial";
+ const button = await screen.findByRole("button", { name: label });
+ const translation = translateText(button, label, "Translated checkout");
+
+ fireEvent.click(button);
+ await waitFor(() => {
+ expect(attempts).toBe(1);
+ expect(button.hasAttribute("disabled")).toBe(false);
+ });
+ expect(screen.getByRole("button", { name: "Translated checkout" })).toBe(
+ button,
+ );
+ expect(button.contains(translation)).toBe(true);
+ fireEvent.click(button);
+
+ await waitFor(() => {
+ if (mode === "paid")
+ expect(checkoutLocation.href).toBe(
+ "https://checkout.stripe.com/retried",
+ );
+ else
+ expect(checkoutLocation.assign).toHaveBeenCalledWith(
+ "https://checkout.stripe.com/retried",
+ );
+ });
+ expect(attempts).toBe(2);
+ },
+);
+
test.each([
["PRO", "Pro"],
["MAX", "Max"],
diff --git a/autogpt_platform/frontend/src/components/atoms/Button/Button.translation.test.tsx b/autogpt_platform/frontend/src/components/atoms/Button/Button.translation.test.tsx
new file mode 100644
index 0000000000..60ee0d8ac9
--- /dev/null
+++ b/autogpt_platform/frontend/src/components/atoms/Button/Button.translation.test.tsx
@@ -0,0 +1,66 @@
+import { TooltipProvider } from "@/components/atoms/Tooltip/BaseTooltip";
+import { translateText } from "@/tests/integrations/translation";
+import { cleanup, render, screen } from "@testing-library/react";
+import { afterEach, expect, it } from "vitest";
+import { Button } from "./Button";
+import type { ButtonProps } from "./helpers";
+
+afterEach(cleanup);
+
+const cases: Partial<ButtonProps>[] = [
+ { variant: "primary" },
+ { variant: "secondary" },
+ { variant: "ghost" },
+ { variant: "link" },
+ { as: "NextLink", href: "/onboarding" },
+];
+
+it.each(cases)("preserves translated labels through loading: %j", (props) => {
+ function TestButton({ loading }: { loading: boolean }) {
+ return (
+ <TooltipProvider>
+ <Button
+ {...(props as ButtonProps)}
+ loading={loading}
+ data-goal="checkout"
+ >
+ Get Pro
+ </Button>
+ </TooltipProvider>
+ );
+ }
+
+ const { rerender } = render(<TestButton loading={false} />);
+ const role = props.as === "NextLink" ? "link" : "button";
+ const button = screen.getByRole(role, { name: "Get Pro" });
+ const translation = translateText(button, "Get Pro", "احصل على برو");
+
+ rerender(<TestButton loading />);
+
+ expect(screen.getByRole(role, { name: "احصل على برو" })).toBe(button);
+ expect(button.contains(translation)).toBe(true);
+ expect(button.getAttribute("data-goal")).toBe("checkout");
+ expect(
+ role === "link"
+ ? button.getAttribute("aria-disabled") === "true"
+ : button.hasAttribute("disabled"),
+ ).toBe(true);
+
+ rerender(<TestButton loading={false} />);
+
+ expect(screen.getByRole(role, { name: "احصل على برو" })).toBe(button);
+ expect(button.contains(translation)).toBe(true);
+ expect(button.hasAttribute("disabled")).toBe(false);
+ expect(button.getAttribute("aria-disabled")).not.toBe("true");
+});
+
+it("can finish loading after the page is translated during the request", () => {
+ const { rerender } = render(<Button loading>Get Pro</Button>);
+ const button = screen.getByRole("button", { name: "Get Pro" });
+ translateText(button, "Get Pro", "احصل على برو");
+
+ rerender(<Button>Get Pro</Button>);
+
+ expect(screen.getByRole("button", { name: "احصل على برو" })).toBe(button);
+ expect(button.hasAttribute("disabled")).toBe(false);
+});
diff --git a/autogpt_platform/frontend/src/components/atoms/Button/Button.tsx b/autogpt_platform/frontend/src/components/atoms/Button/Button.tsx
index e539e7e96b..af4d24f626 100644
--- a/autogpt_platform/frontend/src/components/atoms/Button/Button.tsx
+++ b/autogpt_platform/frontend/src/components/atoms/Button/Button.tsx
@@ -74,7 +74,7 @@ export function Button(props: ButtonProps) {
<Icon icon={Loading03Icon} className="h-4 w-4 animate-spin" />
)}
{!loading && resolvedLeftIcon}
- {children}
+ <span className="contents">{children}</span>
{!loading && rightIcon}
</>
);
@@ -122,8 +122,7 @@ export function Button(props: ButtonProps) {
className={loadingClassName}
aria-disabled="true"
>
- <Icon icon={Loading03Icon} className="h-4 w-4 animate-spin" />
- {children}
+ {buttonContent}
</NextLink>
);
}
@@ -138,8 +137,7 @@ export function Button(props: ButtonProps) {
className={loadingClassName}
disabled
>
- <Icon icon={Loading03Icon} className="h-4 w-4 animate-spin" />
- {children}
+ {buttonContent}
</button>
);
diff --git a/autogpt_platform/frontend/src/tests/integrations/translation.ts b/autogpt_platform/frontend/src/tests/integrations/translation.ts
new file mode 100644
index 0000000000..df93b56acf
--- /dev/null
+++ b/autogpt_platform/frontend/src/tests/integrations/translation.ts
@@ -0,0 +1,16 @@
+export function translateText(
+ element: HTMLElement,
+ original: string,
+ translated: string,
+) {
+ const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT);
+ while (walker.nextNode()) {
+ const node = walker.currentNode;
+ if (node.textContent !== original) continue;
+ const replacement = document.createElement("font");
+ replacement.textContent = translated;
+ node.parentNode!.replaceChild(replacement, node);
+ return replacement;
+ }
+ throw new Error(`Could not find text to translate: ${original}`);
+}