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
23 changes: 23 additions & 0 deletions src/__tests__/useQueryParam-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
NumberParam,
NumericArrayParam,
DateParam,
JsonParam
} from 'serialize-query-params';
import { QueryParamProvider, useQueryParam } from '../index';
import { calledPushQuery, makeMockHistory, makeMockLocation } from './helpers';
Expand Down Expand Up @@ -152,6 +153,28 @@ describe('useQueryParam', () => {
expect(calledPushQuery(history, 2)).toEqual({ foo: '600' });
});

it('works with functional JsonParam updates', () => {
type ParamType = {a: number, b: string};
const { wrapper, history, location } = setupWrapper({
foo: '{"a":1,"b":"abc"}',
bar: 'xxx',
});
const { result, rerender } = renderHook(
() => useQueryParam('foo', JsonParam),
{
wrapper,
}
);
const [decodedValue, setter] = result.current;

expect(decodedValue).toEqual({a: 1, b: 'abc'});
setter((latestValue: ParamType) => ({...latestValue, a: latestValue.a + 1}), 'push');
expect(calledPushQuery(history, 0)).toEqual({ foo: '{"a":2,"b":"abc"}' });

setter((latestValue: ParamType) => ({...latestValue, b: "yyy"}), 'push');
expect(calledPushQuery(history, 1)).toEqual({ foo: '{"a":2,"b":"yyy"}' });
});

it('properly detects new values when equals is overridden', () => {
const { wrapper } = setupWrapper({
foo: '2020-01-01',
Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/useQueryParams-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EncodedQuery,
NumericArrayParam,
DateParam,
JsonParam
} from 'serialize-query-params';

import { useQueryParams, QueryParamProvider } from '../index';
Expand Down Expand Up @@ -262,6 +263,32 @@ describe('useQueryParams', () => {
expect(calledPushQuery(history, 2)).toEqual({ foo: '600' });
});

it('works with functional JsonParam updates', () => {
const { wrapper, history, location } = setupWrapper({
foo: '{"a":1,"b":"abc"}',
bar: 'xxx',
});
const { result, rerender } = renderHook(
() => useQueryParams({ foo: JsonParam, bar: StringParam }),
{
wrapper,
}
);
const [decodedValue, setter] = result.current;

expect(decodedValue).toEqual({ foo: {a: 1, b: 'abc'}, bar: 'xxx' });
setter(
(latestQuery: any) => ({
foo: {...latestQuery.foo, a: latestQuery.foo.a + 1},
}),
'pushIn'
);
expect(calledPushQuery(history, 0)).toEqual({
foo: '{"a":2,"b":"abc"}',
bar: 'xxx',
});
});

it('properly detects new values when equals is overridden', () => {
const { wrapper } = setupWrapper({
foo: '2020-01-01',
Expand Down
2 changes: 1 addition & 1 deletion src/useQueryParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const useQueryParam = <D, D2 = D>(
);
decodedValueCacheRef.current = latestValue; // keep cache in sync

newEncodedValue = (newValue as Function)(latestValue);
newEncodedValue = paramConfig.encode((newValue as Function)(latestValue));
} else {
newEncodedValue = paramConfig.encode(newValue);
}
Expand Down
2 changes: 1 addition & 1 deletion src/useQueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export const useQueryParams = <QPCMap extends QueryParamConfigMap>(
);
decodedValuesCacheRef.current = latestValues; // keep cache in sync

encodedChanges = (changes as Function)(latestValues);
encodedChanges = encodeQueryParams(paramConfigMap, (changes as Function)(latestValues));
} else {
// encode as strings for the URL
encodedChanges = encodeQueryParams(paramConfigMap, changes);
Expand Down