mockRouterPush.jsx 800 B

1234567891011121314151617181920212223242526272829
  1. import * as 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(({query}) => {
  6. const stringifiedQuery = qs.stringify(query);
  7. const location = {
  8. ...router.location,
  9. // Need to make sure query more closely reflects datatypes in browser
  10. // e.g. if we had a param that was boolean, it would get stringified
  11. query: qs.parse(stringifiedQuery),
  12. search: stringifiedQuery,
  13. };
  14. const newRouter = {
  15. router: {
  16. ...router,
  17. location,
  18. },
  19. location,
  20. };
  21. wrapper.setProps(newRouter);
  22. wrapper.setContext(newRouter);
  23. wrapper.update();
  24. });
  25. }