Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data: Allow undefined return from withSelect mapSelectToProps #5421

Merged
merged 2 commits into from
Mar 7, 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
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