-
Notifications
You must be signed in to change notification settings - Fork 887
fix(site): fix resource selection when workspace resources change #11581
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
Changes from 1 commit
1222a24
8c6e334
1d2e8f3
032c3a1
ec2282e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import { resourceOptionId, useResourcesNav } from "./useResourcesNav"; | ||
import { WorkspaceResource } from "api/typesGenerated"; | ||
import { MockWorkspaceResource } from "testHelpers/entities"; | ||
import { RouterProvider, createMemoryRouter } from "react-router-dom"; | ||
|
||
describe("useResourcesNav", () => { | ||
it("selects the first resource if it has agents and no resource is selected", () => { | ||
const resources: WorkspaceResource[] = [ | ||
MockWorkspaceResource, | ||
{ | ||
...MockWorkspaceResource, | ||
agents: [], | ||
}, | ||
]; | ||
const { result } = renderHook(() => useResourcesNav(resources), { | ||
wrapper: ({ children }) => ( | ||
<RouterProvider | ||
router={createMemoryRouter([{ path: "/", element: children }])} | ||
/> | ||
), | ||
}); | ||
expect(result.current.selected?.id).toBe(MockWorkspaceResource.id); | ||
}); | ||
|
||
it("selects the first resource if it has agents and selected resource is not find", async () => { | ||
const resources: WorkspaceResource[] = [ | ||
MockWorkspaceResource, | ||
{ | ||
...MockWorkspaceResource, | ||
agents: [], | ||
}, | ||
]; | ||
const { result } = renderHook(() => useResourcesNav(resources), { | ||
wrapper: ({ children }) => ( | ||
<RouterProvider | ||
router={createMemoryRouter([{ path: "/", element: children }], { | ||
initialEntries: ["/?resources=not_found_resource_id"], | ||
})} | ||
/> | ||
), | ||
}); | ||
expect(result.current.selected?.id).toBe(MockWorkspaceResource.id); | ||
}); | ||
|
||
it("selects the resource passed in the URL", () => { | ||
const resources: WorkspaceResource[] = [ | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_container", | ||
name: "coder_python", | ||
}, | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_container", | ||
name: "coder_java", | ||
}, | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_image", | ||
name: "coder_image_python", | ||
agents: [], | ||
}, | ||
]; | ||
const { result } = renderHook(() => useResourcesNav(resources), { | ||
wrapper: ({ children }) => ( | ||
<RouterProvider | ||
router={createMemoryRouter([{ path: "/", element: children }], { | ||
initialEntries: [`/?resources=${resourceOptionId(resources[1])}`], | ||
})} | ||
/> | ||
), | ||
}); | ||
expect(result.current.selected?.id).toBe(resources[1].id); | ||
}); | ||
|
||
it("selects a resource when resources are updated", () => { | ||
const startedResources: WorkspaceResource[] = [ | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_container", | ||
name: "coder_python", | ||
}, | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_container", | ||
name: "coder_java", | ||
}, | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_image", | ||
name: "coder_image_python", | ||
agents: [], | ||
}, | ||
]; | ||
const { result, rerender } = renderHook( | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
({ resources }) => useResourcesNav(resources), | ||
{ | ||
wrapper: ({ children }) => ( | ||
<RouterProvider | ||
router={createMemoryRouter([{ path: "/", element: children }])} | ||
/> | ||
), | ||
initialProps: { resources: startedResources }, | ||
}, | ||
); | ||
expect(result.current.selected?.id).toBe(startedResources[0].id); | ||
|
||
// When a workspace is stopped, there is no resource with agents | ||
const stoppedResources: WorkspaceResource[] = [ | ||
{ | ||
...MockWorkspaceResource, | ||
type: "docker_image", | ||
name: "coder_image_python", | ||
agents: [], | ||
}, | ||
]; | ||
rerender({ resources: stoppedResources }); | ||
expect(result.current.selected).toBe(undefined); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it makes sense to add testing logic to check that the URL is syncing correctly on renders/re-renders, too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is too much since we don't care too much about the URL but what the hook is returning. What use case could justify this test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's more for making sure that the value we're getting back isn't getting out of sync with the URL. I don't think the current implementation has any real risks of that, but the way I see the hook, it does two things:
And we're only testing one of those things |
||
|
||
// When a workspace is started again a resource is selected | ||
rerender({ resources: startedResources }); | ||
expect(result.current.selected?.id).toBe(startedResources[0].id); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { WorkspaceResource } from "api/typesGenerated"; | ||
import { useTab } from "hooks"; | ||
import { useCallback, useEffect } from "react"; | ||
|
||
export const resourceOptionId = (resource: WorkspaceResource) => { | ||
return `${resource.type}_${resource.name}`; | ||
}; | ||
|
||
export const useResourcesNav = (resources: WorkspaceResource[]) => { | ||
const resourcesNav = useTab("resources", ""); | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const selectedResource = resources.find( | ||
(r) => resourceOptionId(r) === resourcesNav.value, | ||
); | ||
|
||
useEffect(() => { | ||
const hasResourcesWithAgents = | ||
resources.length > 0 && | ||
resources[0].agents && | ||
resources[0].agents.length > 0; | ||
if (!selectedResource && hasResourcesWithAgents) { | ||
resourcesNav.set(resourceOptionId(resources[0])); | ||
} | ||
}, [resources, selectedResource, resourcesNav]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the only cue for synchronization seems to be when const syncSelectionChange = useEffectEvent((previousResource) => {
const hasResourcesWithAgents =
resources.length > 0 &&
resources[0].agents &&
resources[0].agents.length > 0;
if (!previousResource && hasResourcesWithAgents) {
resourcesNav.set(resourceOptionId(resources[0]));
}
});
useEffect(() => {
syncSelectionChange(selectedResource);
}, [syncSelectionChange, selectedResource]); Even if Though I think there's one more edge case: what would happen if we had a resource selected, but then the resources array becomes completely empty? Would we need to set things back to an empty string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overall, though, I think the effect logic is what's pushing me to think that a custom hook might be justified, mainly because it's relying on URL syncs via React Router. We can't really get away with derived values or inline state syncs because we'd have to talk to an outside system during the renders I do think there's value in abstracting over those syncs and giving you a guarantee that things will "just work". I'm just wondering if it's worth testing the hook itself, or if it's better to treat it like an implementation detail, and test the whole component There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this would be the ideal scenario but rendering the workspace component and testing this behavior requires some server events mocking which I would like to avoid. I rather have a simple solution like this for now and only go for more complex ones if it is needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hm... I think this would not happen because when resources are returned, we can expect at least one resource. I think Coder does not allow workspace creations without a resource but if it does, I think it is not a huge concern right now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I would not care too much about effects running on every render if it is not going to mess up with the state or cause a loop. I would rather optimize it to be ok running on every render one million times. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I said all of that but after some thought I think the useEffectEvent is a good use case for this as you suggested, and I appreciate you providing the code for refactoring for that ❤️ |
||
|
||
const select = useCallback( | ||
(resource: WorkspaceResource) => { | ||
resourcesNav.set(resourceOptionId(resource)); | ||
}, | ||
[resourcesNav], | ||
); | ||
|
||
const isSelected = useCallback( | ||
(resource: WorkspaceResource) => { | ||
return resourceOptionId(resource) === resourcesNav.value; | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
[resourcesNav.value], | ||
); | ||
|
||
return { | ||
isSelected, | ||
select, | ||
selected: selectedResource, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
isSelected
change is an improvement, but I'm wondering if the implementation is getting a little too tied to howuseResourcesNav
is set up.What do you think of turning
isSelected
into just a boolean, and making the parent component call the function to get the right prop to pass in?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to know what is the selected resource and since this is not just an attribute but a "calculation", I think this interface makes things easier to extend.