useParams.spec.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {createMemoryHistory, Route, Router, RouterContext} from 'react-router';
  2. import {render} from 'sentry-test/reactTestingLibrary';
  3. import {useParams} from 'sentry/utils/useParams';
  4. import {RouteContext} from 'sentry/views/routeContext';
  5. describe('useParams', () => {
  6. describe('when the path has no params', () => {
  7. it('returns an empty object', () => {
  8. let params;
  9. function HomePage() {
  10. params = useParams();
  11. return null;
  12. }
  13. const memoryHistory = createMemoryHistory();
  14. memoryHistory.push('/?hello');
  15. render(
  16. <Router
  17. history={memoryHistory}
  18. render={props => {
  19. return (
  20. <RouteContext.Provider value={props}>
  21. <RouterContext {...props} />
  22. </RouteContext.Provider>
  23. );
  24. }}
  25. >
  26. <Route path="/" component={HomePage} />
  27. </Router>
  28. );
  29. expect(params).toEqual({});
  30. });
  31. });
  32. describe('when the path has some params', () => {
  33. it('returns an object of the URL params', () => {
  34. let params;
  35. function HomePage() {
  36. params = useParams();
  37. return null;
  38. }
  39. const memoryHistory = createMemoryHistory();
  40. memoryHistory.push('/sentry');
  41. render(
  42. <Router
  43. history={memoryHistory}
  44. render={props => {
  45. return (
  46. <RouteContext.Provider value={props}>
  47. <RouterContext {...props} />
  48. </RouteContext.Provider>
  49. );
  50. }}
  51. >
  52. <Route path="/:slug" component={HomePage} />
  53. </Router>
  54. );
  55. expect(params).toEqual({slug: 'sentry'});
  56. });
  57. });
  58. });