-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(jest-preset-hops): Add support for importComponent in jest
- Loading branch information
1 parent
83e2767
commit 80af95a
Showing
6 changed files
with
95 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module.exports = function importComponentPlugin({ types: t }) { | ||
return { | ||
visitor: { | ||
ImportSpecifier(path) { | ||
if ( | ||
path.node.imported.name !== 'importComponent' || | ||
!['@untool/react', 'untool', 'hops', this.opts.module].includes( | ||
path.parent.source.value | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
path.scope.getBinding('importComponent').referencePaths.forEach(ref => { | ||
if (!t.isCallExpression(ref.parentPath)) { | ||
return; | ||
} | ||
|
||
const arg = ref.parentPath.get('arguments')[0]; | ||
t.assertObjectExpression(arg); | ||
arg.traverse({ | ||
ObjectProperty(path) { | ||
// Remove `require.resolveWeak`, which does not exist in jest | ||
if (path.node.key.name === 'moduleId') { | ||
path.node.value = t.numericLiteral(0); | ||
} | ||
|
||
if (path.node.key.name === 'load') { | ||
const loadFunction = path.get('value'); | ||
t.assertArrowFunctionExpression(loadFunction); | ||
const moduleName = loadFunction.get('body.arguments.0'); | ||
|
||
loadFunction | ||
.get('body') | ||
.replaceWithSourceString( | ||
`Promise.resolve(require(${moduleName}))` | ||
); | ||
} | ||
}, | ||
}); | ||
}); | ||
}, | ||
}, | ||
}; | ||
}; |
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
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
16 changes: 16 additions & 0 deletions
16
packages/spec/integration/jest-preset/__tests__/setup/import-component.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,16 @@ | ||
import React from 'react'; | ||
import { importComponent } from 'hops'; | ||
|
||
const Text = importComponent('./text'); | ||
const AnotherText = importComponent('./text'); | ||
|
||
const TextContainer = () => { | ||
return ( | ||
<div> | ||
<Text text="hello" /> | ||
<AnotherText text="bye" /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TextContainer; |
7 changes: 7 additions & 0 deletions
7
packages/spec/integration/jest-preset/__tests__/setup/text.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,7 @@ | ||
import React from 'react'; | ||
|
||
const Text = ({ text }) => { | ||
return <h1>{text}</h1>; | ||
}; | ||
|
||
export default Text; |