We haven't tried Enzyme, but we tend to prefer not adding more JS tools when we don't need to. JS is already a land of wayyyy too many tools and minimizing when possible can be a huge advantage when onboarding new devs onto a project.
Also the DOM api is actually pretty simple to use when it comes to traversing the DOM. No need for "jQuery mimicking." Keep it simple.
I've found the best testing pattern is to test the interface of the component, not the implementation.
This means treating the component as an opaque function with inputs (props) and outputs (the rendered result). Your tests just need to that verify that different prop values result in the DOM changes or callbacks that you expect.
Agree 100%. However if you are using component state you will need to test how interactions impact rendering output on the next render pass. I wouldn't consider that implementation detail since you are only asserting against render output.
In the case of testing instance methods, this is definitely a special edge-case scenario, but actually one of the main reasons I put this together. They inevitably happen, but are rare and I tend to forget how to set those tests up.
To give you an idea of how we use this: our application is a "website designer" where the preview is rendered inside an iframe. We use a react component to push CSS changes directly into the iframe via document.styleSheets. Using instance method testing allows us to test the main output results of this functionality without having to render real iframes pointing to external server in our tests.
I prefer this style of component test and tend to bring in jQuery as a test dependency to remove some of the complexity of the assertions (I'm not always up to date on the current DOM APIs).
I find the instance method unit test a bit unsatisfying.
I'm not going to call an instance method of a component from outside of that component (maybe someone else is doing this - I'm just not sure what the use case is), so why would I do that in my test? I want to integrate through the instance method by poking at the DOM rather than calling the method directly.
Good to know, will update the examples when I have time. Not sure I'm a big fan of it though, doesn't seem as clear to a React noob what is going on with ref?
Cool post but I'll play devil's advocate and ask what's so idiomatic about this. Can someone comment if this is what most the community is going? Not blaming the author at all, but skeptical of anything called "idiomatic" nowadays.
It can be very helpful to define all the different states (as props) for the set of components you're building. The data can be static or the result of dynamic calls to API endpoints (both approaches have benefits and trade-offs). For instance, if you're building a page with a list of items and at most 10 should be shown at a time, you could define the empty state (0 items), the half-full (1-10 items) and the overloaded state (11+ items, requiring pagination). Then you can implement the actual UI.
When this is done (and you're happy with the way it looks), you can make your test suite iterate over each of the different states and capture a screenshot of the UI. When something changes, you will know (the test suite should diff it). This approach is obviously mostly for presentation rather than functionality.
The benefits are not limited to testing, this is also very helpful in development, since you could switch between different states very quickly without having to constantly change URLs (for different API results). One method is using a dropdown overlay (only present in the development build). It's also very helpful during code review.
In my experience, as long as you have a well tested data model and use proptypes/Flow/TypeScript, testing the components themselves becomes almost entirely irrelevant.
However, I can see the utility of component testing if I were wrapping something like Ace editor or a jQuery plugin or something of that nature.
If your application has few or very simple interactions then this may make sense. If it does have complex interactions (I consider a text input updating a model field as complex) then I think tests are necessary, especially if working with a team. You want to know when you break things.
This becomes even more important if you're developing generic UI components that may be re-used in different scenarios throughout your app or company. These types of library components need to be well tested as they often have more complex interactions and are going to be used in potentially very different ways.
Do you use anything like Redux? When writing Redux state reducers and actions, it all seems so... simple. There's a lot of code like `return { ...currentState, newProp }` which seems too silly to test.
Do others agree with the `getComponent` pattern described there?
I do like it but I'm wary of moving away from the `beforeEach` pattern. The `beforeEach` pattern is much more common and is a consistent interface that engineers can easily pickup and won't need to recreate in every single test file.
I've found beforeEach breaks down super quickly as soon as you need to change the input props. Keeping things as purely functional as possible also helps manage complexity when writing tests in a large suite. You don't have to trace through all the beforeEach of each describe/context block. That said we use a healthy combination of both, as with everything know when to use the right tool.
[+] [-] jitl|9 years ago|reply
https://github.com/airbnb/enzyme#basic-usage
[+] [-] analyst74|9 years ago|reply
- better event simulation
- easier to update prop
- allow setting and getting state
- easier to refresh after triggering changes
- comprehensive selector support
[+] [-] jfdk|9 years ago|reply
Also the DOM api is actually pretty simple to use when it comes to traversing the DOM. No need for "jQuery mimicking." Keep it simple.
[+] [-] cjcenizal|9 years ago|reply
This means treating the component as an opaque function with inputs (props) and outputs (the rendered result). Your tests just need to that verify that different prop values result in the DOM changes or callbacks that you expect.
[+] [-] jfdk|9 years ago|reply
In the case of testing instance methods, this is definitely a special edge-case scenario, but actually one of the main reasons I put this together. They inevitably happen, but are rare and I tend to forget how to set those tests up.
To give you an idea of how we use this: our application is a "website designer" where the preview is rendered inside an iframe. We use a react component to push CSS changes directly into the iframe via document.styleSheets. Using instance method testing allows us to test the main output results of this functionality without having to render real iframes pointing to external server in our tests.
[+] [-] amavisca|9 years ago|reply
I find the instance method unit test a bit unsatisfying. I'm not going to call an instance method of a component from outside of that component (maybe someone else is doing this - I'm just not sure what the use case is), so why would I do that in my test? I want to integrate through the instance method by poking at the DOM rather than calling the method directly.
[+] [-] shepmaster|9 years ago|reply
What about any components that maintain internal state; do you just step them through the states and assert the DOM state as well?
[+] [-] pietro|9 years ago|reply
[+] [-] jfdk|9 years ago|reply
[+] [-] djacobs|9 years ago|reply
I've found it useful to use wrapper libraries like Enzyme [0] and Teaspoon [1] when testing React components and interactions.
[0] https://github.com/airbnb/enzyme
[1] https://github.com/jquense/teaspoon
[+] [-] sotojuan|9 years ago|reply
[+] [-] haukur|9 years ago|reply
When this is done (and you're happy with the way it looks), you can make your test suite iterate over each of the different states and capture a screenshot of the UI. When something changes, you will know (the test suite should diff it). This approach is obviously mostly for presentation rather than functionality.
The benefits are not limited to testing, this is also very helpful in development, since you could switch between different states very quickly without having to constantly change URLs (for different API results). One method is using a dropdown overlay (only present in the development build). It's also very helpful during code review.
[+] [-] tengbretson|9 years ago|reply
However, I can see the utility of component testing if I were wrapping something like Ace editor or a jQuery plugin or something of that nature.
[+] [-] jfdk|9 years ago|reply
This becomes even more important if you're developing generic UI components that may be re-used in different scenarios throughout your app or company. These types of library components need to be well tested as they often have more complex interactions and are going to be used in potentially very different ways.
[+] [-] unknown|9 years ago|reply
[deleted]
[+] [-] shepmaster|9 years ago|reply
[+] [-] lhnz|9 years ago|reply
I do like it but I'm wary of moving away from the `beforeEach` pattern. The `beforeEach` pattern is much more common and is a consistent interface that engineers can easily pickup and won't need to recreate in every single test file.
[+] [-] jfdk|9 years ago|reply