breadcrumbTitle.spec.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {BreadcrumbContextProvider} from 'sentry-test/providers/breadcrumbContextProvider';
  3. import {render, screen} from 'sentry-test/reactTestingLibrary';
  4. import SettingsBreadcrumb from 'sentry/views/settings/components/settingsBreadcrumb';
  5. import BreadcrumbTitle from 'sentry/views/settings/components/settingsBreadcrumb/breadcrumbTitle';
  6. jest.unmock('sentry/utils/recreateRoute');
  7. describe('BreadcrumbTitle', function () {
  8. const testRoutes = [
  9. {name: 'One', path: '/one/'},
  10. {name: 'Two', path: '/two/'},
  11. {name: 'Three', path: '/three/'},
  12. ];
  13. it('renders settings breadcrumbs and replaces title', function () {
  14. const {routerContext} = initializeOrg({
  15. router: {
  16. routes: testRoutes,
  17. },
  18. } as any);
  19. render(
  20. <BreadcrumbContextProvider routes={testRoutes}>
  21. <SettingsBreadcrumb routes={testRoutes} params={{}} route={{}} />
  22. <BreadcrumbTitle routes={testRoutes} title="Last Title" />
  23. </BreadcrumbContextProvider>,
  24. {context: routerContext}
  25. );
  26. const crumbs = screen.getAllByRole('link');
  27. expect(crumbs).toHaveLength(3);
  28. expect(crumbs[2]).toHaveTextContent('Last Title');
  29. });
  30. it('cleans up routes', () => {
  31. const {routerContext} = initializeOrg({
  32. router: {
  33. routes: testRoutes,
  34. },
  35. } as any);
  36. let upOneRoutes = testRoutes.slice(0, -1);
  37. const {rerender} = render(
  38. <BreadcrumbContextProvider routes={testRoutes}>
  39. <SettingsBreadcrumb routes={testRoutes} params={{}} route={{}} />
  40. <BreadcrumbTitle routes={upOneRoutes} title="Second Title" />
  41. <BreadcrumbTitle routes={testRoutes} title="Last Title" />
  42. </BreadcrumbContextProvider>,
  43. {context: routerContext}
  44. );
  45. const crumbs = screen.getAllByRole('link');
  46. expect(crumbs).toHaveLength(3);
  47. expect(crumbs[1]).toHaveTextContent('Second Title');
  48. expect(crumbs[2]).toHaveTextContent('Last Title');
  49. // Mutate the object so that breadcrumbTitle re-renders
  50. upOneRoutes = testRoutes.slice(0, -1);
  51. // Simulate navigating up a level, trimming the last title
  52. rerender(
  53. <BreadcrumbContextProvider routes={upOneRoutes}>
  54. <SettingsBreadcrumb routes={upOneRoutes} params={{}} route={{}} />
  55. <BreadcrumbTitle routes={upOneRoutes} title="Second Title" />
  56. </BreadcrumbContextProvider>
  57. );
  58. const crumbsNext = screen.getAllByRole('link');
  59. expect(crumbsNext).toHaveLength(2);
  60. expect(crumbsNext[1]).toHaveTextContent('Second Title');
  61. });
  62. });