Javascript 传递React浅酶测试用例的上下文-jest
如何在助手方法中传递上下文并提取数据 请参阅以下代码段:Javascript 传递React浅酶测试用例的上下文-jest,javascript,reactjs,enzyme,Javascript,Reactjs,Enzyme,如何在助手方法中传递上下文并提取数据 请参阅以下代码段: import AppContext from '../../context/AppContext' import extractDatta from '../../helper'; class App extends Component{ static contextType = AppContext componentWillMount(){ const resolvedData = extractData("/hom
import AppContext from '../../context/AppContext'
import extractDatta from '../../helper';
class App extends Component{
static contextType = AppContext
componentWillMount(){
const resolvedData = extractData("/home",this.context)
}
render(){
return(
)
}
}
helper/index.js:
const extractData = (path, context) => {
// getting context as undefined
}
App.test.js:
describe('App test suites', () => {
let wrapper;
let appContext;
beforeEach(() => {
appContext = {name: "Application"}
wrapper = shallow(<Supplier />, { appContext })
})
it('Should render the App Component', () => {
expect(wrapper).toMatchSnapshot();
})
})
description('apptestsuites',()=>{
让包装纸;
让我们看看上下文;
在每个之前(()=>{
appContext={name:“应用程序”}
包装器=浅(,{appContext})
})
它('应该呈现应用程序组件',()=>{
expect(wrapper.toMatchSnapshot();
})
})
感谢您的帮助:)因此,这是我在酵素团队完成[16支持]工作时的黑客解决方法。我在组件类上提供了遗留的
contextTypes
,允许按照文档传递上下文
import PropTypes from "prop-types";
describe('App test suites', () => {
let wrapper;
let appContext;
beforeEach(() => {
appContext = {name: "Application"}
// Hack in the legacy context API just for tests. You'll get a warning, but it
// ought to work...
Supplier.contextTypes = {
name: PropTypes.string
}
wrapper = shallow(<Supplier />, { appContext })
})
it('Should render the App Component', () => {
expect(wrapper).toMatchSnapshot();
})
})
从“道具类型”导入道具类型;
描述('应用程序测试套件',()=>{
让包装纸;
让我们看看上下文;
在每个之前(()=>{
appContext={name:“应用程序”}
//仅仅为了测试而入侵旧的上下文API。你会得到一个警告,但是它
//应该工作。。。
Supplier.contextTypes={
名称:PropTypes.string
}
包装器=浅(,{appContext})
})
它('应该呈现应用程序组件',()=>{
expect(wrapper.toMatchSnapshot();
})
})
这可能不会有帮助。但您不应该使用上下文参数名而不是appContext传递Options对象吗
wrapper=shallow(,{context:appContext})
不幸的是,这还没有在Ezyme中实现。这里有相同的问题..您找到解决方法了吗?