Skip to content

Commit

Permalink
Merge pull request #1325 from JamieDixon/master
Browse files Browse the repository at this point in the history
subscribe to throw when listener is not a function
  • Loading branch information
gaearon committed Jan 30, 2016
2 parents 32b82b2 + 69f8d97 commit d98c3f0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export default function createStore(reducer, initialState, enhancer) {
* @returns {Function} A function to remove this change listener.
*/
function subscribe(listener) {
if (typeof listener !== 'function') {
throw new Error('Expected listener to be a function.')
}

listeners.push(listener)
var isSubscribed = true

Expand Down
20 changes: 20 additions & 0 deletions test/createStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,24 @@ describe('createStore', () => {
store.replaceReducer(() => {})
).toNotThrow()
})

it('throws if listener is not a function', () => {
const store = createStore(reducers.todos)

expect(() =>
store.subscribe()
).toThrow()

expect(() =>
store.subscribe('')
).toThrow()

expect(() =>
store.subscribe(null)
).toThrow()

expect(() =>
store.subscribe(undefined)
).toThrow()
})
})

0 comments on commit d98c3f0

Please sign in to comment.