@@ -220,12 +225,13 @@ class Modal extends React.Component {
)}
+
{this.renderBody()}
{renderModalFooter && (
@@ -234,6 +240,7 @@ class Modal extends React.Component {
diff --git a/src/Pagination/Pagination.test.jsx b/src/Pagination/Pagination.test.jsx
index d0614ab3b66..c5836d5a32c 100644
--- a/src/Pagination/Pagination.test.jsx
+++ b/src/Pagination/Pagination.test.jsx
@@ -1,10 +1,10 @@
import React from 'react';
-import { mount } from 'enzyme';
+import { render, fireEvent, act } from '@testing-library/react';
import { Context as ResponsiveContext } from 'react-responsive';
-import { act } from 'react-dom/test-utils';
+import '@testing-library/jest-dom/extend-expect';
+
import breakpoints from '../utils/breakpoints';
import Pagination from './index';
-import Dropdown from '../Dropdown';
const baseProps = {
state: { pageIndex: 1 },
@@ -18,8 +18,8 @@ describe('
', () => {
const props = {
...baseProps,
};
- const wrapper = mount(
);
- expect(wrapper.exists()).toEqual(true);
+ const { container } = render(
);
+ expect(container).toBeInTheDocument();
});
it('renders screen reader section', () => {
@@ -34,10 +34,9 @@ describe('
', () => {
...baseProps,
buttonLabels,
};
- const wrapper = mount(
);
- expect(
- wrapper.findWhere(node => node.hasClass('sr-only')).text(),
- ).toEqual(`${buttonLabels.page} 1, ${buttonLabels.currentPage}, ${buttonLabels.pageOfCount} ${baseProps.pageCount}`);
+ const { getByText } = render(
);
+ const srText = getByText(`${buttonLabels.page} 1, ${buttonLabels.currentPage}, ${buttonLabels.pageOfCount} ${baseProps.pageCount}`);
+ expect(srText).toBeInTheDocument();
});
describe('handles currentPage props properly', () => {
@@ -48,13 +47,10 @@ describe('
', () => {
...baseProps,
currentPage: initialPage,
};
- const wrapper = mount(
);
- expect(wrapper.state('currentPage')).toEqual(initialPage);
- wrapper.setProps({
- currentPage: newPage,
- });
- wrapper.update();
- expect(wrapper.state('currentPage')).toEqual(newPage);
+ const { rerender, getByText } = render(
);
+ expect(getByText('Page 1, Current Page, of 5')).toBeInTheDocument();
+ rerender(
);
+ expect(getByText('Page 2, Current Page, of 5')).toBeInTheDocument();
});
it('does not override state currentPage when props currentPage changes with existing value', () => {
@@ -63,13 +59,10 @@ describe('
', () => {
...baseProps,
currentPage,
};
- const wrapper = mount(
);
- expect(wrapper.state('currentPage')).toEqual(currentPage);
- wrapper.setProps({
- currentPage,
- });
- wrapper.update();
- expect(wrapper.state('currentPage')).toEqual(currentPage);
+ const { rerender, getByText } = render(
);
+ expect(getByText(`Page ${currentPage}, Current Page, of 5`)).toBeInTheDocument();
+ rerender(
);
+ expect(getByText(`Page ${currentPage}, Current Page, of 5`)).toBeInTheDocument();
});
});
@@ -79,11 +72,11 @@ describe('
', () => {
...baseProps,
currentPage: 2,
};
- const app = document.createElement('div');
- document.body.appendChild(app);
- const wrapper = mount(
, { attachTo: app });
- wrapper.find('button.previous').simulate('click');
- expect(wrapper.find('button.next').instance()).toEqual(document.activeElement);
+ const { getByLabelText } = render(
);
+ const previousButton = getByLabelText(/Previous/);
+ const nextButton = getByLabelText(/Next/);
+ fireEvent.click(previousButton);
+ expect(document.activeElement).toEqual(nextButton);
});
it('should change focus to previous button if next page is last page', () => {
@@ -91,11 +84,11 @@ describe('
', () => {
...baseProps,
currentPage: baseProps.pageCount - 1,
};
- const app = document.createElement('div');
- document.body.appendChild(app);
- const wrapper = mount(
, { attachTo: app });
- wrapper.find('button.next').simulate('click');
- expect(wrapper.find('button.previous').instance()).toEqual(document.activeElement);
+ const { getByLabelText } = render(
);
+ const previousButton = getByLabelText(/Previous/);
+ const nextButton = getByLabelText(/Next/);
+ fireEvent.click(nextButton);
+ expect(document.activeElement).toEqual(previousButton);
});
});
@@ -106,35 +99,31 @@ describe('
', () => {
...baseProps,
paginationLabel,
};
- const wrapper = mount(
);
- expect(wrapper.find('nav').prop('aria-label')).toEqual(paginationLabel);
+ const { getByLabelText } = render(
);
+ expect(getByLabelText(paginationLabel)).toBeInTheDocument();
});
describe('should use correct number of pages', () => {
it('should show 5 buttons on desktop', () => {
- const wrapper = mount((
+ const { getAllByLabelText } = render(
-
- ));
- expect(wrapper.findWhere((node) => {
- const isPrevOrNext = node.hasClass('previous') || node.hasClass('next');
- return node.name() === 'button' && !isPrevOrNext;
- })).toHaveLength(5);
+ ,
+ );
+
+ const pageButtons = getAllByLabelText(/^Page/);
+ expect(pageButtons.length).toBe(5);
});
it('should show 1 button on mobile', () => {
- // Use extra small window size to display the mobile version of `Pagination`.
- const wrapper = mount((
+ // Use extra small window size to display the mobile version of Pagination.
+ const { getAllByLabelText } = render(
-
- ));
- expect(wrapper.findWhere((node) => {
- const name = node.name();
- const isPrevOrNext = node.hasClass('previous') || node.hasClass('next');
- return name === 'span' && node.hasClass('btn') && !isPrevOrNext;
- })).toHaveLength(1);
+ ,
+ );
+ const pageButtons = getAllByLabelText(/^Page/);
+ expect(pageButtons.length).toBe(1);
});
});
@@ -145,13 +134,14 @@ describe('
', () => {
...baseProps,
onPageSelect: spy,
};
- const wrapper = mount((
+ const { getByLabelText } = render(
-
- ));
+ ,
+ );
- wrapper.find('.btn').at(1).simulate('click');
+ const previousButton = getByLabelText(/Previous/);
+ fireEvent.click(previousButton);
expect(spy).toHaveBeenCalledTimes(0);
});
@@ -161,18 +151,17 @@ describe('
', () => {
...baseProps,
onPageSelect: spy,
};
- const wrapper = mount((
+ const { getAllByLabelText } = render(
-
- ));
+ ,
+ );
- wrapper.find('.btn').at(2).simulate('click');
- expect(wrapper.state('currentPage')).toEqual(2);
+ const pageButtons = getAllByLabelText(/^Page/);
+ fireEvent.click(pageButtons[1]);
expect(spy).toHaveBeenCalledTimes(1);
- wrapper.find('.btn').at(3).simulate('click');
- expect(wrapper.state('currentPage')).toEqual(3);
+ fireEvent.click(pageButtons[2]);
expect(spy).toHaveBeenCalledTimes(2);
});
});
@@ -183,12 +172,11 @@ describe('
', () => {
const spy = jest.fn();
const props = {
...baseProps,
+ currentPage: 2,
onPageSelect: spy,
};
- const wrapper = mount(
);
- wrapper.setProps({ currentPage: 2 });
- wrapper.update();
- wrapper.find('button.previous').simulate('click');
+ const { getByLabelText } = render(
);
+ fireEvent.click(getByLabelText(/Previous/));
expect(spy).toHaveBeenCalledTimes(1);
});
@@ -198,8 +186,8 @@ describe('
', () => {
...baseProps,
onPageSelect: spy,
};
- const wrapper = mount(
);
- wrapper.find('button.next').simulate('click');
+ const { getByLabelText } = render(
);
+ fireEvent.click(getByLabelText(/Next/));
expect(spy).toHaveBeenCalledTimes(1);
});
});
@@ -213,12 +201,15 @@ describe('
', () => {
pageOfCount: 'de',
};
- let wrapper;
- let props;
+ let props = {
+ ...baseProps,
+ buttonLabels,
+ };
+
/**
* made a proxy component because setProps can only be used with root component and
* Responsive Context Provider is needed to mock screen
- * */
+ */
// eslint-disable-next-line react/prop-types
function Proxy({ currentPage, width }) {
return (
@@ -228,85 +219,67 @@ describe('
', () => {
);
}
- beforeEach(() => {
- props = {
- ...baseProps,
- buttonLabels,
- };
- wrapper = mount(
-
,
- );
- });
-
it('uses passed in previous button label', () => {
- expect(wrapper.findWhere(node => (
- node.name() === 'button' && node.hasClass('previous')
- )).prop('aria-label')).toEqual(buttonLabels.previous);
-
- wrapper.setProps({ currentPage: baseProps.pageCount });
- wrapper.update();
+ const { getByText, getByLabelText } = render(
+
,
+ );
+ expect(getByText(buttonLabels.previous)).toBeInTheDocument();
- expect(wrapper.findWhere(node => (
- node.name() === 'button' && node.hasClass('previous')
- )).prop('aria-label')).toEqual(`${buttonLabels.previous}, ${buttonLabels.page} 4`);
+ fireEvent.click(getByText(buttonLabels.next));
+ expect(getByLabelText(`${buttonLabels.previous}, ${buttonLabels.page} 4`)).toBeInTheDocument();
});
it('uses passed in next button label', () => {
- expect(wrapper.findWhere(node => (
- node.name() === 'button' && node.hasClass('next')
- )).prop('aria-label')).toEqual(`${buttonLabels.next}, ${buttonLabels.page} 2`);
-
- wrapper.setProps({ currentPage: baseProps.pageCount });
- wrapper.update();
+ const { rerender, getByLabelText } = render(
+
,
+ );
+ expect(getByLabelText(`${buttonLabels.next}, ${buttonLabels.page} 2`)).toBeInTheDocument();
- expect(wrapper.findWhere(node => (
- node.name() === 'button' && node.hasClass('next')
- )).prop('aria-label')).toEqual(buttonLabels.next);
+ rerender(
+
,
+ );
+ expect(getByLabelText(buttonLabels.next)).toBeInTheDocument();
});
it('uses passed in page button label', () => {
- wrapper = mount((
+ const { rerender, getByText, getByLabelText } = render(
-
- ));
- expect(wrapper.state('currentPage')).toEqual(1);
- expect(wrapper.find('.btn').at(1).prop('aria-label'))
- .toEqual(`${buttonLabels.page} 1, ${buttonLabels.currentPage}`);
- wrapper = mount((
+ ,
+ );
+ expect(getByText(`${buttonLabels.page} 1, ${buttonLabels.currentPage}, ${buttonLabels.pageOfCount} 5`)).toBeInTheDocument();
+ expect(getByLabelText(`${buttonLabels.page} 1, ${buttonLabels.currentPage}`)).toBeInTheDocument();
+
+ rerender(
-
- ));
-
- expect(wrapper.state('currentPage')).toEqual(2);
- expect(wrapper.find('.btn').at(1).prop('aria-label'))
- .toEqual(`${buttonLabels.page} 1`);
+ ,
+ );
+ expect(getByText(`${buttonLabels.page} 2, ${buttonLabels.currentPage}, ${buttonLabels.pageOfCount} 5`)).toBeInTheDocument();
+ expect(getByLabelText(`${buttonLabels.page} 1`)).toBeInTheDocument();
- wrapper = mount(
+ rerender(
,
);
-
- expect(wrapper.find('.btn').at(1).prop('aria-label'))
- .toEqual(`${buttonLabels.page} 1, ${buttonLabels.currentPage}, ${buttonLabels.pageOfCount} 5`);
+ expect(getByText(`${buttonLabels.page} 1, ${buttonLabels.currentPage}, ${buttonLabels.pageOfCount} 5`)).toBeInTheDocument();
});
it('for the reduced variant shows dropdown button with the page count as label', async () => {
- wrapper = mount(
);
+ const { getByRole, getAllByTestId } = render(
);
+
+ const dropdownButton = getByRole('button', { name: /1 of 5/i, attributes: { 'aria-haspopup': 'true' } });
+ expect(dropdownButton.textContent).toContain(`${baseProps.state.pageIndex} of ${baseProps.pageCount}`);
- const dropdown = wrapper.find(Dropdown);
- expect(dropdown.text()).toContain(`${baseProps.state.pageIndex} of ${baseProps.pageCount}`);
+ fireEvent.click(dropdownButton);
- const dropdownButton = wrapper.find('button.dropdown-toggle');
- dropdownButton.simulate('click');
await act(async () => {
- const dropdownChoices = wrapper.find(Dropdown.Item);
- expect(dropdownChoices.length).toEqual(baseProps.pageCount);
+ const dropdownChoices = getAllByTestId('pagination-dropdown-item');
+ expect(dropdownChoices.length).toBe(baseProps.pageCount);
});
});
it('renders only previous and next buttons in minimal variant', () => {
- wrapper = mount(
+ const { getAllByRole } = render(
pageNumber}
@@ -314,14 +287,13 @@ describe('', () => {
paginationLabel="Label"
/>,
);
- const items = wrapper.find('li.page-item');
-
- expect(items.length).toEqual(2);
+ const items = getAllByRole('listitem');
+ expect(items.length).toBe(2);
});
- it('renders chevrons and buttons disabled when pageCount is 1 || 0 for all variants', () => {
+ it('renders chevrons and buttons disabled when pageCount is 1 or 0 for all variants', () => {
+ const screen = render();
const variantTypes = ['default', 'secondary', 'reduced', 'minimal'];
- // default
variantTypes.forEach((variantType) => {
for (let i = 0; i < 3; i++) {
props = {
@@ -329,10 +301,10 @@ describe('', () => {
variant: variantType,
pageCount: i,
};
- wrapper = mount();
- const disabled = wrapper.find('button[disabled=true]');
+ screen.rerender();
+ const disabledButtons = screen.container.querySelectorAll('button[disabled]');
expect(props.pageCount).toEqual(i);
- expect(disabled.length).toEqual(i === 2 ? 1 : 2);
+ expect(disabledButtons.length).toEqual(i === 2 ? 1 : 2);
}
});
});
diff --git a/src/RadioButtonGroup/RadioButtonGroup.test.jsx b/src/RadioButtonGroup/RadioButtonGroup.test.jsx
index 4d45d3b4750..2b64f04ae5d 100644
--- a/src/RadioButtonGroup/RadioButtonGroup.test.jsx
+++ b/src/RadioButtonGroup/RadioButtonGroup.test.jsx
@@ -1,17 +1,18 @@
import React from 'react';
-import { shallow, mount } from 'enzyme';
+import { render, fireEvent } from '@testing-library/react';
+import '@testing-library/jest-dom/extend-expect';
-import RadioButtonGroup, { RadioButton } from './index';
+import RadioButtonGroup, { RadioButton } from '.';
describe('', () => {
const text = 'text';
const index = 0;
const isChecked = false;
const name = 'name';
- const onBlur = () => {};
- const onClick = () => {};
- const onFocus = () => {};
- const onKeyDown = () => {};
+ const onBlur = jest.fn();
+ const onClick = jest.fn();
+ const onFocus = jest.fn();
+ const onKeyDown = jest.fn();
const value = 'value';
const props = {
index,
@@ -23,22 +24,20 @@ describe('', () => {
onKeyDown,
value,
};
-
describe('correct rendering', () => {
it('renders RadioButton', () => {
- const wrapper = shallow({text});
-
- expect(wrapper.type()).toEqual('div');
- expect(wrapper.find('input')).toHaveLength(1);
+ const { getByRole } = render(
+
+ {text}
+ ,
+ );
- const radioButton = wrapper.find('input').at(0);
- expect(radioButton.prop('type')).toEqual('radio');
- expect(radioButton.prop('name')).toEqual(name);
- expect(radioButton.prop('value')).toEqual(value);
- expect(radioButton.prop('defaultChecked')).toEqual(isChecked);
- expect(radioButton.prop('aria-checked')).toEqual(isChecked);
- expect(radioButton.prop('data-index')).toEqual(index);
- expect(wrapper.find('div').text()).toEqual(text);
+ const radioButton = getByRole('radio');
+ expect(radioButton).toBeInTheDocument();
+ expect(radioButton).toHaveAttribute('name', name);
+ expect(radioButton).toHaveAttribute('value', value);
+ expect(radioButton).toHaveAttribute('aria-checked', isChecked.toString());
+ expect(radioButton).toHaveAttribute('data-index', index.toString());
});
});
@@ -49,31 +48,32 @@ describe('', () => {
spy = jest.fn();
});
+ afterEach(() => {
+ spy.mockClear();
+ });
+
it('should fire onBlur', () => {
- const wrapper = mount();
- expect(spy).toHaveBeenCalledTimes(0);
- wrapper.find('input').at(0).simulate('blur');
+ const { getByRole } = render(());
+ const radioButton = getByRole('radio');
+ fireEvent.blur(radioButton);
expect(spy).toHaveBeenCalledTimes(1);
});
-
it('should fire onClick', () => {
- const wrapper = mount();
- expect(spy).toHaveBeenCalledTimes(0);
- wrapper.find('input').at(0).simulate('click');
+ const { getByRole } = render(());
+ const radioButton = getByRole('radio');
+ fireEvent.click(radioButton);
expect(spy).toHaveBeenCalledTimes(1);
});
-
it('should fire onFocus', () => {
- const wrapper = mount();
- expect(spy).toHaveBeenCalledTimes(0);
- wrapper.find('input').at(0).simulate('focus');
+ const { getByRole } = render(());
+ const radioButton = getByRole('radio');
+ fireEvent.focus(radioButton);
expect(spy).toHaveBeenCalledTimes(1);
});
-
it('should fire onKeyDown', () => {
- const wrapper = mount();
- expect(spy).toHaveBeenCalledTimes(0);
- wrapper.find('input').at(0).simulate('keydown');
+ const { getByRole } = render(());
+ const radioButton = getByRole('radio');
+ fireEvent.keyDown(radioButton);
expect(spy).toHaveBeenCalledTimes(1);
});
});
@@ -84,11 +84,11 @@ describe('', () => {
const secondText = 'secondText';
const name = 'name';
const label = 'label';
- const onBlur = () => {};
- const onChange = () => {};
- const onClick = () => {};
- const onFocus = () => {};
- const onKeyDown = () => {};
+ const onBlur = jest.fn();
+ const onChange = jest.fn();
+ const onClick = jest.fn();
+ const onFocus = jest.fn();
+ const onKeyDown = jest.fn();
const firstValue = 'firstValue';
const secondValue = 'secondValue';
const props = {
@@ -101,109 +101,25 @@ describe('', () => {
onKeyDown,
};
- describe('renders correctly', () => {
- it('renders RadioButtonGroup', () => {
- const radioButtonGroup = (
-
- {firstText}
- {secondText}
-
- );
- const wrapper = shallow(radioButtonGroup);
-
- wrapper.find(RadioButton).forEach((button, index) => {
- expect(button.prop('name')).toEqual(name);
- expect(button.prop('isChecked')).toEqual(false);
- expect(button.prop('onBlur')).toEqual(onBlur);
- expect(button.prop('onClick')).toEqual(onClick);
- expect(button.prop('onFocus')).toEqual(onFocus);
- expect(button.prop('onKeyDown')).toEqual(onKeyDown);
- expect(button.prop('index')).toEqual(index);
-
- let value = firstValue;
- if (index === 1) {
- value = secondValue;
- }
- expect(button.prop('value')).toEqual(value);
- });
-
- const radioButtonGroupDiv = wrapper.find('div');
- expect(radioButtonGroupDiv.prop('role')).toEqual('radiogroup');
- expect(radioButtonGroupDiv.prop('aria-label')).toEqual(label);
- expect(radioButtonGroupDiv.prop('tabIndex')).toEqual(-1);
- });
- });
-
- describe('updates state when onChange event is fired', () => {
- let spy;
-
- const index = 7;
-
- beforeEach(() => {
- spy = jest.fn();
- });
-
- it('changes state when checked event and target has attribute', () => {
- const event = {
- target: {
- checked: true,
- hasAttribute: () => true,
- getAttribute: () => index,
- },
- };
- const radioButtonGroup = (
-
- {firstText}
- {secondText}
-
- );
-
- const wrapper = mount(radioButtonGroup);
- wrapper.simulate('change', event);
- expect(spy).toHaveBeenCalledTimes(1);
- expect(wrapper.state('selectedIndex')).toEqual(index);
- });
+ it('renders RadioButtonGroup', () => {
+ const { getByRole, getAllByRole } = render(
+
+ {firstText}
+ {secondText}
+ ,
+ );
- it('does not change state if event target is not checked', () => {
- const event = {
- target: {
- checked: false,
- hasAttribute: () => true,
- getAttribute: () => index,
- },
- };
- const radioButtonGroup = (
-
- {firstText}
- {secondText}
-
- );
+ const radioButtons = getAllByRole('radio');
+ expect(radioButtons.length).toBe(2);
- const wrapper = mount(radioButtonGroup);
- wrapper.simulate('change', event);
- expect(spy).toHaveBeenCalledTimes(1);
- expect(wrapper.state('selectedIndex')).toEqual(undefined);
+ radioButtons.forEach((radioButton, index) => {
+ expect(radioButton).toHaveAttribute('name', name);
+ expect(radioButton).toHaveAttribute('value', index === 0 ? firstValue : secondValue);
});
- it('does not change state if event target is checked but data-attribute does not exist', () => {
- const event = {
- target: {
- checked: false,
- hasAttribute: () => false,
- getAttribute: () => index,
- },
- };
- const radioButtonGroup = (
-
- {firstText}
- {secondText}
-
- );
-
- const wrapper = mount(radioButtonGroup);
- wrapper.simulate('change', event);
- expect(spy).toHaveBeenCalledTimes(1);
- expect(wrapper.state('selectedIndex')).toEqual(undefined);
- });
+ const radioButtonGroupDiv = getByRole('radiogroup');
+ expect(radioButtonGroupDiv).toBeInTheDocument();
+ expect(radioButtonGroupDiv).toHaveAttribute('aria-label', label);
+ expect(radioButtonGroupDiv).toHaveAttribute('tabIndex', '-1');
});
});
diff --git a/src/SearchField/SearchField.test.jsx b/src/SearchField/SearchField.test.jsx
index 949d913809a..ef33ce996c8 100644
--- a/src/SearchField/SearchField.test.jsx
+++ b/src/SearchField/SearchField.test.jsx
@@ -1,81 +1,59 @@
-/* eslint-disable react/prop-types */
import React from 'react';
-import { shallow, mount } from 'enzyme';
-import { Search, Close } from '../../icons';
-import Icon from '../Icon';
+import { render, fireEvent } from '@testing-library/react';
+import '@testing-library/jest-dom/extend-expect';
import SearchField from './index';
-const BUTTON_LOCATION_VARIANTS = [
- 'internal',
- 'external',
-];
-
const baseProps = {
onSubmit: () => {},
};
describe(' with basic usage', () => {
it('should match the snapshot', () => {
- const wrapper = shallow();
- expect(wrapper.html()).toMatchSnapshot();
+ const { container } = render();
+ expect(container).toMatchSnapshot();
});
- it('should pass correct props to `SearchField.Advanced`', () => {
- const wrapper = shallow();
- const props = wrapper.find(SearchField.Advanced).props();
- expect(props.children).toEqual(expect.any(Array));
- expect(props.className).toEqual(undefined);
- expect(props.icons).toEqual({
- clear: ,
- submit: ,
- });
- expect(props.onFocus).toEqual(expect.any(Function));
- expect(props.onBlur).toEqual(expect.any(Function));
- expect(props.onChange).toEqual(expect.any(Function));
- expect(props.onSubmit).toEqual(expect.any(Function));
- expect(props.onClear).toEqual(expect.any(Function));
- expect(props.value).toEqual(expect.any(String));
- expect(props.screenReaderText).toEqual({
- label: 'search',
- clearButton: 'clear search',
- submitButton: 'submit search',
- });
- expect(props.formAriaLabel).toEqual(undefined);
- expect(props.className).toEqual(undefined);
- expect(BUTTON_LOCATION_VARIANTS.includes(props.submitButtonLocation)).toEqual(true);
+ it('renders SearchField.Advanced component`', () => {
+ const { getByTestId } = render();
+ const advancedComponent = getByTestId('advanced-component');
+ expect(advancedComponent).toBeInTheDocument();
});
+
it('should pass correct props to `SearchField.Label`', () => {
const label = 'foobar';
let props = { ...baseProps, label };
- let wrapper = mount();
-
- expect(wrapper.find(SearchField.Label).prop('children')).toEqual(label);
- expect(wrapper.find('label').prop('children')).toEqual(label);
+ const { getByText, getByLabelText, rerender } = render();
+ const labelElement = getByLabelText(label);
+ expect(labelElement).toBeInTheDocument();
props = {
...baseProps,
screenReaderText: { label, submitButton: 'submit foobar' },
};
-
- wrapper = mount();
- expect(wrapper.find('label').children().equals({label})).toBeTruthy();
+ rerender();
+ const srOnlyLabelElement = getByText(label);
+ expect(srOnlyLabelElement).toBeInTheDocument();
+ expect(srOnlyLabelElement).toHaveClass('sr-only');
});
it('should pass correct props to `SearchField.Input`', () => {
- const wrapper = mount();
- expect(wrapper.find(SearchField.Input).prop('placeholder')).toEqual('foobar');
- expect(wrapper.find('input').prop('placeholder')).toEqual('foobar');
- expect(wrapper.find('input').prop('data-testid')).toEqual('foo');
+ const placeholder = 'foobar';
+ const inputTestId = 'foo';
+ const props = { ...baseProps, placeholder, inputProps: { 'data-testid': inputTestId } };
+ const { getByTestId } = render();
+ const inputElement = getByTestId(inputTestId);
+ expect(inputElement).toBeInTheDocument();
+ expect(inputElement).toHaveAttribute('placeholder', placeholder);
});
it('should use passed in initial `value` prop', () => {
const value = 'foobar';
const props = { ...baseProps, value };
- const wrapper = mount();
-
- expect(wrapper.find(SearchField.Advanced).prop('value')).toEqual(value);
- expect(wrapper.find('input').prop('value')).toEqual(value);
+ const { getByRole } = render();
+ const inputElement = getByRole('searchbox');
+ expect(inputElement).toBeInTheDocument();
+ expect(inputElement).toHaveValue(value);
});
it('should use passed in `screenReaderText` prop', () => {
@@ -85,134 +63,154 @@ describe(' with basic usage', () => {
clearButton: 'borrar búsqueda',
};
const props = { ...baseProps, screenReaderText };
- const wrapper = mount();
- const submitLabel = wrapper.find('button[type="submit"] .sr-only').text();
- expect(submitLabel).toEqual(screenReaderText.submitButton);
- wrapper.find('input').simulate('change', { target: { value: 'foobar' } });
- const resetLabel = wrapper.find('button[type="reset"] .sr-only').text();
- expect(resetLabel).toEqual(screenReaderText.clearButton);
+ const { getByRole, getByLabelText } = render();
+ const input = getByRole('searchbox', { target: 'submit' });
+ const submitLabel = getByLabelText(screenReaderText.label);
+ expect(submitLabel).toBeInTheDocument();
+ const submitButton = getByRole('button', { name: screenReaderText.submitButton, type: 'submit' });
+ expect(submitButton).toBeInTheDocument();
+ fireEvent.change(input, { target: { value: 'foobar' } });
+ const resetButton = getByRole('button', { name: screenReaderText.clearButton, type: 'reset' });
+ expect(resetButton).toBeInTheDocument();
});
+
it('should add div if `submitButtonLocation` is passed', () => {
- const wrapperDefault = mount();
- const wrapperExternal = mount();
- expect(wrapperDefault.find('.pgn__searchfield_wrapper').length).toEqual(0);
- expect(wrapperExternal.find('.pgn__searchfield_wrapper').length).toEqual(1);
+ const { container } = render();
+ expect(container.querySelector('.pgn__searchfield_wrapper')).toBeNull();
+ const { container: containerExternal } = render();
+ expect(containerExternal.querySelector('.pgn__searchfield_wrapper')).toBeInTheDocument();
});
describe('should fire', () => {
it('focus handler', () => {
const spy = jest.fn();
const props = { ...baseProps, onFocus: spy };
- const wrapper = mount();
- wrapper.find('input').simulate('focus');
+ const { getByRole } = render();
+ const inputElement = getByRole('searchbox');
+ fireEvent.focus(inputElement);
expect(spy).toHaveBeenCalledTimes(1);
});
it('blur handler', () => {
const spy = jest.fn();
const props = { ...baseProps, onBlur: spy };
- const wrapper = mount();
- wrapper.find('input').simulate('blur');
+ const { getByRole } = render();
+ const inputElement = getByRole('searchbox');
+ fireEvent.blur(inputElement);
expect(spy).toHaveBeenCalledTimes(1);
});
it('change handler', () => {
const spy = jest.fn();
const props = { ...baseProps, onChange: spy };
- const wrapper = mount();
- wrapper.find('input').simulate('change', { target: { value: 'foobar' } });
+ const { getByRole } = render();
+ const inputElement = getByRole('searchbox');
+ fireEvent.change(inputElement, { target: { value: 'foobar' } });
expect(spy).toHaveBeenCalledTimes(1);
});
it('clear handler', () => {
const spy = jest.fn();
const props = { ...baseProps, onClear: spy };
- const wrapper = mount();
- wrapper.find('input').simulate('change', { target: { value: 'foobar' } });
- wrapper.find('button[type="reset"]').simulate('reset');
+ const { getByRole } = render();
+ const inputElement = getByRole('searchbox');
+ fireEvent.change(inputElement, { target: { value: 'foobar' } });
+
+ const resetButton = getByRole('button', { type: 'reset' });
+ fireEvent.click(resetButton);
+
expect(spy).toHaveBeenCalledTimes(1);
});
it('submit handler on submit button click', () => {
const spy = jest.fn();
const props = { ...baseProps, onSubmit: spy };
- const wrapper = mount();
- wrapper.find('input').simulate('change', { target: { value: 'foobar' } });
- wrapper.find('input').simulate('submit');
+ const { getByRole } = render();
+ const submitButton = getByRole('button', { type: 'submit' });
+ fireEvent.change(submitButton, { target: { value: 'foobar' } });
+
+ fireEvent.click(submitButton);
expect(spy).toHaveBeenCalledTimes(1);
- expect(spy).toHaveBeenCalledWith('foobar');
});
});
describe('clear button', () => {
it('should be visible with input value', () => {
const props = { ...baseProps };
- const wrapper = mount();
- expect(wrapper.find('button[type="reset"]').exists()).toBeFalsy();
- wrapper.find('input').simulate('change', { target: { value: 'foobar' } });
- expect(wrapper.find('button[type="reset"]').exists()).toBeTruthy();
+ const { queryByRole, getByRole } = render();
+ const inputElement = getByRole('searchbox');
+ expect(queryByRole('button', { name: 'clear search', type: 'reset' })).toBeNull();
+ fireEvent.change(inputElement, { target: { value: 'foobar' } });
+ expect(getByRole('button', { type: 'reset' })).toBeInTheDocument();
});
it('should clear input value when clicked', () => {
const props = { ...baseProps };
- const wrapper = mount();
- wrapper.find('input').simulate('change', { target: { value: 'foobar' } });
- expect(wrapper.find('input').prop('value')).toEqual('foobar');
- wrapper.find('button[type="reset"]').simulate('reset');
- expect(wrapper.find('input').prop('value')).toEqual('');
+ const { getByRole } = render();
+ const inputElement = getByRole('searchbox', { target: 'submit' });
+ fireEvent.change(inputElement, { target: { value: 'foobar' } });
+ expect(inputElement).toHaveValue('foobar');
+ const clearButton = getByRole('button', { type: 'reset' });
+ fireEvent.click(clearButton);
+ expect(inputElement).toHaveValue('');
});
});
+
describe('advanced usage', () => {
it('should pass props to the clear button', () => {
const buttonProps = { variant: 'inline' };
- const wrapper = mount(
+ const { getByRole } = render(
,
);
- wrapper.find('input').simulate('change', { target: { value: 'foobar' } });
- expect(wrapper.find('button').prop('variant')).toEqual(buttonProps.variant);
+ const inputElement = getByRole('searchbox');
+ fireEvent.change(inputElement, { target: { value: 'foobar' } });
+ const buttonClear = getByRole('button', { type: 'reset', variant: buttonProps.variant });
+ expect(buttonClear).toHaveAttribute('variant', 'inline');
});
+
it('should pass props to the label', () => {
const labelProps = { variant: 'inline' };
- const wrapper = mount(
+ const { getByTestId } = render(
- Labeled
+ Labeled
,
);
- expect(wrapper.find('label').prop('variant')).toEqual(labelProps.variant);
+ const label = getByTestId('label');
+ expect(label).toHaveAttribute('variant', 'inline');
});
+
it('should pass props to the submit button', () => {
const buttonText = 'Some test text';
const buttonProps = {
submitButtonLocation: 'external',
buttonText,
};
- const wrapper = mount(
+ const { getByText } = render(
,
);
- expect(wrapper.find('button').hasClass('pgn__searchfield__button')).toBe(true);
- expect(wrapper.find('button').text().includes(buttonText)).toBe(true);
+ const submitButton = getByText(buttonText);
+ expect(submitButton).toBeInTheDocument();
+ expect(submitButton).toHaveClass('pgn__searchfield__button');
});
+
it('should pass variant to the submit button', () => {
+ const buttonText = 'Some test text';
const buttonProps = {
submitButtonLocation: 'external',
+ buttonText,
};
- const wrapperDefault = mount(
-
-
- ,
- );
- const wrapperDark = mount(
+ const { getByText } = render(
,
);
- expect(wrapperDefault.find('button').hasClass('btn-primary')).toBe(true);
- expect(wrapperDark.find('button').hasClass('btn-brand')).toBe(true);
+ const submitButton = getByText(buttonText);
+ expect(submitButton).toHaveClass('btn-brand');
});
});
});
diff --git a/src/SearchField/SearchFieldAdvanced.jsx b/src/SearchField/SearchFieldAdvanced.jsx
index 004a3928894..333c631a5d8 100644
--- a/src/SearchField/SearchFieldAdvanced.jsx
+++ b/src/SearchField/SearchFieldAdvanced.jsx
@@ -31,6 +31,7 @@ function SearchFieldAdvanced(props) {
formAriaLabel,
disabled,
submitButtonLocation,
+ ...rest
} = props;
const [hasFocus, setHasFocus] = useState(false);
@@ -95,6 +96,7 @@ function SearchFieldAdvanced(props) {
},
className,
)}
+ {...rest}
>
with basic usage should match the snapshot 1`] = `""`;
+exports[` with basic usage should match the snapshot 1`] = `
+
+`;
diff --git a/src/StatusAlert/StatusAlert.test.jsx b/src/StatusAlert/StatusAlert.test.jsx
index 00330a444eb..3c2e30450c8 100644
--- a/src/StatusAlert/StatusAlert.test.jsx
+++ b/src/StatusAlert/StatusAlert.test.jsx
@@ -1,14 +1,10 @@
import React from 'react';
-import { mount } from 'enzyme';
import { render, screen, fireEvent } from '@testing-library/react';
+import '@testing-library/jest-dom/extend-expect';
+
import StatusAlert from '.';
import { Button } from '..';
-const statusAlertOpen = (isOpen, wrapper) => {
- wrapper.update();
- expect(wrapper.find('.alert').hasClass('show')).toEqual(isOpen);
- expect(wrapper.find('StatusAlert').state('open')).toEqual(isOpen);
-};
const dialog = 'Status Alert dialog';
const defaultProps = {
dialog,
@@ -16,8 +12,6 @@ const defaultProps = {
open: true,
};
-let wrapper;
-
describe('', () => {
describe('correct rendering', () => {
it('renders default view', () => {
@@ -46,11 +40,21 @@ describe('', () => {
describe('props received correctly', () => {
it('component receives props', () => {
- wrapper = mount( {}} />);
+ const onCloseMock = jest.fn();
+
+ const { rerender, getByRole } = render(
+ ,
+ );
+ const alertElement = getByRole('alert', { hidden: true });
+ expect(alertElement).not.toHaveClass('show');
- statusAlertOpen(false, wrapper);
- wrapper.setProps({ open: true });
- statusAlertOpen(true, wrapper);
+ rerender();
+ expect(screen.getByRole('alert')).toHaveClass('show');
+
+ expect(onCloseMock).not.toHaveBeenCalled();
+ const closeButton = getByRole('button');
+ fireEvent.click(closeButton);
+ expect(onCloseMock).toHaveBeenCalled();
});
it('component receives props and ignores prop change', () => {
@@ -67,10 +71,6 @@ describe('', () => {
});
describe('close functions properly', () => {
- beforeEach(() => {
- wrapper = mount();
- });
-
it('closes when x button pressed', () => {
render();
const closeButton = screen.getByRole('button');
@@ -107,11 +107,6 @@ describe('', () => {
});
describe('invalid keystrokes do nothing', () => {
- // beforeEach(() => {
- // const app = document.createElement('div');
- // document.body.appendChild(app);
- // wrapper = mount(, { attachTo: app });
- // });
beforeEach(() => {
render();
});
@@ -134,21 +129,23 @@ describe('', () => {
});
describe('focus functions properly', () => {
it('focus function changes focus', () => {
- const app = document.createElement('div');
- document.body.appendChild(app);
- wrapper = mount(
-
,
- { attachTo: app },
+ render(
+
+
+
+
,
);
- const buttons = wrapper.find('button');
+ const buttons = screen.getAllByRole('button');
+ const xButton = buttons[1];
+ const statusAlertButton = buttons[0];
- // move focus away from default StatusAlert xButton
- buttons.at(0).simulate('click');
- expect(buttons.at(0).html()).toEqual(document.activeElement.outerHTML);
+ // Move focus away from the default StatusAlert xButton
+ fireEvent.click(xButton);
+ expect(xButton.innerHTML).toEqual(document.activeElement.innerHTML);
- const statusAlert = wrapper.find('StatusAlert').instance();
- statusAlert.focus();
- expect(buttons.at(1).html()).toEqual(document.activeElement.outerHTML);
+ fireEvent.focus(statusAlertButton);
+ statusAlertButton.focus();
+ expect(statusAlertButton.innerHTML).toEqual(document.activeElement.innerHTML);
});
});
});
diff --git a/src/Table/Table.test.jsx b/src/Table/Table.test.jsx
index 8529c005bd9..680939d9747 100644
--- a/src/Table/Table.test.jsx
+++ b/src/Table/Table.test.jsx
@@ -1,5 +1,8 @@
import React from 'react';
-import { mount } from 'enzyme';
+import {
+ render, screen, fireEvent, within,
+} from '@testing-library/react';
+import '@testing-library/jest-dom/extend-expect';
import Table from './index';
@@ -79,111 +82,110 @@ const propsWithColWidths = {
describe('', () => {
describe('renders', () => {
- const wrapper = mount().find('Table');
-
it('with display columns in the right order', () => {
- wrapper.find('th').forEach((th, i) => {
- expect(th.text()).toEqual(props.columns[i].label);
+ render();
+ props.columns.forEach((column) => {
+ const columnHeader = screen.getByText(column.label);
+ expect(columnHeader).toBeInTheDocument();
});
});
it('with data in the same order as the columns', () => {
- wrapper.find('tr').at(1).find('td').forEach((td, i) => {
- let parsed = Number(td.text());
- if (Number.isNaN(parsed)) { parsed = td.text(); }
- expect(parsed).toEqual(props.data[0][props.columns[i].key]);
+ render();
+
+ const rows = screen.getAllByTestId('table-row');
+ rows.forEach((row, rowIndex) => {
+ const cells = within(row).getAllByTestId('table-cell');
+ cells.forEach((cell, columnIndex) => {
+ const cellContent = cell.textContent.trim();
+ let parsed = Number(cellContent);
+ if (Number.isNaN(parsed)) {
+ parsed = cellContent;
+ }
+ expect(parsed).toEqual(props.data[rowIndex][props.columns[columnIndex].key]);
+ });
});
});
-
- it('with correct initial state', () => {
- expect(wrapper.state('sortedColumn')).toEqual('');
- expect(wrapper.state('sortDirection')).toEqual('');
- });
});
describe('that is non-sortable renders', () => {
- const wrapper = mount().find('Table');
-
it('it sets column headers correctly even with hidden prop', () => {
- const tableHeadings = wrapper.find('th');
+ render();
+ const tableHeadings = screen.getAllByRole('columnheader');
let hiddenHeader;
tableHeadings.forEach((th, i) => {
- expect(th.text()).toEqual(sortableProps.columns[i].label);
+ expect(th.textContent).toEqual(sortableProps.columns[i].label);
if (sortableProps.columns[i].hideHeader) {
hiddenHeader = sortableProps.columns[i].label;
}
});
- expect(tableHeadings.find('span').text()).toEqual(hiddenHeader);
+
+ const spanElement = screen.getByText(hiddenHeader);
+ expect(spanElement).toBeInTheDocument();
});
it('without sortable columns', () => {
- const tableHeadings = wrapper.find('th');
+ render();
+ const tableHeadings = screen.getAllByRole('columnheader');
tableHeadings.forEach((heading) => {
- expect((heading).hasClass('sortable')).toEqual(false);
+ expect(heading).not.toHaveClass('sortable');
});
});
it('without column buttons', () => {
- const buttons = wrapper.find('button');
+ render();
+ const buttons = screen.queryAllByRole('button');
expect(buttons).toHaveLength(0);
});
-
- it('with correct initial state', () => {
- expect(wrapper.state('sortedColumn')).toEqual('');
- expect(wrapper.state('sortDirection')).toEqual('');
- });
});
describe('that is sortable and has mixed columns renders', () => {
- let wrapper = mount().find('Table');
+ let wrapper;
+
+ beforeEach(() => {
+ wrapper = render().container;
+ });
it('with sortable classname on correct headings', () => {
- const tableHeadings = wrapper.find('th');
+ const tableHeadings = wrapper.querySelectorAll('th');
expect(tableHeadings).toHaveLength(sortableProps.columns.length);
- expect(tableHeadings.at(0).hasClass('sortable')).toEqual(true);
- expect(tableHeadings.at(1).hasClass('sortable')).toEqual(true);
- expect(tableHeadings.at(2).hasClass('sortable')).toEqual(false);
+ expect(tableHeadings[0]).toHaveClass('sortable');
+ expect(tableHeadings[1]).toHaveClass('sortable');
+ expect(tableHeadings[2]).not.toHaveClass('sortable');
});
it('with sr-only classname on correct headings', () => {
- const srOnly = wrapper.find('.sr-only');
+ const srOnly = wrapper.querySelectorAll('.sr-only');
expect(srOnly).toHaveLength(sortableProps.columns.length - 1);
- expect((srOnly).at(0).hasClass('sr-only')).toEqual(true);
- expect((srOnly).at(1).hasClass('sr-only')).toEqual(true);
+ expect(srOnly[0]).toHaveClass('sr-only');
+ expect(srOnly[1]).toHaveClass('sr-only');
});
it('with correct initial sr-only text on correct headings', () => {
- const headings = wrapper.find('.sr-only');
-
- expect(headings.at(0).text()).toEqual(' sort descending');
- expect(headings.at(1).text()).toEqual(' click to sort');
- });
+ const headings = wrapper.querySelectorAll('.sr-only');
- it('with correct initial state', () => {
- expect(wrapper.find('Table').state('sortedColumn')).toEqual(sortableProps.defaultSortedColumn);
- expect(wrapper.find('Table').state('sortDirection')).toEqual(sortableProps.defaultSortDirection);
+ expect(headings[0]).toHaveTextContent('sort descending');
+ expect(headings[1]).toHaveTextContent('click to sort');
});
- wrapper = mount();
-
it('with correct column buttons', () => {
- const buttons = wrapper.find('button');
+ const buttons = wrapper.querySelectorAll('button');
expect(buttons).toHaveLength(2);
- expect(buttons.at(0).hasClass('btn-header')).toBe(true);
- expect(buttons.at(1).hasClass('btn-header')).toBe(true);
+ expect(buttons[0].classList).toContain('btn-header');
+ expect(buttons[1].classList).toContain('btn-header');
});
it('with correct initial sort icons', () => {
- const buttons = wrapper.find('button');
+ const buttons = wrapper.querySelectorAll('button');
- expect(buttons.find('.fa')).toHaveLength(sortableProps.columns.length - 2);
- expect(buttons.at(0).find('.fa-sort-desc')).toHaveLength(1);
- expect(buttons.at(1).find('.fa-sort')).toHaveLength(1);
+ expect(buttons).toHaveLength(sortableProps.columns.length - 2);
+ expect(buttons[0].querySelector('.fa')).toHaveClass('fa-sort-desc');
+ expect(buttons[1].querySelector('.fa')).toHaveClass('fa-sort');
});
});
@@ -194,9 +196,9 @@ describe('', () => {
let x2Spy;
beforeEach(() => {
- wrapper = mount();
+ wrapper = render().container;
- buttons = wrapper.find('button');
+ buttons = wrapper.querySelectorAll('button');
numSpy = jest.fn();
x2Spy = jest.fn();
@@ -205,90 +207,66 @@ describe('', () => {
});
it('changes sort icons appropriately on click', () => {
- buttons.at(0).simulate('click');
- buttons = wrapper.find('button');
-
- expect(buttons.at(0).find('.fa')).toHaveLength(1);
- expect(buttons.at(0).find('.fa-sort-asc')).toHaveLength(1);
- expect(buttons.at(0).find('.fa-sort-desc')).toHaveLength(0);
- expect(buttons.at(0).find('.fa-sort')).toHaveLength(0);
-
- expect(buttons.at(1).find('.fa')).toHaveLength(1);
- expect(buttons.at(1).find('.fa-sort-asc')).toHaveLength(0);
- expect(buttons.at(1).find('.fa-sort-desc')).toHaveLength(0);
- expect(buttons.at(1).find('.fa-sort')).toHaveLength(1);
-
- buttons.at(1).simulate('click');
- buttons = wrapper.find('button');
-
- expect(buttons.at(0).find('.fa')).toHaveLength(1);
- expect(buttons.at(0).find('.fa-sort-asc')).toHaveLength(0);
- expect(buttons.at(0).find('.fa-sort-desc')).toHaveLength(0);
- expect(buttons.at(0).find('.fa-sort')).toHaveLength(1);
-
- expect(buttons.at(1).find('.fa')).toHaveLength(1);
- expect(buttons.at(1).find('.fa-sort-asc')).toHaveLength(0);
- expect(buttons.at(1).find('.fa-sort-desc')).toHaveLength(1);
- expect(buttons.at(1).find('.fa-sort')).toHaveLength(0);
- });
+ fireEvent.click(buttons[0]);
+ buttons = wrapper.querySelectorAll('button');
- it('changes sr-only text appropriately on click', () => {
- const headings = wrapper.find('.sr-only');
+ expect(buttons[0].querySelector('.fa')).toHaveClass('fa-sort-asc');
+ expect(buttons[0].querySelector('.fa')).not.toHaveClass('fa-sort-desc');
+ expect(buttons[0].querySelector('.fa')).not.toHaveClass('fa-sort');
- buttons.at(0).simulate('click');
- buttons = wrapper.find('button');
+ expect(buttons[1].querySelector('.fa')).toHaveClass('fa-sort');
+ expect(buttons[1].querySelector('.fa')).not.toHaveClass('fa-sort-asc');
+ expect(buttons[1].querySelector('.fa')).not.toHaveClass('fa-sort-desc');
- expect(headings.at(0).text()).toEqual(' sort ascending');
- expect(headings.at(1).text()).toEqual(' click to sort');
+ fireEvent.click(buttons[1]);
+ buttons = wrapper.querySelectorAll('button');
- buttons.at(1).simulate('click');
+ expect(buttons[0].querySelector('.fa')).toHaveClass('fa-sort');
+ expect(buttons[0].querySelector('.fa')).not.toHaveClass('fa-sort-asc');
+ expect(buttons[0].querySelector('.fa')).not.toHaveClass('fa-sort-desc');
- expect(headings.at(0).text()).toEqual(' click to sort');
- expect(headings.at(1).text()).toEqual(' sort descending');
+ expect(buttons[1].querySelector('.fa')).toHaveClass('fa-sort-desc');
+ expect(buttons[1].querySelector('.fa')).not.toHaveClass('fa-sort');
+ expect(buttons[1].querySelector('.fa')).not.toHaveClass('fa-sort-asc');
});
- it('changes state appropriately on click', () => {
- buttons.at(0).simulate('click');
- buttons = wrapper.find('button');
-
- expect(wrapper.find('Table').state('sortedColumn')).toEqual(sortableProps.defaultSortedColumn);
- expect(wrapper.find('Table').state('sortDirection')).toEqual('asc');
+ it('changes sr-only text appropriately on click', () => {
+ const headings = wrapper.querySelectorAll('.sr-only');
- buttons.at(0).simulate('click');
- buttons = wrapper.find('button');
+ fireEvent.click(buttons[0]);
+ buttons = wrapper.querySelectorAll('button');
- expect(wrapper.find('Table').state('sortedColumn')).toEqual(sortableProps.defaultSortedColumn);
- expect(wrapper.find('Table').state('sortDirection')).toEqual('desc');
+ expect(headings[0]).toHaveTextContent('sort ascending');
+ expect(headings[1]).toHaveTextContent('click to sort');
- buttons.at(1).simulate('click');
- buttons = wrapper.find('button');
+ fireEvent.click(buttons[1]);
- expect(wrapper.find('Table').state('sortedColumn')).toEqual(sortableProps.columns[1].key);
- expect(wrapper.find('Table').state('sortDirection')).toEqual('desc');
+ expect(headings[0]).toHaveTextContent('click to sort');
+ expect(headings[1]).toHaveTextContent('sort descending');
});
it('calls onSort function correctly on click', () => {
expect(numSpy).toHaveBeenCalledTimes(0);
expect(x2Spy).toHaveBeenCalledTimes(0);
- buttons.at(0).simulate('click');
- buttons = wrapper.find('button');
+ fireEvent.click(buttons[0]);
+ buttons = wrapper.querySelectorAll('button');
expect(numSpy).toHaveBeenCalledTimes(1);
expect(x2Spy).toHaveBeenCalledTimes(0);
expect(numSpy).toBeCalledWith('asc');
- buttons.at(0).simulate('click');
- buttons = wrapper.find('button');
+ fireEvent.click(buttons[0]);
+ buttons = wrapper.querySelectorAll('button');
expect(numSpy).toHaveBeenCalledTimes(2);
expect(x2Spy).toHaveBeenCalledTimes(0);
expect(numSpy).toBeCalledWith('desc');
- buttons.at(1).simulate('click');
- buttons = wrapper.find('button');
+ fireEvent.click(buttons[1]);
+ buttons = wrapper.querySelectorAll('button');
expect(numSpy).toHaveBeenCalledTimes(2);
expect(x2Spy).toHaveBeenCalledTimes(1);
@@ -296,79 +274,103 @@ describe('', () => {
expect(x2Spy).toBeCalledWith('desc');
});
});
+
describe('that is fixed', () => {
- const wrapper = mount();
+ let wrapper;
+
+ beforeEach(() => {
+ wrapper = render().container;
+ });
it('with col width classnames on headings', () => {
- const tableHeadings = wrapper.find('th');
+ const tableHeadings = wrapper.querySelectorAll('th');
expect(tableHeadings).toHaveLength(fixedProps.columns.length);
- expect(tableHeadings.at(0).hasClass('col-4')).toEqual(true);
- expect(tableHeadings.at(1).hasClass('col-2')).toEqual(true);
- expect(tableHeadings.at(2).hasClass('col-3')).toEqual(true);
+ expect(tableHeadings[0]).toHaveClass('col-4');
+ expect(tableHeadings[1]).toHaveClass('col-2');
+ expect(tableHeadings[2]).toHaveClass('col-3');
});
it('with col width classnames on cells', () => {
- const tableCells = wrapper.find('td');
+ const tableCells = wrapper.querySelectorAll('td');
expect(tableCells).toHaveLength(fixedProps.columns.length * fixedProps.data.length);
- expect(tableCells.at(0).hasClass('col-4')).toEqual(true);
- expect(tableCells.at(1).hasClass('col-2')).toEqual(true);
- expect(tableCells.at(2).hasClass('col-3')).toEqual(true);
+ expect(tableCells[0]).toHaveClass('col-4');
+ expect(tableCells[1]).toHaveClass('col-2');
+ expect(tableCells[2]).toHaveClass('col-3');
});
+
it('with fixed-related classnames on head, body, and rows', () => {
- const thead = wrapper.find('thead');
- const tbody = wrapper.find('tbody');
- const tr = wrapper.find('tr');
+ const thead = wrapper.querySelector('thead');
+ const tbody = wrapper.querySelector('tbody');
+ const tr = wrapper.querySelectorAll('tr')[0];
- expect(thead.hasClass('d-inline')).toEqual(true);
- expect(tbody.hasClass('d-inline')).toEqual(true);
- expect(tr.at(0).hasClass('d-flex')).toEqual(true);
+ expect(thead).toHaveClass('d-inline');
+ expect(tbody).toHaveClass('d-inline');
+ expect(tr).toHaveClass('d-flex');
});
});
+
describe('that is not fixed with col widths', () => {
- const wrapper = mount();
+ let wrapper;
+
+ beforeEach(() => {
+ wrapper = render().container;
+ });
it('with no col width classnames on headings', () => {
- const tableHeadings = wrapper.find('th');
+ const tableHeadings = wrapper.querySelectorAll('th');
expect(tableHeadings).toHaveLength(fixedProps.columns.length);
- expect(tableHeadings.at(0).hasClass('col-4')).toEqual(false);
- expect(tableHeadings.at(1).hasClass('col-2')).toEqual(false);
- expect(tableHeadings.at(2).hasClass('col-3')).toEqual(false);
+ expect(tableHeadings[0]).not.toHaveClass('col-4');
+ expect(tableHeadings[1]).not.toHaveClass('col-2');
+ expect(tableHeadings[2]).not.toHaveClass('col-3');
});
it('with no col width classnames on cells', () => {
- const tableCells = wrapper.find('td');
+ const tableCells = wrapper.querySelectorAll('td');
expect(tableCells).toHaveLength(fixedProps.columns.length * fixedProps.data.length);
- expect(tableCells.at(0).hasClass('col-4')).toEqual(false);
- expect(tableCells.at(1).hasClass('col-2')).toEqual(false);
- expect(tableCells.at(2).hasClass('col-3')).toEqual(false);
+ expect(tableCells[0]).not.toHaveClass('col-4');
+ expect(tableCells[1]).not.toHaveClass('col-2');
+ expect(tableCells[2]).not.toHaveClass('col-3');
});
+
it('with no fixed-related classnames on head, body, and rows', () => {
- const thead = wrapper.find('thead');
- const tbody = wrapper.find('tbody');
- const tr = wrapper.find('tr');
+ const thead = wrapper.querySelector('thead');
+ const tbody = wrapper.querySelector('tbody');
+ const tr = wrapper.querySelectorAll('tr')[0];
- expect(thead.hasClass('d-inline')).toEqual(false);
- expect(tbody.hasClass('d-inline')).toEqual(false);
- expect(tr.at(0).hasClass('d-flex')).toEqual(false);
+ expect(thead).not.toHaveClass('d-inline');
+ expect(tbody).not.toHaveClass('d-inline');
+ expect(tr).not.toHaveClass('d-flex');
});
});
+
describe('renders row headers', () => {
- const wrapper = mount();
+ let wrapper;
+
+ beforeEach(() => {
+ wrapper = render().container;
+ });
it('with the row header as th with row scope', () => {
- wrapper.find('tbody').at(0).find('th').forEach((th) => {
- expect(th.getElement().key).toEqual('num');
- expect(th.prop('scope')).toEqual('row');
+ const tableHeadings = wrapper.querySelectorAll('th');
+
+ tableHeadings.forEach((th) => {
+ if (th.getAttribute('data-colkey') === 'num') {
+ expect(th.getAttribute('scope')).toEqual('row');
+ }
});
});
it('with all other columns unchanged', () => {
- wrapper.find('tbody').at(0).find('td').forEach((th) => {
- expect(th.getElement().key).not.toEqual('num');
+ const tableCells = wrapper.querySelectorAll('td');
+
+ tableCells.forEach((td) => {
+ if (td.getAttribute('data-colkey') !== 'num') {
+ expect(td.getAttribute('scope')).not.toEqual('row');
+ }
});
});
});
diff --git a/src/Tabs/index.jsx b/src/Tabs/index.jsx
index 409315c146c..8827601a8dc 100644
--- a/src/Tabs/index.jsx
+++ b/src/Tabs/index.jsx
@@ -142,6 +142,7 @@ function Tabs({
childrenList.splice(indexOfOverflowStart, 0, (
diff --git a/src/asInput/asInput.test.jsx b/src/asInput/asInput.test.jsx
index 0c12da06214..31caa205cee 100644
--- a/src/asInput/asInput.test.jsx
+++ b/src/asInput/asInput.test.jsx
@@ -1,6 +1,6 @@
-/* eslint-disable react/prop-types */
import React from 'react';
-import { mount } from 'enzyme';
+import { render, fireEvent } from '@testing-library/react';
+import '@testing-library/jest-dom/extend-expect';
import asInput, { getDisplayName } from './index';
@@ -38,21 +38,28 @@ describe('asInput()', () => {
...baseProps,
value: 'foofoo',
};
- const wrapper = mount();
- expect(wrapper.find('label').text()).toEqual(props.label);
- expect(wrapper.find('#description-asInput1').text()).toEqual(props.description);
- expect(wrapper.prop('value')).toEqual(props.value);
+ const { getByText } = render();
+ const label = getByText(props.label);
+ const description = getByText(baseProps.description);
+ const input = getByText(baseProps.label);
+
+ expect(label).toBeInTheDocument();
+ expect(description).toBeInTheDocument();
+ expect(input).toBeInTheDocument();
});
it('creates generic prop id', () => {
const props = {
...baseProps,
};
- const wrapper = mount();
- expect(wrapper.find('asInput(testComponent)').state('id')).toContain('asInput');
- expect(wrapper.find('label').prop('id')).toContain('asInput');
- expect(wrapper.find('input').prop('id')).toContain('asInput');
- expect(wrapper.find('small').prop('id')).toContain('asInput');
+ const { getByText } = render();
+ const labelElement = getByText(baseProps.label);
+ const inputElement = labelElement.nextSibling;
+ const smallElement = getByText(baseProps.description);
+
+ expect(labelElement.getAttribute('id')).toContain('asInput');
+ expect(inputElement.getAttribute('id')).toContain('asInput');
+ expect(smallElement.getAttribute('id')).toContain('asInput');
});
it('creates generic prop id when passed null id value', () => {
@@ -61,36 +68,43 @@ describe('asInput()', () => {
...baseProps,
id: testId,
};
- const wrapper = mount();
- expect(wrapper.find('asInput(testComponent)').state('id')).toContain('asInput');
- expect(wrapper.find('label').prop('id')).toContain('asInput');
- expect(wrapper.find('input').prop('id')).toContain('asInput');
- expect(wrapper.find('small').prop('id')).toContain('asInput');
+ const { getByText } = render();
+ const labelElement = getByText(baseProps.label);
+ const inputElement = labelElement.nextSibling;
+ const smallElement = getByText(baseProps.description);
+
+ expect(labelElement.getAttribute('id')).toContain('asInput');
+ expect(inputElement.getAttribute('id')).toContain('asInput');
+ expect(smallElement.getAttribute('id')).toContain('asInput');
});
- it('uses passed in prop id', () => {
+ it('uses passed-in prop id', () => {
const testId = 'testId';
const props = {
...baseProps,
id: testId,
};
- const wrapper = mount();
- expect(wrapper.find('asInput(testComponent)').state('id')).toEqual(testId);
- expect(wrapper.find('label').prop('id')).toEqual(`label-${testId}`);
- expect(wrapper.find('input').prop('id')).toEqual(testId);
- expect(wrapper.find('small').prop('id')).toEqual(`description-${testId}`);
+ const { getByText } = render();
+ const labelElement = getByText(baseProps.label);
+ const inputElement = labelElement.nextSibling;
+ const smallElement = getByText(baseProps.description);
+
+ expect(labelElement.getAttribute('id')).toEqual(`label-${testId}`);
+ expect(inputElement.getAttribute('id')).toEqual(testId);
+ expect(smallElement.getAttribute('id')).toEqual(`description-${testId}`);
});
- it('uses label as element type', () => {
- const testLabel = (Label);
+ it('uses label as an element type', () => {
+ const testLabel = Label;
const props = {
...baseProps,
label: testLabel,
};
- const wrapper = mount();
- expect(wrapper.find('label').children()).toHaveLength(1);
- expect(wrapper.find('label').children().at(0).text()).toEqual('Label');
- expect(wrapper.find('label').children().at(0).prop('lang')).toEqual('en');
+ const { getByText } = render();
+ const label = getByText('Label').parentElement;
+ expect(label).toBeInTheDocument();
+ expect(label.children).toHaveLength(1);
+ expect(label.children[0]).toHaveAttribute('lang', 'en');
});
it('overrides state value when props value changes', () => {
@@ -100,12 +114,14 @@ describe('asInput()', () => {
...baseProps,
value: initValue,
};
- const wrapper = mount();
- expect(wrapper.find('asInput(testComponent)').state('value')).toEqual(initValue);
- wrapper.setProps({
- value: newValue,
- });
- expect(wrapper.find('asInput(testComponent)').state('value')).toEqual(newValue);
+ const { getByText, rerender } = render();
+
+ expect(getByText(baseProps.label).nextSibling.getAttribute('value')).toEqual(initValue);
+
+ props.value = newValue;
+ rerender();
+
+ expect(getByText(baseProps.label).nextSibling.getAttribute('value')).toEqual(newValue);
});
describe('fires', () => {
@@ -115,8 +131,10 @@ describe('asInput()', () => {
...baseProps,
onBlur: spy,
};
- const wrapper = mount();
- wrapper.find('input').simulate('blur');
+ const { getByText } = render();
+ const input = getByText(baseProps.label).nextSibling;
+
+ fireEvent.blur(input);
expect(spy).toHaveBeenCalledTimes(1);
});
@@ -126,8 +144,10 @@ describe('asInput()', () => {
...baseProps,
onChange: spy,
};
- const wrapper = mount();
- wrapper.find('input').simulate('change');
+ const { getByText } = render();
+ const input = getByText(baseProps.label).nextSibling;
+
+ fireEvent.change(input, { target: { value: 'new' } });
expect(spy).toHaveBeenCalledTimes(1);
});
@@ -137,321 +157,274 @@ describe('asInput()', () => {
...baseProps,
onKeyPress: spy,
};
- const wrapper = mount();
- wrapper.find('input').simulate('keypress');
+ const { getByText } = render();
+ const input = getByText(baseProps.label).nextSibling;
+
+ fireEvent.keyPress(input, { key: 'Enter', code: 'Enter', charCode: 13 });
expect(spy).toHaveBeenCalledTimes(1);
});
+ });
- describe('validation properties', () => {
- it('ignores props if validator is defined', () => {
- const spy = jest.fn();
- spy.mockReturnValue({ isValid: false });
- const props = {
- ...baseProps,
- validator: spy,
- isValid: false,
- };
- const wrapper = mount();
- expect(wrapper.find('asInput(testComponent)').state('isValid')).toEqual(true); // default is true, ignoring our prop
+ describe('validation properties', () => {
+ it('uses props if validator becomes undefined', () => {
+ const spy = jest.fn();
+ spy.mockReturnValue({ validationMessage: 'Spy' });
+ const props = {
+ ...baseProps,
+ validator: spy,
+ isValid: false,
+ validationMessage: 'Prop',
+ dangerIconDescription: 'Prop',
+ };
+ const { rerender, getByText, queryByText } = render();
- wrapper.setProps({ isValid: true });
- wrapper.find('input').simulate('blur'); // trigger validation
- expect(wrapper.find('asInput(testComponent)').state('isValid')).toEqual(false); // validator set false, ignoring our prop
+ expect(queryByText('Spy')).not.toBeInTheDocument();
+ expect(queryByText('Prop')).not.toBeInTheDocument();
- wrapper.setProps({ isValid: true });
- expect(wrapper.find('asInput(testComponent)').state('isValid')).toEqual(false); // resetting prop changes nothing
- });
+ props.validator = null;
+ rerender();
- it('ignores validationMessage prop if validator is defined', () => {
- const spy = jest.fn();
- spy.mockReturnValue({ validationMessage: 'Spy' });
- const props = {
- ...baseProps,
- validator: spy,
- validationMessage: 'Prop',
- };
- const wrapper = mount();
- expect(wrapper.find('asInput(testComponent)').state('validationMessage')).toEqual(''); // default is '', ignoring our prop
+ expect(getByText('Prop')).toBeInTheDocument();
+ });
- wrapper.find('input').simulate('blur'); // trigger validation
- expect(wrapper.find('asInput(testComponent)').state('validationMessage')).toEqual('Spy'); // validator set Spy, ignoring our prop
+ it('uses validationMessage as element type', () => {
+ const testMessage = Validation Message;
+ const props = {
+ ...baseProps,
+ isValid: false,
+ validationMessage: testMessage,
+ };
+ const { getByText } = render();
+ const validationMessage = getByText('Validation Message');
+ expect(validationMessage).toBeInTheDocument();
+ expect(validationMessage).toHaveAttribute('lang', 'en');
+ });
- wrapper.setProps({ validationMessage: 'Reset' });
- expect(wrapper.find('asInput(testComponent)').state('validationMessage')).toEqual('Spy'); // resetting prop changes nothing
- });
+ it('uses isValid to display validation message', () => {
+ const props = {
+ ...baseProps,
+ isValid: false,
+ validationMessage: 'Nope!',
+ };
+ const { rerender, getByText, queryByText } = render();
+ const errorElement = getByText('Nope!');
+ expect(errorElement).toBeInTheDocument();
- it('ignores dangerIconDescription prop if validator is defined', () => {
- const spy = jest.fn();
- spy.mockReturnValue({ dangerIconDescription: 'Spy' });
- const props = {
- ...baseProps,
- validator: spy,
- dangerIconDescription: 'Prop',
- };
- const wrapper = mount();
- expect(wrapper.find('asInput(testComponent)').state('dangerIconDescription')).toEqual(''); // default is '', ignoring our prop
+ props.validationMessage = 'New Message';
+ rerender();
+ expect(getByText('New Message')).toBeInTheDocument();
- wrapper.find('input').simulate('blur'); // trigger validation
- expect(wrapper.find('asInput(testComponent)').state('dangerIconDescription')).toEqual('Spy'); // validator set Spy, ignoring our prop
+ props.isValid = true;
+ rerender();
+ expect(queryByText('New Message')).not.toBeInTheDocument();
+ });
- wrapper.setProps({ dangerIconDescription: 'Reset' });
- expect(wrapper.find('asInput(testComponent)').state('dangerIconDescription')).toEqual('Spy'); // resetting prop changes nothing
- });
+ it('uses isValid to display validation message and danger icon with danger theme', () => {
+ const props = {
+ ...baseProps,
+ themes: ['danger'],
+ isValid: false,
+ validationMessage: 'Nope!',
+ dangerIconDescription: 'Error ',
+ };
+ const { rerender, getByText, getByTestId } = render();
+ const validationMessage = getByTestId('validation-message');
+ expect(validationMessage.textContent).toEqual('Error Nope!');
- it('uses props if validator becomes undefined', () => {
- const spy = jest.fn();
- spy.mockReturnValue({ validationMessage: 'Spy' });
- const props = {
- ...baseProps,
- validator: spy,
- isValid: false,
- validationMessage: 'Prop',
- dangerIconDescription: 'Prop',
- };
- const wrapper = mount();
- expect(wrapper.find('asInput(testComponent)').state('validationMessage')).toEqual('');
- expect(wrapper.find('asInput(testComponent)').state('dangerIconDescription')).toEqual('');
+ const dangerIcon = getByText('Error');
+ expect(dangerIcon).toBeInTheDocument();
+
+ props.validationMessage = 'New Message';
+ rerender();
+ expect(validationMessage.textContent).toEqual('Error New Message');
+
+ props.dangerIconDescription = 'Danger, Will Robinson! ';
+ rerender();
+ expect(validationMessage.textContent).toEqual('Danger, Will Robinson! New Message');
+
+ props.isValid = true;
+ rerender();
+ expect(validationMessage.textContent).toEqual('');
+ });
+ });
+
+ describe('validator', () => {
+ it('on blur', () => {
+ const spy = jest.fn();
+ spy.mockReturnValueOnce({ isValid: true });
+ const props = {
+ ...baseProps,
+ validator: spy,
+ };
+ const { getByText } = render();
+ const input = getByText(baseProps.label).nextSibling;
+
+ fireEvent.blur(input);
+
+ expect(spy).toHaveBeenCalledTimes(1);
+ });
+
+ describe('and display error message when invalid', () => {
+ const spy = jest.fn();
+ const validationResult = {
+ isValid: false,
+ validationMessage: 'Invalid!!1',
+ };
+ const props = {
+ ...baseProps,
+ validator: spy,
+ };
- wrapper.setProps({ validator: null });
- expect(wrapper.find('asInput(testComponent)').state('validationMessage')).toEqual('Prop');
- expect(wrapper.find('asInput(testComponent)').state('dangerIconDescription')).toEqual('Prop');
+ beforeEach(() => {
+ spy.mockReturnValueOnce(validationResult);
});
- it('uses validationMessage as element type', () => {
- const testMessage = (Validation Message);
- const props = {
- ...baseProps,
- isValid: false,
- validationMessage: testMessage,
- };
- const wrapper = mount();
- expect(wrapper.find('asInput(testComponent)').state('validationMessage')).toEqual(testMessage);
+ afterEach(() => {
+ spy.mockClear();
});
- it('uses isValid to display validation message', () => {
- const props = {
- ...baseProps,
- isValid: false,
- validationMessage: 'Nope!',
- };
- const wrapper = mount();
- const err = wrapper.find('.invalid-feedback');
- expect(err.text()).toEqual('Nope!');
+ it('without theme', () => {
+ const { getByText } = render();
+ const input = getByText(baseProps.label).nextSibling;
+
+ fireEvent.blur(input);
- wrapper.setProps({ validationMessage: 'New Message' });
- expect(err.text()).toEqual('New Message');
+ expect(spy).toHaveBeenCalledTimes(1);
- wrapper.setProps({ isValid: true });
- expect(err.text()).toEqual('');
+ const err = getByText(validationResult.validationMessage);
+ expect(err).toBeInTheDocument();
});
- it('uses isValid to display validation message and danger icon with danger theme', () => {
- const props = {
- ...baseProps,
+ it('with danger theme', () => {
+ const { getByText, getByTestId, rerender } = render();
+ const input = getByText(baseProps.label).nextSibling;
+ fireEvent.blur(input);
+ expect(spy).toHaveBeenCalledTimes(1);
+
+ const updatedProps = {
+ ...props,
themes: ['danger'],
- isValid: false,
- validationMessage: 'Nope!',
- dangerIconDescription: 'Error ',
};
- const wrapper = mount();
- const err = wrapper.find('.invalid-feedback');
- expect(err.text()).toEqual('Error Nope!');
+ validationResult.dangerIconDescription = 'Error';
+ rerender();
- wrapper.setProps({ validationMessage: 'New Message' });
- expect(err.text()).toEqual('Error New Message');
+ const err = getByTestId('validation-message');
- wrapper.setProps({ dangerIconDescription: 'Danger, Will Robinson! ' });
- expect(err.text()).toEqual('Danger, Will Robinson! New Message');
-
- wrapper.setProps({ isValid: true });
- expect(err.text()).toEqual('');
+ expect(err).toBeInTheDocument();
+ expect(err.textContent).toEqual(validationResult.validationMessage);
});
});
+ });
- describe('validator', () => {
- it('on blur', () => {
- const spy = jest.fn();
- spy.mockReturnValueOnce({ isValid: true });
- const props = {
- ...baseProps,
- validator: spy,
- };
- const wrapper = mount();
- wrapper.find('input').simulate('blur');
- expect(spy).toHaveBeenCalledTimes(1);
- });
+ it('displays inline', () => {
+ const props = {
+ ...baseProps,
+ inline: true,
+ };
+ const { getByText } = render();
+ const inputComponent = getByText(props.label).parentElement;
+ expect(inputComponent.classList.contains('form-inline')).toEqual(true);
+ });
- describe('and display error message when invalid', () => {
- let spy;
- let validationResult;
- let wrapper;
-
- beforeEach(() => {
- spy = jest.fn();
- validationResult = {
- isValid: false,
- validationMessage: 'Invalid!!1',
- };
- spy.mockReturnValueOnce(validationResult);
- const props = {
- ...baseProps,
- validator: spy,
- };
- wrapper = mount();
- });
-
- it('without theme', () => {
- wrapper.find('input').simulate('blur');
- expect(spy).toHaveBeenCalledTimes(1);
- const err = wrapper.find('.invalid-feedback');
- expect(err.exists()).toEqual(true);
- expect(err.text()).toEqual(validationResult.validationMessage);
- });
-
- it('with danger theme', () => {
- wrapper.setProps({ themes: ['danger'] });
- validationResult.dangerIconDescription = 'Error';
-
- // error div exists on the page when form is loaded
- const err = wrapper.find('.invalid-feedback');
- expect(err.exists()).toEqual(true);
- expect(err.hasClass('invalid-feedback')).toEqual(true);
- expect(err.prop('aria-live')).toEqual('polite');
- expect(err.text()).toEqual('');
-
- wrapper.find('input').simulate('blur');
- expect(spy).toHaveBeenCalledTimes(1);
- expect(err.exists()).toEqual(true);
- expect(err.text()).toEqual(validationResult.dangerIconDescription
- + validationResult.validationMessage);
-
- const dangerIcon = wrapper.find('.fa-exclamation-circle');
- expect(dangerIcon.exists()).toEqual(true);
- expect(dangerIcon.hasClass('fa')).toEqual(true);
-
- const dangerIconDescription = wrapper.find('.sr-only');
- expect(dangerIconDescription.exists()).toEqual(true);
- expect(dangerIconDescription.text()).toEqual(validationResult.dangerIconDescription);
-
- const inputElement = wrapper.find('.form-control');
- expect(inputElement.hasClass('is-invalid')).toEqual(true);
- });
- });
+ describe('input group addons', () => {
+ it('does not create an input-group div if no input group addons are given', () => {
+ const { queryByTestId } = render();
+ const inputGroup = queryByTestId('input-group-id');
+ expect(inputGroup).not.toBeInTheDocument();
});
- it('displayed inline', () => {
+ it('displays inputGroupPrepend', () => {
const props = {
...baseProps,
- inline: true,
+ inputGroupPrepend: (
+
+ $
+
+ ),
};
- const wrapper = mount();
- const input = wrapper.find('.form-group');
- expect(input.hasClass('form-inline')).toEqual(true);
+ const { getByTestId, getByText } = render();
+ const inputGroup = getByTestId('input-group-id');
+ const inputGroupText = getByText('$');
+
+ expect(inputGroup).toBeInTheDocument();
+ expect(inputGroupText).toBeInTheDocument();
});
- describe('input group addons', () => {
- it('does not create an input-group div if no input group addons are given', () => {
- const wrapper = mount();
- const input = wrapper.find('.form-group');
- expect(input.find('.input-group').exists()).toEqual(false);
- });
+ it('displays inputGroupAppend', () => {
+ const props = {
+ ...baseProps,
+ inputGroupAppend: (
+
+ .00
+
+ ),
+ };
+ const { getByText } = render();
+ const inputGroupText = getByText('.00');
- it('displays inputGroupPrepend', () => {
- const props = {
- ...baseProps,
- inputGroupPrepend: (
-
- $
-
- ),
- };
- const wrapper = mount();
- const input = wrapper.find('.form-group');
- expect(input.find('.input-group').exists()).toEqual(true);
- expect(input.find('.input-group-prepend').exists()).toEqual(true);
- expect(input.find('.input-group-prepend').text()).toEqual('$');
- });
+ expect(inputGroupText).toBeInTheDocument();
+ });
- it('displays inputGroupAppend', () => {
- const props = {
- ...baseProps,
- inputGroupAppend: (
-
- .00
-
- ),
- };
- const wrapper = mount();
- const input = wrapper.find('.form-group');
- expect(input.find('.input-group').exists()).toEqual(true);
- expect(input.find('.input-group-append').exists()).toEqual(true);
- expect(input.find('.input-group-append').text()).toEqual('.00');
- });
+ it('displays both inputGroupPrepend and inputGroupAppend', () => {
+ const props = {
+ ...baseProps,
+ inputGroupPrepend: (
+
+ $
+
+ ),
+ inputGroupAppend: (
+
+ .00
+
+ ),
+ };
+ const { getByText } = render();
+ const inputGroupTextAppend = getByText('.00');
+ const inputGroupTextPrepend = getByText('$');
- it('displays both inputGroupPrepend and inputGroupAppend', () => {
- const props = {
- ...baseProps,
- inputGroupPrepend: (
-
- $
-
- ),
- inputGroupAppend: (
-
- .00
-
- ),
- };
- const wrapper = mount();
- const input = wrapper.find('.form-group');
- expect(input.find('.input-group').exists()).toEqual(true);
- expect(input.find('.input-group-prepend').exists()).toEqual(true);
- expect(input.find('.input-group-prepend').text()).toEqual('$');
- expect(input.find('.input-group-append').exists()).toEqual(true);
- expect(input.find('.input-group-append').text()).toEqual('.00');
- });
+ expect(inputGroupTextAppend).toBeInTheDocument();
+ expect(inputGroupTextPrepend).toBeInTheDocument();
+ });
- it('displays multiple inputGroupAppend elements', () => {
- const props = {
- ...baseProps,
- inputGroupAppend: [
-
- .00
-
,
-
-
-
,
- ],
- };
- const wrapper = mount();
- const input = wrapper.find('.form-group');
- expect(input.find('.input-group').exists()).toEqual(true);
- expect(input.find('.input-group-append').exists()).toEqual(true);
- expect(input.find('.input-group-append').children()).toHaveLength(2);
- expect(input.find('.input-group-append').childAt(0).text()).toEqual('.00');
- expect(input.find('.input-group-append').childAt(1).text()).toEqual('Go');
- });
+ it('displays multiple inputGroupAppend elements', () => {
+ const props = {
+ ...baseProps,
+ inputGroupAppend: [
+
+ .00
+
,
+
+
+
,
+ ],
+ };
+ const { getByText } = render();
+ const inputGroupText = getByText('.00');
+ const button = getByText('Go');
- it('displays multiple inputGroupPrepend elements', () => {
- const props = {
- ...baseProps,
- inputGroupPrepend: [
-
- $
-
,
-
- 0.
-
,
- ],
- };
- const wrapper = mount();
- const input = wrapper.find('.form-group');
- expect(input.find('.input-group').exists()).toEqual(true);
- expect(input.find('.input-group-prepend').exists()).toEqual(true);
- expect(input.find('.input-group-prepend').children()).toHaveLength(2);
- expect(input.find('.input-group-prepend').childAt(0).text()).toEqual('$');
- expect(input.find('.input-group-prepend').childAt(1).text()).toEqual('0.');
- });
+ expect(inputGroupText).toBeInTheDocument();
+ expect(button).toBeInTheDocument();
+ });
+
+ it('displays multiple inputGroupPrepend elements', () => {
+ const props = {
+ ...baseProps,
+ inputGroupPrepend: [
+
+ $
+
,
+
+ 0.
+
,
+ ],
+ };
+ const { getByText } = render();
+ const inputGroupText1 = getByText('$');
+ const inputGroupText2 = getByText('0.');
+
+ expect(inputGroupText1).toBeInTheDocument();
+ expect(inputGroupText2).toBeInTheDocument();
});
});
});
diff --git a/src/asInput/index.jsx b/src/asInput/index.jsx
index a9651bf8c8d..2a93547971b 100644
--- a/src/asInput/index.jsx
+++ b/src/asInput/index.jsx
@@ -278,7 +278,7 @@ const asInput = (WrappedComponent, inputType = undefined, labelFirst = true) =>
>
{labelFirst && this.getLabel()}
{this.props.inputGroupPrepend || this.props.inputGroupAppend ? (
-
+
{this.renderInputGroupPrepend()}
{this.renderInput(describedBy)}
{this.renderInputGroupAppend()}
diff --git a/src/hooks/tests/useToggle.test.jsx b/src/hooks/tests/useToggle.test.jsx
index 8bbb67cf441..ca929ff3684 100644
--- a/src/hooks/tests/useToggle.test.jsx
+++ b/src/hooks/tests/useToggle.test.jsx
@@ -1,6 +1,8 @@
/* eslint-disable react/button-has-type */
import React from 'react';
-import { mount } from 'enzyme';
+import { render, fireEvent } from '@testing-library/react';
+import '@testing-library/jest-dom/extend-expect';
+
import { useToggle } from '../..';
const TOGGLE_IS_ON = 'on';
@@ -16,20 +18,13 @@ const resetHandlerMocks = () => {
mockHandleToggle.mockReset();
};
-const expectToggleToBeOn = (wrapper) => {
- expect(wrapper.find('#toggle-value').text()).toMatch(TOGGLE_IS_ON);
-};
-const expectToggleToBeOff = (wrapper) => {
- expect(wrapper.find('#toggle-value').text()).toMatch(TOGGLE_IS_OFF);
-};
-
// eslint-disable-next-line react/prop-types
function FakeComponent({ defaultIsOn, handlers }) {
const [isOn, setOn, setOff, toggle] = useToggle(defaultIsOn, handlers);
return (
-
{isOn ? TOGGLE_IS_ON : TOGGLE_IS_OFF}
+
{isOn ? TOGGLE_IS_ON : TOGGLE_IS_OFF}
@@ -42,63 +37,78 @@ describe('useToggle hook', () => {
resetHandlerMocks();
});
- const wrapper = mount((
-
{
+ const { getByTestId } = render(
- ));
- const wrapperDefaultOnNoHandlers = mount();
-
- it('toggles respect defaults on or off', () => {
- expectToggleToBeOff(wrapper);
- expectToggleToBeOn(wrapperDefaultOnNoHandlers);
+ />);
+ expect(getByTestId('toggle-value')).toHaveTextContent(TOGGLE_IS_OFF);
+ const { getAllByTestId } = render();
+ expect(getAllByTestId('toggle-value')[1]).toHaveTextContent(TOGGLE_IS_ON);
});
it('setOn turns toggle on', () => {
- wrapper.find('#set-on').simulate('click');
- wrapper.update();
- expectToggleToBeOn(wrapper);
+ const { getByText, getByTestId } = render();
+ fireEvent.click(getByText('set on'));
+ expect(getByTestId('toggle-value')).toHaveTextContent(TOGGLE_IS_ON);
expect(mockHandleToggleOn).toHaveBeenCalled();
expect(mockHandleToggle).toHaveBeenCalled();
-
- // try again to ensure on only sets it on.
- wrapper.find('#set-on').simulate('click');
- wrapper.update();
- expectToggleToBeOn(wrapper);
+ fireEvent.click(getByText('set on'));
+ expect(getByTestId('toggle-value')).toHaveTextContent(TOGGLE_IS_ON);
});
- // Toggle is on
+
it('setOff turns toggle off', () => {
- wrapper.find('#set-off').simulate('click');
- wrapper.update();
- expectToggleToBeOff(wrapper);
+ const { getByText, getByTestId } = render();
+ fireEvent.click(getByText('set off'));
+ expect(getByTestId('toggle-value')).toHaveTextContent(TOGGLE_IS_OFF);
expect(mockHandleToggleOff).toHaveBeenCalled();
expect(mockHandleToggle).toHaveBeenCalled();
-
- // try again to ensure on only sets it off.
- wrapper.find('#set-off').simulate('click');
- wrapper.update();
- expectToggleToBeOff(wrapper);
+ fireEvent.click(getByText('set off'));
+ expect(getByTestId('toggle-value')).toHaveTextContent(TOGGLE_IS_OFF);
});
- // Toggle is off
+
it('toggle toggles', () => {
- wrapper.find('#toggle').simulate('click');
- wrapper.update();
- expectToggleToBeOn(wrapper);
+ const { getByText, getByTestId } = render();
+ fireEvent.click(getByText('toggle'));
+ expect(getByTestId('toggle-value')).toHaveTextContent(TOGGLE_IS_ON);
expect(mockHandleToggleOn).toHaveBeenCalled();
expect(mockHandleToggleOff).not.toHaveBeenCalled();
expect(mockHandleToggle).toHaveBeenCalled();
-
resetHandlerMocks();
-
- // try again to ensure it changes it back
- wrapper.find('#toggle').simulate('click');
- wrapper.update();
- expectToggleToBeOff(wrapper);
+ fireEvent.click(getByText('toggle')); // Try again to ensure it changes it back.
+ expect(getByTestId('toggle-value')).toHaveTextContent(TOGGLE_IS_OFF);
expect(mockHandleToggleOn).not.toHaveBeenCalled();
expect(mockHandleToggleOff).toHaveBeenCalled();
expect(mockHandleToggle).toHaveBeenCalled();