Repository navigation
fix(workspaces): scope a departure's ownership scan to the workspaces it leaves #10751
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
5 commits
Select commit
Hold shift + click to select a range
7b301c7
fix(workspaces): scope a departure's ownership scan to the workspaces…
myxamediyar 33b4361
improvement(workspaces): leave a workspace and remove a teammate opti…
waleedlatif1 6abfe22
fix(workspaces): keep a left workspace out of the switcher while a li…
myxamediyar e69cfbb
fix(workspaces): return the user to where they were when a leave is r…
myxamediyar 00e9b72
test(workspaces): yield to the macrotask queue instead of sleeping in…
myxamediyar 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
128 changes: 128 additions & 0 deletions
128
.../workspace/[workspaceId]/w/components/sidebar/hooks/use-workspace-management.dom.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,128 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
|
|
||
| import { act } from 'react' | ||
| import { flushMacrotask } from '@sim/testing/helpers/async' | ||
| import { | ||
| apiClientRequestMock, | ||
| apiClientRequestMockFns, | ||
| } from '@sim/testing/mocks/api-client-request.mock' | ||
| import { nextNavigationMock, nextNavigationMockFns } from '@sim/testing/mocks/next-navigation.mock' | ||
| import { workflowRegistryStoreMock } from '@sim/testing/mocks/workflow-registry-store.mock' | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
| import { createRoot, type Root } from 'react-dom/client' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('@/lib/api/client/request', () => apiClientRequestMock) | ||
| vi.mock('next/navigation', () => nextNavigationMock) | ||
| vi.mock('@/stores/workflows/registry/store', () => workflowRegistryStoreMock) | ||
|
|
||
| import { ApiClientError } from '@/lib/api/client/errors' | ||
| import { listWorkspacesContract } from '@/lib/api/contracts' | ||
| import { removeWorkspaceMemberContract } from '@/lib/api/contracts/invitations' | ||
| import { useWorkspaceManagement } from '@/app/workspace/[workspaceId]/w/components/sidebar/hooks/use-workspace-management' | ||
|
|
||
| const mockRequestJson = apiClientRequestMockFns.mockRequestJson | ||
| const push = nextNavigationMockFns.router.push | ||
| const LEFT_PATH = '/workspace/ws-a/w/flow-1' | ||
|
|
||
| const workspace = (id: string) => ({ id, name: id, ownerId: 'owner', permissions: 'write' }) | ||
|
|
||
| let root: Root | undefined | ||
| let management: ReturnType<typeof useWorkspaceManagement> | undefined | ||
|
|
||
| function Probe({ workspaceId }: { workspaceId: string }) { | ||
| management = useWorkspaceManagement({ workspaceId, sessionUserId: 'me' }) | ||
| return null | ||
| } | ||
|
|
||
| const queryClient = () => | ||
| new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }) | ||
|
|
||
| function render(client: QueryClient, workspaceId: string) { | ||
| act(() => { | ||
| root?.render( | ||
| <QueryClientProvider client={client}> | ||
| <Probe workspaceId={workspaceId} /> | ||
| </QueryClientProvider> | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| async function flush() { | ||
| await act(async () => { | ||
| for (let i = 0; i < 10; i++) await flushMacrotask() | ||
| }) | ||
| } | ||
|
|
||
| describe('leaving the open workspace', () => { | ||
| let refuseLeave: (error: unknown) => void = () => {} | ||
|
|
||
| beforeEach(() => { | ||
| ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true | ||
| mockRequestJson.mockReset() | ||
| nextNavigationMockFns.mockUsePathname.mockReturnValue(LEFT_PATH) | ||
| mockRequestJson.mockImplementation((contract) => { | ||
| if (contract === listWorkspacesContract) { | ||
| return Promise.resolve({ | ||
| workspaces: ['ws-a', 'ws-b', 'ws-c'].map(workspace), | ||
| lastActiveWorkspaceId: 'ws-a', | ||
| pinnedWorkspaceIds: [], | ||
| creationPolicy: null, | ||
| }) | ||
| } | ||
| if (contract === removeWorkspaceMemberContract) { | ||
| return new Promise((_resolve, reject) => { | ||
| refuseLeave = reject | ||
| }) | ||
| } | ||
| return Promise.resolve({ success: true }) | ||
| }) | ||
| root = createRoot(document.createElement('div')) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| act(() => root?.unmount()) | ||
| root = undefined | ||
| management = undefined | ||
| }) | ||
|
|
||
| const refusal = () => | ||
| new ApiClientError({ status: 400, message: 'Cannot remove the last admin', body: {} }) | ||
|
|
||
| it('returns the user to where they were when the leave is refused', async () => { | ||
| const client = queryClient() | ||
| render(client, 'ws-a') | ||
| await flush() | ||
|
|
||
| let leaving: Promise<void> | undefined | ||
| act(() => { | ||
| leaving = management?.handleLeaveWorkspace(workspace('ws-a') as never) | ||
| }) | ||
| await flush() | ||
| expect(push).toHaveBeenLastCalledWith('/workspace/ws-b') | ||
|
|
||
| render(client, 'ws-b') | ||
| refuseLeave(refusal()) | ||
| await act(() => expect(leaving).rejects.toThrow('Cannot remove the last admin')) | ||
| expect(push).toHaveBeenLastCalledWith(LEFT_PATH) | ||
| }) | ||
|
|
||
| it('leaves a user who has moved on to another workspace where they are', async () => { | ||
| const client = queryClient() | ||
| render(client, 'ws-a') | ||
| await flush() | ||
|
|
||
| let leaving: Promise<void> | undefined | ||
| act(() => { | ||
| leaving = management?.handleLeaveWorkspace(workspace('ws-a') as never) | ||
| }) | ||
| await flush() | ||
|
|
||
| render(client, 'ws-c') | ||
| refuseLeave(refusal()) | ||
| await act(() => expect(leaving).rejects.toThrow('Cannot remove the last admin')) | ||
| expect(push).not.toHaveBeenCalledWith(LEFT_PATH) | ||
| }) | ||
| }) |
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
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
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.
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.