From dc077b2bf51bbac8a98a90efb5c4a6402f8d9674 Mon Sep 17 00:00:00 2001 From: motiazu Date: Mon, 23 Jul 2018 14:28:43 +0300 Subject: [PATCH] Fix withPropsOnChange shouldMap consistency --- .size-snapshot.json | 16 ++-- .../__tests__/withPropsOnChange-test.js | 77 ++++++++++++++++++- src/packages/recompose/withPropsOnChange.js | 4 +- 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/.size-snapshot.json b/.size-snapshot.json index 7490a07b..192f1a35 100644 --- a/.size-snapshot.json +++ b/.size-snapshot.json @@ -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, diff --git a/src/packages/recompose/__tests__/withPropsOnChange-test.js b/src/packages/recompose/__tests__/withPropsOnChange-test.js index 08667128..693dfd35 100644 --- a/src/packages/recompose/__tests__/withPropsOnChange-test.js +++ b/src/packages/recompose/__tests__/withPropsOnChange-test.js @@ -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) @@ -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() + 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) +}) diff --git a/src/packages/recompose/withPropsOnChange.js b/src/packages/recompose/withPropsOnChange.js index 7a05a474..3f31491a 100644 --- a/src/packages/recompose/withPropsOnChange.js +++ b/src/packages/recompose/withPropsOnChange.js @@ -30,7 +30,9 @@ const withPropsOnChange = (shouldMapOrKeys, propsMapper) => BaseComponent => { } } - return null + return { + prevProps: nextProps, + } } render() {