1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and 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. jest.resetModules();
    
  13. const React = require('react');
    
  14. let ReactFreshRuntime;
    
  15. if (__DEV__) {
    
  16.   ReactFreshRuntime = require('react-refresh/runtime');
    
  17.   ReactFreshRuntime.injectIntoGlobalHook(global);
    
  18. }
    
  19. const ReactDOM = require('react-dom');
    
  20. 
    
  21. jest.resetModules();
    
  22. const ReactART = require('react-art');
    
  23. const ARTSVGMode = require('art/modes/svg');
    
  24. const ARTCurrentMode = require('art/modes/current');
    
  25. ARTCurrentMode.setCurrent(ARTSVGMode);
    
  26. 
    
  27. describe('ReactFresh', () => {
    
  28.   let container;
    
  29. 
    
  30.   beforeEach(() => {
    
  31.     if (__DEV__) {
    
  32.       container = document.createElement('div');
    
  33.       document.body.appendChild(container);
    
  34.     }
    
  35.   });
    
  36. 
    
  37.   afterEach(() => {
    
  38.     if (__DEV__) {
    
  39.       document.body.removeChild(container);
    
  40.       container = null;
    
  41.     }
    
  42.   });
    
  43. 
    
  44.   it('can update components managed by different renderers independently', () => {
    
  45.     if (__DEV__) {
    
  46.       const InnerV1 = function () {
    
  47.         return <ReactART.Shape fill="blue" />;
    
  48.       };
    
  49.       ReactFreshRuntime.register(InnerV1, 'Inner');
    
  50. 
    
  51.       const OuterV1 = function () {
    
  52.         return (
    
  53.           <div style={{color: 'blue'}}>
    
  54.             <ReactART.Surface>
    
  55.               <InnerV1 />
    
  56.             </ReactART.Surface>
    
  57.           </div>
    
  58.         );
    
  59.       };
    
  60.       ReactFreshRuntime.register(OuterV1, 'Outer');
    
  61. 
    
  62.       ReactDOM.render(<OuterV1 />, container);
    
  63.       const el = container.firstChild;
    
  64.       const pathEl = el.querySelector('path');
    
  65.       expect(el.style.color).toBe('blue');
    
  66.       expect(pathEl.getAttributeNS(null, 'fill')).toBe('rgb(0, 0, 255)');
    
  67. 
    
  68.       // Perform a hot update to the ART-rendered component.
    
  69.       const InnerV2 = function () {
    
  70.         return <ReactART.Shape fill="red" />;
    
  71.       };
    
  72.       ReactFreshRuntime.register(InnerV2, 'Inner');
    
  73. 
    
  74.       ReactFreshRuntime.performReactRefresh();
    
  75.       expect(container.firstChild).toBe(el);
    
  76.       expect(el.querySelector('path')).toBe(pathEl);
    
  77.       expect(el.style.color).toBe('blue');
    
  78.       expect(pathEl.getAttributeNS(null, 'fill')).toBe('rgb(255, 0, 0)');
    
  79. 
    
  80.       // Perform a hot update to the DOM-rendered component.
    
  81.       const OuterV2 = function () {
    
  82.         return (
    
  83.           <div style={{color: 'red'}}>
    
  84.             <ReactART.Surface>
    
  85.               <InnerV1 />
    
  86.             </ReactART.Surface>
    
  87.           </div>
    
  88.         );
    
  89.       };
    
  90.       ReactFreshRuntime.register(OuterV2, 'Outer');
    
  91. 
    
  92.       ReactFreshRuntime.performReactRefresh();
    
  93.       expect(el.style.color).toBe('red');
    
  94.       expect(container.firstChild).toBe(el);
    
  95.       expect(el.querySelector('path')).toBe(pathEl);
    
  96.       expect(pathEl.getAttributeNS(null, 'fill')).toBe('rgb(255, 0, 0)');
    
  97.     }
    
  98.   });
    
  99. });