Skip to content

Commit 18ad19c

Browse files
authored
Merge pull request #7 from AllenFang/redux-test
Redux Tests
2 parents 69d2397 + 81a0dac commit 18ad19c

27 files changed

+945
-144
lines changed

.travis.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
sudo: required
22
language: node_js
33
node_js:
4-
- "4.1"
5-
- "5.3"
64
- "6.0"
75
branches:
86
only:
9-
- master
7+
- phase-1
108
env:
119
global:
1210
- CXX=g++-4.8
@@ -19,13 +17,15 @@ addons:
1917
- ubuntu-toolchain-r-test
2018
packages:
2119
- g++-4.8
22-
before_script:
20+
before_install:
2321
- npm cache clean
24-
- npm install -g grunt-cli
2522
- if [ "$GIT_VERSION" = "edge" ]; then sudo add-apt-repository ppa:git-core/ppa -y && sudo apt-get update -q && sudo apt-get install -y git; fi
2623
- git config --global user.email "test@testy.com"
2724
- git config --global user.name "Test testy"
2825
- git version
29-
- grunt -d
30-
after_script:
31-
- grunt travisnpmpublish
26+
install:
27+
- npm install
28+
29+
script:
30+
- node -v
31+
- npm run test-react

config/localStorageMock.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class LocalStorageMock {
2+
constructor() {
3+
this.store = {};
4+
}
5+
6+
clear() {
7+
this.store = {};
8+
}
9+
10+
getItem(key) {
11+
return this.store[key];
12+
}
13+
14+
setItem(key, value) {
15+
this.store[key] = value.toString();
16+
}
17+
18+
removeItem(key) {
19+
delete this.store[key];
20+
}
21+
};
22+
23+
global.localStorage = new LocalStorageMock;

package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"react-dom": "^15.5.4",
6262
"react-redux": "^5.0.4",
6363
"redux": "^3.6.0",
64-
"redux-thunk": "^2.2.0",
64+
"redux-api-middleware": "^1.0.3",
6565
"rimraf": "~2.6.1",
6666
"semver": "~5.3.0",
6767
"serve-static": "~1.12.2",
@@ -125,13 +125,16 @@
125125
"jest": "18.1.0",
126126
"json-loader": "0.5.4",
127127
"mocha": "~3.3.0",
128+
"nock": "^9.0.13",
128129
"node-sass": "^4.5.3",
129130
"object-assign": "4.1.1",
130131
"phantomjs-prebuilt": "~2.1.14",
131132
"postcss-loader": "1.2.2",
132133
"promise": "7.1.1",
133134
"react-dev-utils": "^0.5.2",
135+
"redux-mock-store": "^1.2.3",
134136
"sass-loader": "^6.0.5",
137+
"sinon": "^2.3.4",
135138
"style-loader": "0.13.1",
136139
"supertest": "~3.0.0",
137140
"url-loader": "0.5.7",
@@ -145,10 +148,11 @@
145148
"src/**/*.{js,jsx}"
146149
],
147150
"setupFiles": [
148-
"<rootDir>/config/polyfills.js"
151+
"<rootDir>/config/polyfills.js",
152+
"<rootDir>/config/localStorageMock.js"
149153
],
150154
"testPathIgnorePatterns": [
151-
"<rootDir>[/\\\\](build|docs|node_modules|scripts)[/\\\\]"
155+
"<rootDir>[/\\\\](build|docs|node_modules|scripts|clicktests)[/\\\\]"
152156
],
153157
"testEnvironment": "node",
154158
"testURL": "http://localhost",
@@ -162,7 +166,10 @@
162166
],
163167
"moduleNameMapper": {
164168
"^react-native$": "react-native-web"
165-
}
169+
},
170+
"modulePaths": [
171+
"src-react"
172+
]
166173
},
167174
"babel": {
168175
"presets": [

scripts/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
'use strict';
2+
const regeneratorRuntime = require('babel-runtime/regenerator');
3+
4+
if (!regeneratorRuntime.default) {
5+
regeneratorRuntime.default = regeneratorRuntime;
6+
}
27

38
process.env.NODE_ENV = 'test';
49
process.env.PUBLIC_URL = '';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { CALL_API } from 'redux-api-middleware';
2+
import expect from 'expect.js';
3+
4+
import * as types from 'constants/action-types';
5+
import { fetchUngitConfig } from 'actions/ungit-config';
6+
7+
describe('ungit-config.js action', () => {
8+
describe('fetchUngitConfig', () => {
9+
it('returns CALL_API action object', function(){
10+
const action = fetchUngitConfig();
11+
12+
expect(action[CALL_API]).to.be.an('object');
13+
expect(action[CALL_API].endpoint).to.eql('http://localhost:8448/ungit/config');
14+
expect(action[CALL_API].method).to.eql('GET');
15+
expect(action[CALL_API].types).to.be.an('array');
16+
expect(action[CALL_API].types[0]).to.eql(types.FETCH_UNGIT_CONFIG_REQUEST);
17+
expect(action[CALL_API].types[1]).to.be.an('object');
18+
expect(action[CALL_API].types[1].type).to.eql(types.FETCH_UNGIT_CONFIG_SUCCESS);
19+
expect(action[CALL_API].types[2]).to.eql(types.FETCH_UNGIT_CONFIG_FAILURE);
20+
});
21+
});
22+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { CALL_API } from 'redux-api-middleware';
2+
import expect from 'expect.js';
3+
4+
import * as types from 'constants/action-types';
5+
import { fetchUserConfig } from 'actions/user-config';
6+
7+
describe('user-config.js action', () => {
8+
describe('fetchUserConfig', () => {
9+
it('returns CALL_API action object', function(){
10+
const action = fetchUserConfig();
11+
12+
expect(action[CALL_API]).to.be.an('object');
13+
expect(action[CALL_API].endpoint).to.eql('http://localhost:8448/api/userconfig');
14+
expect(action[CALL_API].method).to.eql('GET');
15+
expect(action[CALL_API].types).to.be.an('array');
16+
expect(action[CALL_API].types[0]).to.eql(types.FETCH_USER_CONFIG_REQUEST);
17+
expect(action[CALL_API].types[1]).to.eql(types.FETCH_USER_CONFIG_SUCCESS);
18+
expect(action[CALL_API].types[2]).to.eql(types.FETCH_USER_CONFIG_FAILURE);
19+
});
20+
});
21+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { CALL_API } from 'redux-api-middleware';
2+
import expect from 'expect.js';
3+
4+
import * as types from 'constants/action-types';
5+
import { fetchLatestVersion, fetchGitVersion } from 'actions/version';
6+
7+
describe('version.js action', () => {
8+
describe('fetchLatestVersion', () => {
9+
it('returns CALL_API action object', function(){
10+
const action = fetchLatestVersion();
11+
12+
expect(action[CALL_API]).to.be.an('object');
13+
expect(action[CALL_API].endpoint).to.eql('http://localhost:8448/api/latestversion');
14+
expect(action[CALL_API].method).to.eql('GET');
15+
expect(action[CALL_API].types).to.be.an('array');
16+
expect(action[CALL_API].types[0]).to.eql(types.FETCH_LATEST_VERSION_REQUEST);
17+
expect(action[CALL_API].types[1]).to.eql(types.FETCH_LATEST_VERSION_SUCCESS);
18+
expect(action[CALL_API].types[2]).to.eql(types.FETCH_LATEST_VERSION_FAILURE);
19+
});
20+
});
21+
22+
describe('fetchGitVersion', () => {
23+
it('returns CALL_API action object', function(){
24+
const action = fetchGitVersion();
25+
26+
expect(action[CALL_API]).to.be.an('object');
27+
expect(action[CALL_API].endpoint).to.eql('http://localhost:8448/api/gitversion');
28+
expect(action[CALL_API].method).to.eql('GET');
29+
expect(action[CALL_API].types).to.be.an('array');
30+
expect(action[CALL_API].types[0]).to.eql(types.FETCH_GIT_VERSION_REQUEST);
31+
expect(action[CALL_API].types[1]).to.eql(types.FETCH_GIT_VERSION_SUCCESS);
32+
expect(action[CALL_API].types[2]).to.eql(types.FETCH_GIT_VERSION_FAILURE);
33+
});
34+
});
35+
});
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import nock from 'nock';
2+
import sinon from 'sinon';
3+
import expect from 'expect.js';
4+
import configureStore from 'redux-mock-store';
5+
import { createStore, applyMiddleware } from 'redux';
6+
import { apiMiddleware, CALL_API } from 'redux-api-middleware';
7+
8+
import * as types from 'constants/action-types';
9+
import { fetchUngitConfig } from 'actions/ungit-config';
10+
11+
const createMockStore = configureStore([ apiMiddleware ]);
12+
13+
describe('redux-api-middleware::ungit-config', () => {
14+
describe('fetchUngitConfig successfully', () => {
15+
const mockPayload = { message: 'success' };
16+
17+
beforeEach(() => {
18+
nock('http://localhost:8448')
19+
.get('/ungit/config')
20+
.reply(200, mockPayload);
21+
22+
nock('http://localhost:8448')
23+
.get('/api/userconfig')
24+
.reply(200, mockPayload);
25+
});
26+
27+
it('should dispatch \'FETCH_UNGIT_CONFIG_REQUEST\'', done => {
28+
const store = createMockStore({});
29+
30+
store.subscribe(() => {
31+
const dispatchedActions = store.getActions();
32+
if (dispatchedActions.length === 1) {
33+
expect(dispatchedActions[0].type).to.eql(types.FETCH_UNGIT_CONFIG_REQUEST);
34+
done();
35+
}
36+
});
37+
store.dispatch(fetchUngitConfig());
38+
});
39+
40+
it('should dispatch \'FETCH_UNGIT_CONFIG_SUCCESS\'', done => {
41+
const store = createMockStore({});
42+
43+
store.subscribe(() => {
44+
const dispatchedActions = store.getActions();
45+
if (dispatchedActions.length === 2) {
46+
expect(dispatchedActions[0].type).to.eql(types.FETCH_UNGIT_CONFIG_REQUEST);
47+
expect(dispatchedActions[1].type).to.eql(types.FETCH_UNGIT_CONFIG_SUCCESS);
48+
expect(dispatchedActions[1].payload).to.eql(mockPayload);
49+
done();
50+
}
51+
});
52+
store.dispatch(fetchUngitConfig());
53+
});
54+
});
55+
56+
describe('fetchUngitConfig fails', () => {
57+
58+
beforeEach(() => {
59+
nock('http://localhost:8448')
60+
.get('/ungit/config')
61+
.reply(500, null);
62+
});
63+
64+
it('should dispatch \'FETCH_UNGIT_CONFIG_FAILURE\'', done => {
65+
const store = createMockStore({});
66+
67+
store.subscribe(() => {
68+
const dispatchedActions = store.getActions();
69+
if (dispatchedActions.length === 2) {
70+
expect(dispatchedActions[0].type).to.eql(types.FETCH_UNGIT_CONFIG_REQUEST);
71+
expect(dispatchedActions[1].type).to.eql(types.FETCH_UNGIT_CONFIG_FAILURE);
72+
expect(dispatchedActions[1].payload).to.be.an('object');
73+
done();
74+
}
75+
});
76+
store.dispatch(fetchUngitConfig());
77+
});
78+
});
79+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import nock from 'nock';
2+
import sinon from 'sinon';
3+
import expect from 'expect.js';
4+
import configureStore from 'redux-mock-store';
5+
import { createStore, applyMiddleware } from 'redux';
6+
import { apiMiddleware, CALL_API } from 'redux-api-middleware';
7+
8+
import * as types from 'constants/action-types';
9+
import { fetchUserConfig } from 'actions/user-config';
10+
11+
const createMockStore = configureStore([ apiMiddleware ]);
12+
13+
describe('redux-api-middleware::user-config', () => {
14+
describe('fetchUserConfig successfully', () => {
15+
const mockPayload = { message: 'success' };
16+
17+
beforeEach(() => {
18+
nock('http://localhost:8448')
19+
.get('/api/userconfig')
20+
.reply(200, mockPayload);
21+
});
22+
23+
it('should dispatch \'FETCH_USER_CONFIG_REQUEST\'', done => {
24+
const store = createMockStore({});
25+
26+
store.subscribe(() => {
27+
const dispatchedActions = store.getActions();
28+
if (dispatchedActions.length === 1) {
29+
expect(dispatchedActions[0].type).to.eql(types.FETCH_USER_CONFIG_REQUEST);
30+
done();
31+
}
32+
});
33+
store.dispatch(fetchUserConfig());
34+
});
35+
36+
it('should dispatch \'FETCH_USER_CONFIG_SUCCESS\'', done => {
37+
const store = createMockStore({});
38+
39+
store.subscribe(() => {
40+
const dispatchedActions = store.getActions();
41+
if (dispatchedActions.length === 2) {
42+
expect(dispatchedActions[0].type).to.eql(types.FETCH_USER_CONFIG_REQUEST);
43+
expect(dispatchedActions[1].type).to.eql(types.FETCH_USER_CONFIG_SUCCESS);
44+
expect(dispatchedActions[1].payload).to.eql(mockPayload);
45+
done();
46+
}
47+
});
48+
store.dispatch(fetchUserConfig());
49+
});
50+
});
51+
52+
describe('fetchUserConfig fails', () => {
53+
54+
beforeEach(() => {
55+
nock('http://localhost:8448')
56+
.get('/api/userconfig')
57+
.reply(500, null);
58+
});
59+
60+
it('should dispatch \'FETCH_USER_CONFIG_FAILURE\'', done => {
61+
const store = createMockStore({});
62+
63+
store.subscribe(() => {
64+
const dispatchedActions = store.getActions();
65+
if (dispatchedActions.length === 2) {
66+
expect(dispatchedActions[0].type).to.eql(types.FETCH_USER_CONFIG_REQUEST);
67+
expect(dispatchedActions[1].type).to.eql(types.FETCH_USER_CONFIG_FAILURE);
68+
expect(dispatchedActions[1].payload).to.be.an('object');
69+
done();
70+
}
71+
});
72+
store.dispatch(fetchUserConfig());
73+
});
74+
});
75+
});

0 commit comments

Comments
 (0)