2019-08-09 02:03:38 +08:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Facebook, Inc. and its 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';
|
|
|
|
|
|
|
|
|
|
jest.resetModules();
|
2020-04-02 03:35:52 +08:00
|
|
|
const React = require('react');
|
2019-08-09 02:03:38 +08:00
|
|
|
let ReactFreshRuntime;
|
|
|
|
|
if (__DEV__) {
|
|
|
|
|
ReactFreshRuntime = require('react-refresh/runtime');
|
|
|
|
|
ReactFreshRuntime.injectIntoGlobalHook(global);
|
|
|
|
|
}
|
2020-04-02 03:35:52 +08:00
|
|
|
const ReactDOM = require('react-dom');
|
2019-08-09 02:03:38 +08:00
|
|
|
|
|
|
|
|
jest.resetModules();
|
2020-04-02 03:35:52 +08:00
|
|
|
const ReactART = require('react-art');
|
|
|
|
|
const ARTSVGMode = require('art/modes/svg');
|
|
|
|
|
const ARTCurrentMode = require('art/modes/current');
|
2019-08-09 02:03:38 +08:00
|
|
|
ARTCurrentMode.setCurrent(ARTSVGMode);
|
|
|
|
|
|
|
|
|
|
describe('ReactFresh', () => {
|
|
|
|
|
let container;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
if (__DEV__) {
|
|
|
|
|
container = document.createElement('div');
|
|
|
|
|
document.body.appendChild(container);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
if (__DEV__) {
|
|
|
|
|
document.body.removeChild(container);
|
|
|
|
|
container = null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-16 07:59:44 +08:00
|
|
|
it('can update components managed by different renderers independently', () => {
|
2019-08-09 02:03:38 +08:00
|
|
|
if (__DEV__) {
|
2020-04-02 03:35:52 +08:00
|
|
|
const InnerV1 = function() {
|
2019-08-09 02:03:38 +08:00
|
|
|
return <ReactART.Shape fill="blue" />;
|
|
|
|
|
};
|
|
|
|
|
ReactFreshRuntime.register(InnerV1, 'Inner');
|
|
|
|
|
|
2020-04-02 03:35:52 +08:00
|
|
|
const OuterV1 = function() {
|
2019-08-09 02:03:38 +08:00
|
|
|
return (
|
|
|
|
|
<div style={{color: 'blue'}}>
|
|
|
|
|
<ReactART.Surface>
|
|
|
|
|
<InnerV1 />
|
|
|
|
|
</ReactART.Surface>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
ReactFreshRuntime.register(OuterV1, 'Outer');
|
|
|
|
|
|
|
|
|
|
ReactDOM.render(<OuterV1 />, container);
|
|
|
|
|
const el = container.firstChild;
|
|
|
|
|
const pathEl = el.querySelector('path');
|
|
|
|
|
expect(el.style.color).toBe('blue');
|
|
|
|
|
expect(pathEl.getAttributeNS(null, 'fill')).toBe('rgb(0, 0, 255)');
|
|
|
|
|
|
|
|
|
|
// Perform a hot update to the ART-rendered component.
|
2020-04-02 03:35:52 +08:00
|
|
|
const InnerV2 = function() {
|
2019-08-09 02:03:38 +08:00
|
|
|
return <ReactART.Shape fill="red" />;
|
|
|
|
|
};
|
|
|
|
|
ReactFreshRuntime.register(InnerV2, 'Inner');
|
|
|
|
|
|
|
|
|
|
ReactFreshRuntime.performReactRefresh();
|
|
|
|
|
expect(container.firstChild).toBe(el);
|
|
|
|
|
expect(el.querySelector('path')).toBe(pathEl);
|
|
|
|
|
expect(el.style.color).toBe('blue');
|
|
|
|
|
expect(pathEl.getAttributeNS(null, 'fill')).toBe('rgb(255, 0, 0)');
|
|
|
|
|
|
|
|
|
|
// Perform a hot update to the DOM-rendered component.
|
2020-04-02 03:35:52 +08:00
|
|
|
const OuterV2 = function() {
|
2019-08-09 02:03:38 +08:00
|
|
|
return (
|
|
|
|
|
<div style={{color: 'red'}}>
|
|
|
|
|
<ReactART.Surface>
|
|
|
|
|
<InnerV1 />
|
|
|
|
|
</ReactART.Surface>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
ReactFreshRuntime.register(OuterV2, 'Outer');
|
|
|
|
|
|
|
|
|
|
ReactFreshRuntime.performReactRefresh();
|
|
|
|
|
expect(el.style.color).toBe('red');
|
|
|
|
|
expect(container.firstChild).toBe(el);
|
|
|
|
|
expect(el.querySelector('path')).toBe(pathEl);
|
|
|
|
|
expect(pathEl.getAttributeNS(null, 'fill')).toBe('rgb(255, 0, 0)');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|