|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 6 | +import { createRoot, type Root } from 'react-dom/client' |
| 7 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 8 | +import type { SubBlockConfig } from '@/blocks/types' |
| 9 | +import { useDynamicSubBlockOptionDisplayName } from '@/hooks/queries/dynamic-subblock-options' |
| 10 | + |
| 11 | +interface HookHarness<T> { |
| 12 | + result: () => T |
| 13 | + unmount: () => void |
| 14 | +} |
| 15 | + |
| 16 | +function renderHookWithClient<T>(useHook: () => T): HookHarness<T> { |
| 17 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 18 | + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) |
| 19 | + const container = document.createElement('div') |
| 20 | + const root: Root = createRoot(container) |
| 21 | + let latest!: T |
| 22 | + |
| 23 | + function Probe() { |
| 24 | + latest = useHook() |
| 25 | + return null |
| 26 | + } |
| 27 | + |
| 28 | + act(() => { |
| 29 | + root.render( |
| 30 | + <QueryClientProvider client={queryClient}> |
| 31 | + <Probe /> |
| 32 | + </QueryClientProvider> |
| 33 | + ) |
| 34 | + }) |
| 35 | + |
| 36 | + return { |
| 37 | + result: () => latest, |
| 38 | + unmount: () => act(() => root.unmount()), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +async function waitForResult(assertion: () => void) { |
| 43 | + await act(async () => { |
| 44 | + await vi.waitFor(assertion, { interval: 1 }) |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +describe('useDynamicSubBlockOptionDisplayName', () => { |
| 49 | + const mounted: Array<() => void> = [] |
| 50 | + |
| 51 | + afterEach(() => { |
| 52 | + mounted.splice(0).forEach((unmount) => unmount()) |
| 53 | + vi.clearAllMocks() |
| 54 | + }) |
| 55 | + |
| 56 | + it('hydrates a stored dynamic dropdown id to its label', async () => { |
| 57 | + const fetchOptionById = vi.fn(async (_blockId: string, optionId: string) => ({ |
| 58 | + id: optionId, |
| 59 | + label: 'Customer support accounts', |
| 60 | + })) |
| 61 | + const subBlock = { |
| 62 | + id: 'credentialGroup', |
| 63 | + title: 'Credential Group', |
| 64 | + type: 'dropdown', |
| 65 | + options: [], |
| 66 | + fetchOptionById, |
| 67 | + } satisfies SubBlockConfig |
| 68 | + |
| 69 | + const hook = renderHookWithClient(() => |
| 70 | + useDynamicSubBlockOptionDisplayName({ |
| 71 | + workspaceId: 'workspace-1', |
| 72 | + blockId: 'block-1', |
| 73 | + subBlock, |
| 74 | + value: 'group-uuid', |
| 75 | + }) |
| 76 | + ) |
| 77 | + mounted.push(hook.unmount) |
| 78 | + |
| 79 | + await waitForResult(() => expect(hook.result()).toBe('Customer support accounts')) |
| 80 | + |
| 81 | + expect(fetchOptionById).toHaveBeenCalledWith('block-1', 'group-uuid', expect.any(AbortSignal)) |
| 82 | + }) |
| 83 | + |
| 84 | + it('summarizes every selected dynamic option without dropping ids', async () => { |
| 85 | + const fetchOptionById = vi.fn(async (_blockId: string, optionId: string) => ({ |
| 86 | + id: optionId, |
| 87 | + label: optionId === 'gmail' ? 'Gmail' : 'Slack', |
| 88 | + })) |
| 89 | + const subBlock = { |
| 90 | + id: 'providerFilter', |
| 91 | + title: 'Provider', |
| 92 | + type: 'dropdown', |
| 93 | + options: [], |
| 94 | + multiSelect: true, |
| 95 | + fetchOptionById, |
| 96 | + } satisfies SubBlockConfig |
| 97 | + |
| 98 | + const hook = renderHookWithClient(() => |
| 99 | + useDynamicSubBlockOptionDisplayName({ |
| 100 | + workspaceId: 'workspace-1', |
| 101 | + blockId: 'block-1', |
| 102 | + subBlock, |
| 103 | + value: ['gmail', 'slack'], |
| 104 | + }) |
| 105 | + ) |
| 106 | + mounted.push(hook.unmount) |
| 107 | + |
| 108 | + await waitForResult(() => expect(hook.result()).toBe('Gmail, Slack')) |
| 109 | + }) |
| 110 | +}) |
0 commit comments