Skip to content

Commit

Permalink
fix(stateParamsObserver): Allow prop changes to apply to wrapped comp…
Browse files Browse the repository at this point in the history
…onent correctly

Changed the order of props and mappedProps so that props takes precendence; allows prop changes on
wrapper component to correctly apply to wrapped component
  • Loading branch information
wms committed Nov 7, 2017
1 parent 3cd6825 commit fc8502f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/hoc/stateParamsObserver.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ router.stateRegistry.register({
})

const updateCountedComponent = () => {
const self = React.StatelessComponent = () => {
const self = React.StatelessComponent = (props: { color: string }) => {
self.updates += 1;
return (<div />);
};
Expand All @@ -33,23 +33,25 @@ it('maps state param changes to props', async () => {
const innerComponent = updateCountedComponent();
const WrappedComponent = stateParamsObserver(innerComponent);

const component = mount(<WrappedComponent />, { context: { router } });
const component = mount(<WrappedComponent color="red" />, { context: { router } });
const inner = component.find('InnerComponent');

await router.stateService.go('test', { page: 1 });
expect(inner.props()).toEqual({
'#': null,
page: 1,
search: null,
showDeleted: null
showDeleted: null,
color: 'red'
});

await router.stateService.go('test', { page: 2, showDeleted: true });
expect(inner.props()).toEqual({
'#': null,
page: 2,
search: null,
showDeleted: true
showDeleted: true,
color: 'red'
});

component.unmount();
Expand Down Expand Up @@ -123,6 +125,23 @@ it('maps props to wrapped component on mount', async () => {
});
});

it('passes through changes to non-observed props', async () => {
const innerComponent = updateCountedComponent();
const WrappedComponent = stateParamsObserver(innerComponent);
const component = mount(<WrappedComponent color="red" />, { context: { router } });

component.setProps({ color: 'blue' });

const props = component.find('InnerComponent').props();
expect(props).toEqual({
'#': null,
page: 3,
search: 'widgets',
showDeleted: true,
color: 'blue'
});
});

it('throws an error if @ui-router/rx is not installed', () => {
const innerComponent = updateCountedComponent();
const WrappedComponent = stateParamsObserver(innerComponent);
Expand Down
2 changes: 1 addition & 1 deletion src/hoc/stateParamsObserver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export const stateParamsObserver =
render() {
return (
<Component
{...this.props}
{...this.state.mappedProps}
{...this.props}
ref={this.props.wrappedComponentRef}
/>
);
Expand Down

0 comments on commit fc8502f

Please sign in to comment.