routerFixture.ts 822 B

1234567891011121314151617181920212223242526272829303132333435
  1. import type {InjectedRouter} from 'react-router';
  2. import {stringify} from 'query-string';
  3. import {LocationFixture} from 'sentry-fixture/locationFixture';
  4. export function RouterFixture(params = {}): InjectedRouter {
  5. return {
  6. push: jest.fn(),
  7. replace: jest.fn(),
  8. go: jest.fn(),
  9. goBack: jest.fn(),
  10. goForward: jest.fn(),
  11. setRouteLeaveHook: jest.fn(),
  12. isActive: jest.fn(),
  13. createHref: jest.fn().mockImplementation(to => {
  14. if (typeof to === 'string') {
  15. return to;
  16. }
  17. if (typeof to === 'object') {
  18. if (!to.query) {
  19. return to.pathname;
  20. }
  21. return `${to.pathname}?${stringify(to.query)}`;
  22. }
  23. return '';
  24. }),
  25. location: LocationFixture(),
  26. createPath: jest.fn(),
  27. routes: [],
  28. params: {},
  29. ...params,
  30. };
  31. }