@@ -1680,31 +1680,38 @@ describe('ReactDOMFizzServer', () => { |
1680 | 1680 |
|
1681 | 1681 | // @gate experimental |
1682 | 1682 | it('calls getServerSnapshot instead of getSnapshot', async () => { |
| 1683 | + const ref = React.createRef(); |
| 1684 | + |
1683 | 1685 | function getServerSnapshot() { |
1684 | 1686 | return 'server'; |
1685 | 1687 | } |
| 1688 | + |
1686 | 1689 | function getClientSnapshot() { |
1687 | 1690 | return 'client'; |
1688 | 1691 | } |
| 1692 | + |
1689 | 1693 | function subscribe() { |
1690 | 1694 | return () => {}; |
1691 | 1695 | } |
| 1696 | + |
1692 | 1697 | function Child({text}) { |
1693 | 1698 | Scheduler.unstable_yieldValue(text); |
1694 | 1699 | return text; |
1695 | 1700 | } |
| 1701 | + |
1696 | 1702 | function App() { |
1697 | 1703 | const value = useSyncExternalStore( |
1698 | 1704 | subscribe, |
1699 | 1705 | getClientSnapshot, |
1700 | 1706 | getServerSnapshot, |
1701 | 1707 | ); |
1702 | 1708 | return ( |
1703 | | - <div> |
| 1709 | + <div ref={ref}> |
1704 | 1710 | <Child text={value} /> |
1705 | 1711 | </div> |
1706 | 1712 | ); |
1707 | 1713 | } |
| 1714 | + |
1708 | 1715 | const loggedErrors = []; |
1709 | 1716 | await act(async () => { |
1710 | 1717 | const {pipe} = ReactDOMFizzServer.renderToPipeableStream( |
@@ -1723,14 +1730,29 @@ describe('ReactDOMFizzServer', () => { |
1723 | 1730 |
|
1724 | 1731 | ReactDOM.hydrateRoot(container, <App />); |
1725 | 1732 |
|
1726 | | - expect(() => { |
1727 | | - // The first paint switches to client rendering due to mismatch |
| 1733 | + if (gate(flags => flags.enableClientRenderFallbackOnHydrationMismatch)) { |
| 1734 | + expect(() => { |
| 1735 | + // The first paint switches to client rendering due to mismatch |
| 1736 | + expect(Scheduler).toFlushUntilNextPaint(['client']); |
| 1737 | + }).toErrorDev( |
| 1738 | + 'Warning: An error occurred during hydration. The server HTML was replaced with client content', |
| 1739 | + {withoutStack: true}, |
| 1740 | + ); |
| 1741 | + expect(getVisibleChildren(container)).toEqual(<div>client</div>); |
| 1742 | + } else { |
| 1743 | + const serverRenderedDiv = container.getElementsByTagName('div')[0]; |
| 1744 | + // The first paint uses the server snapshot |
| 1745 | + expect(Scheduler).toFlushUntilNextPaint(['server']); |
| 1746 | + expect(getVisibleChildren(container)).toEqual(<div>server</div>); |
| 1747 | + // Hydration succeeded |
| 1748 | + expect(ref.current).toEqual(serverRenderedDiv); |
| 1749 | + |
| 1750 | + // Asynchronously we detect that the store has changed on the client, |
| 1751 | + // and patch up the inconsistency |
1728 | 1752 | expect(Scheduler).toFlushUntilNextPaint(['client']); |
1729 | | - }).toErrorDev( |
1730 | | - 'Warning: An error occurred during hydration. The server HTML was replaced with client content', |
1731 | | - {withoutStack: true}, |
1732 | | - ); |
1733 | | - expect(getVisibleChildren(container)).toEqual(<div>client</div>); |
| 1753 | + expect(getVisibleChildren(container)).toEqual(<div>client</div>); |
| 1754 | + expect(ref.current).toEqual(serverRenderedDiv); |
| 1755 | + } |
1734 | 1756 | }); |
1735 | 1757 |
|
1736 | 1758 | // The selector implementation uses the lazy ref initialization pattern |
@@ -1790,15 +1812,31 @@ describe('ReactDOMFizzServer', () => { |
1790 | 1812 |
|
1791 | 1813 | ReactDOM.hydrateRoot(container, <App />); |
1792 | 1814 |
|
1793 | | - // The first paint uses the client due to mismatch forcing client render |
1794 | | - expect(() => { |
1795 | | - // The first paint switches to client rendering due to mismatch |
| 1815 | + if (gate(flags => flags.enableClientRenderFallbackOnHydrationMismatch)) { |
| 1816 | + // The first paint uses the client due to mismatch forcing client render |
| 1817 | + expect(() => { |
| 1818 | + // The first paint switches to client rendering due to mismatch |
| 1819 | + expect(Scheduler).toFlushUntilNextPaint(['client']); |
| 1820 | + }).toErrorDev( |
| 1821 | + 'Warning: An error occurred during hydration. The server HTML was replaced with client content', |
| 1822 | + {withoutStack: true}, |
| 1823 | + ); |
| 1824 | + expect(getVisibleChildren(container)).toEqual(<div>client</div>); |
| 1825 | + } else { |
| 1826 | + const serverRenderedDiv = container.getElementsByTagName('div')[0]; |
| 1827 | + |
| 1828 | + // The first paint uses the server snapshot |
| 1829 | + expect(Scheduler).toFlushUntilNextPaint(['server']); |
| 1830 | + expect(getVisibleChildren(container)).toEqual(<div>server</div>); |
| 1831 | + // Hydration succeeded |
| 1832 | + expect(ref.current).toEqual(serverRenderedDiv); |
| 1833 | + |
| 1834 | + // Asynchronously we detect that the store has changed on the client, |
| 1835 | + // and patch up the inconsistency |
1796 | 1836 | expect(Scheduler).toFlushUntilNextPaint(['client']); |
1797 | | - }).toErrorDev( |
1798 | | - 'Warning: An error occurred during hydration. The server HTML was replaced with client content', |
1799 | | - {withoutStack: true}, |
1800 | | - ); |
1801 | | - expect(getVisibleChildren(container)).toEqual(<div>client</div>); |
| 1837 | + expect(getVisibleChildren(container)).toEqual(<div>client</div>); |
| 1838 | + expect(ref.current).toEqual(serverRenderedDiv); |
| 1839 | + } |
1802 | 1840 | }); |
1803 | 1841 |
|
1804 | 1842 | // @gate experimental |
|