Testing Actions
We'll test Redux actions in our Weather app that's built with React and Redux.
We'll cover the following...
Create a new file called actions.test.js
in the src/__tests__ /
folder. (create that if you haven’t already) Let’s start by testing the good ol’ changeLocation
action. Add the default structure, we’ll need to import the action we want to test and describe
“actions” and changeLocation
:
// actions.test.js
import {
changeLocation
} from '../actions';
describe('actions', function() {
describe('changeLocation', function () {
});
});
There’s two things we want to verify of our action function: that it has the correct type and that it passes on the data we tell it to pass on. Let’s verify the type of the changeLocation
action is 'CHANGE_LOCATION'
:
// actions.test.js
/* … */
describe('changeLocation', function () {
it('should have a type of "CHANGE_LOCATION"', function() {
expect(changeLocation().type).toEqual('CHANGE_LOCATION');
});
});
Run npm run test
in the console and this is what you should see:
PASS src/__tests__/actions.test.js (0.525s)
1 test passed (1 total in 1 test suite, run time 0.55s)
Nice, let’s verify that it passes on the location we pass into it:
// actions.test.js
/* … */
describe('changeLocation', function () {
it('should have a type of "CHANGE_LOCATION"', function() {
expect(changeLocation().type).toEqual('CHANGE_LOCATION');
});
it('should pass
...