useRoutes.spec.tsx 1.4 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. try {
  33. const memoryHistory = createMemoryHistory();
  34. memoryHistory.push('/');
  35. render(
  36. <Router history={memoryHistory}>
  37. <Route
  38. path="/"
  39. component={() => {
  40. useRoutes();
  41. return null;
  42. }}
  43. />
  44. </Router>
  45. );
  46. } catch (error) {
  47. expect(error.message).toBe('useRouteContext called outside of routes provider');
  48. }
  49. });
  50. });