-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DevTools: Add support for
useFormStatus
- Loading branch information
Sebastian Silbermann
committed
Mar 5, 2024
1 parent
0775186
commit f0d6592
Showing
4 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegrationDOM-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and 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 | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let React; | ||
let ReactDOM; | ||
let ReactDOMClient; | ||
let ReactDebugTools; | ||
let act; | ||
|
||
function normalizeSourceLoc(tree) { | ||
tree.forEach(node => { | ||
if (node.hookSource) { | ||
node.hookSource.fileName = '**'; | ||
node.hookSource.lineNumber = 0; | ||
node.hookSource.columnNumber = 0; | ||
} | ||
normalizeSourceLoc(node.subHooks); | ||
}); | ||
return tree; | ||
} | ||
|
||
describe('ReactHooksInspectionIntegration', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
ReactDOMClient = require('react-dom/client'); | ||
act = require('internal-test-utils').act; | ||
ReactDebugTools = require('react-debug-tools'); | ||
}); | ||
|
||
// @gate enableFormActions && enableAsyncActions | ||
it('should support useFormStatus hook', async () => { | ||
function FormStatus() { | ||
const status = ReactDOM.useFormStatus(); | ||
React.useMemo(() => 'memo', []); | ||
React.useMemo(() => 'not used', []); | ||
|
||
return JSON.stringify(status); | ||
} | ||
|
||
const treeWithoutFiber = ReactDebugTools.inspectHooks(FormStatus); | ||
expect(normalizeSourceLoc(treeWithoutFiber)).toEqual([ | ||
{ | ||
debugInfo: null, | ||
hookSource: { | ||
columnNumber: 0, | ||
fileName: '**', | ||
functionName: 'FormStatus', | ||
lineNumber: 0, | ||
}, | ||
id: null, | ||
isStateEditable: false, | ||
name: '.useFormStatus', | ||
subHooks: [ | ||
{ | ||
debugInfo: null, | ||
hookSource: { | ||
columnNumber: 0, | ||
fileName: '**', | ||
functionName: 'Object.useFormStatus', | ||
lineNumber: 0, | ||
}, | ||
id: null, | ||
isStateEditable: false, | ||
name: 'HostTransitionStatus', | ||
subHooks: [], | ||
value: null, | ||
}, | ||
], | ||
value: null, | ||
}, | ||
{ | ||
debugInfo: null, | ||
hookSource: { | ||
columnNumber: 0, | ||
fileName: '**', | ||
functionName: 'FormStatus', | ||
lineNumber: 0, | ||
}, | ||
id: 0, | ||
isStateEditable: false, | ||
name: 'Memo', | ||
subHooks: [], | ||
value: 'memo', | ||
}, | ||
{ | ||
debugInfo: null, | ||
hookSource: { | ||
columnNumber: 0, | ||
fileName: '**', | ||
functionName: 'FormStatus', | ||
lineNumber: 0, | ||
}, | ||
id: 1, | ||
isStateEditable: false, | ||
name: 'Memo', | ||
subHooks: [], | ||
value: 'not used', | ||
}, | ||
]); | ||
|
||
const root = ReactDOMClient.createRoot(document.createElement('div')); | ||
|
||
await act(() => { | ||
root.render( | ||
<form> | ||
<FormStatus /> | ||
</form>, | ||
); | ||
}); | ||
|
||
// Implementation detail. Feel free to adjust the position of the Fiber in the tree. | ||
const formStatusFiber = root._internalRoot.current.child.child; | ||
const treeWithFiber = ReactDebugTools.inspectHooksOfFiber(formStatusFiber); | ||
expect(normalizeSourceLoc(treeWithFiber)).toEqual([ | ||
{ | ||
debugInfo: null, | ||
hookSource: { | ||
columnNumber: 0, | ||
fileName: '**', | ||
functionName: 'FormStatus', | ||
lineNumber: 0, | ||
}, | ||
id: null, | ||
isStateEditable: false, | ||
name: '.useFormStatus', | ||
subHooks: [ | ||
{ | ||
debugInfo: null, | ||
hookSource: { | ||
columnNumber: 0, | ||
fileName: '**', | ||
functionName: 'Object.useFormStatus', | ||
lineNumber: 0, | ||
}, | ||
id: null, | ||
isStateEditable: false, | ||
name: 'HostTransitionStatus', | ||
subHooks: [], | ||
value: null, | ||
}, | ||
], | ||
value: null, | ||
}, | ||
{ | ||
debugInfo: null, | ||
hookSource: { | ||
columnNumber: 0, | ||
fileName: '**', | ||
functionName: 'FormStatus', | ||
lineNumber: 0, | ||
}, | ||
id: 0, | ||
isStateEditable: false, | ||
name: 'Memo', | ||
subHooks: [], | ||
value: 'memo', | ||
}, | ||
{ | ||
debugInfo: null, | ||
hookSource: { | ||
columnNumber: 0, | ||
fileName: '**', | ||
functionName: 'FormStatus', | ||
lineNumber: 0, | ||
}, | ||
id: 1, | ||
isStateEditable: false, | ||
name: 'Memo', | ||
subHooks: [], | ||
value: 'not used', | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters