Skip to content
Open
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
86 changes: 86 additions & 0 deletions src/components/stripe-form/__tests__/stripe-form.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React from 'react';
import { render } from '@testing-library/react';

import StripeForm from '../index';

// Stripe's own hooks reach for a real Elements context and a live iframe, so the
// SDK is stubbed down to the one thing these tests care about: where in the tree
// the PaymentElement ends up.
// Every DOM node the mock is ever mounted into, so a test can assert the Element
// was never attached inside a shadow tree, not even for one render.
const mountRoots = [];

jest.mock('@stripe/react-stripe-js', () => ({
useStripe: () => ({}),
useElements: () => ({ getElement: () => null }),
PaymentElement: () => (
<div
data-testid="payment-element"
ref={(node) => { if (node) mountRoots.push(node.getRootNode()); }}
/>
),
}));

beforeEach(() => {
mountRoots.length = 0;
document.body.innerHTML = '';
});

const props = {
reservation: { owner_first_name: 'Ada', owner_last_name: 'Lovelace' },
payTicket: jest.fn(),
userProfile: {},
provider: 'stripe',
hidePostalCode: false,
stripeReturnUrl: 'https://example.test/return',
onError: jest.fn(),
};

const renderInShadowRoot = () => {
const host = document.createElement('div');
document.body.appendChild(host);
const shadowRoot = host.attachShadow({ mode: 'open' });
const mountPoint = document.createElement('div');
shadowRoot.appendChild(mountPoint);
render(<StripeForm {...props} />, { container: mountPoint });
return { host, shadowRoot };
};

describe('StripeForm shadow DOM handling', () => {
it('keeps the PaymentElement in the light DOM when shadow-mounted', () => {
const { host, shadowRoot } = renderInShadowRoot();

// Stripe cannot reach an element inside a shadow tree, so the wrapper has
// to be a child of the host itself.
const slotted = host.querySelector(':scope > [slot="stripe-payment"]');
expect(slotted).not.toBeNull();
expect(slotted.querySelector('[data-testid="payment-element"]')).not.toBeNull();
expect(shadowRoot.contains(slotted)).toBe(false);
});

it('leaves a matching slot in the form to display it in flow', () => {
const { shadowRoot } = renderInShadowRoot();

const form = shadowRoot.querySelector('form#payment-form');
expect(form.querySelector('slot[name="stripe-payment"]')).not.toBeNull();
});

it('mounts the PaymentElement inline when there is no shadow root', () => {
const { container } = render(<StripeForm {...props} />);

const form = container.querySelector('form#payment-form');
expect(form.querySelector('[data-testid="payment-element"]')).not.toBeNull();
expect(form.querySelector('slot')).toBeNull();
expect(document.querySelector('[slot="stripe-payment"]')).toBeNull();
});

it('never attaches the PaymentElement inside the shadow tree', () => {
// The callback ref resolves the mount context during commit, and nothing
// is rendered until it has. Without that wait the first render would mount
// the Element in the shadow tree, where Stripe cannot reach it.
const { shadowRoot } = renderInShadowRoot();

expect(mountRoots.length).toBeGreaterThan(0);
expect(mountRoots).not.toContain(shadowRoot);
});
});
34 changes: 31 additions & 3 deletions src/components/stripe-form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* limitations under the License.
**/

import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
import { useForm } from 'react-hook-form';

import {
Expand Down Expand Up @@ -63,11 +64,24 @@ const stripeErrorCodeMap = {
};


// Slot names are scoped to their shadow root, so widgets on one page don't collide.
const PAYMENT_SLOT = 'stripe-payment';

const StripeForm = ({ reservation, payTicket, userProfile, provider, hidePostalCode, stripeReturnUrl, onError }) => {
const stripe = useStripe();
const elements = useElements();
const [paymentElement, setPaymentElement] = useState(null);

// Stripe cannot see into a shadow tree, so when shadow-mounted the Element is
// kept in the light DOM and slotted back in flow (stripe/stripe-js#143).
// undefined while detecting, null in the light DOM, else the shadow host.
const [slotHost, setSlotHost] = useState(undefined);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gcutrini No test covers the new shadow-DOM detection/portal logic (slotHost state + detectSlotHost ref + the slotted-render branch below). stripe-form had zero tests before this PR and still has none, so a regression here (wrong portal target, slot never receiving the projected element) would fail silently in production for any shadow-DOM host, with nothing catching it in CI.

Suggested fix: add a unit test that renders StripeForm inside a real ShadowRoot (jsdom 16.6, already a devDependency, supports element.attachShadow) asserting the PaymentElement wrapper lands as a light-DOM child of the shadow host with slot="stripe-payment", plus one asserting the inline (non-shadow) path is unchanged.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in bb4e081. Four tests, rendering into a real ShadowRoot via attachShadow as you suggested.

They pin the portal target lands on the host and not in the shadow tree, the slot left behind in the form, the inline path unchanged, and that nothing mounts before the callback ref resolves the context.

I checked each one fails on a matching break: portal into the shadow root instead of the host, dropping the slot, dropping the wait for the context, and taking the slot path in the light DOM.

const detectSlotHost = useCallback((node) => {
if (!node) return;
const rootNode = node.getRootNode();
setSlotHost(rootNode instanceof ShadowRoot ? rootNode.host : null);
}, []);

useEffect(() => {
if (elements) {
setPaymentElement(elements.getElement('payment'));
Expand Down Expand Up @@ -156,9 +170,23 @@ const StripeForm = ({ reservation, payTicket, userProfile, provider, hidePostalC
}
}

const renderPaymentElement = () => {
const paymentEl = <PaymentElement options={paymentOptions} />;
// Wait for the callback ref: mounting before the context is known would
// put the Element in the shadow tree, out of Stripe's reach.
if (slotHost === undefined) return null;
if (!slotHost) return paymentEl;
return (
<>
<slot name={PAYMENT_SLOT} />
{createPortal(<div slot={PAYMENT_SLOT}>{paymentEl}</div>, slotHost)}
</>
);
};

return (
<form className={styles.form} id="payment-form" onSubmit={handleSubmit(onSubmit)}>
<PaymentElement options={paymentOptions} />
<form ref={detectSlotHost} className={styles.form} id="payment-form" onSubmit={handleSubmit(onSubmit)}>
{renderPaymentElement()}
</form>
)
};
Expand Down
Loading