Reactjs 如何使用连接的react路由器';s单元测试中的推送
在测试过程中,我想更新存储在我的Reactjs 如何使用连接的react路由器';s单元测试中的推送,reactjs,redux,react-router,jestjs,connected-react-router,Reactjs,Redux,React Router,Jestjs,Connected React Router,在测试过程中,我想更新存储在我的reduxstorebyconnected react router中的URL,因为我的一些逻辑依赖于此。当Idispatchapush时,会记录操作(我可以使用记录器中间件验证),但实际状态不会改变 我有此代码来重现具体问题: 从'redux'导入{applyMiddleware,combinereducer,compose,createStore}; 从“redux thunk”导入thunk中间件; 从“连接的路由器”导入{connectRouter,pus
redux
storebyconnected react router
中的URL,因为我的一些逻辑依赖于此。当Idispatch
apush
时,会记录操作(我可以使用记录器中间件验证),但实际状态不会改变
我有此代码来重现具体问题:
从'redux'导入{applyMiddleware,combinereducer,compose,createStore};
从“redux thunk”导入thunk中间件;
从“连接的路由器”导入{connectRouter,push,routerMiddleware};
从“历史”导入{createMemoryHistory};
const history=createMemoryHistory();
导出函数createReduxStore(){
返回createStore(
组合传感器({
路由器:连接路由器(历史记录),
}),
{},
谱写(
applyMiddleware(
路由器管理软件(历史),
Thunk,
)
)
);
}
描述('路由器',()=>{
它('在URL中获取正确的数据',()=>{
const store=createReduxStore();
expect(store.getState().router.location.pathname).toEqual('/');
dispatch(push('/kg/firefox/resource/?string=42');
//此操作失败,`pathname`仍然是“/”
expect(store.getState().router.location.pathname).toEqual('/kg/firefox/resource/');
});
});
有人知道为什么这不能像我期望的那样起作用吗
相关套餐:
"react": "16.12.0",
"connected-react-router": "6.8.0",
"react-redux": "7.1.3",
"react-router": "5.2.0",
"react-router-dom": "5.2.0",
"react-scripts": "3.3.0",
"redux": "4.0.5",
"redux-logger": "3.0.6",
"redux-thunk": "2.3.0",
我在连接的路由器测试中找到了解决方案: 您需要使用enzime安装组件:
import { configure, mount } from 'enzyme';
configure({ adapter: new Adapter() });
beforeEach(() => {
const history = createMemoryHistory();
const store = createReduxStore(history);
//init connected-react-router to connect history and store
mount(
<Provider store={store}>
<ConnectedRouter history={history}>
<Route path="/" render={() => <div>Test</div>} />
</ConnectedRouter>
</Provider>
);
});
从“酶”导入{configure,mount};
配置({adapter:newadapter()});
在每个之前(()=>{
const history=createMemoryHistory();
const store=createReduxStore(历史);
//初始化已连接的路由器以连接历史记录和存储
坐骑(
Test}/>
);
});
我在连接的路由器测试中找到了解决方案:
您需要使用enzime安装组件:
import { configure, mount } from 'enzyme';
configure({ adapter: new Adapter() });
beforeEach(() => {
const history = createMemoryHistory();
const store = createReduxStore(history);
//init connected-react-router to connect history and store
mount(
<Provider store={store}>
<ConnectedRouter history={history}>
<Route path="/" render={() => <div>Test</div>} />
</ConnectedRouter>
</Provider>
);
});
从“酶”导入{configure,mount};
配置({adapter:newadapter()});
在每个之前(()=>{
const history=createMemoryHistory();
const store=createReduxStore(历史);
//初始化已连接的路由器以连接历史记录和存储
坐骑(
Test}/>
);
});
如果您可以提供有关解决方案详细信息的链接,那就太好了。如果您可以提供有关解决方案详细信息的链接,那就太好了。