mockRouterPush.jsx 577 B

123456789101112131415161718192021222324
  1. import qs from 'query-string';
  2. // More closely mocks a router push -- updates wrapper's props/context
  3. // with updated `router` and calls `wrapper.update()`
  4. export function mockRouterPush(wrapper, router) {
  5. router.push.mockImplementation(({pathname, query}) => {
  6. const location = {
  7. ...router.location,
  8. query,
  9. search: qs.stringify(query),
  10. };
  11. const newRouter = {
  12. router: {
  13. ...router,
  14. location,
  15. },
  16. location,
  17. };
  18. wrapper.setProps(newRouter);
  19. wrapper.setContext(newRouter);
  20. wrapper.update();
  21. });
  22. }