diff --git a/packages/react/src/components/Breadcrumb/Breadcrumb.test.tsx b/packages/react/src/components/Breadcrumb/Breadcrumb.test.tsx index 1d777ff4a3d44..de8c52dfc73fb 100644 --- a/packages/react/src/components/Breadcrumb/Breadcrumb.test.tsx +++ b/packages/react/src/components/Breadcrumb/Breadcrumb.test.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; -import { mount, ReactWrapper } from 'enzyme'; -import * as renderer from 'react-test-renderer'; +import { act, render } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import { Breadcrumb } from './index'; import { Icon } from '../../Icon'; import { isConformant } from '../../common/isConformant'; @@ -15,84 +15,60 @@ describe('Breadcrumb', () => { { text: 'TestText4', key: 'TestKey4' }, ]; - let component: renderer.ReactTestRenderer | undefined; - let wrapper: ReactWrapper | undefined; - beforeEach(() => { resetIds(); }); afterEach(() => { - if (component) { - component.unmount(); - component = undefined; - } - if (wrapper) { - wrapper.unmount(); - wrapper = undefined; + if ((setTimeout as any).mock) { + jest.useRealTimers(); } }); it('renders empty breadcrumb', () => { - component = renderer.create(); - - const tree = component.toJSON(); + const { container } = render(); - expect(tree).toMatchSnapshot(); + expect(container).toMatchSnapshot(); }); describe('rendering', () => { it('renders correctly', () => { - component = renderer.create(); - - const tree = component.toJSON(); - expect(tree).toMatchSnapshot(); + const { container } = render(); + expect(container).toMatchSnapshot(); }); it('renders correctly with overflow', () => { - component = renderer.create(); - - const tree = component.toJSON(); - expect(tree).toMatchSnapshot(); + const { container } = render(); + expect(container).toMatchSnapshot(); }); it('renders correctly with custom divider', () => { const divider = () => *; - component = renderer.create(); - - const tree = component.toJSON(); - expect(tree).toMatchSnapshot(); + const { container } = render(); + expect(container).toMatchSnapshot(); }); it('renders correctly with overflow and overflowIndex', () => { - component = renderer.create(); - - const tree = component.toJSON(); - expect(tree).toMatchSnapshot(); + const { container } = render(); + expect(container).toMatchSnapshot(); }); it('renders correctly with maxDisplayedItems and overflowIndex', () => { - component = renderer.create(); - - const tree = component.toJSON(); - expect(tree).toMatchSnapshot(); + const { container } = render(); + expect(container).toMatchSnapshot(); }); it('renders correctly with maxDisplayedItems and overflowIndex as 0', () => { - component = renderer.create(); - - const tree = component.toJSON(); - expect(tree).toMatchSnapshot(); + const { container } = render(); + expect(container).toMatchSnapshot(); }); it('renders correctly with custom overflow icon', () => { const overflowIcon = () => ; - component = renderer.create( + const { container } = render( , ); - - const tree = component.toJSON(); - expect(tree).toMatchSnapshot(); + expect(container).toMatchSnapshot(); }); }); @@ -105,27 +81,35 @@ describe('Breadcrumb', () => { }); it('renders items with expected element type', () => { + jest.useFakeTimers(); + const items2: IBreadcrumbItem[] = [ { text: 'Test1', key: 'Test1', href: 'http://bing.com', onClick: () => undefined }, { text: 'Test2', key: 'Test2', onClick: () => undefined }, { text: 'Test3', key: 'Test3', as: 'h1' }, ]; - wrapper = mount(); + const { getAllByRole } = render(); + //Initial rendering of component is "hidden" while measurements are made + // therefore we need to wait a bit before getting roles. + act(() => { + jest.runAllTimers(); + }); - // get the first child of each list item (the actual item content) - const renderedItems = wrapper.find('.ms-Breadcrumb-listItem > *:first-child'); + const renderedItems = getAllByRole('listitem'); expect(renderedItems).toHaveLength(3); // should be a link since it has a href (even though it also has onclick) - expect(renderedItems.at(0).getDOMNode().tagName).toBe('A'); + expect(renderedItems[0].firstElementChild!.tagName).toBe('A'); // should be a button since it doesn't have a href // (can't use a link without a href because it won't respond to key events) - expect(renderedItems.at(1).getDOMNode().tagName).toBe('BUTTON'); + expect(renderedItems[1].firstElementChild!.tagName).toBe('BUTTON'); // specified type of h1 overrides default - expect(renderedItems.at(2).getDOMNode().tagName).toBe('H1'); + expect(renderedItems[2].firstElementChild!.tagName).toBe('H1'); }); it('calls the callback when an item is clicked', () => { + jest.useFakeTimers(); + let callbackValue; const clickCallback = (ev: React.MouseEvent, item: IBreadcrumbItem) => { ev.preventDefault(); // in case it's a navigation event @@ -137,37 +121,61 @@ describe('Breadcrumb', () => { { text: 'Test2', key: 'Test2', onClick: clickCallback }, ]; - wrapper = mount(); - - const renderedItems = wrapper.find('.ms-Breadcrumb-itemLink').hostNodes(); + const { getByRole } = render(); + //Initial rendering of component is "hidden" while measurements are made + // therefore we need to wait a bit before getting roles. + act(() => { + jest.runAllTimers(); + }); - renderedItems.at(0).simulate('click'); + userEvent.click(getByRole('link')); expect(callbackValue).toEqual('Test1'); - renderedItems.at(1).simulate('click'); + userEvent.click(getByRole('button')); expect(callbackValue).toEqual('Test2'); }); it('moves items to overflow in the correct order', () => { - wrapper = mount(); + jest.useFakeTimers(); - expect(wrapper.find('.ms-Breadcrumb-item').first().text()).toContain('TestText3'); + const { getAllByRole } = render(); + //Initial rendering of component is "hidden" while measurements are made + // therefore we need to wait a bit before getting roles. + act(() => { + jest.runAllTimers(); + }); + + const firstListItem = getAllByRole('listitem')[1].firstElementChild; + expect(firstListItem!.textContent).toContain('TestText3'); }); it('supports native props on the root element', () => { - wrapper = mount(); + jest.useFakeTimers(); - expect(wrapper.find('.ms-Breadcrumb').prop('role')).toEqual('region'); + const { getByRole } = render(); + //Initial rendering of component is "hidden" while measurements are made + // therefore we need to wait a bit before getting roles. + act(() => { + jest.runAllTimers(); + }); + + expect(getByRole('region')).toBeTruthy(); }); it('opens the overflow menu on click', () => { - wrapper = mount(); + jest.useFakeTimers(); + + const { getByRole, getAllByRole } = render(); + //Initial rendering of component is "hidden" while measurements are made + // therefore we need to wait a bit before getting roles. + act(() => { + jest.runAllTimers(); + }); - const overflowButton = wrapper.find('.ms-Breadcrumb-overflowButton'); - // without hostNodes it returns the same element x4 - overflowButton.hostNodes().simulate('click'); + const overflowButton = getByRole('button'); + userEvent.click(overflowButton!); - const overfowItems = document.querySelectorAll('.ms-ContextualMenu-item'); + const overfowItems = getAllByRole('menuitem'); expect(overfowItems).toHaveLength(2); expect(overfowItems[0].textContent).toEqual('TestText1'); expect(overfowItems[1].textContent).toEqual('TestText2'); @@ -175,6 +183,8 @@ describe('Breadcrumb', () => { describe('ARIA prop propagation to breadcrumb items', () => { it('for Link', () => { + jest.useFakeTimers(); + const itemsWithAdditionalProps: IBreadcrumbItem[] = [ { key: 'ItemKey1', @@ -184,13 +194,20 @@ describe('Breadcrumb', () => { }, ]; - wrapper = mount(); + const { getByRole } = render(); + //Initial rendering of component is "hidden" while measurements are made + // therefore we need to wait a bit before getting roles. + act(() => { + jest.runAllTimers(); + }); - const item = wrapper.find('LinkBase'); - expect(item.prop('aria-label')).toEqual("I'm an aria prop"); + const item = getByRole('link'); + expect(item.getAttribute('aria-label')).toEqual("I'm an aria prop"); }); it('for Tag', () => { + jest.useFakeTimers(); + const itemsWithAdditionalProps: IBreadcrumbItem[] = [ { key: 'ItemKey1', @@ -199,10 +216,15 @@ describe('Breadcrumb', () => { }, ]; - wrapper = mount(); + const { getByRole } = render(); + //Initial rendering of component is "hidden" while measurements are made + // therefore we need to wait a bit before getting roles. + act(() => { + jest.runAllTimers(); + }); - const item = wrapper.find('.ms-Breadcrumb-item'); - expect(item.prop('aria-label')).toEqual("I'm an aria prop"); + const item = getByRole('listitem', { hidden: true }).firstElementChild; + expect(item!.getAttribute('aria-label')).toEqual("I'm an aria prop"); }); }); }); diff --git a/packages/react/src/components/Breadcrumb/__snapshots__/Breadcrumb.test.tsx.snap b/packages/react/src/components/Breadcrumb/__snapshots__/Breadcrumb.test.tsx.snap index c7e1366f22981..5d4a20435b637 100644 --- a/packages/react/src/components/Breadcrumb/__snapshots__/Breadcrumb.test.tsx.snap +++ b/packages/react/src/components/Breadcrumb/__snapshots__/Breadcrumb.test.tsx.snap @@ -2,435 +2,387 @@ exports[`Breadcrumb rendering renders correctly with custom overflow icon 1`] = `
-
+
@@ -440,485 +392,411 @@ exports[`Breadcrumb rendering renders correctly with custom overflow icon 1`] = exports[`Breadcrumb rendering renders correctly 1`] = `
-
+
@@ -928,404 +806,330 @@ exports[`Breadcrumb rendering renders correctly 1`] = ` exports[`Breadcrumb rendering renders correctly with custom divider 1`] = `
-
+
@@ -1335,342 +1139,306 @@ exports[`Breadcrumb rendering renders correctly with custom divider 1`] = ` exports[`Breadcrumb rendering renders correctly with maxDisplayedItems and overflowIndex 1`] = `
-
+
@@ -1680,230 +1448,210 @@ exports[`Breadcrumb rendering renders correctly with maxDisplayedItems and overf exports[`Breadcrumb rendering renders correctly with maxDisplayedItems and overflowIndex as 0 1`] = `
-
+
@@ -1913,454 +1661,402 @@ exports[`Breadcrumb rendering renders correctly with maxDisplayedItems and overf exports[`Breadcrumb rendering renders correctly with overflow 1`] = `
-
+
@@ -2370,454 +2066,402 @@ exports[`Breadcrumb rendering renders correctly with overflow 1`] = ` exports[`Breadcrumb rendering renders correctly with overflow and overflowIndex 1`] = `
-
+
@@ -2827,66 +2471,56 @@ exports[`Breadcrumb rendering renders correctly with overflow and overflowIndex exports[`Breadcrumb renders empty breadcrumb 1`] = `
-
+