-
Notifications
You must be signed in to change notification settings - Fork 13
fix(hub-ui): restore docks after initialization #276
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
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,154 @@ | ||
| import type { DevframeDockEntry } from '@devframes/hub' | ||
| import type { DevframeRpcClient, DockSessionStorage } from '@devframes/hub/client' | ||
| import type { SharedState } from 'devframe/utils/shared-state' | ||
| import { DEVFRAME_EVENTS } from 'devframe/constants' | ||
| import { createEventEmitter } from 'devframe/utils/events' | ||
| import { createSharedState } from 'devframe/utils/shared-state' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { nextTick, ref } from 'vue' | ||
| import { createDocksContext } from './context' | ||
| import { executeSetupScript } from './setup-script' | ||
|
|
||
| vi.mock('./setup-script', () => ({ | ||
| executeSetupScript: vi.fn(async () => {}), | ||
| })) | ||
|
|
||
| const gitEntry = { | ||
| id: 'git', | ||
| type: 'custom-render', | ||
| title: 'Git', | ||
| icon: 'ph:git-branch-duotone', | ||
| renderer: { importFrom: '/git-client.js' }, | ||
| } satisfies DevframeDockEntry | ||
|
|
||
| interface StubSharedState<Value extends object> extends SharedState<Value> { | ||
| push: (value: Value) => void | ||
| } | ||
|
|
||
| function createStubSharedState<Value extends object>(initialValue: Value): StubSharedState<Value> { | ||
| const state = createSharedState({ initialValue }) as StubSharedState<Value> | ||
| state.push = value => state.mutate(() => value) | ||
| return state | ||
| } | ||
|
|
||
| function createStubRpc() { | ||
| let isTrusted = false | ||
| const events = createEventEmitter<any>() | ||
| const sharedStates = new Map<string, StubSharedState<any>>() | ||
| const rpc = { | ||
| get isTrusted() { | ||
| return isTrusted | ||
| }, | ||
| status: 'connected', | ||
| connectionError: null, | ||
| connectionMeta: { backend: 'live', configs: {} }, | ||
| connection: {}, | ||
| events, | ||
| sharedState: { | ||
| async get(key: string, options?: { initialValue?: object }) { | ||
| if (!sharedStates.has(key)) | ||
| sharedStates.set(key, createStubSharedState(options?.initialValue ?? {})) | ||
| return sharedStates.get(key)! | ||
| }, | ||
| }, | ||
| client: { | ||
| register: vi.fn(), | ||
| }, | ||
| call: vi.fn(), | ||
| } as unknown as DevframeRpcClient | ||
|
|
||
| return { | ||
| rpc, | ||
| sharedStates, | ||
| trust() { | ||
| isTrusted = true | ||
| events.emit(DEVFRAME_EVENTS.client.isTrustedUpdated, true) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| async function flushRestore(): Promise<void> { | ||
| await Promise.resolve() | ||
| await Promise.resolve() | ||
| await nextTick() | ||
| } | ||
|
|
||
| describe('createDocksContext', () => { | ||
| it('mounts a restored dock once after all initial server state arrives', async () => { | ||
| expect.assertions(7) | ||
|
|
||
| const { rpc, sharedStates, trust } = createStubRpc() | ||
| const executeSetupScriptMock = vi.mocked(executeSetupScript) | ||
| executeSetupScriptMock.mockClear() | ||
| const session = ref<DockSessionStorage>({ | ||
| open: true, | ||
| selectedDockId: 'git', | ||
| selectedDockRoute: null, | ||
| }) | ||
| const context = await createDocksContext('embedded', rpc, undefined, session) | ||
|
|
||
| /** Mirrors the authorization gate temporarily closing the panel on reload. */ | ||
| session.value.open = false | ||
| trust() | ||
|
|
||
| expect(session.value.open).toBe(false) | ||
|
|
||
| sharedStates.get('devframe:docks')!.push([gitEntry]) | ||
| await flushRestore() | ||
|
|
||
| expect(context.docks.selected).toBeNull() | ||
| expect(session.value.open).toBe(false) | ||
| expect(executeSetupScriptMock).not.toHaveBeenCalled() | ||
|
|
||
| sharedStates.get('devframe:dock-renderers')!.push({}) | ||
| await flushRestore() | ||
|
|
||
| expect(context.docks.selected?.id).toBe('git') | ||
| expect(session.value.open).toBe(true) | ||
| expect(executeSetupScriptMock).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| it('keeps navigation performed before the initial server registry arrives', async () => { | ||
| expect.assertions(2) | ||
|
|
||
| const { rpc, sharedStates, trust } = createStubRpc() | ||
| const session = ref<DockSessionStorage>({ | ||
| open: true, | ||
| selectedDockId: 'git', | ||
| selectedDockRoute: null, | ||
| }) | ||
| const context = await createDocksContext('embedded', rpc, undefined, session) | ||
|
|
||
| session.value.open = false | ||
| trust() | ||
| await context.docks.switchEntry('~settings') | ||
|
|
||
| sharedStates.get('devframe:docks')!.push([gitEntry]) | ||
| sharedStates.get('devframe:dock-renderers')!.push({}) | ||
| await flushRestore() | ||
|
|
||
| expect(context.docks.selected?.id).toBe('~settings') | ||
| expect(session.value.open).toBe(true) | ||
| }) | ||
|
|
||
| it('keeps a dock closed when the user closes it before initialization finishes', async () => { | ||
| expect.assertions(2) | ||
|
|
||
| const { rpc, sharedStates, trust } = createStubRpc() | ||
| const session = ref<DockSessionStorage>({ | ||
| open: true, | ||
| selectedDockId: 'git', | ||
| selectedDockRoute: null, | ||
| }) | ||
| const context = await createDocksContext('embedded', rpc, undefined, session) | ||
|
|
||
| trust() | ||
| await context.docks.switchEntry(null) | ||
| sharedStates.get('devframe:docks')!.push([gitEntry]) | ||
| sharedStates.get('devframe:dock-renderers')!.push({}) | ||
| await flushRestore() | ||
|
|
||
| expect(context.docks.selected).toBeNull() | ||
| expect(session.value.open).toBe(false) | ||
| }) | ||
| }) | ||
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.