Update `useQueryStateByContext` to remove need for context when setting values (https://github.com/woocommerce/woocommerce-blocks/pull/1239)

* Don't require context for set

* Update tests
This commit is contained in:
Mike Jolley 2019-11-21 15:06:52 +00:00 committed by Seghir Nadir
parent 6e2978413e
commit 2f3c4623b7
2 changed files with 12 additions and 3 deletions

View File

@ -145,7 +145,9 @@ describe( 'Testing Query State Hooks', () => {
act( () => {
setQueryState( { foo: 'bar' } );
} );
expect( action ).toHaveBeenCalledWith( { foo: 'bar' } );
expect( action ).toHaveBeenCalledWith( 'test-context', {
foo: 'bar',
} );
}
);
} );

View File

@ -37,7 +37,14 @@ export const useQueryStateByContext = ( context ) => {
},
[ context ]
);
const { setValueForQueryContext: setQueryState } = useDispatch( storeKey );
const { setValueForQueryContext } = useDispatch( storeKey );
const setQueryState = useCallback(
( value ) => {
setValueForQueryContext( context, value );
},
[ context ]
);
return [ queryState, setQueryState ];
};
@ -115,7 +122,7 @@ export const useSynchronizedQueryState = ( synchronizedQuery, context ) => {
const isInitialized = useRef( false );
// update queryState anytime incoming synchronizedQuery changes
useEffect( () => {
setQueryState( context, {
setQueryState( {
...queryState,
...currentSynchronizedQuery,
} );