Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Fix withPropsOnChange shouldMap consistency #707

Merged
merged 1 commit into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"lib/packages/recompose/dist/Recompose.umd.js": {
"bundled": 85972,
"minified": 31684,
"gzipped": 10057
"bundled": 86014,
"minified": 31693,
"gzipped": 10059
},
"lib/packages/recompose/dist/Recompose.min.js": {
"bundled": 82350,
"minified": 30401,
"gzipped": 9666
"bundled": 82392,
"minified": 30410,
"gzipped": 9667
},
"lib/packages/recompose/dist/Recompose.esm.js": {
"bundled": 32336,
"minified": 16109,
"bundled": 32374,
"minified": 16118,
"gzipped": 3577,
"treeshaked": {
"rollup": 601,
Expand Down
77 changes: 76 additions & 1 deletion src/packages/recompose/__tests__/withPropsOnChange-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as React from 'react'
import { mount } from 'enzyme'
import sinon from 'sinon'
import { withPropsOnChange, withState, flattenProp, compose } from '../'
import {
withPropsOnChange,
withState,
withStateHandlers,
flattenProp,
compose,
} from '../'

test('withPropsOnChange maps subset of owner props to child props', () => {
const component = sinon.spy(() => null)
Expand Down Expand Up @@ -43,3 +49,72 @@ test('withPropsOnChange maps subset of owner props to child props', () => {
expect(component.calledThrice).toBe(true)
expect(mapSpy.callCount).toBe(2)
})

test('withPropsOnChange maps subset of owner props to child props with custom predicate', () => {
const component = sinon.spy(() => null)
component.displayName = 'component'

const mapSpy = sinon.spy()
const shouldMapSpy = sinon.spy()
const PageContainer = compose(
withStateHandlers(
{ result: { hasError: false, loading: true, error: null } },
{
updateResult: ({ result }) => payload => ({
result: { ...result, ...payload },
}),
}
),
withPropsOnChange(
({ result }, { result: nextResult }) => {
shouldMapSpy(result, nextResult)
return !result.hasError && nextResult.hasError
},
({ result: { hasError, error } }) => {
mapSpy()

if (hasError) {
return {
errorEverHappened: true,
lastError: error,
}
}

return {
errorEverHappened: false,
}
}
)
)(component)

expect(PageContainer.displayName).toBe(
'withStateHandlers(withPropsOnChange(component))'
)

mount(<PageContainer />)
const { updateResult } = component.firstCall.args[0]
expect(component.lastCall.args[0].errorEverHappened).toBe(false)
expect(component.lastCall.args[0].lastError).toBeUndefined()
expect(component.calledOnce).toBe(true)
expect(mapSpy.callCount).toBe(1)
expect(shouldMapSpy.callCount).toBe(1)

updateResult({ loading: false, hasError: true, error: '1' })
expect(component.lastCall.args[0].errorEverHappened).toBe(true)
expect(component.lastCall.args[0].lastError).toBe('1')
expect(component.calledTwice).toBe(true)
expect(mapSpy.callCount).toBe(2)

// Does not re-map for false map result
updateResult({ loading: true, hasError: false, error: null })
expect(component.lastCall.args[0].errorEverHappened).toBe(true)
expect(component.lastCall.args[0].lastError).toBe('1')
expect(component.calledThrice).toBe(true)
expect(mapSpy.callCount).toBe(2)

updateResult({ loading: false, hasError: true, error: '2' })
expect(component.lastCall.args[0].errorEverHappened).toBe(true)
expect(component.lastCall.args[0].lastError).toBe('2')
expect(component.callCount).toBe(4)
expect(mapSpy.callCount).toBe(3)
})
4 changes: 3 additions & 1 deletion src/packages/recompose/withPropsOnChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const withPropsOnChange = (shouldMapOrKeys, propsMapper) => BaseComponent => {
}
}

return null
return {
prevProps: nextProps,
}
}

render() {
Expand Down