Skip to content

Commit c9e6547

Browse files
chore(footer): migrate Footer.test.js to react-testing-library
Co-Authored-By: [email protected] <[email protected]>
1 parent a8c324c commit c9e6547

File tree

1 file changed

+63
-34
lines changed

1 file changed

+63
-34
lines changed
Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,59 @@
11
import * as React from 'react';
2-
import { mount } from 'enzyme';
2+
import '@testing-library/jest-dom';
3+
import { screen, render } from '../../../test-utils/testing-library';
34
import Footer from '../Footer';
45

56
describe('elements/content-picker/Footer', () => {
67
const defaultProps = {
7-
children: <div className="footer-child" />,
8+
children: <div data-testid="footer-child" className="footer-child" />,
89
currentCollection: { id: '123', name: 'Folder' },
910
hasHitSelectionLimit: false,
1011
isSingleSelect: false,
11-
onCancel: () => {},
12-
onChoose: () => {},
13-
onSelectedClick: () => {},
12+
onCancel: jest.fn(),
13+
onChoose: jest.fn(),
14+
onSelectedClick: jest.fn(),
1415
selectedCount: 0,
1516
selectedItems: [],
17+
showSelectedButton: false,
1618
};
1719

18-
const getWrapper = props => mount(<Footer {...defaultProps} {...props} />);
20+
const renderComponent = (props = {}) => render(<Footer {...defaultProps} {...props} />);
1921

2022
describe('render()', () => {
21-
test('should render Footer', () => {
22-
const wrapper = getWrapper();
23+
test('should render Footer with basic elements', () => {
24+
renderComponent();
2325

24-
expect(wrapper.find('ButtonGroup').length).toBe(1);
25-
expect(wrapper.find('.footer-child').length).toBe(1);
26+
const footer = screen.getByRole('contentinfo');
27+
const child = screen.getByTestId('footer-child');
28+
29+
expect(footer).toBeInTheDocument();
30+
expect(footer).toHaveClass('bcp-footer');
31+
expect(child).toBeInTheDocument();
2632
});
2733

28-
test('should render footer with disabled button', () => {
29-
const buttons = getWrapper().find('Button');
30-
const chooseButton = buttons.at(1);
34+
test('should render footer with disabled choose button', () => {
35+
renderComponent();
3136

32-
// https://www.w3.org/WAI/ARIA/apg/patterns/button/
33-
// When the action associated with a button is unavailable, the button has aria-disabled set to true.
34-
expect(chooseButton.html().includes('aria-disabled')).toBe(true);
35-
expect(chooseButton.prop('disabled')).toBe(true);
37+
const chooseButton = screen.getByRole('button', { name: 'Choose' });
38+
expect(chooseButton).toBeDisabled();
39+
expect(chooseButton).toHaveAttribute('aria-disabled', 'true');
3640
});
3741

38-
test('should render Footer buttons with aria-label', () => {
39-
const buttons = getWrapper().find('Button');
42+
test('should render Footer buttons with correct aria-labels', () => {
43+
renderComponent();
4044

41-
expect(buttons.at(0).prop('aria-label')).toBe('Cancel');
42-
expect(buttons.at(1).prop('aria-label')).toBe('Choose');
45+
expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
46+
expect(screen.getByRole('button', { name: 'Choose' })).toBeInTheDocument();
4347
});
4448

4549
test('should render Footer with custom action button', () => {
46-
const renderCustomActionButtons = jest.fn();
50+
const renderCustomActionButtons = jest.fn().mockReturnValue(<div data-testid="custom-button" />);
4751

48-
const wrapper = getWrapper({
49-
renderCustomActionButtons: renderCustomActionButtons.mockReturnValue(<div className="custom-button" />),
52+
renderComponent({
53+
renderCustomActionButtons,
5054
});
5155

52-
expect(wrapper.find('.custom-button').length).toBe(1);
56+
expect(screen.getByTestId('custom-button')).toBeInTheDocument();
5357
expect(renderCustomActionButtons).toHaveBeenCalledWith({
5458
currentFolderId: defaultProps.currentCollection.id,
5559
currentFolderName: defaultProps.currentCollection.name,
@@ -60,16 +64,41 @@ describe('elements/content-picker/Footer', () => {
6064
});
6165
});
6266

63-
test.each`
64-
showSelectedButton | isSingleSelect | shown | should
65-
${false} | ${false} | ${false} | ${'should not show selected button'}
66-
${false} | ${true} | ${false} | ${'should not show selected button'}
67-
${true} | ${false} | ${true} | ${'should show selected button'}
68-
${true} | ${true} | ${false} | ${'should not show selected button'}
69-
`('$should', ({ isSingleSelect, shown, showSelectedButton }) => {
70-
const wrapper = getWrapper({ isSingleSelect, showSelectedButton });
67+
test.each([
68+
{
69+
showSelectedButton: false,
70+
isSingleSelect: false,
71+
shown: false,
72+
description: 'should not show selected button when showSelectedButton is false',
73+
},
74+
{
75+
showSelectedButton: false,
76+
isSingleSelect: true,
77+
shown: false,
78+
description: 'should not show selected button when isSingleSelect is true',
79+
},
80+
{
81+
showSelectedButton: true,
82+
isSingleSelect: false,
83+
shown: true,
84+
description: 'should show selected button when conditions are met',
85+
},
86+
{
87+
showSelectedButton: true,
88+
isSingleSelect: true,
89+
shown: false,
90+
description:
91+
'should not show selected button when isSingleSelect is true regardless of showSelectedButton',
92+
},
93+
])('$description', ({ isSingleSelect, shown, showSelectedButton }) => {
94+
renderComponent({ isSingleSelect, showSelectedButton });
7195

72-
expect(wrapper.exists('.bcp-selected')).toBe(shown);
96+
const selectedButton = screen.queryByRole('button', { name: /selected/i });
97+
if (shown) {
98+
expect(selectedButton).toBeInTheDocument();
99+
} else {
100+
expect(selectedButton).not.toBeInTheDocument();
101+
}
73102
});
74103
});
75104
});

0 commit comments

Comments
 (0)