breadcrumbs.spec.jsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import Breadcrumbs from 'sentry/components/breadcrumbs';
  3. describe('Breadcrumbs', () => {
  4. const routerContext = TestStubs.routerContext();
  5. afterEach(() => {
  6. jest.resetAllMocks();
  7. });
  8. function createWrapper() {
  9. return render(
  10. <Breadcrumbs
  11. crumbs={[
  12. {
  13. label: 'Test 1',
  14. to: '/test1',
  15. },
  16. {
  17. label: 'Test 2',
  18. to: '/test2',
  19. },
  20. {
  21. label: 'Test 3',
  22. to: null,
  23. },
  24. ]}
  25. />,
  26. {context: routerContext}
  27. );
  28. }
  29. it('returns null when 0 crumbs', () => {
  30. const empty = render(<Breadcrumbs crumbs={[]} />);
  31. expect(empty.container).toBeEmptyDOMElement();
  32. });
  33. it('renders crumbs with icon', () => {
  34. const wrapper = createWrapper();
  35. expect(wrapper.container).toSnapshot();
  36. });
  37. it('generates correct links', () => {
  38. createWrapper();
  39. userEvent.click(screen.getByText('Test 1'));
  40. expect(routerContext.context.router.push).toHaveBeenCalledWith('/test1');
  41. userEvent.click(screen.getByText('Test 2'));
  42. expect(routerContext.context.router.push).toHaveBeenCalledWith('/test2');
  43. });
  44. it('does not make links where no `to` is provided', () => {
  45. createWrapper();
  46. userEvent.click(screen.getByText('Test 3'));
  47. expect(routerContext.context.router.push).not.toHaveBeenCalled();
  48. });
  49. it('renders a crumb dropdown', async () => {
  50. const onSelect = jest.fn();
  51. render(
  52. <Breadcrumbs
  53. crumbs={[
  54. {
  55. label: 'dropdown crumb',
  56. onSelect,
  57. items: [{label: 'item1'}, {label: 'item2'}, {label: 'item3'}],
  58. },
  59. {
  60. label: 'Test 2',
  61. to: '/test2',
  62. },
  63. {
  64. label: 'Test 3',
  65. to: null,
  66. },
  67. ]}
  68. />,
  69. {context: routerContext}
  70. );
  71. userEvent.hover(screen.getByText('dropdown crumb'));
  72. const item3 = await screen.findByText('item3');
  73. expect(item3).toBeInTheDocument();
  74. userEvent.click(item3);
  75. expect(onSelect).toHaveBeenCalledWith(
  76. expect.objectContaining({label: 'item3'}),
  77. expect.anything(),
  78. expect.anything()
  79. );
  80. });
  81. });