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

Add push behavior to useQueryPersistedState #28302

Merged
merged 5 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export function useAssetSelectionState() {
return useQueryPersistedState<string>({
queryKey: 'asset-selection',
defaults: {['asset-selection']: ''},
behavior: 'push',
});
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {render, screen, waitFor} from '@testing-library/react';
import {act, render, screen, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {useCallback, useMemo} from 'react';
import {MemoryRouter} from 'react-router-dom';
import {MemoryRouter, useHistory} from 'react-router-dom';

import {Route} from '../../app/Route';
import {useQueryPersistedState} from '../useQueryPersistedState';
Expand Down Expand Up @@ -348,4 +348,112 @@ describe('useQueryPersistedState', () => {
);
});
});

it('supports push behavior', async () => {
let querySearch: string | undefined;

let goback: () => void;

const Test = ({options}: {options: Parameters<typeof useQueryPersistedState>[0]}) => {
const [_, setQuery] = useQueryPersistedState(options);
const history = useHistory();
goback = () => history.goBack();
return (
<>
<div onClick={() => setQuery('one')}>one</div>
<div onClick={() => setQuery('two')}>two</div>
<div onClick={() => setQuery('three')}>three</div>
</>
);
};

render(
<MemoryRouter initialEntries={['/page?q=B']}>
<Test options={{queryKey: 'q', behavior: 'push'}} />
<Route path="*" render={({location}) => (querySearch = location.search) && <span />} />
</MemoryRouter>,
);

await userEvent.click(screen.getByText(`one`));
await waitFor(() => {
expect(querySearch).toEqual('?q=one');
});

await userEvent.click(screen.getByText(`two`));
await waitFor(() => {
expect(querySearch).toEqual('?q=two');
});

await userEvent.click(screen.getByText(`three`));
await waitFor(() => {
expect(querySearch).toEqual('?q=three');
});

await act(() => {
goback();
});

await waitFor(() => {
expect(querySearch).toEqual('?q=two');
});

await act(() => {
goback();
});

await waitFor(() => {
expect(querySearch).toEqual('?q=one');
});
});

it('supports replace behavior', async () => {
let querySearch: string | undefined;

let goback: () => void;
let push: (...args: Parameters<ReturnType<typeof useHistory>['push']>) => void;

const Test = ({options}: {options: Parameters<typeof useQueryPersistedState>[0]}) => {
const [_, setQuery] = useQueryPersistedState(options);
const history = useHistory();
goback = () => history.goBack();
push = history.push.bind(history);
return (
<>
<div onClick={() => setQuery('one')}>one</div>
<div onClick={() => setQuery('two')}>two</div>
<div onClick={() => setQuery('three')}>three</div>
</>
);
};

render(
<MemoryRouter initialEntries={['/page?q=B']}>
<Test options={{queryKey: 'q', behavior: 'replace'}} />
<Route path="*" render={({location}) => (querySearch = location.search) && <span />} />
</MemoryRouter>,
);

push!('/page?');

await userEvent.click(screen.getByText(`one`));
await waitFor(() => {
expect(querySearch).toEqual('?q=one');
});

await userEvent.click(screen.getByText(`two`));
await waitFor(() => {
expect(querySearch).toEqual('?q=two');
});

await userEvent.click(screen.getByText(`three`));
await waitFor(() => {
expect(querySearch).toEqual('?q=three');
});

await act(() => {
goback();
});

expect(querySearch).toEqual('?q=B'); // end up back on initial route
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type QueryPersistedStateConfig<T extends QueryPersistedDataType> = {
defaults?: {[key: string]: any};
decode?: (raw: {[key: string]: any}) => T;
encode?: (raw: T) => {[key: string]: any};
behavior?: 'push' | 'replace';
};

const defaultEncode = memoize(<T,>(queryKey: string) => (raw: T) => ({[queryKey]: raw}));
Expand Down Expand Up @@ -63,7 +64,7 @@ const defaultDecode = memoize(
export function useQueryPersistedState<T extends QueryPersistedDataType>(
options: QueryPersistedStateConfig<T>,
): [T, React.Dispatch<React.SetStateAction<T>>] {
const {queryKey, defaults} = options;
const {queryKey, defaults, behavior = 'replace'} = options;
let {encode, decode} = options;

if (queryKey) {
Expand Down Expand Up @@ -113,9 +114,15 @@ export function useQueryPersistedState<T extends QueryPersistedDataType>(
// the `replace` so that we surface any unwanted loops during development.
if (process.env.NODE_ENV !== 'production' || !areQueriesEqual(currentQueryString, next)) {
currentQueryString = next;
history.replace(
`${history.location.pathname}?${qs.stringify(next, {arrayFormat: 'brackets'})}`,
);
if (behavior === 'replace') {
history.replace(
`${history.location.pathname}?${qs.stringify(next, {arrayFormat: 'brackets'})}`,
);
} else {
history.push(
`${history.location.pathname}?${qs.stringify(next, {arrayFormat: 'brackets'})}`,
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build the string once prior to the conditionals so that if something in the callsite has to change, we don't accidentally overlook one?

}
}
},
[history, encode, options],
Expand Down