Skip to content

Commit ec1023d

Browse files
committed
driveby spelling
1 parent b04f6d5 commit ec1023d

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

_posts/2016-02-02-react-redux-unit-testing.md

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ categories: react
77
In this post I'll go through how to get enzyme setup to test your presentational components.
88
---
99

10-
---
11-
layout: post
12-
title: Unit Testing React Components and Redux Containers
13-
author: Mike James
14-
categories: react
15-
---
16-
In this post I'll go through how to get enzyme setup to test your presentational components.
17-
---
18-
1910
### Unit testing React Components and Redux reducers
2011

2112
NB: this post assumes knowledge of react, redux and webpack.
@@ -29,18 +20,18 @@ In this post we'll go through the setup of a React, Redux Webpack application wi
2920
### Folder Structure of our application.
3021
![http://i.imgur.com/43wv76d.png](http://i.imgur.com/43wv76d.png)
3122

32-
karma.config we'll need to load up our components and containers into a browser for testing as we're using postcss and other loaders that don't work natively in node.js. So to get our tests to run in browser we will also include a copy of our webpack config to get karma to load it up. If you load up your tests which rely on webpack loaders down the line you'll get Unexpected token errors.
23+
karma.config we'll need to load up our components and containers into a browser for testing as we're using postcss and other loaders that don't work natively in node.js. So to get our tests to run in browser we will also include a copy of our webpack config to get karma to load it up. If you load up your tests normally withouy karma, down the line you'll get Unexpected token errors.
3324

34-
server.js selects an appropriate .dev.js, .prod.js file on startup. server.dev.js uses webpack dev server. prod.js uses just express to host the static output in the dist folder.
25+
server.js selects an appropriate .dev.js, .prod.js file on startup. server.dev.js uses webpack dev server. prod.js is a very simple express app hosting static output in the dist folder.
3526

36-
src is where our application code lives. Components contain our presentational components such as TexBox, Button(s), ListItem etc. All components are decoupled from redux. With styles being loaded in ```import styles from './styles.css'``` by using [Css Modules](https://github.com/css-modules/css-modules). Everything a component behaves on is passed down into its props.
27+
src is where our application code lives. Components contain our presentational components such as TextBox, Button(s), ListItem etc. All components are decoupled from redux. With styles being loaded in ```import styles from './styles.css'``` by using [Css Modules](https://github.com/css-modules/css-modules). Redux provides us with top down rendering so we keep our components clean and stateless.
3728

38-
containers, react components that have very little if any styling. With the glue to pass down state to its child components. In here we're also making use of redux-forms higher order component to give us validation and state management as forms generally behave is the same way. This library has dramatically helped reduce boilerplate.
29+
containers (redux glue), react components that have very little if any styling. With the glue to pass down state to its child components. Within this directory we create our top level routes, such as home, about etc. In here we're also making use of redux-forms higher order component to give us validation and state management as forms generally behave is the same way. This library has dramatically helped reduce boilerplate.
3930

40-
actions contains our functions that are triggered by user actions or workers in our application.
31+
actions contains our functions that are triggered by user actions or workers in our application. We can use these actions later in our reducer tests.
4132

4233
### Testing our reducers
43-
This is trivial, redux has no coupling to the browser here, so we can test our application like a state machine.
34+
This is trivial, redux has no coupling to the browser here, so we can test our application like a state machine with simple input output. With tests running fast in node environment.
4435

4536
Example Test:
4637

@@ -88,7 +79,7 @@ export default createReducer(initialState, {
8879
})
8980
});
9081
```
91-
If you're interested in what createReducer does [see](https://github.com/joshgeller/react-redux-jwt-auth-example/blob/master/src/utils/index.js#L12) it gives a nicer switch structure.
82+
If you're interested in what createReducer does [see](https://github.com/joshgeller/react-redux-jwt-auth-example/blob/master/src/utils/index.js#L12) it gives a nicer switch structure. I quite like it.
9283

9384
### Testing react components with enzyme
9485
Enzyme gives us a simple jQuery like selector interface which is really powerful for asserting whats been rendered.
@@ -253,13 +244,13 @@ module.exports = (config) => {
253244
};
254245
```
255246

256-
you'll notice we're doing some funky stuff with sinon to stop it breaking require.
247+
you'll notice we're doing some funky stuff with sinon to stop it breaking require. Yeah we can share webpack.config here to reduce duplication but for this example we've copied and pasted it.
257248

258-
We can now test our react components in silo and with the power of PhantomJS, we're able to test our components extremly fast. Setup a ```npm run karma:watch``` task to run in the background whilst developing. Here we're currently testing a dumb presentational component, but next we can go ahead and test our redux containers for more functional testing.
249+
We can now test our react components in silo and with the power of PhantomJS, we're able to test our components very fast. Setup a ```npm run karma:watch``` task to run in the background whilst developing. Here we're currently testing a dumb presentational component, but we can go further and test our redux containers for more functional testing.
259250

260-
And thats it! in the coming week I'll release a bare bones boilerplate project, complete with postcss, hot module replacement with the testing config shown here.
251+
And thats it! in the coming week I'll release a bare bones boilerplate project, complete with postcss, hot module replacement and the testing config shown here.
261252

262-
voice you're opinion at me [@export_mike](https://twitter.com/@export_mike) on twitter or [@pebblecode](https://twitter.com/@pebblecode). Thanks for reading, oh! I almost forgot we're [hiring](http://pebblecode.com/careers/#job-1220)!
253+
voice you're opinion at me [@export_mike](https://twitter.com/@export_mike) on twitter or [@pebblecode](https://twitter.com/@pebblecode). I'm just scratching the surface of the react testing story, I'd be interested if you've had any pains or gains in testing your React components. Is this useful information? Let me know. Thanks for reading, oh! I almost forgot we're [hiring](http://pebblecode.com/careers/#job-1220)!
263254

264255

265256

0 commit comments

Comments
 (0)