123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
- import Breadcrumbs from 'sentry/components/breadcrumbs';
- describe('Breadcrumbs', () => {
- const routerContext = TestStubs.routerContext();
- afterEach(() => {
- jest.resetAllMocks();
- });
- function createWrapper() {
- return render(
- <Breadcrumbs
- crumbs={[
- {
- label: 'Test 1',
- to: '/test1',
- },
- {
- label: 'Test 2',
- to: '/test2',
- },
- {
- label: 'Test 3',
- to: null,
- },
- ]}
- />,
- {context: routerContext}
- );
- }
- it('returns null when 0 crumbs', () => {
- const empty = render(<Breadcrumbs crumbs={[]} />);
- expect(empty.container).toBeEmptyDOMElement();
- });
- it('renders crumbs with icon', () => {
- const wrapper = createWrapper();
- expect(wrapper.container).toSnapshot();
- });
- it('generates correct links', () => {
- createWrapper();
- userEvent.click(screen.getByText('Test 1'));
- expect(routerContext.context.router.push).toHaveBeenCalledWith('/test1');
- userEvent.click(screen.getByText('Test 2'));
- expect(routerContext.context.router.push).toHaveBeenCalledWith('/test2');
- });
- it('does not make links where no `to` is provided', () => {
- createWrapper();
- userEvent.click(screen.getByText('Test 3'));
- expect(routerContext.context.router.push).not.toHaveBeenCalled();
- });
- it('renders a crumb dropdown', async () => {
- const onSelect = jest.fn();
- render(
- <Breadcrumbs
- crumbs={[
- {
- label: 'dropdown crumb',
- onSelect,
- items: [{label: 'item1'}, {label: 'item2'}, {label: 'item3'}],
- },
- {
- label: 'Test 2',
- to: '/test2',
- },
- {
- label: 'Test 3',
- to: null,
- },
- ]}
- />,
- {context: routerContext}
- );
- userEvent.hover(screen.getByText('dropdown crumb'));
- const item3 = await screen.findByText('item3');
- expect(item3).toBeInTheDocument();
- userEvent.click(item3);
- expect(onSelect).toHaveBeenCalledWith(
- expect.objectContaining({label: 'item3'}),
- expect.anything(),
- expect.anything()
- );
- });
- });
|