|
| 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 | +import type {FrontendBridge} from 'react-devtools-shared/src/bridge'; |
| 11 | +import type Store from 'react-devtools-shared/src/devtools/store'; |
| 12 | + |
| 13 | +describe('editable props and state', () => { |
| 14 | + let PropTypes; |
| 15 | + let React; |
| 16 | + let ReactDOM; |
| 17 | + let bridge: FrontendBridge; |
| 18 | + let store: Store; |
| 19 | + let utils; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + utils = require('./utils'); |
| 23 | + utils.beforeEachProfiling(); |
| 24 | + |
| 25 | + bridge = global.bridge; |
| 26 | + store = global.store; |
| 27 | + store.collapseNodesByDefault = false; |
| 28 | + |
| 29 | + PropTypes = require('prop-types'); |
| 30 | + React = require('react'); |
| 31 | + ReactDOM = require('react-dom'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should support editing props', async () => { |
| 35 | + class ClassComponent extends React.Component { |
| 36 | + render() { |
| 37 | + return this.props.deep.nested + this.props.shallow; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + function FunctionComponent(props) { |
| 42 | + return props.deep.nested + props.shallow; |
| 43 | + } |
| 44 | + |
| 45 | + const container = document.createElement('div'); |
| 46 | + await utils.actAsync(() => |
| 47 | + ReactDOM.render( |
| 48 | + <> |
| 49 | + <ClassComponent deep={{nested: 'a'}} shallow="b" />, |
| 50 | + <FunctionComponent deep={{nested: 'a'}} shallow="b" />, |
| 51 | + </>, |
| 52 | + container, |
| 53 | + ), |
| 54 | + ); |
| 55 | + |
| 56 | + expect(container.innerHTML).toBe('ab,ab,'); |
| 57 | + |
| 58 | + function overrideProps(id, path, value) { |
| 59 | + const rendererID = utils.getRendererID(); |
| 60 | + bridge.send('overrideProps', {id, path, rendererID, value}); |
| 61 | + jest.runOnlyPendingTimers(); |
| 62 | + } |
| 63 | + |
| 64 | + const classID = ((store.getElementIDAtIndex(0): any): number); |
| 65 | + const funcID = ((store.getElementIDAtIndex(1): any): number); |
| 66 | + |
| 67 | + overrideProps(classID, ['shallow'], 'c'); |
| 68 | + expect(container.innerHTML).toBe('ac,ab,'); |
| 69 | + |
| 70 | + overrideProps(classID, ['deep', 'nested'], 'd'); |
| 71 | + expect(container.innerHTML).toBe('dc,ab,'); |
| 72 | + |
| 73 | + overrideProps(funcID, ['shallow'], 'c'); |
| 74 | + expect(container.innerHTML).toBe('dc,ac,'); |
| 75 | + |
| 76 | + overrideProps(funcID, ['deep', 'nested'], 'd'); |
| 77 | + expect(container.innerHTML).toBe('dc,dc,'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should support editing state', async () => { |
| 81 | + class ClassComponent extends React.Component { |
| 82 | + state = { |
| 83 | + deep: { |
| 84 | + nested: 'a', |
| 85 | + }, |
| 86 | + shallow: 'b', |
| 87 | + }; |
| 88 | + render() { |
| 89 | + return this.state.deep.nested + this.state.shallow; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + const container = document.createElement('div'); |
| 94 | + await utils.actAsync(() => ReactDOM.render(<ClassComponent />, container)); |
| 95 | + |
| 96 | + expect(container.innerHTML).toBe('ab'); |
| 97 | + |
| 98 | + function overrideState(id, path, value) { |
| 99 | + const rendererID = utils.getRendererID(); |
| 100 | + bridge.send('overrideState', {id, path, rendererID, value}); |
| 101 | + jest.runOnlyPendingTimers(); |
| 102 | + } |
| 103 | + |
| 104 | + const id = ((store.getElementIDAtIndex(0): any): number); |
| 105 | + |
| 106 | + overrideState(id, ['shallow'], 'c'); |
| 107 | + expect(container.innerHTML).toBe('ac'); |
| 108 | + |
| 109 | + overrideState(id, ['deep', 'nested'], 'd'); |
| 110 | + expect(container.innerHTML).toBe('dc'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should support editing hooks', async () => { |
| 114 | + function FunctionComponent() { |
| 115 | + const [state] = React.useState({ |
| 116 | + deep: { |
| 117 | + nested: 'a', |
| 118 | + }, |
| 119 | + shallow: 'b', |
| 120 | + }); |
| 121 | + return state.deep.nested + state.shallow; |
| 122 | + } |
| 123 | + |
| 124 | + const container = document.createElement('div'); |
| 125 | + await utils.actAsync(() => |
| 126 | + ReactDOM.render(<FunctionComponent />, container), |
| 127 | + ); |
| 128 | + |
| 129 | + expect(container.innerHTML).toBe('ab'); |
| 130 | + |
| 131 | + function overrideHookState(id, hookID, path, value) { |
| 132 | + const rendererID = utils.getRendererID(); |
| 133 | + bridge.send('overrideHookState', {id, hookID, path, rendererID, value}); |
| 134 | + jest.runOnlyPendingTimers(); |
| 135 | + } |
| 136 | + |
| 137 | + const id = ((store.getElementIDAtIndex(0): any): number); |
| 138 | + const hookID = 0; // index |
| 139 | + |
| 140 | + overrideHookState(id, hookID, ['shallow'], 'c'); |
| 141 | + expect(container.innerHTML).toBe('ac'); |
| 142 | + |
| 143 | + overrideHookState(id, hookID, ['deep', 'nested'], 'd'); |
| 144 | + expect(container.innerHTML).toBe('dc'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should support editing context', async () => { |
| 148 | + class LegacyContextProvider extends React.Component<any> { |
| 149 | + static childContextTypes = { |
| 150 | + deep: PropTypes.object, |
| 151 | + shallow: PropTypes.string, |
| 152 | + }; |
| 153 | + getChildContext() { |
| 154 | + return { |
| 155 | + deep: { |
| 156 | + nested: 'a', |
| 157 | + }, |
| 158 | + shallow: 'b', |
| 159 | + }; |
| 160 | + } |
| 161 | + render() { |
| 162 | + return this.props.children; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + class ClassComponent extends React.Component<any> { |
| 167 | + static contextTypes = { |
| 168 | + deep: PropTypes.object, |
| 169 | + shallow: PropTypes.string, |
| 170 | + }; |
| 171 | + |
| 172 | + render() { |
| 173 | + return this.context.deep.nested + this.context.shallow; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + const container = document.createElement('div'); |
| 178 | + await utils.actAsync(() => |
| 179 | + ReactDOM.render( |
| 180 | + <LegacyContextProvider> |
| 181 | + <ClassComponent />, |
| 182 | + <ClassComponent />, |
| 183 | + </LegacyContextProvider>, |
| 184 | + container, |
| 185 | + ), |
| 186 | + ); |
| 187 | + |
| 188 | + expect(container.innerHTML).toBe('ab,ab,'); |
| 189 | + |
| 190 | + function overrideContext(id, path, value) { |
| 191 | + const rendererID = utils.getRendererID(); |
| 192 | + // To simplify hydration and display of primitive context values (e.g. number, string) |
| 193 | + // the inspectElement() method wraps context in a {value: ...} object. |
| 194 | + path = ['value', ...path]; |
| 195 | + |
| 196 | + bridge.send('overrideContext', {id, path, rendererID, value}); |
| 197 | + jest.runOnlyPendingTimers(); |
| 198 | + } |
| 199 | + |
| 200 | + const classID = ((store.getElementIDAtIndex(1): any): number); |
| 201 | + |
| 202 | + overrideContext(classID, ['shallow'], 'c'); |
| 203 | + expect(container.innerHTML).toBe('ac,ab,'); |
| 204 | + |
| 205 | + overrideContext(classID, ['deep', 'nested'], 'd'); |
| 206 | + expect(container.innerHTML).toBe('dc,ab,'); |
| 207 | + |
| 208 | + // Function components using legacy context are not editable. |
| 209 | + }); |
| 210 | +}); |
0 commit comments