diff --git a/packages/account-ui/src/billing/stripe/StripePaymentForm.test.ts b/packages/account-ui/src/billing/stripe/StripePaymentForm.test.ts index 0973e5b86e4..523331681f6 100644 --- a/packages/account-ui/src/billing/stripe/StripePaymentForm.test.ts +++ b/packages/account-ui/src/billing/stripe/StripePaymentForm.test.ts @@ -1,6 +1,14 @@ import userEvent from '@testing-library/user-event' import { cleanup, render, screen, waitFor } from '@testing-library/vue' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { + afterEach, + beforeEach, + describe, + expect, + it, + onTestFinished, + vi +} from 'vitest' import { h } from 'vue' import type { @@ -75,10 +83,12 @@ function renderForm( publishableKey?: string onConfirm?: (token: string) => void onSubmittingChange?: (submitting: boolean) => void + container?: HTMLElement } = {} ) { - const { publishableKey = 'pk_test_example', ...rest } = props + const { publishableKey = 'pk_test_example', container, ...rest } = props return render(StripePaymentForm, { + container, props: { publishableKey, amountCents, @@ -156,6 +166,36 @@ describe('StripePaymentForm', () => { }) }) + it('themes Stripe from the theme scope the form renders in, not the page body', async () => { + document.body.style.setProperty('--base-foreground', 'rgb(20, 20, 20)') + document.body.style.setProperty('--base-background', 'rgb(255, 255, 255)') + document.body.style.fontFamily = 'serif' + const darkScope = document.createElement('div') + darkScope.style.setProperty('--base-foreground', 'rgb(250, 250, 250)') + darkScope.style.setProperty('--base-background', 'rgb(30, 30, 30)') + darkScope.style.fontFamily = 'Inter' + document.body.append(darkScope) + onTestFinished(() => { + darkScope.remove() + document.body.removeAttribute('style') + }) + + renderForm(66500, 'pmc_test', { container: darkScope }) + await waitFor(() => expect(stripeMocks.stripe.elements).toHaveBeenCalled()) + + expect(stripeMocks.stripe.elements).toHaveBeenCalledWith( + expect.objectContaining({ + appearance: expect.objectContaining({ + variables: expect.objectContaining({ + colorText: 'rgb(250, 250, 250)', + colorBackground: 'rgb(30, 30, 30)', + fontFamily: 'Inter' + }) + }) + }) + ) + }) + describe('checkout journey instrumentation', () => { it('emits payment_element_ready on the ready callback for the live mount', async () => { renderForm() diff --git a/packages/account-ui/src/billing/stripe/StripePaymentForm.vue b/packages/account-ui/src/billing/stripe/StripePaymentForm.vue index bcf629d54c1..69e0ccef168 100644 --- a/packages/account-ui/src/billing/stripe/StripePaymentForm.vue +++ b/packages/account-ui/src/billing/stripe/StripePaymentForm.vue @@ -234,6 +234,9 @@ onMounted(async () => { failElementInit() return } + const themeRoot = paymentElementTarget.value + const resolveThemeColor = (variable: string) => + resolveColorIn(themeRoot, variable) stripeElements.value = stripe.elements({ mode: 'subscription', @@ -257,7 +260,7 @@ onMounted(async () => { // Same token as the pricing table's "Save 20%" pill, so all // deal/discount badges share one accent. colorSuccess: resolveThemeColor('--primary-background'), - fontFamily: getComputedStyle(document.body).fontFamily, + fontFamily: getComputedStyle(themeRoot).fontFamily, borderRadius: '10px', spacingUnit: '5px' }, @@ -363,10 +366,10 @@ async function submit() { } } -function resolveThemeColor(variable: string) { +function resolveColorIn(themeRoot: HTMLElement, variable: string) { const probe = document.createElement('span') probe.style.color = `var(${variable})` - document.body.append(probe) + themeRoot.append(probe) const color = getComputedStyle(probe).color probe.remove() return color