|
11 | 11 |
|
12 | 12 | import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
13 | 13 |
|
| 14 | +import type {HostInstance} from 'react-native'; |
| 15 | + |
| 16 | +import ensureInstance from '../../../__tests__/utilities/ensureInstance'; |
| 17 | +import * as Fantom from '@react-native/fantom'; |
| 18 | +import * as React from 'react'; |
| 19 | +import {View} from 'react-native'; |
| 20 | +import setUpIntersectionObserver from 'react-native/src/private/setup/setUpIntersectionObserver'; |
| 21 | +import setUpMutationObserver from 'react-native/src/private/setup/setUpMutationObserver'; |
| 22 | +import EventTarget from 'react-native/src/private/webapis/dom/events/EventTarget'; |
| 23 | +import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement'; |
14 | 24 | import DOMException from 'react-native/src/private/webapis/errors/DOMException'; |
| 25 | +import IntersectionObserver from 'react-native/src/private/webapis/intersectionobserver/IntersectionObserver'; |
| 26 | +import MutationObserver from 'react-native/src/private/webapis/mutationobserver/MutationObserver'; |
15 | 27 | import structuredClone from 'react-native/src/private/webapis/structuredClone/structuredClone'; |
16 | 28 |
|
| 29 | +setUpIntersectionObserver(); |
| 30 | +setUpMutationObserver(); |
| 31 | + |
17 | 32 | function expectDataCloneError(fn: () => mixed) { |
18 | 33 | try { |
19 | 34 | fn(); |
@@ -289,4 +304,141 @@ describe('structuredClone', () => { |
289 | 304 | // $FlowExpectedError[prop-missing] |
290 | 305 | expect([...clone.map.get('set')][0]).toBe(clone.map); |
291 | 306 | }); |
| 307 | + |
| 308 | + describe('platform objects', () => { |
| 309 | + describe('serializable platform objects', () => { |
| 310 | + it('clones DOMRectReadOnly', () => { |
| 311 | + let value = new DOMRectReadOnly(1, 2, 3, 4); |
| 312 | + let clone = structuredClone(value); |
| 313 | + expect(clone).not.toBe(value); |
| 314 | + expect(clone).toBeInstanceOf(DOMRectReadOnly); |
| 315 | + expect(clone).toEqual(value); |
| 316 | + }); |
| 317 | + |
| 318 | + it('clones DOMRect', () => { |
| 319 | + let value = new DOMRect(1, 2, 3, 4); |
| 320 | + let clone = structuredClone(value); |
| 321 | + expect(clone).not.toBe(value); |
| 322 | + expect(clone).toBeInstanceOf(DOMRect); |
| 323 | + expect(clone).toEqual(value); |
| 324 | + }); |
| 325 | + |
| 326 | + it('clones DOMException', () => { |
| 327 | + const value = new DOMException('error message', 'Error'); |
| 328 | + const clone = structuredClone(value); |
| 329 | + expect(clone).not.toBe(value); |
| 330 | + expect(clone).toBeInstanceOf(DOMException); |
| 331 | + expect(clone.name).toEqual(value.name); |
| 332 | + expect(clone.message).toEqual(value.message); |
| 333 | + }); |
| 334 | + }); |
| 335 | + |
| 336 | + describe('non-serializable platform objects', () => { |
| 337 | + it('does NOT clone ReadOnlyNode', () => { |
| 338 | + const ref = React.createRef<HostInstance>(); |
| 339 | + const root = Fantom.createRoot(); |
| 340 | + Fantom.runTask(() => { |
| 341 | + root.render(<View ref={ref} />); |
| 342 | + }); |
| 343 | + expect(ref.current).not.toBe(null); |
| 344 | + expectDataCloneError(() => structuredClone(ref.current)); |
| 345 | + }); |
| 346 | + |
| 347 | + it('does NOT clone EventTarget', () => { |
| 348 | + expectDataCloneError(() => structuredClone(new EventTarget())); |
| 349 | + }); |
| 350 | + |
| 351 | + it('does NOT clone XMLHttpRequest', () => { |
| 352 | + const xhr = new XMLHttpRequest(); |
| 353 | + expectDataCloneError(() => structuredClone(xhr)); |
| 354 | + }); |
| 355 | + |
| 356 | + it('does NOT clone performance', () => { |
| 357 | + expectDataCloneError(() => structuredClone(performance)); |
| 358 | + }); |
| 359 | + |
| 360 | + it('does NOT clone performance.memory', () => { |
| 361 | + // $FlowExpectedError[prop-missing] |
| 362 | + expectDataCloneError(() => structuredClone(performance.memory)); |
| 363 | + }); |
| 364 | + |
| 365 | + it('does NOT clone performance.rnStartupTiming', () => { |
| 366 | + expectDataCloneError(() => |
| 367 | + // $FlowExpectedError[prop-missing] |
| 368 | + structuredClone(performance.rnStartupTiming), |
| 369 | + ); |
| 370 | + }); |
| 371 | + |
| 372 | + it('does NOT clone PerformanceEntry', () => { |
| 373 | + // $FlowExpectedError[prop-missing] |
| 374 | + expectDataCloneError(() => structuredClone(performance.mark('foo'))); |
| 375 | + }); |
| 376 | + |
| 377 | + it('does NOT clone IntersectionObserver', () => { |
| 378 | + expectDataCloneError(() => |
| 379 | + structuredClone(new IntersectionObserver(() => {})), |
| 380 | + ); |
| 381 | + }); |
| 382 | + |
| 383 | + it('does NOT clone IntersectionObserverEntry', () => { |
| 384 | + const ref = React.createRef<HostInstance>(); |
| 385 | + const root = Fantom.createRoot(); |
| 386 | + Fantom.runTask(() => { |
| 387 | + root.render(<View ref={ref} />); |
| 388 | + }); |
| 389 | + expect(ref.current).not.toBe(null); |
| 390 | + |
| 391 | + const entries: Array<mixed> = []; |
| 392 | + Fantom.runTask(() => { |
| 393 | + const observer = new IntersectionObserver(e => { |
| 394 | + entries.push(...e); |
| 395 | + }); |
| 396 | + |
| 397 | + observer.observe(ensureInstance(ref.current, ReactNativeElement)); |
| 398 | + |
| 399 | + Fantom.scheduleTask(() => { |
| 400 | + observer.disconnect(); |
| 401 | + }); |
| 402 | + }); |
| 403 | + |
| 404 | + expectDataCloneError(() => structuredClone(entries[0])); |
| 405 | + }); |
| 406 | + |
| 407 | + it('does NOT clone MutationObserver', () => { |
| 408 | + expectDataCloneError(() => |
| 409 | + structuredClone(new MutationObserver(() => {})), |
| 410 | + ); |
| 411 | + }); |
| 412 | + |
| 413 | + it('does NOT clone MutationRecord', () => { |
| 414 | + const ref = React.createRef<HostInstance>(); |
| 415 | + const root = Fantom.createRoot(); |
| 416 | + Fantom.runTask(() => { |
| 417 | + root.render(<View ref={ref} />); |
| 418 | + }); |
| 419 | + expect(ref.current).not.toBe(null); |
| 420 | + |
| 421 | + const records: Array<mixed> = []; |
| 422 | + Fantom.runTask(() => { |
| 423 | + const observer = new MutationObserver(e => { |
| 424 | + records.push(...e); |
| 425 | + }); |
| 426 | + |
| 427 | + observer.observe(ensureInstance(ref.current, ReactNativeElement), { |
| 428 | + childList: true, |
| 429 | + }); |
| 430 | + }); |
| 431 | + |
| 432 | + Fantom.runTask(() => { |
| 433 | + root.render( |
| 434 | + <View> |
| 435 | + <View /> |
| 436 | + </View>, |
| 437 | + ); |
| 438 | + }); |
| 439 | + |
| 440 | + expectDataCloneError(() => structuredClone(records[0])); |
| 441 | + }); |
| 442 | + }); |
| 443 | + }); |
292 | 444 | }); |
0 commit comments