Skip to content

Commit

Permalink
didierfranc#99 state is not updated when nothing is returned
Browse files Browse the repository at this point in the history
  • Loading branch information
Valery Buchinsky committed Oct 16, 2019
1 parent 7cafb35 commit 2cd4635
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/__test__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ exports[`render provider with its children 1`] = `
Children
</h1>
`;

exports[`state is not updated when nothing is returned 1`] = `"0"`;

exports[`state is not updated when nothing is returned 2`] = `"2"`;
38 changes: 38 additions & 0 deletions src/__test__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ beforeEach(() => {
const { stars } = await fakeFetch()
return { stars }
},
doNothing: () => {},
},
}

Expand Down Expand Up @@ -128,3 +129,40 @@ test('connect(): allow ownprops from mapStateToProps', async () => {
const instance = tree.root.findByType(Stars).children[0]
expect(instance.props.stars).toBe(20000)
})

test('state is not updated when nothing is returned', async () => {
const { Provider, connect, actions } = store
const didUpdateSpy = jest.fn()

/* eslint-disable react/no-multi-comp,react/prop-types */
class Count extends React.PureComponent {
componentDidUpdate() {
didUpdateSpy()
}

render() {
return this.props.count
}
}
const ConnectedCount = connect(({ count }) => ({ count }))(Count)
/* eslint-enable */

const App = () => (
<Provider>
<ConnectedCount />
</Provider>
)
const tree = renderer.create(<App />)

const instance = tree.root.findByType(ConnectedCount).children[0]
instance.componentDidUpdate = jest.fn()
expect(tree.toJSON()).toMatchSnapshot()

await actions.increment()
expect(didUpdateSpy).toHaveBeenCalledTimes(1)
await actions.increment()
expect(didUpdateSpy).toHaveBeenCalledTimes(2)
actions.doNothing()
expect(didUpdateSpy).toHaveBeenCalledTimes(2)
expect(tree.toJSON()).toMatchSnapshot()
})
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const createStore: CreateStore = (
let state = initialState

const setState: CustomSetState = (action, result, ...args) => {
if (typeof result === 'undefined') {
return Promise.resolve()
}
state = { ...state, ...result }
return new Promise(resolve => {
const subscriptions = getSubscriptions()
Expand All @@ -68,7 +71,7 @@ const createStore: CreateStore = (

const result = actionsCreators[v](state, actions, ...args)

return result.then
return (typeof result === 'object' && result.then)
? result.then(result => setState(v, result, ...args))
: setState(v, result, ...args)
},
Expand Down

0 comments on commit 2cd4635

Please sign in to comment.