Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neilff committed Aug 22, 2015
1 parent ea59b82 commit 6b27614
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Circle CI](https://circleci.com/gh/neilff/ng-redux-ui-router.svg?style=svg)](https://circleci.com/gh/neilff/ng-redux-ui-router)

#ng-redux-ui-router

[![Circle CI](https://circleci.com/gh/neilff/ng-redux-ui-router.svg?style=svg)](https://circleci.com/gh/neilff/ng-redux-ui-router)

This package contains a middleware for performing UI Router methods such as `$state.go` as well as a reducer for storing Angular UI state inside your store.

Inspired by [Redux React Router](https://github.com/acdlite/redux-react-router) by [Andrew Clark](https://github.com/acdlite)
40 changes: 40 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Karma configuration
// Generated on Sat Aug 22 2015 14:55:12 GMT-0400 (EDT)

module.exports = function(config) {
config.set({
basePath: './',
frameworks: ['mocha'],
files: [
'src/**/*.js'
],
exclude: [
],
plugins: [
'karma-babel-preprocessor',
'karma-mocha',
'karma-phantomjs-launcher'
],
preprocessors: {
'src/**/*.js': ['babel']
},
babelPreprocessor: {
options: {
sourceMap: 'inline'
},
filename: function (file) {
return file.originalPath.replace(/\.js$/, '.es5.js');
},
sourceFileName: function (file) {
return file.originalPath;
}
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: false
});
};
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
"dependencies": {
"angular": "^1.4.4",
"angular-ui-router": "^0.2.15",
"babel": "^5.8.21",
"immutable": "^3.7.4"
"immutable": "^3.7.4",
"redux": "1.0.0-alpha"
},
"devDependencies": {
"babel": "^5.8.21",
"babel-core": "5.6.15",
"babel-eslint": "^3.1.20",
"babel-loader": "^5.3.1",
"chai": "^3.0.0",
"eslint": "^0.24.0",
"mocha": "^2.2.5",
"node-libs-browser": "^0.5.2",
"redux": "1.0.0-alpha",
"sinon": "^1.15.4"
"q": "^1.4.1",
"sinon": "^1.16.1",
"sinon-as-promised": "^4.0.0"
}
}
2 changes: 2 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import chai from 'chai';
global.expect = chai.expect;
24 changes: 24 additions & 0 deletions src/__tests__/router-actions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'chai';
import routerActions from '../router-actions';

let $ngReduxMock = {
getStore: () => {
return {
dispatch: () => {}
};
}
};

describe('routerActions', () => {
it('should bind the action creators', () => {
let actions = routerActions($ngReduxMock);

expect(actions.onStateChangeStart).to.be.a('function');
expect(actions.onStateChangeSuccess).to.be.a('function');
expect(actions.onStateChangeError).to.be.a('function');
expect(actions.onStateNotFound).to.be.a('function');
expect(actions.stateGo).to.be.a('function');
expect(actions.stateReload).to.be.a('function');
expect(actions.stateTransitionTo).to.be.a('function');
});
});
35 changes: 35 additions & 0 deletions src/__tests__/router-state-reducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'chai';
import routerStateReducer from '../router-state-reducer';

describe('routerStateReducer', () => {
it('should return the initial state', () => {
let action = {
type: 'null',
payload: {}
};

let state = routerStateReducer(undefined, action);
expect(state.toJS().currentState).to.equal(null);
expect(state.toJS().currentParams).to.equal(null);
expect(state.toJS().prevState).to.equal(null);
expect(state.toJS().prevParams).to.equal(null);
});

it('should set the provided state if the $stateChangeSuccess type is used', () => {
let action = {
type: '@@reduxUiRouter/$stateChangeSuccess',
payload: {
currentState: 'currentState',
currentParams: 'currentParams',
prevState: 'prevState',
prevParams: 'prevParams'
}
};

let state = routerStateReducer(undefined, action);
expect(state.toJS().currentState).to.equal('currentState');
expect(state.toJS().currentParams).to.equal('currentParams');
expect(state.toJS().prevState).to.equal('prevState');
expect(state.toJS().prevParams).to.equal('prevParams');
});
});
15 changes: 15 additions & 0 deletions src/__tests__/state-change-error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'chai';
import stateChangeError from '../state-change-error';

describe('stateChangeError', () => {
it('should create an action with the provided params', () => {
let action = stateChangeError('evt', 'toState', 'toParams', 'fromState', 'fromParams', 'err');

expect(action.payload.toState).to.equal('toState');
expect(action.payload.toParams).to.equal('toParams');
expect(action.payload.fromState).to.equal('fromState');
expect(action.payload.fromParams).to.equal('fromParams');
expect(action.payload.err).to.equal('err');
expect(action.type).to.equal('@@reduxUiRouter/$stateChangeError');
});
});
14 changes: 14 additions & 0 deletions src/__tests__/state-change-start.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'chai';
import stateChangeStart from '../state-change-start';

describe('stateChangeStart', () => {
it('should create an action with the provided params', () => {
let action = stateChangeStart('evt', 'toState', 'toParams', 'fromState', 'fromParams');

expect(action.payload.toState).to.equal('toState');
expect(action.payload.toParams).to.equal('toParams');
expect(action.payload.fromState).to.equal('fromState');
expect(action.payload.fromParams).to.equal('fromParams');
expect(action.type).to.equal('@@reduxUiRouter/$stateChangeStart');
});
});
14 changes: 14 additions & 0 deletions src/__tests__/state-change-success.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'chai';
import stateChangeSuccess from '../state-change-success';

describe('stateChangeSuccess', () => {
it('should create an action with the provided params', () => {
let action = stateChangeSuccess('evt', 'currentState', 'currentParams', 'prevState', 'prevParams');

expect(action.payload.currentState).to.equal('currentState');
expect(action.payload.currentParams).to.equal('currentParams');
expect(action.payload.prevState).to.equal('prevState');
expect(action.payload.prevParams).to.equal('prevParams');
expect(action.type).to.equal('@@reduxUiRouter/$stateChangeSuccess');
});
});
20 changes: 20 additions & 0 deletions src/__tests__/state-go.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'chai';
import stateGo from '../state-go';

describe('stateGo', () => {
it('should create an action containing the state to go to', () => {
let action = stateGo('example', { test: 'hello' }, { option: 'world' });
expect(action.payload.to).to.equal('example');
expect(action.payload.params.test).to.equal('hello');
expect(action.payload.options.option).to.equal('world');
expect(action.type).to.equal('@@reduxUiRouter/stateGo');
});

it('should create an action when to params are undefined', () => {
let action = stateGo('example', undefined, undefined);
expect(action.payload.to).to.equal('example');
expect(action.payload.params).to.equal(undefined);
expect(action.payload.options).to.equal(undefined);
expect(action.type).to.equal('@@reduxUiRouter/stateGo');
});
});
85 changes: 85 additions & 0 deletions src/__tests__/state-middleware.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import 'chai';
import Q from 'q';
import sinon from 'sinon';
import 'sinon-as-promised';
import routerMiddleware from '../router-middleware';

import {
STATE_GO,
STATE_RELOAD,
STATE_TRANSITION_TO
} from '../action-types';

let $state;
let nextSpy;

describe('routerMiddleware', () => {
beforeEach(() => {
$state = {
go: () => {},
reload: () => {},
transitionTo: () => {}
};

$state.go = sinon.stub().resolves('foo');
$state.reload = sinon.stub().resolves('foo');
$state.transitionTo = sinon.stub().resolves('foo');

nextSpy = sinon.stub();
});

it('should call the next action if the router middleware doesn\'t care about it', () => {
let middleware = routerMiddleware($state)({})(nextSpy);

middleware({
payload: {}
});

expect(nextSpy.called).to.equal(true);
expect($state.go.called).to.equal(false);
expect($state.reload.called).to.equal(false);
expect($state.transitionTo.called).to.equal(false);
});

it('should call $state.go if the STATE_GO action is sent', () => {
let middleware = routerMiddleware($state)({})(nextSpy);

middleware({
type: STATE_GO,
payload: {}
});

expect(nextSpy.called).to.equal(true);
expect($state.go.called).to.equal(true);
expect($state.reload.called).to.equal(false);
expect($state.transitionTo.called).to.equal(false);
});

it('should call $state.reload if the STATE_GO action is sent', () => {
let middleware = routerMiddleware($state)({})(nextSpy);

middleware({
type: STATE_RELOAD,
payload: {}
});

expect(nextSpy.called).to.equal(true);
expect($state.reload.called).to.equal(true);
expect($state.go.called).to.equal(false);
expect($state.transitionTo.called).to.equal(false);
});

it('should call $state.transitionTo if the STATE_GO action is sent', () => {
let middleware = routerMiddleware($state)({})(nextSpy);

middleware({
type: STATE_TRANSITION_TO,
payload: {}
});

expect(nextSpy.called).to.equal(true);
expect($state.transitionTo.called).to.equal(true);
expect($state.go.called).to.equal(false);
expect($state.reload.called).to.equal(false);
});
});
14 changes: 14 additions & 0 deletions src/__tests__/state-not-found.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'chai';
import stateNotFound from '../state-not-found';

describe('stateNotFound', () => {
it('should create an action with the provided params', () => {
let action = stateNotFound('evt', 'unfoundState', 'fromState', 'fromParams');

expect(action.payload.evt).to.equal('evt');
expect(action.payload.unfoundState).to.equal('unfoundState');
expect(action.payload.fromState).to.equal('fromState');
expect(action.payload.fromParams).to.equal('fromParams');
expect(action.type).to.equal('@@reduxUiRouter/$stateNotFound');
});
});
10 changes: 10 additions & 0 deletions src/__tests__/state-reload.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'chai';
import stateReload from '../state-reload';

describe('stateReload', () => {
it('should create an action containing the state to reload', () => {
let action = stateReload('example');
expect(action.payload).to.equal('example');
expect(action.type).to.equal('@@reduxUiRouter/stateReload');
});
});
20 changes: 20 additions & 0 deletions src/__tests__/state-transition-to.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'chai';
import stateTransitionTo from '../state-transition-to';

describe('stateTransitionTo', () => {
it('should create an action containing the state to transition to', () => {
let action = stateTransitionTo('example', { test: 'hello' }, { option: 'world' });
expect(action.payload.to).to.equal('example');
expect(action.payload.toParams.test).to.equal('hello');
expect(action.payload.options.option).to.equal('world');
expect(action.type).to.equal('@@reduxUiRouter/transitionTo');
});

it('should create an action when to params are undefined', () => {
let action = stateTransitionTo('example', undefined, undefined);
expect(action.payload.to).to.equal('example');
expect(action.payload.toParams).to.equal(undefined);
expect(action.payload.options).to.equal(undefined);
expect(action.type).to.equal('@@reduxUiRouter/transitionTo');
});
});

0 comments on commit 6b27614

Please sign in to comment.