|
| 1 | +import React from "react"; |
| 2 | +import { act } from "react"; |
| 3 | +import { createRoot, type Root } from "react-dom/client"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +vi.mock("../../../donations", () => ({ NonAuthDonation: (props: { recaptchaSiteKey: string }) => <div data-testid="donation-form" data-sitekey={props.recaptchaSiteKey} /> })); |
| 7 | +vi.mock("../../helpers", () => ({ EnvironmentHelper: { init: () => {} } })); |
| 8 | + |
| 9 | +import { NonAuthDonationWrapper } from "../donate/NonAuthDonationWrapper"; |
| 10 | + |
| 11 | +let container: HTMLDivElement | null = null; |
| 12 | +let root: Root | null = null; |
| 13 | + |
| 14 | +const mount = (props: { recaptchaSiteKey?: string } = {}) => { |
| 15 | + container = document.createElement("div"); |
| 16 | + document.body.appendChild(container); |
| 17 | + root = createRoot(container); |
| 18 | + act(() => { root!.render(<NonAuthDonationWrapper churchId="CHU1" {...props} />); }); |
| 19 | + return container; |
| 20 | +}; |
| 21 | + |
| 22 | +afterEach(() => { |
| 23 | + if (root) act(() => { root!.unmount(); }); |
| 24 | + container?.remove(); |
| 25 | + root = null; |
| 26 | + container = null; |
| 27 | + vi.unstubAllEnvs(); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("NonAuthDonationWrapper reCAPTCHA site key", () => { |
| 31 | + it("shows the missing-key error when there is no key and no bypass", () => { |
| 32 | + vi.stubEnv("NEXT_PUBLIC_GOOGLE_RECAPTCHA_SITE_KEY", ""); |
| 33 | + vi.stubEnv("NEXT_PUBLIC_BYPASS_RECAPTCHA", ""); |
| 34 | + const el = mount(); |
| 35 | + expect(el.textContent).toContain("reCAPTCHA site key is missing"); |
| 36 | + expect(el.querySelector("[data-testid='donation-form']")).toBeNull(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("renders the form without the error when the captcha is bypassed", () => { |
| 40 | + vi.stubEnv("NEXT_PUBLIC_GOOGLE_RECAPTCHA_SITE_KEY", ""); |
| 41 | + vi.stubEnv("NEXT_PUBLIC_BYPASS_RECAPTCHA", "true"); |
| 42 | + const el = mount(); |
| 43 | + expect(el.textContent).not.toContain("reCAPTCHA site key is missing"); |
| 44 | + expect(el.querySelector("[data-testid='donation-form']")).not.toBeNull(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("passes a provided key through to the form", () => { |
| 48 | + vi.stubEnv("NEXT_PUBLIC_BYPASS_RECAPTCHA", ""); |
| 49 | + const el = mount({ recaptchaSiteKey: "site-key" }); |
| 50 | + expect(el.querySelector("[data-testid='donation-form']")?.getAttribute("data-sitekey")).toBe("site-key"); |
| 51 | + }); |
| 52 | +}); |
0 commit comments