|
| 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 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +describe('Fast Refresh', () => { |
| 11 | + let React; |
| 12 | + let ReactDOM; |
| 13 | + let ReactFreshRuntime; |
| 14 | + let act; |
| 15 | + let babel; |
| 16 | + let container; |
| 17 | + let exportsObj; |
| 18 | + let freshPlugin; |
| 19 | + let store; |
| 20 | + let withErrorsOrWarningsIgnored; |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + jest.resetModules(); |
| 24 | + }); |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + exportsObj = undefined; |
| 28 | + container = document.createElement('div'); |
| 29 | + |
| 30 | + babel = require('@babel/core'); |
| 31 | + freshPlugin = require('react-refresh/babel'); |
| 32 | + |
| 33 | + store = global.store; |
| 34 | + |
| 35 | + React = require('react'); |
| 36 | + |
| 37 | + ReactFreshRuntime = require('react-refresh/runtime'); |
| 38 | + ReactFreshRuntime.injectIntoGlobalHook(global); |
| 39 | + |
| 40 | + ReactDOM = require('react-dom'); |
| 41 | + |
| 42 | + const utils = require('./utils'); |
| 43 | + act = utils.act; |
| 44 | + withErrorsOrWarningsIgnored = utils.withErrorsOrWarningsIgnored; |
| 45 | + }); |
| 46 | + |
| 47 | + function execute(source) { |
| 48 | + const compiled = babel.transform(source, { |
| 49 | + babelrc: false, |
| 50 | + presets: ['@babel/react'], |
| 51 | + plugins: [ |
| 52 | + [freshPlugin, {skipEnvCheck: true}], |
| 53 | + '@babel/plugin-transform-modules-commonjs', |
| 54 | + '@babel/plugin-transform-destructuring', |
| 55 | + ].filter(Boolean), |
| 56 | + }).code; |
| 57 | + exportsObj = {}; |
| 58 | + // eslint-disable-next-line no-new-func |
| 59 | + new Function( |
| 60 | + 'global', |
| 61 | + 'React', |
| 62 | + 'exports', |
| 63 | + '$RefreshReg$', |
| 64 | + '$RefreshSig$', |
| 65 | + compiled, |
| 66 | + )(global, React, exportsObj, $RefreshReg$, $RefreshSig$); |
| 67 | + // Module systems will register exports as a fallback. |
| 68 | + // This is useful for cases when e.g. a class is exported, |
| 69 | + // and we don't want to propagate the update beyond this module. |
| 70 | + $RefreshReg$(exportsObj.default, 'exports.default'); |
| 71 | + return exportsObj.default; |
| 72 | + } |
| 73 | + |
| 74 | + function render(source) { |
| 75 | + const Component = execute(source); |
| 76 | + act(() => { |
| 77 | + ReactDOM.render(<Component />, container); |
| 78 | + }); |
| 79 | + // Module initialization shouldn't be counted as a hot update. |
| 80 | + expect(ReactFreshRuntime.performReactRefresh()).toBe(null); |
| 81 | + } |
| 82 | + |
| 83 | + function patch(source) { |
| 84 | + const prevExports = exportsObj; |
| 85 | + execute(source); |
| 86 | + const nextExports = exportsObj; |
| 87 | + |
| 88 | + // Check if exported families have changed. |
| 89 | + // (In a real module system we'd do this for *all* exports.) |
| 90 | + // For example, this can happen if you convert a class to a function. |
| 91 | + // Or if you wrap something in a HOC. |
| 92 | + const didExportsChange = |
| 93 | + ReactFreshRuntime.getFamilyByType(prevExports.default) !== |
| 94 | + ReactFreshRuntime.getFamilyByType(nextExports.default); |
| 95 | + if (didExportsChange) { |
| 96 | + // In a real module system, we would propagate such updates upwards, |
| 97 | + // and re-execute modules that imported this one. (Just like if we edited them.) |
| 98 | + // This makes adding/removing/renaming exports re-render references to them. |
| 99 | + // Here, we'll just force a re-render using the newer type to emulate this. |
| 100 | + const NextComponent = nextExports.default; |
| 101 | + act(() => { |
| 102 | + ReactDOM.render(<NextComponent />, container); |
| 103 | + }); |
| 104 | + } |
| 105 | + act(() => { |
| 106 | + const result = ReactFreshRuntime.performReactRefresh(); |
| 107 | + if (!didExportsChange) { |
| 108 | + // Normally we expect that some components got updated in our tests. |
| 109 | + expect(result).not.toBe(null); |
| 110 | + } else { |
| 111 | + // However, we have tests where we convert functions to classes, |
| 112 | + // and in those cases it's expected nothing would get updated. |
| 113 | + // (Instead, the export change branch above would take care of it.) |
| 114 | + } |
| 115 | + }); |
| 116 | + expect(ReactFreshRuntime._getMountedRootCount()).toBe(1); |
| 117 | + } |
| 118 | + |
| 119 | + function $RefreshReg$(type, id) { |
| 120 | + ReactFreshRuntime.register(type, id); |
| 121 | + } |
| 122 | + |
| 123 | + function $RefreshSig$() { |
| 124 | + return ReactFreshRuntime.createSignatureFunctionForTransform(); |
| 125 | + } |
| 126 | + |
| 127 | + it('should not break the DevTools store', () => { |
| 128 | + render(` |
| 129 | + function Parent() { |
| 130 | + return <Child key="A" />; |
| 131 | + }; |
| 132 | +
|
| 133 | + function Child() { |
| 134 | + return <div />; |
| 135 | + }; |
| 136 | +
|
| 137 | + export default Parent; |
| 138 | + `); |
| 139 | + expect(store).toMatchInlineSnapshot(` |
| 140 | + [root] |
| 141 | + ▾ <Parent> |
| 142 | + <Child key="A"> |
| 143 | + `); |
| 144 | + |
| 145 | + let element = container.firstChild; |
| 146 | + expect(container.firstChild).not.toBe(null); |
| 147 | + |
| 148 | + patch(` |
| 149 | + function Parent() { |
| 150 | + return <Child key="A" />; |
| 151 | + }; |
| 152 | +
|
| 153 | + function Child() { |
| 154 | + return <div />; |
| 155 | + }; |
| 156 | +
|
| 157 | + export default Parent; |
| 158 | + `); |
| 159 | + expect(store).toMatchInlineSnapshot(` |
| 160 | + [root] |
| 161 | + ▾ <Parent> |
| 162 | + <Child key="A"> |
| 163 | + `); |
| 164 | + |
| 165 | + // State is preserved; this verifies that Fast Refresh is wired up. |
| 166 | + expect(container.firstChild).toBe(element); |
| 167 | + element = container.firstChild; |
| 168 | + |
| 169 | + patch(` |
| 170 | + function Parent() { |
| 171 | + return <Child key="B" />; |
| 172 | + }; |
| 173 | +
|
| 174 | + function Child() { |
| 175 | + return <div />; |
| 176 | + }; |
| 177 | +
|
| 178 | + export default Parent; |
| 179 | + `); |
| 180 | + expect(store).toMatchInlineSnapshot(` |
| 181 | + [root] |
| 182 | + ▾ <Parent> |
| 183 | + <Child key="B"> |
| 184 | + `); |
| 185 | + |
| 186 | + // State is reset because hooks changed. |
| 187 | + expect(container.firstChild).not.toBe(element); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should not break when there are warnings in between patching', () => { |
| 191 | + withErrorsOrWarningsIgnored(['Expected warning during render'], () => { |
| 192 | + render(` |
| 193 | + const {useState} = React; |
| 194 | +
|
| 195 | + export default function Component() { |
| 196 | + const [state, setState] = useState(1); |
| 197 | + console.warn("Expected warning during render"); |
| 198 | + return null; |
| 199 | + } |
| 200 | + `); |
| 201 | + }); |
| 202 | + expect(store).toMatchInlineSnapshot(` |
| 203 | + ✕ 0, ⚠ 1 |
| 204 | + [root] |
| 205 | + <Component> ⚠ |
| 206 | + `); |
| 207 | + |
| 208 | + withErrorsOrWarningsIgnored(['Expected warning during render'], () => { |
| 209 | + patch(` |
| 210 | + const {useEffect, useState} = React; |
| 211 | +
|
| 212 | + export default function Component() { |
| 213 | + const [state, setState] = useState(1); |
| 214 | + console.warn("Expected warning during render"); |
| 215 | + return null; |
| 216 | + } |
| 217 | + `); |
| 218 | + }); |
| 219 | + expect(store).toMatchInlineSnapshot(` |
| 220 | + ✕ 0, ⚠ 2 |
| 221 | + [root] |
| 222 | + <Component> ⚠ |
| 223 | + `); |
| 224 | + |
| 225 | + withErrorsOrWarningsIgnored(['Expected warning during render'], () => { |
| 226 | + patch(` |
| 227 | + const {useEffect, useState} = React; |
| 228 | +
|
| 229 | + export default function Component() { |
| 230 | + const [state, setState] = useState(1); |
| 231 | + useEffect(() => {}); |
| 232 | + console.warn("Expected warning during render"); |
| 233 | + return null; |
| 234 | + } |
| 235 | + `); |
| 236 | + }); |
| 237 | + expect(store).toMatchInlineSnapshot(` |
| 238 | + ✕ 0, ⚠ 1 |
| 239 | + [root] |
| 240 | + <Component> ⚠ |
| 241 | + `); |
| 242 | + |
| 243 | + withErrorsOrWarningsIgnored(['Expected warning during render'], () => { |
| 244 | + patch(` |
| 245 | + const {useEffect, useState} = React; |
| 246 | +
|
| 247 | + export default function Component() { |
| 248 | + const [state, setState] = useState(1); |
| 249 | + console.warn("Expected warning during render"); |
| 250 | + return null; |
| 251 | + } |
| 252 | + `); |
| 253 | + }); |
| 254 | + expect(store).toMatchInlineSnapshot(` |
| 255 | + ✕ 0, ⚠ 1 |
| 256 | + [root] |
| 257 | + <Component> ⚠ |
| 258 | + `); |
| 259 | + }); |
| 260 | +}); |
0 commit comments