|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { dirname, join } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { DonationHelper } from "../helpers/DonationHelper"; |
| 6 | + |
| 7 | +const dir = dirname(fileURLToPath(import.meta.url)); |
| 8 | +const read = (rel: string) => readFileSync(join(dir, rel), "utf8"); |
| 9 | + |
| 10 | +const guestForms = [ |
| 11 | + "../providers/stripe/NonAuthDonationInner.tsx", |
| 12 | + "../providers/paypal/PayPalNonAuthDonationInner.tsx", |
| 13 | + "../providers/paystack/PaystackNonAuthDonationInner.tsx", |
| 14 | + "../providers/kingdomfunding/KingdomFundingNonAuthDonationInner.tsx" |
| 15 | +]; |
| 16 | + |
| 17 | +describe("giving link ?recurring param", () => { |
| 18 | + afterEach(() => { window.history.replaceState(null, "", "/"); }); |
| 19 | + |
| 20 | + it("treats 1, true and yes as a recurring link", () => { |
| 21 | + for (const v of ["1", "true", "TRUE", " yes "]) expect(DonationHelper.isRecurringLinkValue(v), v).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + it("defaults to a one-time gift when the param is missing or anything else", () => { |
| 25 | + for (const v of [null, undefined, "", "0", "false", "no", "monthly"]) expect(DonationHelper.isRecurringLinkValue(v as any), String(v)).toBe(false); |
| 26 | + }); |
| 27 | + |
| 28 | + it("reads the param from a query string", () => { |
| 29 | + expect(DonationHelper.isRecurringLink("?fundId=abc&amount=25&recurring=1")).toBe(true); |
| 30 | + expect(DonationHelper.isRecurringLink("?fundId=abc&amount=25")).toBe(false); |
| 31 | + expect(DonationHelper.isRecurringLink("")).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + it("falls back to the page URL when no query string is passed", () => { |
| 35 | + window.history.replaceState(null, "", "/donate?recurring=1"); |
| 36 | + expect(DonationHelper.isRecurringLink()).toBe(true); |
| 37 | + window.history.replaceState(null, "", "/donate"); |
| 38 | + expect(DonationHelper.isRecurringLink()).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it("every guest form starts on Recurring from the link but keeps one-time as the default", () => { |
| 42 | + for (const rel of guestForms) { |
| 43 | + const src = read(rel); |
| 44 | + expect(src, rel).toMatch(/DonationHelper\.isRecurringLinkValue\(getUrlParam\("recurring"\)\)\) setDonationType\("recurring"\)/); |
| 45 | + // The default stays one-time unless the church only allows recurring gifts. |
| 46 | + expect(src, rel).not.toMatch(/useState<"once" \| "recurring">\(\s*"recurring"\s*\)/); |
| 47 | + // The donor can still switch back before paying. |
| 48 | + expect(src, rel).toContain('onClick={() => setDonationType("once")}'); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + it("only applies the link where the one-time/recurring choice is shown", () => { |
| 53 | + expect(read(guestForms[0])).toContain('if (paymentType !== "bank" && allowSingleGift && allowRecurring && DonationHelper.isRecurringLinkValue'); |
| 54 | + expect(read(guestForms[1])).toContain("if (allowSingleGift && allowRecurring && DonationHelper.isRecurringLinkValue"); |
| 55 | + expect(read(guestForms[2])).toContain("if (props.allowRecurring !== false && DonationHelper.isRecurringLinkValue"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("the member form starts on Recurring from the link and keeps both buttons", () => { |
| 59 | + const src = read("../components/MultiGatewayDonationForm.tsx"); |
| 60 | + expect(src).toContain('useState<string | undefined>("once")'); |
| 61 | + expect(src).toMatch(/if \(DonationHelper\.isRecurringLink\(\)\) setDonationType\("recurring"\)/); |
| 62 | + expect(src).toContain('id="single-donation-button"'); |
| 63 | + expect(src).toContain('id="recurring-donation-button"'); |
| 64 | + }); |
| 65 | +}); |
0 commit comments