|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The Flow Runs flow picker is the shared `Select` (objectui#11865). |
| 5 | + * |
| 6 | + * The page picked the flow to test with a browser-native select, beside the |
| 7 | + * shared Radix `Select` the rest of the console picks with. The card asks for |
| 8 | + * one control for one kind of choice, surface by surface. |
| 9 | + * |
| 10 | + * What is pinned: |
| 11 | + * - the picker IS the primitive (a Radix combobox trigger), shows the picked |
| 12 | + * flow, and no native select is left; |
| 13 | + * - the trigger's accessible name is what the native control's was; |
| 14 | + * - every flow, picked, is the one Run Flow executes and whose runs are |
| 15 | + * listed, the same calls the native control led to, compared as JSON text; |
| 16 | + * - a flow a Refresh no longer lists is what the trigger shows; |
| 17 | + * - the keyboard alone opens the picker and selects. |
| 18 | + * |
| 19 | + * Read-only: the page has no such state. |
| 20 | + * |
| 21 | + * DIRECTION, observed against the native control: every pin here but the name |
| 22 | + * pin is red there, because each one reads the picker as the primitive's |
| 23 | + * trigger. What makes the call pins guards of "the conversion changed nothing |
| 24 | + * the page runs" is the literal each compares against: a `change` event on the |
| 25 | + * pre-conversion page's native control, then the same Run Flow click, led to |
| 26 | + * those same calls, read once on that page with these fixtures. The name and |
| 27 | + * the Refresh reading were taken there the same way. |
| 28 | + * |
| 29 | + * A pick reloads the flow list (the page's `loadFlows` depends on the picked |
| 30 | + * name), so the picker unmounts while the list loads and mounts again: each |
| 31 | + * case finds the trigger afresh after a pick, as a user's next look would. |
| 32 | + */ |
| 33 | + |
| 34 | +import '@testing-library/jest-dom/vitest'; |
| 35 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 36 | +import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 37 | + |
| 38 | +const { execute, listRuns, catalog, ADAPTER } = vi.hoisted(() => { |
| 39 | + const catalog = { flows: [] as Array<{ spec: Record<string, unknown> }> }; |
| 40 | + const execute = vi.fn(async () => ({ success: true, status: 'completed', durationMs: 1 })); |
| 41 | + const listRuns = vi.fn(async () => ({ runs: [] })); |
| 42 | + // A STABLE singleton: a fresh object per render loops the page's fetch effects. |
| 43 | + const client = { |
| 44 | + meta: { getItems: async (type: string) => (type === 'flow' ? catalog.flows : []) }, |
| 45 | + automation: { execute, listRuns }, |
| 46 | + }; |
| 47 | + return { execute, listRuns, catalog, ADAPTER: { getClient: () => client } }; |
| 48 | +}); |
| 49 | + |
| 50 | +vi.mock('@object-ui/app-shell', async (importOriginal) => ({ |
| 51 | + ...(await importOriginal<Record<string, unknown>>()), |
| 52 | + useAdapter: () => ADAPTER, |
| 53 | + useMetadata: () => ({ objects: [] }), |
| 54 | +})); |
| 55 | + |
| 56 | +// Imported AFTER the mocks so the page picks them up. |
| 57 | +import { FlowRunsPage } from './FlowRunsPage'; |
| 58 | + |
| 59 | +const REASSIGN = { spec: { name: 'reassign_wizard', label: 'Reassign', variables: [] } }; |
| 60 | +const NIGHTLY = { spec: { name: 'nightly_sync', variables: [] } }; |
| 61 | + |
| 62 | +beforeEach(() => { |
| 63 | + catalog.flows = [REASSIGN, NIGHTLY]; |
| 64 | + execute.mockClear(); |
| 65 | + listRuns.mockClear(); |
| 66 | +}); |
| 67 | +afterEach(cleanup); |
| 68 | + |
| 69 | +/** The picker, once the flow list has loaded (again). */ |
| 70 | +async function picker(): Promise<HTMLElement> { |
| 71 | + await screen.findByRole('button', { name: /Run Flow/i }); |
| 72 | + return screen.findByRole('combobox'); |
| 73 | +} |
| 74 | + |
| 75 | +/** Open the picker from the keyboard and return the options it lists, in order. */ |
| 76 | +async function openPicker(trigger: HTMLElement): Promise<HTMLElement[]> { |
| 77 | + fireEvent.keyDown(trigger, { key: 'ArrowDown' }); |
| 78 | + const listbox = await screen.findByRole('listbox'); |
| 79 | + return within(listbox).getAllByRole('option'); |
| 80 | +} |
| 81 | + |
| 82 | +async function pick(label: string): Promise<void> { |
| 83 | + const options = await openPicker(await picker()); |
| 84 | + const option = options.find((o) => o.textContent === label); |
| 85 | + if (!option) throw new Error(`no "${label}" listed: ${options.map((o) => o.textContent).join(' | ')}`); |
| 86 | + fireEvent.click(option); |
| 87 | + await waitFor(() => expect(screen.queryByRole('listbox')).not.toBeInTheDocument()); |
| 88 | +} |
| 89 | + |
| 90 | +describe('the flow picker is the shared Select (objectui#11865)', () => { |
| 91 | + it('renders the picker as the Radix combobox trigger, showing the first flow', async () => { |
| 92 | + render(<FlowRunsPage />); |
| 93 | + const trigger = await picker(); |
| 94 | + expect(trigger.tagName).toBe('BUTTON'); |
| 95 | + expect(trigger).toHaveTextContent('Reassign (reassign_wizard)'); |
| 96 | + expect(document.querySelector('select')).toBeNull(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('lists the flows in the order the native control did', async () => { |
| 100 | + render(<FlowRunsPage />); |
| 101 | + expect((await openPicker(await picker())).map((o) => o.textContent)).toEqual([ |
| 102 | + 'Reassign (reassign_wizard)', |
| 103 | + 'nightly_sync', |
| 104 | + ]); |
| 105 | + }); |
| 106 | + |
| 107 | + // Green against the native control too, by design. That control had no |
| 108 | + // label, id or aria-label, so its accessible name was empty; the swap keeps |
| 109 | + // that, and adds none. |
| 110 | + it('keeps the accessible name the native control had: none', async () => { |
| 111 | + render(<FlowRunsPage />); |
| 112 | + const trigger = await picker(); |
| 113 | + expect(screen.getByRole('combobox', { name: '' })).toBe(trigger); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +/** |
| 118 | + * [the flow picked away from, its label, option label, Run Flow's `execute` |
| 119 | + * calls, the panel's `listRuns` calls once the pick has settled]. |
| 120 | + */ |
| 121 | +const RUN_WRITES: ReadonlyArray< |
| 122 | + readonly [from: string, fromLabel: string, label: string, execute: string, listRuns: string] |
| 123 | +> = [ |
| 124 | + ['nightly_sync', 'nightly_sync', 'Reassign (reassign_wizard)', '[["reassign_wizard",{"params":{}}]]', '[["reassign_wizard",{"limit":20}]]'], |
| 125 | + ['reassign_wizard', 'Reassign (reassign_wizard)', 'nightly_sync', '[["nightly_sync",{"params":{}}]]', '[["nightly_sync",{"limit":20}]]'], |
| 126 | +]; |
| 127 | + |
| 128 | +describe('every flow, picked, is the one the page runs', () => { |
| 129 | + it.each(RUN_WRITES)('from %s (shown as %s), picking %s', async (from, fromLabel, label, executed, listed) => { |
| 130 | + render(<FlowRunsPage />); |
| 131 | + await picker(); |
| 132 | + if (from !== 'reassign_wizard') await pick(fromLabel); |
| 133 | + // Settled on the flow picked away from before anything is counted. |
| 134 | + expect(await picker()).toHaveTextContent(fromLabel); |
| 135 | + await waitFor(() => expect(listRuns).toHaveBeenLastCalledWith(from, { limit: 20 })); |
| 136 | + execute.mockClear(); |
| 137 | + listRuns.mockClear(); |
| 138 | + await pick(label); |
| 139 | + await waitFor(() => expect(listRuns).toHaveBeenCalled()); |
| 140 | + expect(await picker()).toHaveTextContent(label); |
| 141 | + fireEvent.click(screen.getByRole('button', { name: /Run Flow/i })); |
| 142 | + await waitFor(() => expect(execute).toHaveBeenCalledTimes(1)); |
| 143 | + expect(JSON.stringify(execute.mock.calls)).toBe(executed); |
| 144 | + expect(JSON.stringify(listRuns.mock.calls)).toBe(listed); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe('a flow no option carries is what the trigger shows', () => { |
| 149 | + it('a picked flow a Refresh no longer lists: shown, not the first flow, and re-picking it runs nothing', async () => { |
| 150 | + render(<FlowRunsPage />); |
| 151 | + await pick('nightly_sync'); |
| 152 | + await picker(); |
| 153 | + catalog.flows = [REASSIGN]; |
| 154 | + fireEvent.click(screen.getAllByRole('button', { name: /Refresh/i })[0]); |
| 155 | + // The page still holds `nightly_sync` and shows no runner for it. The |
| 156 | + // native control showed "Reassign (reassign_wizard)" here. |
| 157 | + await waitFor(() => expect(screen.queryByRole('button', { name: /Run Flow/i })).not.toBeInTheDocument()); |
| 158 | + const trigger = await screen.findByRole('combobox'); |
| 159 | + expect(trigger).toHaveTextContent('nightly_sync'); |
| 160 | + const listed = (await openPicker(trigger)).map((o) => o.textContent); |
| 161 | + expect(listed).toEqual(['nightly_sync', 'Reassign (reassign_wizard)']); |
| 162 | + execute.mockClear(); |
| 163 | + listRuns.mockClear(); |
| 164 | + fireEvent.click(screen.getAllByRole('option')[0]); |
| 165 | + await waitFor(() => expect(screen.queryByRole('listbox')).not.toBeInTheDocument()); |
| 166 | + expect(screen.getByRole('combobox')).toHaveTextContent('nightly_sync'); |
| 167 | + expect(screen.queryByRole('button', { name: /Run Flow/i })).not.toBeInTheDocument(); |
| 168 | + expect(listRuns).not.toHaveBeenCalled(); |
| 169 | + }); |
| 170 | +}); |
| 171 | + |
| 172 | +describe('the keyboard alone picks', () => { |
| 173 | + it('Enter opens the picker and Enter on a flow selects it', async () => { |
| 174 | + render(<FlowRunsPage />); |
| 175 | + fireEvent.keyDown(await picker(), { key: 'Enter' }); |
| 176 | + const listbox = await screen.findByRole('listbox'); |
| 177 | + fireEvent.keyDown(within(listbox).getByRole('option', { name: 'nightly_sync' }), { key: 'Enter' }); |
| 178 | + await waitFor(() => expect(screen.queryByRole('listbox')).not.toBeInTheDocument()); |
| 179 | + expect(await picker()).toHaveTextContent('nightly_sync'); |
| 180 | + fireEvent.click(screen.getByRole('button', { name: /Run Flow/i })); |
| 181 | + await waitFor(() => expect(execute).toHaveBeenCalledWith('nightly_sync', { params: {} })); |
| 182 | + }); |
| 183 | +}); |
0 commit comments