Skip to content

Commit

Permalink
Refactor reducer, fix tests
Browse files Browse the repository at this point in the history
- returns an object, not an array like other components
  • Loading branch information
mattwr18 committed Jan 18, 2019
1 parent 2d6d72e commit d3be540
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 52 deletions.
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"presets": ["react", "env"],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread",
["transform-runtime", { "polyfill": false, "regenerator": true }],
["emotion"]
]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"axios": "^0.18.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"global": "^4.3.2",
"izitoast": "^1.4.0",
"react": "^16.7.0",
Expand Down
3 changes: 1 addition & 2 deletions src/actions/postLogInInfoAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ export let getUser = user => ({ type: POST_LOGIN_INFO, payload: user })
export let postLogInInfo = props => dispatch => {
return axios({
method: 'post',
url: 'http://localhost:3000/users/sign_in',
url: '/users/sign_in',
data: {
user: {
email: props.email,
password: props.password
}
}
}).then(response => {
console.log(response.data)
dispatch(getUser(response.data))
})
}
14 changes: 2 additions & 12 deletions src/containers/LogIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@ import '../assets/LogIn.scss'
export class LogIn extends Component {
state = {
email: '',
password: '',
loggedInUser: []
}

static getDerivedStateFromProps (props, state) {
if (props.loggedInUser.length === state.loggedInUser.length) {
return null
}
return {
loggedInUser: props.loggedInUser
}
password: ''
}

handleChange = (e, { name, value }) => this.setState({ [name]: value });
Expand All @@ -31,7 +21,7 @@ export class LogIn extends Component {
iziToast.show({
theme: 'light',
title: 'Success',
message: 'Welcome back, ' + `${this.state.loggedInUser[0].slug}`,
message: 'Welcome back, ' + `${this.props.loggedInUser.slug}`,
position: 'topRight',
color: 'green',
backgroundColor: 'lime',
Expand Down
2 changes: 1 addition & 1 deletion src/reducers/initialState.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
users: [],
loggedInUser: []
loggedInUser: {}
}
2 changes: 1 addition & 1 deletion src/reducers/loggedInUserReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import initialState from './initialState'
const loggedInUserReducer = (state = initialState.loggedInUser, action) => {
switch (action.type) {
case POST_LOGIN_INFO:
return [ ...state, action.payload ]
return { ...action.payload }
default:
return state
}
Expand Down
17 changes: 17 additions & 0 deletions src/tests/reducers/loggedInUserReducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import loggedInUserReducer from '../../reducers/loggedInUserReducer'
import { POST_LOGIN_INFO } from '../../types'

describe('reduces a user', () => {
it('defaults to empty projects if none are passed in', () => {
expect(loggedInUserReducer(undefined, {})).toEqual({})
})

it('reduces the signed in user', () => {
expect(
loggedInUserReducer([], {
type: POST_LOGIN_INFO,
payload: { id: 1, email: 'some.email@example.com' }
})
).toEqual({ id: 1, email: 'some.email@example.com' })
})
})
11 changes: 1 addition & 10 deletions src/tests/reducers/usersReducer.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import usersReducer from '../../reducers/usersReducer'
import { GET_USERS, POST_LOGIN_INFO } from '../../types'
import { GET_USERS } from '../../types'

describe('reduces users', () => {
it('defaults to empty projects if none are passed in', () => {
Expand All @@ -14,13 +14,4 @@ describe('reduces users', () => {
})
).toEqual(['User to be added to store'])
})

it('reduces incoming user after successful login', () => {
expect(
usersReducer([], {
type: POST_LOGIN_INFO,
payload: ['User to be added to store']
})
).toEqual(['User to be added to store'])
})
})
15 changes: 15 additions & 0 deletions src/tests/store/loggedInUser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import store from '../../store'

describe('Store', () => {
it('should update after login', async () => {
await store.dispatch({
type: 'POST_LOGIN_INFO',
payload: { id: 2, email: 'someEmail@example.com' }
})

expect(store.getState()).toEqual({
loggedInUser: { id: 2, email: 'someEmail@example.com' },
users: []
})
})
})
25 changes: 0 additions & 25 deletions src/tests/store/store.test.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/tests/store/users.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import store from '../../store'

describe('Store', () => {
it('should update', async () => {
await store.dispatch({
type: 'GET_USERS',
payload: ['Run the tests']
})

expect(store.getState()).toEqual({
users: ['Run the tests'],
loggedInUser: {}
})
})
})
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ babel-plugin-syntax-jsx@^6.18.0, babel-plugin-syntax-jsx@^6.3.13, babel-plugin-s
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946"
integrity sha1-CvMqmm4Tyno/1QaeYtew9Y0NiUY=

babel-plugin-syntax-object-rest-spread@^6.13.0:
babel-plugin-syntax-object-rest-spread@^6.13.0, babel-plugin-syntax-object-rest-spread@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5"
integrity sha1-/WU28rzhODb/o6VFjEkDpZe7O/U=
Expand Down Expand Up @@ -1156,6 +1156,14 @@ babel-plugin-transform-flow-strip-types@^6.22.0:
babel-plugin-syntax-flow "^6.18.0"
babel-runtime "^6.22.0"

babel-plugin-transform-object-rest-spread@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz#0f36692d50fef6b7e2d4b3ac1478137a963b7b06"
integrity sha1-DzZpLVD+9rfi1LOsFHgTepY7ewY=
dependencies:
babel-plugin-syntax-object-rest-spread "^6.8.0"
babel-runtime "^6.26.0"

babel-plugin-transform-react-display-name@^6.23.0:
version "6.25.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-react-display-name/-/babel-plugin-transform-react-display-name-6.25.0.tgz#67e2bf1f1e9c93ab08db96792e05392bf2cc28d1"
Expand Down

0 comments on commit d3be540

Please sign in to comment.