Skip to content

Commit 5ef17ec

Browse files
committed
feat(tests): migrate from enzyme to RTL
1 parent a4902a0 commit 5ef17ec

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

src/components/counter/Counter.spec.tsx

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React from 'react'
1+
import { render, screen, fireEvent } from '@testing-library/react'
22
import { Provider } from 'react-redux'
3-
import { mount } from 'enzyme'
43
import configureStore from 'redux-mock-store'
54

65
import { actionTypes } from '../../features/counter'
@@ -17,47 +16,43 @@ describe('Counter', () => {
1716
// Add jest mock spy to watch for store.dispatch method. See https://jestjs.io/docs/en/jest-object#jestspyonobject-methodname for more info
1817
jest.spyOn(store, 'dispatch')
1918

20-
it('renders without crashing.', () => {
21-
const wrapper = mount(
19+
test('renders without crashing.', () => {
20+
render(
2221
<Provider store={store}>
2322
<Counter />
2423
</Provider>
2524
)
2625

27-
const countValue = wrapper.find('strong').text()
28-
expect(countValue).toBe('42')
26+
const countValue = screen.getByText('42')
27+
expect(countValue).toBeInTheDocument()
2928
})
3029

31-
it('should be possible to increment counter.', () => {
32-
const wrapper = mount(
30+
test('should be possible to increment counter.', () => {
31+
render(
3332
<Provider store={store}>
3433
<Counter />
3534
</Provider>
3635
)
3736

38-
wrapper
39-
.find('button')
40-
.filter({ 'data-qa': 'increment-counter' })
41-
.simulate('click')
37+
const incrementButton = screen.getByRole('button', { name: 'increment' })
38+
fireEvent.click(incrementButton)
4239

43-
expect(store.dispatch).toBeCalledTimes(1)
40+
expect(store.dispatch).toHaveBeenCalledTimes(1)
4441

45-
expect(store.dispatch).toBeCalledWith({
42+
expect(store.dispatch).toHaveBeenCalledWith({
4643
type: actionTypes.INCREMENT_COUNTER,
4744
})
4845
})
4946

50-
it('should be possible to decrement counter.', () => {
51-
const wrapper = mount(
47+
test('should be possible to decrement counter.', () => {
48+
render(
5249
<Provider store={store}>
5350
<Counter />
5451
</Provider>
5552
)
5653

57-
wrapper
58-
.find('button')
59-
.filter({ 'data-qa': 'decrement-counter' })
60-
.simulate('click')
54+
const decrementButton = screen.getByRole('button', { name: 'decrement' })
55+
fireEvent.click(decrementButton)
6156

6257
expect(store.dispatch).toHaveBeenCalledTimes(1)
6358

src/features/counter/counterReducer.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import counterReducer from './counterReducer'
33
import { CounterActionTypes } from './types'
44

55
describe('features > counter > counterReducer', () => {
6-
it(`increments value, if ${INCREMENT_COUNTER} action is provided`, () => {
6+
test(`increments value, if ${INCREMENT_COUNTER} action is provided`, () => {
77
const initialState = {
88
value: 0,
99
}
@@ -19,7 +19,7 @@ describe('features > counter > counterReducer', () => {
1919
expect(counterReducer(initialState, action)).toEqual(expectedState)
2020
})
2121

22-
it(`increments value, if ${DECREMENT_COUNTER} action is provided`, () => {
22+
test(`increments value, if ${DECREMENT_COUNTER} action is provided`, () => {
2323
const initialState = {
2424
value: 0,
2525
}

src/setupTests.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
import { configure } from 'enzyme'
2-
import Adapter from '@wojtekmaj/enzyme-adapter-react-17'
3-
4-
configure({ adapter: new Adapter() })
1+
import '@testing-library/jest-dom'

0 commit comments

Comments
 (0)