breadcrumbTitle.spec.tsx 2.1 KB

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