useRoutes.spec.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {createMemoryHistory, Route, Router, RouterContext} from 'react-router';
  2. import {render} from 'sentry-test/reactTestingLibrary';
  3. import {useRoutes} from 'sentry/utils/useRoutes';
  4. import {RouteContext} from 'sentry/views/routeContext';
  5. describe('useRoutes', () => {
  6. it('returns the current routes object', function () {
  7. let routes;
  8. function HomePage() {
  9. routes = useRoutes();
  10. return null;
  11. }
  12. const memoryHistory = createMemoryHistory();
  13. memoryHistory.push('/');
  14. render(
  15. <Router
  16. history={memoryHistory}
  17. render={props => {
  18. return (
  19. <RouteContext.Provider value={props}>
  20. <RouterContext {...props} />
  21. </RouteContext.Provider>
  22. );
  23. }}
  24. >
  25. <Route path="/" component={HomePage} />
  26. </Router>
  27. );
  28. expect(routes.length).toEqual(1);
  29. expect(routes[0]).toEqual({path: '/', component: HomePage});
  30. });
  31. it('throws error when called outside of routes provider', function () {
  32. // Error is expected, do not fail when calling console.error
  33. jest.spyOn(console, 'error').mockImplementation();
  34. const memoryHistory = createMemoryHistory();
  35. memoryHistory.push('/');
  36. expect(() =>
  37. render(
  38. <Router history={memoryHistory}>
  39. <Route
  40. path="/"
  41. component={() => {
  42. useRoutes();
  43. return null;
  44. }}
  45. />
  46. </Router>
  47. )
  48. ).toThrow(/useRouteContext called outside of routes provider/);
  49. });
  50. });