|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @emails react-core |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +let React; |
| 13 | +let ReactDOM; |
| 14 | +let ReactDOMServer; |
| 15 | +let Scheduler; |
| 16 | +let act; |
| 17 | +let useMutableSource; |
| 18 | + |
| 19 | +describe('useMutableSourceHydration', () => { |
| 20 | + beforeEach(() => { |
| 21 | + jest.resetModules(); |
| 22 | + |
| 23 | + React = require('react'); |
| 24 | + ReactDOM = require('react-dom'); |
| 25 | + ReactDOMServer = require('react-dom/server'); |
| 26 | + Scheduler = require('scheduler'); |
| 27 | + |
| 28 | + useMutableSource = React.useMutableSource; |
| 29 | + act = require('react-dom/test-utils').act; |
| 30 | + }); |
| 31 | + |
| 32 | + const defaultGetSnapshot = source => source.value; |
| 33 | + const defaultSubscribe = (source, callback) => source.subscribe(callback); |
| 34 | + |
| 35 | + function createComplexSource(initialValueA, initialValueB) { |
| 36 | + const callbacksA = []; |
| 37 | + const callbacksB = []; |
| 38 | + let revision = 0; |
| 39 | + let valueA = initialValueA; |
| 40 | + let valueB = initialValueB; |
| 41 | + |
| 42 | + const subscribeHelper = (callbacks, callback) => { |
| 43 | + if (callbacks.indexOf(callback) < 0) { |
| 44 | + callbacks.push(callback); |
| 45 | + } |
| 46 | + return () => { |
| 47 | + const index = callbacks.indexOf(callback); |
| 48 | + if (index >= 0) { |
| 49 | + callbacks.splice(index, 1); |
| 50 | + } |
| 51 | + }; |
| 52 | + }; |
| 53 | + |
| 54 | + return { |
| 55 | + subscribeA(callback) { |
| 56 | + return subscribeHelper(callbacksA, callback); |
| 57 | + }, |
| 58 | + subscribeB(callback) { |
| 59 | + return subscribeHelper(callbacksB, callback); |
| 60 | + }, |
| 61 | + |
| 62 | + get listenerCountA() { |
| 63 | + return callbacksA.length; |
| 64 | + }, |
| 65 | + get listenerCountB() { |
| 66 | + return callbacksB.length; |
| 67 | + }, |
| 68 | + |
| 69 | + set valueA(newValue) { |
| 70 | + revision++; |
| 71 | + valueA = newValue; |
| 72 | + callbacksA.forEach(callback => callback()); |
| 73 | + }, |
| 74 | + get valueA() { |
| 75 | + return valueA; |
| 76 | + }, |
| 77 | + |
| 78 | + set valueB(newValue) { |
| 79 | + revision++; |
| 80 | + valueB = newValue; |
| 81 | + callbacksB.forEach(callback => callback()); |
| 82 | + }, |
| 83 | + get valueB() { |
| 84 | + return valueB; |
| 85 | + }, |
| 86 | + |
| 87 | + get version() { |
| 88 | + return revision; |
| 89 | + }, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + function createSource(initialValue) { |
| 94 | + const callbacks = []; |
| 95 | + let revision = 0; |
| 96 | + let value = initialValue; |
| 97 | + return { |
| 98 | + subscribe(callback) { |
| 99 | + if (callbacks.indexOf(callback) < 0) { |
| 100 | + callbacks.push(callback); |
| 101 | + } |
| 102 | + return () => { |
| 103 | + const index = callbacks.indexOf(callback); |
| 104 | + if (index >= 0) { |
| 105 | + callbacks.splice(index, 1); |
| 106 | + } |
| 107 | + }; |
| 108 | + }, |
| 109 | + get listenerCount() { |
| 110 | + return callbacks.length; |
| 111 | + }, |
| 112 | + set value(newValue) { |
| 113 | + revision++; |
| 114 | + value = newValue; |
| 115 | + callbacks.forEach(callback => callback()); |
| 116 | + }, |
| 117 | + get value() { |
| 118 | + return value; |
| 119 | + }, |
| 120 | + get version() { |
| 121 | + return revision; |
| 122 | + }, |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + function createMutableSource(source) { |
| 127 | + return React.createMutableSource(source, param => param.version); |
| 128 | + } |
| 129 | + |
| 130 | + function Component({getSnapshot, label, mutableSource, subscribe}) { |
| 131 | + const snapshot = useMutableSource(mutableSource, getSnapshot, subscribe); |
| 132 | + Scheduler.unstable_yieldValue(`${label}:${snapshot}`); |
| 133 | + return <div>{`${label}:${snapshot}`}</div>; |
| 134 | + } |
| 135 | + |
| 136 | + // @gate experimental |
| 137 | + it('should render and hydrate', () => { |
| 138 | + const source = createSource('one'); |
| 139 | + const mutableSource = createMutableSource(source); |
| 140 | + |
| 141 | + function TestComponent() { |
| 142 | + return ( |
| 143 | + <Component |
| 144 | + label="only" |
| 145 | + getSnapshot={defaultGetSnapshot} |
| 146 | + mutableSource={mutableSource} |
| 147 | + subscribe={defaultSubscribe} |
| 148 | + /> |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + const container = document.createElement('div'); |
| 153 | + document.body.appendChild(container); |
| 154 | + |
| 155 | + const htmlString = ReactDOMServer.renderToString(<TestComponent />); |
| 156 | + container.innerHTML = htmlString; |
| 157 | + expect(Scheduler).toHaveYielded(['only:one']); |
| 158 | + expect(source.listenerCount).toBe(0); |
| 159 | + |
| 160 | + const root = ReactDOM.unstable_createRoot(container, {hydrate: true}); |
| 161 | + act(() => { |
| 162 | + root.registerMutableSourceForHydration(mutableSource); |
| 163 | + root.render(<TestComponent />); |
| 164 | + }); |
| 165 | + expect(Scheduler).toHaveYielded(['only:one']); |
| 166 | + expect(source.listenerCount).toBe(1); |
| 167 | + }); |
| 168 | + |
| 169 | + // @gate experimental |
| 170 | + it('should detect a tear before hydrating a component', () => { |
| 171 | + const source = createSource('one'); |
| 172 | + const mutableSource = createMutableSource(source); |
| 173 | + |
| 174 | + function TestComponent() { |
| 175 | + return ( |
| 176 | + <Component |
| 177 | + label="only" |
| 178 | + getSnapshot={defaultGetSnapshot} |
| 179 | + mutableSource={mutableSource} |
| 180 | + subscribe={defaultSubscribe} |
| 181 | + /> |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + const container = document.createElement('div'); |
| 186 | + document.body.appendChild(container); |
| 187 | + |
| 188 | + const htmlString = ReactDOMServer.renderToString(<TestComponent />); |
| 189 | + container.innerHTML = htmlString; |
| 190 | + expect(Scheduler).toHaveYielded(['only:one']); |
| 191 | + expect(source.listenerCount).toBe(0); |
| 192 | + |
| 193 | + const root = ReactDOM.unstable_createRoot(container, {hydrate: true}); |
| 194 | + expect(() => { |
| 195 | + act(() => { |
| 196 | + root.registerMutableSourceForHydration(mutableSource); |
| 197 | + root.render(<TestComponent />); |
| 198 | + |
| 199 | + source.value = 'two'; |
| 200 | + }); |
| 201 | + }).toErrorDev( |
| 202 | + 'Warning: Did not expect server HTML to contain a <div> in <div>.', |
| 203 | + {withoutStack: true}, |
| 204 | + ); |
| 205 | + expect(Scheduler).toHaveYielded(['only:two']); |
| 206 | + expect(source.listenerCount).toBe(1); |
| 207 | + }); |
| 208 | + |
| 209 | + // @gate experimental |
| 210 | + it('should detect a tear between hydrating components', () => { |
| 211 | + const source = createSource('one'); |
| 212 | + const mutableSource = createMutableSource(source); |
| 213 | + |
| 214 | + function TestComponent() { |
| 215 | + return ( |
| 216 | + <> |
| 217 | + <Component |
| 218 | + label="a" |
| 219 | + getSnapshot={defaultGetSnapshot} |
| 220 | + mutableSource={mutableSource} |
| 221 | + subscribe={defaultSubscribe} |
| 222 | + /> |
| 223 | + <Component |
| 224 | + label="b" |
| 225 | + getSnapshot={defaultGetSnapshot} |
| 226 | + mutableSource={mutableSource} |
| 227 | + subscribe={defaultSubscribe} |
| 228 | + /> |
| 229 | + </> |
| 230 | + ); |
| 231 | + } |
| 232 | + |
| 233 | + const container = document.createElement('div'); |
| 234 | + document.body.appendChild(container); |
| 235 | + |
| 236 | + const htmlString = ReactDOMServer.renderToString(<TestComponent />); |
| 237 | + container.innerHTML = htmlString; |
| 238 | + expect(Scheduler).toHaveYielded(['a:one', 'b:one']); |
| 239 | + expect(source.listenerCount).toBe(0); |
| 240 | + |
| 241 | + const root = ReactDOM.unstable_createRoot(container, {hydrate: true}); |
| 242 | + expect(() => { |
| 243 | + act(() => { |
| 244 | + root.registerMutableSourceForHydration(mutableSource); |
| 245 | + root.render(<TestComponent />); |
| 246 | + expect(Scheduler).toFlushAndYieldThrough(['a:one']); |
| 247 | + source.value = 'two'; |
| 248 | + }); |
| 249 | + }).toErrorDev( |
| 250 | + 'Warning: Did not expect server HTML to contain a <div> in <div>.', |
| 251 | + {withoutStack: true}, |
| 252 | + ); |
| 253 | + expect(Scheduler).toHaveYielded(['a:two', 'b:two']); |
| 254 | + expect(source.listenerCount).toBe(2); |
| 255 | + }); |
| 256 | + |
| 257 | + // @gate experimental |
| 258 | + it('should detect a tear between hydrating components reading from different parts of a source', () => { |
| 259 | + const source = createComplexSource('a:one', 'b:one'); |
| 260 | + const mutableSource = createMutableSource(source); |
| 261 | + |
| 262 | + // Subscribe to part of the store. |
| 263 | + const getSnapshotA = s => s.valueA; |
| 264 | + const subscribeA = (s, callback) => s.subscribeA(callback); |
| 265 | + const getSnapshotB = s => s.valueB; |
| 266 | + const subscribeB = (s, callback) => s.subscribeB(callback); |
| 267 | + |
| 268 | + const container = document.createElement('div'); |
| 269 | + document.body.appendChild(container); |
| 270 | + |
| 271 | + const htmlString = ReactDOMServer.renderToString( |
| 272 | + <> |
| 273 | + <Component |
| 274 | + label="0" |
| 275 | + getSnapshot={getSnapshotA} |
| 276 | + mutableSource={mutableSource} |
| 277 | + subscribe={subscribeA} |
| 278 | + /> |
| 279 | + <Component |
| 280 | + label="1" |
| 281 | + getSnapshot={getSnapshotB} |
| 282 | + mutableSource={mutableSource} |
| 283 | + subscribe={subscribeB} |
| 284 | + /> |
| 285 | + </>, |
| 286 | + ); |
| 287 | + container.innerHTML = htmlString; |
| 288 | + expect(Scheduler).toHaveYielded(['0:a:one', '1:b:one']); |
| 289 | + |
| 290 | + const root = ReactDOM.unstable_createRoot(container, {hydrate: true}); |
| 291 | + expect(() => { |
| 292 | + act(() => { |
| 293 | + root.registerMutableSourceForHydration(mutableSource); |
| 294 | + root.render( |
| 295 | + <> |
| 296 | + <Component |
| 297 | + label="0" |
| 298 | + getSnapshot={getSnapshotA} |
| 299 | + mutableSource={mutableSource} |
| 300 | + subscribe={subscribeA} |
| 301 | + /> |
| 302 | + <Component |
| 303 | + label="1" |
| 304 | + getSnapshot={getSnapshotB} |
| 305 | + mutableSource={mutableSource} |
| 306 | + subscribe={subscribeB} |
| 307 | + /> |
| 308 | + </>, |
| 309 | + ); |
| 310 | + expect(Scheduler).toFlushAndYieldThrough(['0:a:one']); |
| 311 | + source.valueB = 'b:two'; |
| 312 | + }); |
| 313 | + }).toErrorDev( |
| 314 | + 'Warning: Did not expect server HTML to contain a <div> in <div>.', |
| 315 | + {withoutStack: true}, |
| 316 | + ); |
| 317 | + expect(Scheduler).toHaveYielded(['0:a:one', '1:b:two']); |
| 318 | + }); |
| 319 | + |
| 320 | + // @gate experimental |
| 321 | + it('should detect a tear during a higher priority interruption', () => { |
| 322 | + const source = createSource('one'); |
| 323 | + const mutableSource = createMutableSource(source); |
| 324 | + |
| 325 | + function Unrelated({flag}) { |
| 326 | + Scheduler.unstable_yieldValue(flag); |
| 327 | + return flag; |
| 328 | + } |
| 329 | + |
| 330 | + function TestComponent({flag}) { |
| 331 | + return ( |
| 332 | + <> |
| 333 | + <Unrelated flag={flag} /> |
| 334 | + <Component |
| 335 | + label="a" |
| 336 | + getSnapshot={defaultGetSnapshot} |
| 337 | + mutableSource={mutableSource} |
| 338 | + subscribe={defaultSubscribe} |
| 339 | + /> |
| 340 | + </> |
| 341 | + ); |
| 342 | + } |
| 343 | + |
| 344 | + const container = document.createElement('div'); |
| 345 | + document.body.appendChild(container); |
| 346 | + |
| 347 | + const htmlString = ReactDOMServer.renderToString( |
| 348 | + <TestComponent flag={1} />, |
| 349 | + ); |
| 350 | + container.innerHTML = htmlString; |
| 351 | + expect(Scheduler).toHaveYielded([1, 'a:one']); |
| 352 | + expect(source.listenerCount).toBe(0); |
| 353 | + |
| 354 | + const root = ReactDOM.unstable_createRoot(container, {hydrate: true}); |
| 355 | + expect(() => { |
| 356 | + act(() => { |
| 357 | + root.registerMutableSourceForHydration(mutableSource); |
| 358 | + root.render(<TestComponent flag={1} />); |
| 359 | + expect(Scheduler).toFlushAndYieldThrough([1]); |
| 360 | + |
| 361 | + // Render an update which will be higher priority than the hydration. |
| 362 | + Scheduler.unstable_runWithPriority( |
| 363 | + Scheduler.unstable_UserBlockingPriority, |
| 364 | + () => root.render(<TestComponent flag={2} />), |
| 365 | + ); |
| 366 | + expect(Scheduler).toFlushAndYieldThrough([2]); |
| 367 | + |
| 368 | + source.value = 'two'; |
| 369 | + }); |
| 370 | + }).toErrorDev( |
| 371 | + 'Warning: Text content did not match. Server: "1" Client: "2"', |
| 372 | + ); |
| 373 | + expect(Scheduler).toHaveYielded([2, 'a:two']); |
| 374 | + expect(source.listenerCount).toBe(1); |
| 375 | + }); |
| 376 | +}); |
0 commit comments