Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0b186bf
chore: rename useTab to useSearchParamsKey and add test
Parkreiner Feb 19, 2024
a7a0944
chore: mark old renderHookWithAuth as deprecated (temp)
Parkreiner Feb 19, 2024
ec3ba4f
fix: update imports for useResourcesNav
Parkreiner Feb 19, 2024
6d7ef9f
Merge branch 'main' into mes/hook-test-revamps-4
Parkreiner Mar 3, 2024
afffb26
refactor: change API for useSearchParamsKey
Parkreiner Mar 3, 2024
590f02b
chore: let user pass in their own URLSearchParams value
Parkreiner Mar 3, 2024
9e00ea6
refactor: clean up comments for clarity
Parkreiner Mar 3, 2024
d7110c5
fix: update import
Parkreiner Mar 3, 2024
2cc63d7
wip: commit progress on useWorkspaceDuplication revamp
Parkreiner Mar 3, 2024
26089e3
chore: migrate duplication test to new helper
Parkreiner Mar 3, 2024
8057397
refactor: update code for clarity
Parkreiner Mar 3, 2024
f8f87a3
refactor: reorder test cases for clarity
Parkreiner Mar 3, 2024
dcc89dc
refactor: split off hook helper into separate file
Parkreiner Mar 3, 2024
1ab6f03
refactor: remove reliance on internal React Router state property
Parkreiner Mar 4, 2024
b32603f
refactor: move variables around for more clarity
Parkreiner Mar 4, 2024
b0fabde
refactor: more updates for clarity
Parkreiner Mar 4, 2024
b94decc
refactor: reorganize test cases for clarity
Parkreiner Mar 4, 2024
3e41877
refactor: clean up test cases for useWorkspaceDupe
Parkreiner Mar 8, 2024
1fcf9e2
refactor: clean up test cases for useWorkspaceDupe
Parkreiner Mar 8, 2024
13f5e53
Merge branch 'main' into mes/hook-test-revamps-4
Parkreiner Mar 8, 2024
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
Next Next commit
chore: rename useTab to useSearchParamsKey and add test
  • Loading branch information
Parkreiner committed Feb 19, 2024
commit 0b186bfcfc37eb577b5e9149e5a1b00a1b3fc36c
1 change: 0 additions & 1 deletion site/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export * from "./useClickable";
export * from "./useClickableTableRow";
export * from "./useClipboard";
export * from "./usePagination";
export * from "./useTab";
10 changes: 7 additions & 3 deletions site/src/hooks/usePaginatedQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ function render<
route?: `/?page=${string}`,
) {
return renderHookWithAuth(({ options }) => usePaginatedQuery(options), {
route,
path: "/",
initialProps: { options },
routingOptions: {
route,
path: "/",
},
renderOptions: {
initialProps: { options },
},
});
}

Expand Down
115 changes: 115 additions & 0 deletions site/src/hooks/useSearchParamsKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { useSearchParamsKey } from "./useSearchParamsKey";
import { renderHookWithAuth } from "testHelpers/renderHelpers";
import { act, waitFor } from "@testing-library/react";

/**
* Tried to extract the setup logic into one place, but it got surprisingly
* messy. Went with straightforward approach of calling things individually
*
* @todo See if there's a way to test the interaction with the history object
* (particularly, for replace behavior). It's traditionally very locked off, and
* React Router gives you no way of interacting with it directly.
*/
describe(useSearchParamsKey.name, () => {
it("Returns out a default value of an empty string if the key does not exist in URL", async () => {
const { result } = await renderHookWithAuth(
() => useSearchParamsKey("blah"),
{ routingOptions: { route: `/` } },
);

expect(result.current.value).toEqual("");
});

it("Uses the 'defaultValue' config override if provided", async () => {
const defaultValue = "dogs";
const { result } = await renderHookWithAuth(
() => useSearchParamsKey("blah", { defaultValue }),
{ routingOptions: { route: `/` } },
);

expect(result.current.value).toEqual(defaultValue);
});

it("Is able to read to read keys from the URL on mounting render", async () => {
const key = "blah";
const value = "cats";

const { result } = await renderHookWithAuth(() => useSearchParamsKey(key), {
routingOptions: {
route: `/?${key}=${value}`,
},
});

expect(result.current.value).toEqual(value);
});

it("Updates state and URL when the setValue callback is called with a new value", async () => {
const key = "blah";
const initialValue = "cats";

const { result, getLocationSnapshot } = await renderHookWithAuth(
() => useSearchParamsKey(key),
{
routingOptions: {
route: `/?${key}=${initialValue}`,
},
},
);

const newValue = "dogs";
act(() => result.current.onValueChange(newValue));
await waitFor(() => expect(result.current.value).toEqual(newValue));

const { search } = getLocationSnapshot();
expect(search.get(key)).toEqual(newValue);
});

it("Clears value for the given key from the state and URL when removeValue is called", async () => {
const key = "blah";
const initialValue = "cats";

const { result, getLocationSnapshot } = await renderHookWithAuth(
() => useSearchParamsKey(key),
{
routingOptions: {
route: `/?${key}=${initialValue}`,
},
},
);

act(() => result.current.removeValue());
await waitFor(() => expect(result.current.value).toEqual(""));

const { search } = getLocationSnapshot();
expect(search.get(key)).toEqual(null);
});

it("Does not have methods change previous values if 'key' argument changes during re-renders", async () => {
const readonlyKey = "readonlyKey";
const mutableKey = "mutableKey";
const initialReadonlyValue = "readonly";
const initialMutableValue = "mutable";

const { result, rerender, getLocationSnapshot } = await renderHookWithAuth(
({ key }) => useSearchParamsKey(key),
{
routingOptions: {
route: `/?${readonlyKey}=${initialReadonlyValue}&${mutableKey}=${initialMutableValue}`,
},

renderOptions: {
initialProps: { key: readonlyKey },
},
},
);

const swapValue = "dogs";
rerender({ key: mutableKey });
act(() => result.current.onValueChange(swapValue));
await waitFor(() => expect(result.current.value).toEqual(swapValue));

const { search } = getLocationSnapshot();
expect(search.get(readonlyKey)).toEqual(initialReadonlyValue);
expect(search.get(mutableKey)).toEqual(swapValue);
});
});
52 changes: 52 additions & 0 deletions site/src/hooks/useSearchParamsKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useCallback } from "react";
import { useSearchParams } from "react-router-dom";
import { useEffectEvent } from "./hookPolyfills";

export type UseSearchParamKeyConfig = Readonly<{
defaultValue?: string;
replace?: boolean;
}>;

export type UseSearchParamKeyResult = Readonly<{
value: string;
onValueChange: (newValue: string) => void;
removeValue: () => void;
}>;

export const useSearchParamsKey = (
key: string,
config: UseSearchParamKeyConfig = {},
): UseSearchParamKeyResult => {
const { defaultValue = "", replace = true } = config;
const [searchParams, setSearchParams] = useSearchParams();
const stableSetSearchParams = useEffectEvent(setSearchParams);

const onValueChange = useCallback(
(newValue: string) => {
stableSetSearchParams(
(currentParams) => {
currentParams.set(key, newValue);
return currentParams;
},
{ replace },
);
},
[stableSetSearchParams, key, replace],
);

const removeValue = useCallback(() => {
stableSetSearchParams(
(currentParams) => {
currentParams.delete(key);
return currentParams;
},
{ replace },
);
}, [stableSetSearchParams, key, replace]);

return {
value: searchParams.get(key) ?? defaultValue,
onValueChange,
removeValue,
};
};
19 changes: 0 additions & 19 deletions site/src/hooks/useTab.ts

This file was deleted.

11 changes: 5 additions & 6 deletions site/src/pages/WorkspacePage/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useNavigate } from "react-router-dom";
import type * as TypesGen from "api/typesGenerated";
import { Alert, AlertDetail } from "components/Alert/Alert";
import { AgentRow } from "modules/resources/AgentRow";
import { useTab } from "hooks";
import { useSearchParamsKey } from "hooks/useSearchParamsKey";
import {
ActiveTransition,
WorkspaceBuildProgress,
Expand Down Expand Up @@ -89,13 +89,12 @@ export const Workspace: FC<WorkspaceProps> = ({
const transitionStats =
template !== undefined ? ActiveTransition(template, workspace) : undefined;

const sidebarOption = useTab("sidebar", "");
const sidebarOption = useSearchParamsKey("sidebar");
const setSidebarOption = (newOption: string) => {
const { set, value } = sidebarOption;
if (value === newOption) {
set("");
if (sidebarOption.value === newOption) {
sidebarOption.removeValue();
} else {
set(newOption);
sidebarOption.onValueChange(newOption);
}
};

Expand Down