concept test in category react

This is an excerpt from Manning's book React in Action.
You may be wondering what these tests will look like in code; we’ll get into that shortly, but first we need to talk about how these tests work together in the overall testing approach. If you’ve done testing before, you may have heard of the testing pyramid. This pyramid, illustrated in figure 9.1, generally refers to the proportion of different types of tests you should write. In this chapter, you’ll only be writing unit tests for your components.
Have you ever read test code that hasn’t gotten the same treatment as the code that it’s testing? I’ve had this happen to me more than once. It can be confusing or even frustrating to read through test code that isn’t clean. Tests are just more code, so they still need to be clean and readable, right? I’ve already mentioned in this chapter that testing can sometimes take second priority to writing application code. Test code can be treated as a task that has to be done or even a barrier between you and the application code, and so standards are lowered. This tendency can be easy to slip into, but the reality is that poorly written tests can be as bad as poorly written application code. Tests should serve as another form of documentation for your code, and one that still has to be read by developers. Remember that test code should still be clean code.
The next component you want to test, the CreatePost component, has more functionality than the Content component did, and your tests will need to address this added functionality. Listing 9.6 shows the CreatePost component so you can review it before writing out tests for it. The CreatePost component is used by the Home component to trigger the submission of new posts. It renders out a textarea that gets updated when the user types in it and a button that submits the form with data when a user clicks it. When the user clicks, it invokes a callback function passed by a parent component. You can test all these assumptions and make sure that things work as you expect.
Listing 9.7 shows a suggested skeleton of tests for the CreatePost component. You don’t have enough methods to make it difficult to read through the tests, but if a component had more to it, you might even add nested describe blocks to make it easier to reason about. The functions in listing 9.7 will be executed by the test runner (Jest in this case), and within those tests you can make your assertions. Most tests follow this same sort of pattern. You import the code under test, mock out any dependencies to isolate your tests to one unit of functionality (hence unit tests), and then a test runner and assertion library will work together to run your tests.
Listing 9.7. Testing the CreatePost component (src/components/post/Create.test.js)
jest.mock('mapbox'); import React from 'react'; import renderer from 'react-test-renderer'; import CreatePost from '../../../src/components/post/Create'; describe('CreatePost', () => { #1 test('snapshot', () => { }); test('handlePostChange', () => { #2 }); test('handleRemoveLocation', () => { #2 }); test('handleSubmit', () => { #2 }); test('onLocationUpdate', () => { }); test('handleToggleLocation', () => { }); test('onLocationSelect', () => { }); test('renderLocationControls', () => { }); }); #1 Using one describe call here, but in larger test files you can have many and even nest them #2 Create a test for each method in your component, including a snapshot to ensure it renders correctly !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":6,\"ch\":43},{\"line\":6,\"ch\":45}],[{\"line\":33,\"ch\":0},{\"line\":33,\"ch\":2}],[{\"line\":10,\"ch\":43},{\"line\":10,\"ch\":45}],[{\"line\":13,\"ch\":43},{\"line\":13,\"ch\":45}],[{\"line\":16,\"ch\":43},{\"line\":16,\"ch\":45}],[{\"line\":34,\"ch\":0},{\"line\":34,\"ch\":2}],[{\"line\":10,\"ch\":43},{\"line\":10,\"ch\":45}],[{\"line\":13,\"ch\":43},{\"line\":13,\"ch\":45}],[{\"line\":16,\"ch\":43},{\"line\":16,\"ch\":45}],[{\"line\":34,\"ch\":0},{\"line\":34,\"ch\":2}],[{\"line\":10,\"ch\":43},{\"line\":10,\"ch\":45}],[{\"line\":13,\"ch\":43},{\"line\":13,\"ch\":45}],[{\"line\":16,\"ch\":43},{\"line\":16,\"ch\":45}],[{\"line\":34,\"ch\":0},{\"line\":34,\"ch\":2}]]"} !@%STYLE%@!If you follow a consistent pattern of considering each part of your component that needs to be tested, you’ll be more thorough in developing and testing your components. Feel free to follow whatever structure makes the most sense to you—this is just one that has been helpful for me and for teams I’ve been on. I’ve also found it helpful to start writing tests by writing out the different describe and test blocks for a component or module before writing any other tests. I find that I can more easily think through the cases I want to cover (with an error, without an error, with a condition, and so on) if I’m doing that all at once.