I feel like most component testing with a joke and an enzyme doesn't matter, am I wrong?

I am new to tests with React-Jest-Enzyme , but from all the information I have collected, it seems to me that most of the tests actually check if the React library is breaking, and not my actual business logic.

I will give some examples, and please correct me if I am wrong:

Checking Snapshots:

What is the deal with this strategy?

From what I see, its main purpose is to catch any unwanted changes in my code. he "builds" my component tree and just noticed that if a new line / characters are added, right?

therefore it was most often used for those cases when I accidentally pressed my keyboard? or did someone else accidentally mess up my code?

Enzymes Mounted / Small and Swine

Most of the examples I saw explaining how you use them are something like this:

const wrapper = mount(<MyComponeny />) expect(wrapper.find('button').simulate('click)).toHaveBeenCalledTime(1) 

What value can I get from this? If I simulate a button click using simulate('click') enzymes, then I should expect this to trigger a click event.

what am i testing here? The functionality of enzymes?

also gives us the enzyme of the setState method. if wrapper.setState({value: 'some value')} Suppose, to change my state, why I see usage examples as follows:

 wrapper.setState({value: 'new value')} expect(wrapper.state('value')).toBe('new value') 

?

Why do I need to test the test environment / additional libraries?

it all seems a bit ambiguous

+8
source share
1 answer

Pictures:

therefore it was mainly used for those cases when I could accidentally press the keyboard? or did someone else accidentally mess up my code?

If you configure a common component / service / utility and do not notice that this affects some unexpected components, for example.

Now this can affect it in a good way, for example, fixing unexpected text in a component, but snapshots give you the opportunity to quickly see changes in all affected components.

 const wrapper = mount(<MyComponeny />) expect(wrapper.find('button').simulate('click)).toHaveBeenCalledTime(1) 

What value can I get from him?

This is a simple example. It really would be a very bad test - it does not test anything. Usually you test more important things, such as:

toHaveBeenCalledTime(1) for some process - for example, that a network request is executed only once during the entire flow of clicks and other triggers.

why I see usage examples as follows:

 wrapper.setState({value: 'new value')} expect(wrapper.state('value')).toBe('new value') 

?

This is also a simple example showing that you can set state in a React component. In fact, this does not check anything.

What you can do is set the state of the component and make sure that it displays the right number of children or that it displays other things that you expect.

And it's also related to snapshots -

Set a specific state on the component and take a picture, then when you work with services and utilities that use this component, you can make sure that it does not break for this specific state.

+9
source

Source: https://habr.com/ru/post/1275481/


All Articles