breadcrumbs.spec.jsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(expect.objectContaining({label: 'item3'}));
  76. });
  77. });