-
Notifications
You must be signed in to change notification settings - Fork 52
fix(canvas): make the channel sidebar feel instant #2681
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
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
101 changes: 101 additions & 0 deletions
101
packages/ui/src/features/canvas/hooks/useChannels.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,101 @@ | ||
| import type { Schemas } from "@posthog/api-client"; | ||
| import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
| import { act, renderHook, waitFor } from "@testing-library/react"; | ||
| import type { ReactNode } from "react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mockClient = vi.hoisted(() => ({ | ||
| getDesktopFileSystemChannels: vi.fn(), | ||
| createDesktopFileSystemChannel: vi.fn(), | ||
| })); | ||
| vi.mock("@posthog/ui/features/auth/authClient", () => ({ | ||
| useOptionalAuthenticatedClient: () => mockClient, | ||
| })); | ||
|
|
||
| import { useChannelMutations, useChannels } from "./useChannels"; | ||
|
|
||
| function folder(id: string, path: string): Schemas.FileSystem { | ||
| return { | ||
| id, | ||
| path, | ||
| type: "folder", | ||
| depth: 1, | ||
| created_at: "2026-01-01T00:00:00Z", | ||
| last_viewed_at: null, | ||
| }; | ||
| } | ||
|
|
||
| let queryClient: QueryClient; | ||
| function wrapper({ children }: { children: ReactNode }) { | ||
| return ( | ||
| <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
| ); | ||
| } | ||
|
|
||
| describe("useChannelMutations", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| queryClient = new QueryClient({ | ||
| defaultOptions: { queries: { retry: false } }, | ||
| }); | ||
| }); | ||
|
|
||
| it("shows the created channel immediately, before the refetch resolves", async () => { | ||
| // Seed the list with one existing channel. | ||
| mockClient.getDesktopFileSystemChannels.mockResolvedValue([ | ||
| folder("1", "alpha"), | ||
| ]); | ||
|
|
||
| const list = renderHook(() => useChannels(), { wrapper }); | ||
| await waitFor(() => expect(list.result.current.isLoading).toBe(false)); | ||
| expect(list.result.current.channels.map((c) => c.name)).toEqual(["alpha"]); | ||
|
|
||
| // Make the create return the new channel, but hang any subsequent refetch | ||
| // so we can prove the list updates without waiting on it. | ||
| const created = folder("2", "beta"); | ||
| mockClient.createDesktopFileSystemChannel.mockResolvedValue(created); | ||
| mockClient.getDesktopFileSystemChannels.mockReturnValue( | ||
| new Promise(() => {}), | ||
| ); | ||
|
|
||
| const mutations = renderHook(() => useChannelMutations(), { wrapper }); | ||
| await act(async () => { | ||
| await mutations.result.current.createChannel("beta"); | ||
| }); | ||
|
|
||
| // The new channel is present from the optimistic cache write, sorted | ||
| // alphabetically alongside the existing one — without the hung refetch | ||
| // having resolved. | ||
| await waitFor(() => | ||
| expect(list.result.current.channels.map((c) => c.name)).toEqual([ | ||
| "alpha", | ||
| "beta", | ||
| ]), | ||
| ); | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| it("does not duplicate a channel the poll already landed", async () => { | ||
| // The poll has already surfaced the channel we're about to create. | ||
| const existing = folder("1", "alpha"); | ||
| mockClient.getDesktopFileSystemChannels.mockResolvedValue([existing]); | ||
|
|
||
| const list = renderHook(() => useChannels(), { wrapper }); | ||
| await waitFor(() => expect(list.result.current.isLoading).toBe(false)); | ||
| expect(list.result.current.channels.map((c) => c.id)).toEqual(["1"]); | ||
|
|
||
| // Create returns the same id; hang the refetch so only the optimistic | ||
| // cache write is exercised. | ||
| mockClient.createDesktopFileSystemChannel.mockResolvedValue(existing); | ||
| mockClient.getDesktopFileSystemChannels.mockReturnValue( | ||
| new Promise(() => {}), | ||
| ); | ||
|
|
||
| const mutations = renderHook(() => useChannelMutations(), { wrapper }); | ||
| await act(async () => { | ||
| await mutations.result.current.createChannel("alpha"); | ||
| }); | ||
|
|
||
| // The duplicate-id guard keeps the list at one entry. | ||
| expect(list.result.current.channels.map((c) => c.id)).toEqual(["1"]); | ||
| }); | ||
| }); | ||
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.