Skip to content

Commit

Permalink
Data: Allow undefined return from withSelect mapSelectToProps (#5421)
Browse files Browse the repository at this point in the history
* Data: Avoid lingering props on subsequent mapping changes

* Data: Allow undefined return from mapSelectToProps
  • Loading branch information
aduth authored Mar 7, 2018
1 parent 0ca94f4 commit a468cf5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
12 changes: 8 additions & 4 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,18 @@ export const withSelect = ( mapStateToProps ) => ( WrappedComponent ) => {
return;
}

const newState = mapStateToProps( select, props );
if ( ! isEqualShallow( newState, this.state ) ) {
this.setState( newState );
const { mergeProps } = this.state;
const nextMergeProps = mapStateToProps( select, props ) || {};

if ( ! isEqualShallow( nextMergeProps, mergeProps ) ) {
this.setState( {
mergeProps: nextMergeProps,
} );
}
}

render() {
return <WrappedComponent { ...this.props } { ...this.state } />;
return <WrappedComponent { ...this.props } { ...this.state.mergeProps } />;
}
}

Expand Down
48 changes: 48 additions & 0 deletions data/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,54 @@ describe( 'withSelect', () => {

expect( mapSelectToProps ).toHaveBeenCalledTimes( 1 );
} );

itWithExtraAssertions( 'omits props which are not returned on subsequent mappings', () => {
registerReducer( 'demo', ( state = 'OK' ) => state );
registerSelectors( 'demo', {
getValue: ( state ) => state,
} );

const Component = withSelectImplementation( ( _select, ownProps ) => {
return {
[ ownProps.propName ]: _select( 'demo' ).getValue(),
};
} )( () => <div /> );

wrapper = mount( <Component propName="foo" /> );

expect( wrapper.childAt( 0 ).props() ).toEqual( { foo: 'OK', propName: 'foo' } );

wrapper.setProps( { propName: 'bar' } );

expect( wrapper.childAt( 0 ).props() ).toEqual( { bar: 'OK', propName: 'bar' } );
} );

itWithExtraAssertions( 'allows undefined return from mapSelectToProps', () => {
registerReducer( 'demo', ( state = 'OK' ) => state );
registerSelectors( 'demo', {
getValue: ( state ) => state,
} );

const Component = withSelectImplementation( ( _select, ownProps ) => {
if ( ownProps.pass ) {
return {
count: _select( 'demo' ).getValue(),
};
}
} )( ( props ) => <div>{ props.count || 'Unknown' }</div> );

wrapper = mount( <Component pass={ false } /> );

expect( wrapper.childAt( 0 ).text() ).toBe( 'Unknown' );

wrapper.setProps( { pass: true } );

expect( wrapper.childAt( 0 ).text() ).toBe( 'OK' );

wrapper.setProps( { pass: false } );

expect( wrapper.childAt( 0 ).text() ).toBe( 'Unknown' );
} );
}

cases( withSelect );
Expand Down

0 comments on commit a468cf5

Please sign in to comment.