所以我用酶呈現了一堆上下文的組成部分:語境缺失功能
const surveysByType = "test";
const wrapper = shallow(
<Dashboard routeParams={{}} />,
{
context: {
assetUrl:() => {
return "https://www.example.com/broken.gif"
},
executeAction:() => {},
getStore: (whatever) => {
return {
getState:() => {
return {
_surveysByType: surveysByType,
};
},
on:() => {
return {};
}
}
},
router: {
createHref:() => {},
go:() => {},
goBack:() => {},
goForward:() => {},
isActive:() => {},
push:() => {},
replace:() => {},
setRouteLeaveHook:() => {},
},
siteUrl:() => {
return "https://www.example.com"
},
}
}
);
然而,當我開始嘗試用說下,我發現只有一個單一的功能是存在的,即
console.log(wrapper.context())
產生
{ getStore: [Function: getStore] }
,而不是所有的功能。事實上,如果我嘗試做
expect(wrapper.context().assetUrl())
我得到一個錯誤:
TypeError: wrapper.context(...).assetUrl is not a function
,而這工作得很好:
expect(wrapper.context().getStore().getState())