-
Notifications
You must be signed in to change notification settings - Fork 30
feat: Add switch organization to project switcher #2365
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
Open
charlesvien
wants to merge
4
commits into
05-25-auth_org_keyed_map
Choose a base branch
from
05-25-org_switcher_submenu
base: 05-25-auth_org_keyed_map
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+257
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
205 changes: 205 additions & 0 deletions
205
apps/code/src/renderer/features/sidebar/components/ProjectSwitcher.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| import { Theme } from "@radix-ui/themes"; | ||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| type AuthState = { | ||
| cloudRegion: string | null; | ||
| orgProjectsMap: Record< | ||
| string, | ||
| { orgName: string; projects: { id: number; name: string }[] } | ||
| >; | ||
| currentOrgId: string | null; | ||
| }; | ||
|
|
||
| let mockAuthState: AuthState = { | ||
| cloudRegion: "us", | ||
| orgProjectsMap: {}, | ||
| currentOrgId: null, | ||
| }; | ||
|
|
||
| const switchOrgMutate = vi.fn(); | ||
| const selectProjectMutate = vi.fn(); | ||
| const logoutMutate = vi.fn(); | ||
| const openSettings = vi.fn(); | ||
| const navigateToTaskInput = vi.fn(); | ||
| let switchOrgPending = false; | ||
|
|
||
| vi.mock("@features/auth/hooks/authClient", () => ({ | ||
| useOptionalAuthenticatedClient: () => null, | ||
| })); | ||
|
|
||
| vi.mock("@features/auth/hooks/authMutations", () => ({ | ||
| useLogoutMutation: () => ({ mutate: logoutMutate }), | ||
| useSelectProjectMutation: () => ({ mutate: selectProjectMutate }), | ||
| useSwitchOrgMutation: () => ({ | ||
| mutate: switchOrgMutate, | ||
| isPending: switchOrgPending, | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock("@stores/navigationStore", () => ({ | ||
| useNavigationStore: { | ||
| getState: () => ({ navigateToTaskInput }), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("@features/auth/hooks/authQueries", () => ({ | ||
| useAuthStateValue: (selector: (state: AuthState) => unknown) => | ||
| selector(mockAuthState), | ||
| useCurrentUser: () => ({ | ||
| data: { email: "user@example.com", first_name: "Test", last_name: "User" }, | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock("@features/projects/hooks/useProjects", () => ({ | ||
| useProjects: () => ({ | ||
| groupedProjects: [], | ||
| currentProject: { id: 42, name: "Demo project" }, | ||
| currentProjectId: 42, | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock("@features/settings/stores/settingsDialogStore", () => ({ | ||
| useSettingsDialogStore: ( | ||
| selector: (state: { open: typeof openSettings }) => unknown, | ||
| ) => selector({ open: openSettings }), | ||
| })); | ||
|
|
||
| vi.mock("@renderer/trpc/client", () => ({ | ||
| trpcClient: { | ||
| os: { openExternal: { mutate: vi.fn() } }, | ||
| }, | ||
| })); | ||
|
|
||
| import { ProjectSwitcher } from "./ProjectSwitcher"; | ||
|
|
||
| function renderInTheme() { | ||
| return render( | ||
| <Theme> | ||
| <ProjectSwitcher /> | ||
| </Theme>, | ||
| ); | ||
| } | ||
|
|
||
| describe("ProjectSwitcher org switcher", () => { | ||
| beforeEach(() => { | ||
| switchOrgMutate.mockReset(); | ||
| selectProjectMutate.mockReset(); | ||
| logoutMutate.mockReset(); | ||
| openSettings.mockReset(); | ||
| navigateToTaskInput.mockReset(); | ||
| switchOrgPending = false; | ||
| }); | ||
|
|
||
| it("hides the Switch organization submenu when there is only one org", async () => { | ||
| mockAuthState = { | ||
| cloudRegion: "us", | ||
| currentOrgId: "org-1", | ||
| orgProjectsMap: { | ||
| "org-1": { orgName: "Solo Org", projects: [{ id: 42, name: "P1" }] }, | ||
| }, | ||
| }; | ||
|
|
||
| const user = userEvent.setup({ pointerEventsCheck: 0 }); | ||
| renderInTheme(); | ||
|
|
||
| await user.click(screen.getByText("Demo project")); | ||
|
|
||
| expect(screen.queryByText("Switch organization")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("shows the submenu with every org and marks the current one", async () => { | ||
| mockAuthState = { | ||
| cloudRegion: "us", | ||
| currentOrgId: "org-1", | ||
| orgProjectsMap: { | ||
| "org-1": { orgName: "Alpha", projects: [{ id: 1, name: "P1" }] }, | ||
| "org-2": { orgName: "Beta", projects: [{ id: 2, name: "P2" }] }, | ||
| }, | ||
| }; | ||
|
|
||
| const user = userEvent.setup({ pointerEventsCheck: 0 }); | ||
| renderInTheme(); | ||
|
|
||
| await user.click(screen.getByText("Demo project")); | ||
| await user.hover(await screen.findByText("Switch organization")); | ||
|
|
||
| const alpha = await screen.findByRole("menuitem", { name: /Alpha/ }); | ||
| const beta = await screen.findByRole("menuitem", { name: /Beta/ }); | ||
| expect(alpha).toBeInTheDocument(); | ||
| expect(beta).toBeInTheDocument(); | ||
|
|
||
| expect(alpha.querySelector(".text-accent-11")).not.toBeNull(); | ||
| expect(beta.querySelector(".text-accent-11")).toBeNull(); | ||
| }); | ||
|
|
||
| it("fires switchOrg.mutate when picking a different org and navigates on success", async () => { | ||
| mockAuthState = { | ||
| cloudRegion: "us", | ||
| currentOrgId: "org-1", | ||
| orgProjectsMap: { | ||
| "org-1": { orgName: "Alpha", projects: [{ id: 1, name: "P1" }] }, | ||
| "org-2": { orgName: "Beta", projects: [{ id: 2, name: "P2" }] }, | ||
| }, | ||
| }; | ||
|
|
||
| const user = userEvent.setup({ pointerEventsCheck: 0 }); | ||
| renderInTheme(); | ||
|
|
||
| await user.click(screen.getByText("Demo project")); | ||
| await user.hover(await screen.findByText("Switch organization")); | ||
| fireEvent.click(await screen.findByRole("menuitem", { name: /Beta/ })); | ||
|
|
||
| expect(switchOrgMutate).toHaveBeenCalledTimes(1); | ||
| expect(switchOrgMutate).toHaveBeenCalledWith( | ||
| "org-2", | ||
| expect.objectContaining({ onSuccess: expect.any(Function) }), | ||
| ); | ||
|
|
||
| const onSuccess = switchOrgMutate.mock.calls[0]?.[1]?.onSuccess; | ||
| onSuccess?.(); | ||
| expect(navigateToTaskInput).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("skips switchOrg.mutate while a previous switch is pending", async () => { | ||
| switchOrgPending = true; | ||
| mockAuthState = { | ||
| cloudRegion: "us", | ||
| currentOrgId: "org-1", | ||
| orgProjectsMap: { | ||
| "org-1": { orgName: "Alpha", projects: [{ id: 1, name: "P1" }] }, | ||
| "org-2": { orgName: "Beta", projects: [{ id: 2, name: "P2" }] }, | ||
| }, | ||
| }; | ||
|
|
||
| const user = userEvent.setup({ pointerEventsCheck: 0 }); | ||
| renderInTheme(); | ||
|
|
||
| await user.click(screen.getByText("Demo project")); | ||
| await user.hover(await screen.findByText("Switch organization")); | ||
| fireEvent.click(await screen.findByRole("menuitem", { name: /Beta/ })); | ||
|
|
||
| expect(switchOrgMutate).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not call switchOrg when clicking the active org", async () => { | ||
| mockAuthState = { | ||
| cloudRegion: "us", | ||
| currentOrgId: "org-1", | ||
| orgProjectsMap: { | ||
| "org-1": { orgName: "Alpha", projects: [{ id: 1, name: "P1" }] }, | ||
| "org-2": { orgName: "Beta", projects: [{ id: 2, name: "P2" }] }, | ||
| }, | ||
| }; | ||
|
|
||
| const user = userEvent.setup({ pointerEventsCheck: 0 }); | ||
| renderInTheme(); | ||
|
|
||
| await user.click(screen.getByText("Demo project")); | ||
| await user.hover(await screen.findByText("Switch organization")); | ||
| fireEvent.click(await screen.findByRole("menuitem", { name: /Alpha/ })); | ||
|
|
||
| expect(switchOrgMutate).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.