Skip to content
Open

Tests #187

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
}
]
]
]
],
"env": {
"test": {
"plugins": ["istanbul"]
}
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ umd
node_modules
npm-debug.log
coverage
.nyc_output
48 changes: 46 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,38 @@
"build-es6": "NODE_ENV=production webpack --output-library-target commonjs2 src/index.js ./es6/index.js",
"build-umd": "NODE_ENV=production webpack src/index.js umd/stormpath-sdk-react.js",
"build-min": "NODE_ENV=production webpack -p src/index.js umd/stormpath-sdk-react.min.js",
"build-dist": "NODE_ENV=production webpack src/index.js dist/stormpath-sdk-react.js && NODE_ENV=production webpack -p src/index.js dist/stormpath-sdk-react.min.js"
"build-dist": "NODE_ENV=production webpack src/index.js dist/stormpath-sdk-react.js && NODE_ENV=production webpack -p src/index.js dist/stormpath-sdk-react.min.js",
"test": "better-npm-run test",
"test:watch": "mocha -w test/helpers/browser.js test/*.spec.js test/**/*.spec.js"
},
"betterScripts": {
"test": {
"env": {
"NODE_ENV": "test"
},
"command": "nyc _mocha --report html -- test/helpers/browser.js test/*.spec.js test/**/*.spec.js"
}
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.18.2",
"babel-eslint": "^7.1.0",
"babel-loader": "^6.2.7",
"babel-plugin-dev-expression": "^0.2.1",
"babel-plugin-istanbul": "^4.0.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-es2015-classes": "^6.18.0",
"babel-plugin-transform-proto-to-assign": "^6.9.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"babel-register": "^6.23.0",
"better-npm-run": "0.0.14",
"bundle-loader": "^0.5.4",
"chai": "^3.5.0",
"chai-spies": "^0.7.1",
"css-loader": "^0.25.0",
"enzyme": "^2.7.1",
"eslint": "^3.9.1",
"eslint-plugin-react": "^6.6.0",
"fbjs": "^0.8.5",
Expand All @@ -50,12 +66,16 @@
"history": "^2.1.2",
"invariant": "^2.2.1",
"isparta-loader": "^2.0.0",
"istanbul": "^0.4.5",
"jsdom": "^9.11.0",
"keymirror": "^0.1.1",
"mocha": "^3.2.0",
"nyc": "^10.1.2",
"pretty-bytes": "^4.0.2",
"qs": "^6.3.0",
"react": "^15.3.2",
"react-addons-css-transition-group": "^15.3.2",
"react-addons-test-utils": "15.3.2",
"react-addons-test-utils": "^15.3.2",
"react-dom": "^15.3.2",
"react-router": "^3.0.0",
"react-static-container": "^1.0.1",
Expand All @@ -80,5 +100,29 @@
],
"dependencies": {
"xtend": "^4.0.1"
},
"nyc": {
"lines": "20",
"statements": "20",
"branches": "20",
"functions": "20",
"sourceMap": false,
"instrument": false,
"include": "src/**/*.js",
"exclude": [
"dist/**/*",
"es6/**/*",
"lib/**/*",
"test/**/*",
"umd/**/*"
],
"reporter": [
"lcov",
"text-summary"
],
"check-coverage": true,
"require": [
"./test/helpers/browser.js"
]
}
}
16 changes: 13 additions & 3 deletions src/actions/TokenActions.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import context from './../context';
import TokenConstants from './../constants/TokenConstants';

/* istanbul ignore next */
function dispatch(event) {
setTimeout(() => {
context.getDispatcher().dispatch(event);
}, 0);
}

class TokenActions {
constructor(dispatch) {
this.dispatch = dispatch;
}

// Allows for setting mock dispatch in tests
setDispatch(dispatch) {
this.dispatch = dispatch;
}

set(type, token, callback) {
dispatch({
this.dispatch({
type: TokenConstants.TOKEN_SET,
options: {
type: type,
Expand All @@ -20,7 +30,7 @@ class TokenActions {
}

refresh(token, callback) {
dispatch({
this.dispatch({
type: TokenConstants.TOKEN_REFRESH,
options: {
token: token
Expand All @@ -30,4 +40,4 @@ class TokenActions {
}
}

export default new TokenActions()
export default new TokenActions(dispatch)
1 change: 1 addition & 0 deletions src/components/Authenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class Authenticated extends React.Component {
let authenticated = user !== undefined;

if (authenticated && this.props.inGroup) {
/* istanbul ignore else */
if (user.groups) {
authenticated = utils.groupsMatchExpression(user.groups, this.props.inGroup);
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class Context {
this.tokenStore = tokenStore;
}

getTokenStore() {
return this.tokenStore;
}

setSessionStore(sessionStore) {
this.sessionStore = sessionStore;
}
Expand Down
7 changes: 5 additions & 2 deletions src/dispatchers/FluxDispatcher.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Dispatcher } from 'flux';

// Extracted for testability
const defaultDispatcher = new Dispatcher();

export default class FluxDispatcher {
constructor(reducer) {
this.dispatcher = new Dispatcher();
constructor(reducer, dispatcher = defaultDispatcher) {
this.dispatcher = dispatcher;
this.register(reducer);
}

Expand Down
Empty file removed test/ContextTest.js
Empty file.
54 changes: 54 additions & 0 deletions test/actions/TokenActions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import TokenActions from '../../src/actions/TokenActions';
import TokenConstants from '../../src/constants/TokenConstants';
import chai, {expect} from 'chai';
import spies from 'chai-spies';

chai.use(spies);

describe('TokenActions', () => {
let dispatchSpy;
let callback;

beforeEach(() => {
dispatchSpy = chai.spy(({callback}) => callback());
callback = () => ({});

TokenActions.setDispatch(dispatchSpy);
});

describe('set action', () => {
it('should dispatch a token set action', () => {
const type = 'sometype';
const token = 'sometoken';

TokenActions.set(type, token, callback);

expect(dispatchSpy).to.have.been.calledOnce;
expect(dispatchSpy).to.have.been.called.with({
type: TokenConstants.TOKEN_SET,
options: {
type,
token,
},
callback,
});
});
});

describe('refresh action', () => {
it('should dispatch a refresh action', () => {
const token = 'sometoken';

TokenActions.refresh(token, callback);

expect(dispatchSpy).to.have.been.calledOnce;
expect(dispatchSpy).to.have.been.called.with({
type: TokenConstants.TOKEN_REFRESH,
options: {
token,
},
callback,
});
});
});
});
82 changes: 82 additions & 0 deletions test/components/Authenticated.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React from 'react';
import {shallow} from 'enzyme';
import {expect} from 'chai';

import Authenticated from '../../src/components/Authenticated';

describe('<Authenticated/>', () => {
let userContext;
let noUserContext;

before(() => {
userContext = {
user: {
groups: {
basic: true,
unbasic: true
}
}
};

noUserContext = {};
});

describe('without inGroup property', () => {
it('should render content if there is a user', () => {
const element = shallow(
<Authenticated>Shown</Authenticated>,
{context: userContext}
);

expect(element).to.be.ok;
expect(element.text()).to.equal('Shown');
});

it('should not render content if there is no user', () => {
const element = shallow(
<Authenticated>Not Shown</Authenticated>,
{context: noUserContext}
);

expect(element).to.be.ok;
expect(element.text()).not.to.be.ok;
expect(element.text()).not.to.equal('Not Shown');
});
});


describe('with inGroup property', () => {
it('should not render content if there is no user', () => {
const element = shallow(
<Authenticated>Not Shown</Authenticated>,
{context: noUserContext}
);

expect(element).to.be.ok;
expect(element.text()).not.to.be.ok;
expect(element.text()).not.to.equal('Not Shown');
});

it('should not render content if the user is not in the group', () => {
const element = shallow(
<Authenticated inGroup="admin">Not Shown</Authenticated>,
{context: userContext}
);

expect(element).to.be.ok;
expect(element.text()).not.to.be.ok;
expect(element.text()).not.to.equal('Not Shown');
});

it('should render content if the user is in the group', () => {
const element = shallow(
<Authenticated inGroup="basic">Shown</Authenticated>,
{context: userContext}
);

expect(element).to.be.ok;
expect(element.text()).to.be.ok;
expect(element.text()).to.equal('Shown');
});
});
});
38 changes: 38 additions & 0 deletions test/context.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import chai, {expect} from 'chai';

import context from '../src/context';

describe('Context', () => {
it('should have empty initial values', () => {
expect(context.router).not.to.be.ok;
expect(context.dispatcher).not.to.be.ok;
expect(context.tokenStore).not.to.be.ok;
expect(context.sessionStore).not.to.be.ok;
expect(context.userStore).not.to.be.ok;
});

it('should have setters for all values', () => {
context.setRouter('router');
expect(context.router).to.equal('router');

context.setDispatcher('dispatcher');
expect(context.dispatcher).to.equal('dispatcher');

context.setTokenStore('tokenStore');
expect(context.tokenStore).to.equal('tokenStore');

context.setSessionStore('sessionStore');
expect(context.sessionStore).to.equal('sessionStore');

context.setUserStore('userStore');
expect(context.userStore).to.equal('userStore');
});

it('should have getters for all values', () => {
expect(context.router).to.equal(context.getRouter());
expect(context.dispatcher).to.equal(context.getDispatcher());
expect(context.tokenStore).to.equal(context.getTokenStore());
expect(context.sessionStore).to.equal(context.getSessionStore());
expect(context.userStore).to.equal(context.getUserStore());
});
});
Loading