React Unit Test Code Snippets with Jest and Enzyme
- By : Mydatahack
- Category : Front-End, Web Technologies
- Tags: Enzyme, Jest, React, Unit Test
Sometimes all I need is to see a snippet of code without much explanation when I try to find a solution online. This is because I am familiar enough with the language and framework and need a quick reminder. So, here it is. I created a post with a few React unit test code snippets with Jest and Enzyme.
List
- Snapshot testing
- Testing to see if a DOM element exists
- Testing to see if a class exists in the specific DOM element
- Testing to see if an element has the correct value
- Simulating button click event and its outcome
- Checking function has been called once
- Checking function has been called with a specific argument
Testing React Form
Check out the post here. It has a little bit more focus on React form unit testing. You can check out the complete solutions here: react-form-unit-test-example.
Reference
For Enzyme, I check this cheatsheet all the time. For style, I often refer React style guide from Airbnb.