-
Notifications
You must be signed in to change notification settings - Fork 1
Reg Lite: support mounting inside a shadow DOM (Web Component hosts) #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gcutrini
wants to merge
3
commits into
main
Choose a base branch
from
feat/shadow-dom-embedding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 (
slotHoststate +detectSlotHostref + the slotted-render branch below).stripe-formhad 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
StripeForminside a realShadowRoot(jsdom 16.6, already a devDependency, supportselement.attachShadow) asserting thePaymentElementwrapper lands as a light-DOM child of the shadow host withslot="stripe-payment", plus one asserting the inline (non-shadow) path is unchanged.There was a problem hiding this comment.
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.