|
| 1 | +import type { DevframeViewIframe } from '@devframes/hub' |
| 2 | +import type { DevframeRpcClient } from '@devframes/hub/client' |
| 3 | +import { createEventEmitter } from 'devframe/utils/events' |
| 4 | +import { createSharedState } from 'devframe/utils/shared-state' |
| 5 | +import { afterEach, expect, it, vi } from 'vitest' |
| 6 | +import { nextTick } from 'vue' |
| 7 | +import { loadLocale, setLocalePreference } from '../i18n' |
| 8 | +import { createDocksContext } from './context' |
| 9 | + |
| 10 | +function createRpc(): DevframeRpcClient { |
| 11 | + // The transport is in memory; registration, localization and sorting use the real context. |
| 12 | + // eslint-disable-next-line slop/no-chained-type-assertions -- this fixture supplies only the RPC surface used by the context. |
| 13 | + return { |
| 14 | + sharedState: { get: async (_key: string, options: { initialValue: object }) => createSharedState({ initialValue: options.initialValue }) }, |
| 15 | + connectionMeta: {}, |
| 16 | + events: createEventEmitter(), |
| 17 | + client: { register: vi.fn() }, |
| 18 | + isTrusted: false, |
| 19 | + } as unknown as DevframeRpcClient |
| 20 | +} |
| 21 | + |
| 22 | +afterEach(() => setLocalePreference('auto')) |
| 23 | + |
| 24 | +it.each(['embedded', 'standalone'] as const)('preserves title updates and ordering through the %s context', async (mode) => { |
| 25 | + setLocalePreference('en') |
| 26 | + const context = await createDocksContext(mode, createRpc()) |
| 27 | + const handle = context.docks.register<DevframeViewIframe>({ id: 'demo', type: 'iframe', title: 'Old title', icon: 'ph:cube', url: '/' }) |
| 28 | + context.docks.register({ id: 'peer', type: 'iframe', title: 'Middle', icon: 'ph:cube', url: '/' }) |
| 29 | + const entry = () => context.docks.entries.find(item => item.id === 'demo')! |
| 30 | + const order = () => context.docks.groupedEntries.flatMap(([, items]) => items.map(item => item.id)).filter(id => id !== '~settings') |
| 31 | + expect(order()).toEqual(['demo', 'peer']) |
| 32 | + |
| 33 | + context.docks.update({ ...entry(), title: 'A new title' }) |
| 34 | + await nextTick() |
| 35 | + expect(entry().title).toBe('A new title') |
| 36 | + expect(context.docks.getStateById('demo')?.entryMeta.title).toBe('A new title') |
| 37 | + expect(order()).toEqual(['peer', 'demo']) |
| 38 | + |
| 39 | + handle.update({ titleLocales: { ja: '日本語のタイトル' } }) |
| 40 | + await loadLocale('ja') |
| 41 | + setLocalePreference('ja') |
| 42 | + await nextTick() |
| 43 | + expect(entry().title).toBe('日本語のタイトル') |
| 44 | + expect(order()).toEqual(['peer', 'demo']) |
| 45 | + context.docks.update({ ...entry(), icon: 'ph:star' }) |
| 46 | + setLocalePreference('en') |
| 47 | + await nextTick() |
| 48 | + expect(entry().title).toBe('A new title') |
| 49 | + |
| 50 | + handle.update({ title: 'Z title' }) |
| 51 | + await nextTick() |
| 52 | + expect(entry().title).toBe('Z title') |
| 53 | + expect(order()).toEqual(['demo', 'peer']) |
| 54 | + context.docks.settings.mutate((state) => { |
| 55 | + state.docksCustomOrder = { peer: -1, demo: 1 } |
| 56 | + }) |
| 57 | + await nextTick() |
| 58 | + for (const language of ['ja', 'en'] as const) { |
| 59 | + setLocalePreference(language) |
| 60 | + await nextTick() |
| 61 | + expect(order()).toEqual(['peer', 'demo']) |
| 62 | + } |
| 63 | + handle.dispose() |
| 64 | +}) |
0 commit comments