diff --git a/src/connect/wrapMapToProps.ts b/src/connect/wrapMapToProps.ts index c1d170328..0a95939be 100644 --- a/src/connect/wrapMapToProps.ts +++ b/src/connect/wrapMapToProps.ts @@ -48,7 +48,7 @@ export function wrapMapToPropsConstant( // therefore not reporting its length accurately.. // TODO Can this get pulled out so that we can subscribe directly to the store if we don't need ownProps? function getDependsOnOwnProps(mapToProps: MapToProps) { - return mapToProps.dependsOnOwnProps + return mapToProps.dependsOnOwnProps != null ? Boolean(mapToProps.dependsOnOwnProps) : mapToProps.length !== 1 } diff --git a/test/connect/wrapMapToProps.spec.ts b/test/connect/wrapMapToProps.spec.ts new file mode 100644 index 000000000..b646b4669 --- /dev/null +++ b/test/connect/wrapMapToProps.spec.ts @@ -0,0 +1,48 @@ +import type { MapToProps } from '@internal/connect/wrapMapToProps' +import { wrapMapToPropsFunc } from '@internal/connect/wrapMapToProps' +import type { Dispatch } from 'redux' + +const fakeDispatch = (() => {}) as Dispatch +const fakeOptions = { displayName: 'TestComponent' } + +function getDependsOnOwnProps(mapToProps: MapToProps) { + const proxy = wrapMapToPropsFunc(mapToProps, 'mapStateToProps')( + fakeDispatch, + fakeOptions, + ) + proxy({}, undefined) + return proxy.dependsOnOwnProps +} + +describe('wrapMapToProps', () => { + describe('getDependsOnOwnProps', () => { + it('infers dependsOnOwnProps=true from a 2-arity function with no explicit flag', () => { + const mapToProps = (_state: unknown, _ownProps: unknown) => ({}) + + expect(getDependsOnOwnProps(mapToProps)).toBe(true) + }) + + it('infers dependsOnOwnProps=false from a 1-arity function with no explicit flag', () => { + const mapToProps = (_state: unknown) => ({}) + + expect(getDependsOnOwnProps(mapToProps)).toBe(false) + }) + + it('respects an explicit dependsOnOwnProps=false even when function arity is 2', () => { + const mapToProps = Object.assign( + (_state: unknown, _ownProps: unknown) => ({}), + { dependsOnOwnProps: false }, + ) + + expect(getDependsOnOwnProps(mapToProps)).toBe(false) + }) + + it('respects an explicit dependsOnOwnProps=true even when function arity is 1', () => { + const mapToProps = Object.assign((_state: unknown) => ({}), { + dependsOnOwnProps: true, + }) + + expect(getDependsOnOwnProps(mapToProps)).toBe(true) + }) + }) +})