Skip to content
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
2 changes: 1 addition & 1 deletion src/connect/wrapMapToProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
48 changes: 48 additions & 0 deletions test/connect/wrapMapToProps.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
Loading