S
S
s243442019-12-20 14:02:02
React
s24344, 2019-12-20 14:02:02

How to write test (jest) correctly?

Hello everyone, please tell me where the error can be:
There is a tested component:

const ContractCreate = ({ isFinished, statusLevel, isError }) => {
    return (
        <section data-test="component-contract-create">

And a test for this component:
import React from 'react'
import { Provider } from 'react-redux'
import Enzyme, { shallow } from 'enzyme'
import EnzymeAdapter from 'enzyme-adapter-react-16'

import store from '../__data__/store'
import ContractCreate from './contract-create'

Enzyme.configure({ adapter: new EnzymeAdapter() })

export const findByTestAttr = (wrapper, val) => {
    return wrapper.find(`[data-test="${val}"]`)
}

test('renders without error', () => {
    const wrapper = shallow(
        <Provider store={store}>
            <ContractCreate />
        </Provider>
    )

    const component = findByTestAttr(wrapper, 'component-contract-create')
    console.log('component', component.length) // 0

    expect(component.length)
        .toBe(1)
})

In component.length I have zero, it turns out that wrapper.find() did not find anything.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Didenko, 2019-12-20
@Dasslier

Do this after creating the wrapper constant
. You will see exactly what is rendered in your wrapper. And when you see it, the problem will be easier to solve. And use a better mock store. That is, you create an object with the necessary data of your store and pass it with the store prop to the Provider

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question