-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Fix: Attach custom element event listeners during hydration #35474
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
Omcodes23
wants to merge
3
commits into
facebook:main
Choose a base branch
from
Omcodes23:main
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.
+387
−0
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
363 changes: 363 additions & 0 deletions
363
packages/react-dom/src/__tests__/ReactDOMCustomElementHydration-test.js
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,363 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @emails react-core | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| describe('ReactDOMCustomElementHydration', () => { | ||
| let React; | ||
| let ReactDOM; | ||
| let ReactDOMClient; | ||
| let ReactDOMServer; | ||
| let act; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetModules(); | ||
| React = require('react'); | ||
| ReactDOM = require('react-dom'); | ||
| ReactDOMClient = require('react-dom/client'); | ||
| ReactDOMServer = require('react-dom/server'); | ||
| act = require('internal-test-utils').act; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe('custom element event listener hydration', () => { | ||
| it('should attach custom element event listeners during hydration', async () => { | ||
| const container = document.createElement('div'); | ||
| const myEventHandler = jest.fn(); | ||
|
|
||
| // Mock custom element class | ||
| class CustomElement extends HTMLElement {} | ||
| customElements.define('ce-event-test', CustomElement); | ||
|
|
||
| // Server-side render | ||
| const serverHTML = ReactDOMServer.renderToString( | ||
| <ce-event-test onmy-event={myEventHandler} />, | ||
| ); | ||
|
|
||
| // Inject markup | ||
| container.innerHTML = serverHTML; | ||
| const customElement = container.querySelector('ce-event-test'); | ||
|
|
||
| // Try to dispatch custom event before hydration (should not fire) | ||
| customElement.dispatchEvent(new CustomEvent('my-event')); | ||
| expect(myEventHandler).not.toHaveBeenCalled(); | ||
|
|
||
| // Hydrate with event handler | ||
| await act(async () => { | ||
| ReactDOMClient.hydrateRoot( | ||
| container, | ||
| <ce-event-test onmy-event={myEventHandler} />, | ||
| ); | ||
| }); | ||
|
|
||
| // Dispatch event after hydration | ||
| customElement.dispatchEvent(new CustomEvent('my-event')); | ||
|
|
||
| // Event handler should be attached during hydration | ||
| expect(myEventHandler).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should attach multiple custom event listeners during hydration', async () => { | ||
| const container = document.createElement('div'); | ||
| const moduleLoadedHandler = jest.fn(); | ||
| const moduleErrorHandler = jest.fn(); | ||
| const moduleUpdatedHandler = jest.fn(); | ||
|
|
||
| class CustomElement extends HTMLElement {} | ||
| customElements.define('ce-multi-event', CustomElement); | ||
|
|
||
| // Server-side render | ||
| const serverHTML = ReactDOMServer.renderToString( | ||
| <ce-multi-event | ||
| onmodule-loaded={moduleLoadedHandler} | ||
| onmodule-error={moduleErrorHandler} | ||
| onmodule-updated={moduleUpdatedHandler} | ||
| />, | ||
| ); | ||
|
|
||
| container.innerHTML = serverHTML; | ||
| const customElement = container.querySelector('ce-multi-event'); | ||
|
|
||
| // Hydrate with event handlers | ||
| await act(async () => { | ||
| ReactDOMClient.hydrateRoot( | ||
| container, | ||
| <ce-multi-event | ||
| onmodule-loaded={moduleLoadedHandler} | ||
| onmodule-error={moduleErrorHandler} | ||
| onmodule-updated={moduleUpdatedHandler} | ||
| />, | ||
| ); | ||
| }); | ||
|
|
||
| customElement.dispatchEvent(new CustomEvent('module-loaded')); | ||
| customElement.dispatchEvent(new CustomEvent('module-error')); | ||
| customElement.dispatchEvent(new CustomEvent('module-updated')); | ||
|
|
||
| expect(moduleLoadedHandler).toHaveBeenCalledTimes(1); | ||
| expect(moduleErrorHandler).toHaveBeenCalledTimes(1); | ||
| expect(moduleUpdatedHandler).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should hydrate primitive prop types on custom elements', async () => { | ||
| const container = document.createElement('div'); | ||
|
|
||
| class CustomElementWithProps extends HTMLElement {} | ||
| customElements.define('ce-primitive-props', CustomElementWithProps); | ||
|
|
||
| // Server-side render with primitive props | ||
| const serverHTML = ReactDOMServer.renderToString( | ||
| <ce-primitive-props | ||
| stringValue="test" | ||
| numberValue={42} | ||
| trueProp={true} | ||
| />, | ||
| ); | ||
|
|
||
| container.innerHTML = serverHTML; | ||
| const customElement = container.querySelector('ce-primitive-props'); | ||
|
|
||
| // Hydrate | ||
| await act(async () => { | ||
| ReactDOMClient.hydrateRoot( | ||
| container, | ||
| <ce-primitive-props | ||
| stringValue="test" | ||
| numberValue={42} | ||
| trueProp={true} | ||
| />, | ||
| ); | ||
| }); | ||
|
|
||
| // After hydration, primitive attributes should be present | ||
| expect(customElement.hasAttribute('stringValue')).toBe(true); | ||
| expect(customElement.getAttribute('stringValue')).toBe('test'); | ||
| expect(customElement.hasAttribute('numberValue')).toBe(true); | ||
| expect(customElement.getAttribute('numberValue')).toBe('42'); | ||
| expect(customElement.hasAttribute('trueProp')).toBe(true); | ||
| }); | ||
|
|
||
| it('should not set non-primitive props as attributes during SSR', async () => { | ||
| // Server-side render with non-primitive props | ||
| const serverHTML = ReactDOMServer.renderToString( | ||
| <ce-advanced-props | ||
| objectProp={{key: 'value'}} | ||
| functionProp={() => {}} | ||
| falseProp={false} | ||
| />, | ||
| ); | ||
|
|
||
| // Non-primitive values should not appear as attributes in server HTML | ||
| expect(serverHTML).not.toContain('objectProp'); | ||
| expect(serverHTML).not.toContain('functionProp'); | ||
| expect(serverHTML).not.toContain('falseProp'); | ||
| }); | ||
|
|
||
| it('should handle updating custom element event listeners after hydration', async () => { | ||
| const container = document.createElement('div'); | ||
| const initialHandler = jest.fn(); | ||
| const updatedHandler = jest.fn(); | ||
|
|
||
| class CustomElementForUpdate extends HTMLElement {} | ||
| customElements.define('ce-update-test', CustomElementForUpdate); | ||
|
|
||
| // Server-side render with initial event handler | ||
| const serverHTML = ReactDOMServer.renderToString( | ||
| <ce-update-test onmyevent={initialHandler} />, | ||
| ); | ||
|
|
||
| container.innerHTML = serverHTML; | ||
| const customElement = container.querySelector('ce-update-test'); | ||
|
|
||
| // Hydrate | ||
| let root; | ||
| await act(async () => { | ||
| root = ReactDOMClient.hydrateRoot( | ||
| container, | ||
| <ce-update-test onmyevent={initialHandler} />, | ||
| ); | ||
| }); | ||
|
|
||
| customElement.dispatchEvent(new CustomEvent('myevent')); | ||
| expect(initialHandler).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Update the event handler | ||
| await act(async () => { | ||
| root.render(<ce-update-test onmyevent={updatedHandler} />); | ||
| }); | ||
|
|
||
| customElement.dispatchEvent(new CustomEvent('myevent')); | ||
|
|
||
| // The updated handler should be called, not the initial one | ||
| expect(updatedHandler).toHaveBeenCalledTimes(1); | ||
| expect(initialHandler).toHaveBeenCalledTimes(1); // Still only called once | ||
| }); | ||
|
|
||
| it('should handle custom element registered after hydration', async () => { | ||
| const container = document.createElement('div'); | ||
| const myEventHandler = jest.fn(); | ||
|
|
||
| // Server-side render with event handler for element not yet registered | ||
| const serverHTML = ReactDOMServer.renderToString( | ||
| <ce-registered-after-hydration onmy-event={myEventHandler} />, | ||
| ); | ||
|
|
||
| container.innerHTML = serverHTML; | ||
| const customElement = container.querySelector( | ||
| 'ce-registered-after-hydration', | ||
| ); | ||
|
|
||
| // Hydrate - the element is not yet registered | ||
| // Event listeners should still be attached | ||
| await act(async () => { | ||
| ReactDOMClient.hydrateRoot( | ||
| container, | ||
| <ce-registered-after-hydration onmy-event={myEventHandler} />, | ||
| ); | ||
| }); | ||
|
|
||
| // Register the element after hydration | ||
| class CustomElementRegisteredAfterHydration extends HTMLElement {} | ||
| customElements.define( | ||
| 'ce-registered-after-hydration', | ||
| CustomElementRegisteredAfterHydration, | ||
| ); | ||
|
|
||
| // Dispatch event after registration | ||
| customElement.dispatchEvent(new CustomEvent('my-event')); | ||
|
|
||
| // Event listener should work even if element wasn't registered during hydration | ||
| expect(myEventHandler).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should properly hydrate custom elements with mixed props', async () => { | ||
| const container = document.createElement('div'); | ||
| const myEventHandler = jest.fn(); | ||
|
|
||
| class MixedPropsElement extends HTMLElement {} | ||
| customElements.define('ce-mixed-props', MixedPropsElement); | ||
|
|
||
| // Server-side render with mixed prop types | ||
| const serverHTML = ReactDOMServer.renderToString( | ||
| <ce-mixed-props | ||
| stringAttr="value" | ||
| numberAttr={123} | ||
| onmyevent={myEventHandler} | ||
| className="custom-class" | ||
| />, | ||
| ); | ||
|
|
||
| container.innerHTML = serverHTML; | ||
| const customElement = container.querySelector('ce-mixed-props'); | ||
|
|
||
| // Hydrate | ||
| await act(async () => { | ||
| ReactDOMClient.hydrateRoot( | ||
| container, | ||
| <ce-mixed-props | ||
| stringAttr="value" | ||
| numberAttr={123} | ||
| onmyevent={myEventHandler} | ||
| className="custom-class" | ||
| />, | ||
| ); | ||
| }); | ||
|
|
||
| customElement.dispatchEvent(new CustomEvent('myevent')); | ||
|
|
||
| // Event should be fired | ||
| expect(myEventHandler).toHaveBeenCalledTimes(1); | ||
| // Attributes should be present | ||
| expect(customElement.hasAttribute('stringAttr')).toBe(true); | ||
| expect(customElement.getAttribute('stringAttr')).toBe('value'); | ||
| expect(customElement.hasAttribute('class')).toBe(true); | ||
| expect(customElement.getAttribute('class')).toBe('custom-class'); | ||
| }); | ||
|
|
||
| it('should remove custom element event listeners when prop is removed', async () => { | ||
| const container = document.createElement('div'); | ||
| const myEventHandler = jest.fn(); | ||
|
|
||
| class CustomElementRemovalTest extends HTMLElement {} | ||
| customElements.define('ce-removal-test', CustomElementRemovalTest); | ||
|
|
||
| // Server-side render with event handler | ||
| const serverHTML = ReactDOMServer.renderToString( | ||
| <ce-removal-test onmyevent={myEventHandler} />, | ||
| ); | ||
|
|
||
| container.innerHTML = serverHTML; | ||
| const customElement = container.querySelector('ce-removal-test'); | ||
|
|
||
| // Hydrate | ||
| let root; | ||
| await act(async () => { | ||
| root = ReactDOMClient.hydrateRoot( | ||
| container, | ||
| <ce-removal-test onmyevent={myEventHandler} />, | ||
| ); | ||
| }); | ||
|
|
||
| // Remove the event handler | ||
| await act(async () => { | ||
| root.render(<ce-removal-test />); | ||
| }); | ||
|
|
||
| customElement.dispatchEvent(new CustomEvent('myevent')); | ||
|
|
||
| // Event should not fire after handler removal | ||
| expect(myEventHandler).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('custom element property hydration', () => { | ||
| it('should handle custom properties during hydration when element is defined', async () => { | ||
| const container = document.createElement('div'); | ||
|
|
||
| // Create and register a custom element with custom properties | ||
| class CustomElementWithProperty extends HTMLElement { | ||
| constructor() { | ||
| super(); | ||
| this._internalValue = undefined; | ||
| } | ||
|
|
||
| set customProperty(value) { | ||
| this._internalValue = value; | ||
| } | ||
|
|
||
| get customProperty() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would appreciate if you can carefully go through all the AI generated code yourself before asking for review. For instance these are not used at all. What is the purpose for these? |
||
| return this._internalValue; | ||
| } | ||
| } | ||
| customElements.define('ce-with-property', CustomElementWithProperty); | ||
|
|
||
| // Server-side render | ||
| const serverHTML = ReactDOMServer.renderToString( | ||
| <ce-with-property data-attr="test" />, | ||
| ); | ||
|
|
||
| container.innerHTML = serverHTML; | ||
| const customElement = container.querySelector('ce-with-property'); | ||
|
|
||
| // Hydrate | ||
| await act(async () => { | ||
Omcodes23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ReactDOMClient.hydrateRoot( | ||
| container, | ||
| <ce-with-property data-attr="test" />, | ||
| ); | ||
| }); | ||
|
|
||
| // Verify the element is properly hydrated | ||
| expect(customElement.getAttribute('data-attr')).toBe('test'); | ||
| }); | ||
| }); | ||
| }); | ||
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.
This comment does not bring much additional value. I would still consider keeping #35474 (comment) thread open to hear what maintainers have to say - I only reported the original issue #35446 but I haven't dug deeper in React's inner functionality here.