-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rewrite tests using react testing library
- Loading branch information
Kyrylo Hudym-Levkovych
authored and
Kyrylo Hudym-Levkovych
committed
Aug 30, 2023
1 parent
f41ecf9
commit 52c0990
Showing
95 changed files
with
2,309 additions
and
2,001 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,35 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { render } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import Button from './index'; | ||
|
||
const defaultProps = { | ||
label: 'Click me!', | ||
}; | ||
|
||
describe('<Button />', () => { | ||
let wrapper; | ||
let button; | ||
|
||
beforeEach(() => { | ||
const app = document.createElement('div'); | ||
document.body.appendChild(app); | ||
wrapper = mount(<Button | ||
{...defaultProps} | ||
/>, { attachTo: app }); | ||
|
||
button = wrapper.find('button'); | ||
}); | ||
it('renders', () => { | ||
expect(button).toHaveLength(1); | ||
const { getByText } = render(<Button {...defaultProps} />); | ||
const button = getByText(defaultProps.label); | ||
expect(button).toBeInTheDocument(); | ||
}); | ||
|
||
it('puts focus on button on click', () => { | ||
expect(button.matchesElement(document.activeElement)).toEqual(false); | ||
button.simulate('click'); | ||
button = wrapper.find('button'); | ||
expect(button.at(0).html()).toEqual(document.activeElement.outerHTML); | ||
const { getByText } = render(<Button {...defaultProps} />); | ||
const button = getByText(defaultProps.label); | ||
|
||
expect(button).not.toHaveFocus(); | ||
userEvent.click(button); | ||
expect(button).toHaveFocus(); | ||
}); | ||
|
||
it('calls onClick prop on click', () => { | ||
const onClickSpy = jest.fn(); | ||
wrapper.setProps({ onClick: onClickSpy }); | ||
const { getByText } = render(<Button {...defaultProps} onClick={onClickSpy} />); | ||
const button = getByText(defaultProps.label); | ||
|
||
expect(onClickSpy).toHaveBeenCalledTimes(0); | ||
button.simulate('click'); | ||
userEvent.click(button); | ||
expect(onClickSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,64 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import CheckBox from './index'; | ||
|
||
describe('<CheckBox />', () => { | ||
it('attributes are set correctly', () => { | ||
const wrapper = mount(<CheckBox name="checkbox" label="check me out!" />); | ||
|
||
expect(wrapper.find('[name="checkbox"]').exists()).toEqual(true); | ||
expect(wrapper.find('[type="checkbox"]').exists()).toEqual(true); | ||
expect(wrapper.find('[checked=false]').exists()).toEqual(true); | ||
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true); | ||
const { getByLabelText } = render(<CheckBox name="checkbox" label="check me out!" />); | ||
const checkbox = getByLabelText('check me out!'); | ||
expect(checkbox).toBeInTheDocument(); | ||
expect(checkbox).toHaveAttribute('type', 'checkbox'); | ||
expect(checkbox).not.toBeChecked(); | ||
expect(checkbox).toHaveAttribute('aria-checked', 'false'); | ||
}); | ||
|
||
it('aria-label changes after click', () => { | ||
const wrapper = mount(<CheckBox name="checkbox" label="check me out!" />); | ||
|
||
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true); | ||
|
||
wrapper.find('[type="checkbox"]').simulate('change'); | ||
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(false); | ||
expect(wrapper.find('[aria-checked=true]').exists()).toEqual(true); | ||
|
||
wrapper.find('[type="checkbox"]').simulate('change'); | ||
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true); | ||
expect(wrapper.find('[aria-checked=true]').exists()).toEqual(false); | ||
const { getByLabelText } = render(<CheckBox name="checkbox" label="check me out!" />); | ||
const checkbox = getByLabelText('check me out!'); | ||
expect(checkbox).toHaveAttribute('aria-checked', 'false'); | ||
fireEvent.click(checkbox); | ||
expect(checkbox).toHaveAttribute('aria-checked', 'true'); | ||
fireEvent.click(checkbox); | ||
expect(checkbox).toHaveAttribute('aria-checked', 'false'); | ||
}); | ||
|
||
it('check that callback function is triggered when clicked', () => { | ||
const spy = jest.fn(); | ||
const wrapper = mount(<CheckBox name="checkbox" label="check me out!" onChange={spy} />); | ||
|
||
expect(spy).toHaveBeenCalledTimes(0); | ||
// check | ||
wrapper.find('input').simulate('change', { target: { checked: true, type: 'checkbox' } }); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenLastCalledWith(true, 'checkbox'); | ||
// uncheck | ||
wrapper.find('input').simulate('change', { target: { checked: false, type: 'checkbox' } }); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
expect(spy).toHaveBeenLastCalledWith(false, 'checkbox'); | ||
const onChangeSpy = jest.fn(); | ||
const { getByLabelText } = render(<CheckBox name="checkbox" label="check me out!" onChange={onChangeSpy} />); | ||
const checkbox = getByLabelText('check me out!'); | ||
expect(onChangeSpy).toHaveBeenCalledTimes(0); | ||
fireEvent.click(checkbox); | ||
expect(onChangeSpy).toHaveBeenCalledTimes(1); | ||
expect(onChangeSpy).toHaveBeenCalledWith(true, 'checkbox'); | ||
fireEvent.click(checkbox); | ||
expect(onChangeSpy).toHaveBeenCalledTimes(2); | ||
expect(onChangeSpy).toHaveBeenCalledWith(false, 'checkbox'); | ||
}); | ||
|
||
it('checks if start state can be set to checked', () => { | ||
const wrapper = mount(<CheckBox name="checkbox" label="I start checked" checked />); | ||
|
||
expect(wrapper.find('[checked=true]').exists()).toEqual(true); | ||
expect(wrapper.find('[aria-checked=true]').exists()).toEqual(true); | ||
|
||
wrapper.find('input').simulate('change'); | ||
expect(wrapper.find('[checked=false]').exists()).toEqual(true); | ||
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true); | ||
const { getByLabelText } = render(<CheckBox name="checkbox" label="I start checked" checked />); | ||
const checkbox = getByLabelText('I start checked'); | ||
expect(checkbox).toBeChecked(); | ||
expect(checkbox).toHaveAttribute('aria-checked', 'true'); | ||
fireEvent.click(checkbox); | ||
expect(checkbox).not.toBeChecked(); | ||
expect(checkbox).toHaveAttribute('aria-checked', 'false'); | ||
}); | ||
|
||
it('checkbox can be disabled', () => { | ||
const wrapper = mount(<CheckBox name="checkbox" label="I am disabled" disabled />); | ||
|
||
expect(wrapper.props().disabled).toEqual(true); | ||
|
||
wrapper.find('input').simulate('change'); | ||
expect(wrapper.props().disabled).toEqual(true); | ||
const { getByLabelText } = render(<CheckBox name="checkbox" label="I am disabled" disabled />); | ||
const checkbox = getByLabelText('I am disabled'); | ||
expect(checkbox).toBeDisabled(); | ||
fireEvent.click(checkbox); | ||
expect(checkbox).toBeDisabled(); | ||
}); | ||
|
||
it('overrides state value when props value changes', () => { | ||
const wrapper = mount(<CheckBox name="checkbox" label="I start checked" checked />); | ||
expect(wrapper.find('[checked=true]').exists()).toEqual(true); | ||
expect(wrapper.find('[aria-checked=true]').exists()).toEqual(true); | ||
|
||
wrapper.setProps({ | ||
checked: false, | ||
}); | ||
|
||
wrapper.update(); | ||
|
||
expect(wrapper.find('[checked=true]').exists()).toEqual(false); | ||
expect(wrapper.find('[aria-checked=true]').exists()).toEqual(false); | ||
const { getByLabelText, rerender } = render(<CheckBox name="checkbox" label="I start checked" checked />); | ||
const checkbox = getByLabelText('I start checked'); | ||
expect(checkbox).toBeChecked(); | ||
rerender(<CheckBox name="checkbox" label="I start checked" checked={false} />); | ||
expect(checkbox).not.toBeChecked(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.