Skip to content
Merged
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
44 changes: 42 additions & 2 deletions packages/account-ui/src/billing/stripe/StripePaymentForm.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions packages/account-ui/src/billing/stripe/StripePaymentForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'
},
Expand Down Expand Up @@ -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
Expand Down
Loading