forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for injectHookVariableNames
- Loading branch information
1 parent
a06f8d9
commit 64f9113
Showing
2 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/react-devtools-shared/src/__tests__/injectHookVariableNames-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,49 @@ | ||
/** | ||
* @flow | ||
*/ | ||
|
||
import {parse} from '@babel/parser' | ||
import traverse from '@babel/traverse'; | ||
import {getHookVariableName, getPotentialHookDeclarationsFromAST} from 'react-devtools-shared/src/utils'; | ||
|
||
describe('injectHookVariableNamesFunction', () => { | ||
it('should identify variable names in destructed syntax', async (done) => { | ||
|
||
const jsxCode = ` | ||
const Example = () => { | ||
const [count, setCount] = React.useState(1); | ||
return count; | ||
}; | ||
`; | ||
|
||
const ast = parse(jsxCode, { sourceType: 'unambiguous', plugins: ['jsx', 'typescript']}); | ||
const hookAstNodes = getPotentialHookDeclarationsFromAST(ast); | ||
|
||
// Only one hook node is present in the source code | ||
expect(hookAstNodes).toHaveLength(1); | ||
|
||
const hookName = getHookVariableName(hookAstNodes[0]); | ||
expect(hookName).toBe('count'); | ||
done(); | ||
}); | ||
|
||
it('should identify variable names in direct assignment', async (done) => { | ||
|
||
const jsxCode = ` | ||
const Example = () => { | ||
const count = React.useState(1); | ||
return count; | ||
}; | ||
`; | ||
|
||
const ast = parse(jsxCode, { sourceType: 'unambiguous', plugins: ['jsx', 'typescript']}); | ||
const hookAstNodes = getPotentialHookDeclarationsFromAST(ast); | ||
|
||
// Only one hook node is present in the source code | ||
expect(hookAstNodes).toHaveLength(1); | ||
|
||
const hookName = getHookVariableName(hookAstNodes[0]); | ||
expect(hookName).toBe('count'); | ||
done(); | ||
}); | ||
}); |
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