Skip to content

Commit

Permalink
add test examples for thunks and components
Browse files Browse the repository at this point in the history
  • Loading branch information
tmkelly28 committed Jul 25, 2017
1 parent 003e77d commit 4d0070c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
2 changes: 1 addition & 1 deletion client/components/user-home.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {connect} from 'react-redux'
/**
* COMPONENT
*/
const UserHome = (props) => {
export const UserHome = (props) => {

const {email} = props

Expand Down
17 changes: 17 additions & 0 deletions client/components/user-home.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {expect} from 'chai'
import React from 'react'
import {shallow} from 'enzyme'
import {UserHome} from './user-home'

describe('UserHome', () => {

let userHome

beforeEach(() => {
userHome = shallow(<UserHome email={'[email protected]'} />)
})

it('renders the email in an h3', () => {
expect(userHome.find('h3').text()).to.be.equal('Welcome, [email protected]')
})
})
3 changes: 2 additions & 1 deletion client/history.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import createHistory from 'history/createBrowserHistory';
import createMemoryHistory from 'history/createMemoryHistory'

const history = createHistory();
const history = process.env.NODE_ENV === 'test' ? createMemoryHistory() : createHistory()

export default history;

56 changes: 56 additions & 0 deletions client/store/user.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {expect} from 'chai'
import user, {me, logout} from './user'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import configureMockStore from 'redux-mock-store'
import thunkMiddleware from 'redux-thunk'
import history from '../history'

const mockAxios = new MockAdapter(axios)
const middlewares = [thunkMiddleware]
const mockStore = configureMockStore(middlewares)

describe('thunk creators', () => {

let store

const initialState = {
user: {}
}

beforeEach(() => {
store = mockStore(initialState)
})

afterEach(() => {
store.clearActions()
})

describe('me', () => {
it('eventually dispatches the GET USER action', () => {

const fakeUser = {email: 'Cody'}
mockAxios.onGet('/auth/me').replyOnce(200, fakeUser)
return store.dispatch(me())
.then(() => {
const actions = store.getActions()
expect(actions[0].type).to.be.equal('GET_USER')
expect(actions[0].user).to.be.deep.equal(fakeUser)
})
})
})

describe('logout', () => {
it('logout: eventually dispatches the REMOVE_USER action', () => {
const fakeUser = {email: 'Cody'}
mockAxios.onPost('/auth/logout').replyOnce(204)
return store.dispatch(logout())
.then(() => {
const actions = store.getActions()
expect(actions[0].type).to.be.equal('REMOVE_USER')
expect(history.location.pathname).to.be.equal('/login')
})
})
})
})

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"start": "node server",
"start-dev": "npm run build-client-watch & npm run start-server",
"start-server": "NODE_ENV='development' nodemon server -e html,js,scss --ignore public",
"test": "DATABASE_URL='postgres://localhost:5432/boilermaker-test' mocha ./**/*.spec.js --compilers js:babel-register"
"test": "NODE_ENV='test' DATABASE_URL='postgres://localhost:5432/boilermaker-test' mocha ./**/*.spec.js --compilers js:babel-register"
},
"author": "",
"license": "ISC",
Expand Down Expand Up @@ -39,6 +39,7 @@
"socket.io": "^2.0.3"
},
"devDependencies": {
"axios-mock-adapter": "^1.9.0",
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-preset-es2015": "^6.22.0",
Expand All @@ -51,6 +52,8 @@
"file-loader": "^0.11.2",
"mocha": "^3.3.0",
"node-sass": "^4.5.0",
"react-test-renderer": "^15.6.1",
"redux-mock-store": "^1.2.3",
"sass-loader": "^6.0.0",
"style-loader": "^0.13.1",
"supertest": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = app
* keys as environment variables, so that they can still be read by the
* Node process on process.env
*/
if (process.env.NODE_ENV === 'development') require('../secrets')
if (process.env.NODE_ENV !== 'production') require('../secrets')


// passport registration
Expand Down

0 comments on commit 4d0070c

Please sign in to comment.