Skip to content

Commit

Permalink
Add flashMessageType for propType checking (#8)
Browse files Browse the repository at this point in the history
* Add flashMessageType

* Update readme

* v1.2.0
  • Loading branch information
dpikt authored Mar 6, 2018
1 parent 2257933 commit b0d6fb9
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 77 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Message objects returned by these selectors will have the following format:
}
```

A [PropType](https://github.com/facebook/prop-types) for this object is exported from this module as `flashMessageType`.

### Reducer

`redux-flash` exposes a reducer to handle the actions it creates. This reducer must be attached to your root reducer using the key `flash` in order for the library to function (see [example](#example)).
Expand All @@ -96,7 +98,7 @@ Message objects returned by these selectors will have the following format:
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { getLatestMessage } from 'redux-flash'
import { getLatestMessage, flashMessageType } from 'redux-flash'

function FlashMessages ({ flash }) {
return (
Expand All @@ -109,9 +111,7 @@ function FlashMessages ({ flash }) {
}

FlashMessages.propTypes = {
flash: PropTypes.shape({
message: PropTypes.string.isRequired
})
flash: flashMessageType
}

function mapStateToProps (state) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-flash",
"version": "1.1.3",
"version": "1.2.0",
"description": "Redux action creators for displaying flash messages",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -43,6 +43,7 @@
"rimraf": "^2.5.4"
},
"dependencies": {
"prop-types": "^15.6.1",
"redux-actions": "^2.0.3",
"uuid": "^3.1.0"
},
Expand Down
10 changes: 10 additions & 0 deletions src/flashMessageType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import PropTypes from 'prop-types'

const flashMessageType = PropTypes.shape({
id: PropTypes.string.isRequired,
message: PropTypes.string.isRequired,
isError: PropTypes.bool.isRequired,
props: PropTypes.object.isRequired,
})

export default flashMessageType
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ export {
clearMessages,
removeMessage,
} from './actions'
export flashMessageType from './flashMessageType'
export {
reducer,
getFlashMessages,
getSuccessMessages,
getErrorMessages,
getLatestMessage,
} from './reducer'
} from './reducer'
2 changes: 1 addition & 1 deletion test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
flashMessage,
flashErrorMessage,
} from '../src'
import configureStore from './mock-store'
import configureStore from './mockStore'
import thunkMiddleware from 'redux-thunk'

const mockStore = configureStore([ thunkMiddleware ])
Expand Down
24 changes: 24 additions & 0 deletions test/flashMessageType.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import PropTypes from 'prop-types'
import {
flashMessage,
flashMessageType,
} from '../src'
import configureStore from './mockStore'
import thunkMiddleware from 'redux-thunk'

const mockStore = configureStore([ thunkMiddleware ])

function validatePropType (propType, value) {
// checkPropTypes will log an error if it fails
const spy = jest.spyOn(console, 'error')
PropTypes.checkPropTypes({ value: propType }, { value }, 'prop')
expect(spy).not.toHaveBeenCalled()
}

test('propTypes match flash message object shape', () => {
const store = mockStore({})
store.dispatch(flashMessage('Hi'))
// Get generated flash message
const messageObject = store.getActions().pop().payload
return validatePropType(flashMessageType, messageObject)
})
File renamed without changes.
Loading

0 comments on commit b0d6fb9

Please sign in to comment.