|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act } from 'react' |
| 3 | +import { createRoot, type Root } from 'react-dom/client' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import { ConnectorConfigFields } from '@/app/workspace/[workspaceId]/knowledge/[id]/components/connector-config-fields/connector-config-fields' |
| 6 | +import { |
| 7 | + type ConfigFieldValue, |
| 8 | + useConnectorConfigFields, |
| 9 | +} from '@/app/workspace/[workspaceId]/knowledge/[id]/hooks/use-connector-config-fields' |
| 10 | +import { gmailConnectorMeta } from '@/connectors/gmail/meta' |
| 11 | + |
| 12 | +vi.mock('@/app/workspace/[workspaceId]/knowledge/[id]/components/connector-selector-field', () => ({ |
| 13 | + ConnectorSelectorField: ({ value }: { value: ConfigFieldValue }) => ( |
| 14 | + <span data-testid='selector-value'>{Array.isArray(value) ? value.join(',') : value}</span> |
| 15 | + ), |
| 16 | +})) |
| 17 | + |
| 18 | +const CONNECTOR = { |
| 19 | + ...gmailConnectorMeta, |
| 20 | + configFields: gmailConnectorMeta.configFields.filter( |
| 21 | + (field) => field.canonicalParamId === 'label' |
| 22 | + ), |
| 23 | +} |
| 24 | + |
| 25 | +interface HarnessProps { |
| 26 | + disabled?: boolean |
| 27 | +} |
| 28 | + |
| 29 | +function Harness({ disabled = false }: HarnessProps) { |
| 30 | + const config = useConnectorConfigFields({ |
| 31 | + connectorConfig: CONNECTOR, |
| 32 | + initialSourceConfig: { labelSelector: ['INBOX', 'IMPORTANT'], label: ['STARRED'] }, |
| 33 | + }) |
| 34 | + return ( |
| 35 | + <ConnectorConfigFields |
| 36 | + connectorConfig={CONNECTOR} |
| 37 | + sourceConfig={config.sourceConfig} |
| 38 | + credentialId={null} |
| 39 | + canonicalGroups={config.canonicalGroups} |
| 40 | + canonicalModes={config.canonicalModes} |
| 41 | + isFieldVisible={config.isFieldVisible} |
| 42 | + onFieldChange={config.handleFieldChange} |
| 43 | + onToggleCanonicalMode={config.toggleCanonicalMode} |
| 44 | + disabled={disabled} |
| 45 | + /> |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +let root: Root |
| 50 | +let container: HTMLDivElement |
| 51 | + |
| 52 | +beforeEach(() => { |
| 53 | + vi.useFakeTimers() |
| 54 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 55 | + container = document.createElement('div') |
| 56 | + document.body.appendChild(container) |
| 57 | + root = createRoot(container) |
| 58 | +}) |
| 59 | + |
| 60 | +afterEach(() => { |
| 61 | + act(() => root.unmount()) |
| 62 | + container.remove() |
| 63 | + vi.useRealTimers() |
| 64 | +}) |
| 65 | + |
| 66 | +function radio(label: string): HTMLInputElement { |
| 67 | + const input = container.querySelector<HTMLInputElement>( |
| 68 | + `input[type="radio"][aria-label="${label}"]` |
| 69 | + ) |
| 70 | + if (!input) throw new Error(`Missing mode option: ${label}`) |
| 71 | + return input |
| 72 | +} |
| 73 | + |
| 74 | +describe('connector input mode switch', () => { |
| 75 | + it("preserves each mode's stored values when switching to manual input and back", () => { |
| 76 | + act(() => root.render(<Harness />)) |
| 77 | + expect(radio('Selector').checked).toBe(true) |
| 78 | + |
| 79 | + act(() => radio('Manual input').click()) |
| 80 | + expect(radio('Manual input').checked).toBe(true) |
| 81 | + expect(container.querySelector<HTMLInputElement>('input:not([type="radio"])')?.value).toBe( |
| 82 | + 'STARRED' |
| 83 | + ) |
| 84 | + |
| 85 | + act(() => radio('Manual input').click()) |
| 86 | + expect(radio('Manual input').checked).toBe(true) |
| 87 | + |
| 88 | + act(() => radio('Selector').click()) |
| 89 | + expect(radio('Selector').checked).toBe(true) |
| 90 | + expect(container.querySelector('[data-testid="selector-value"]')?.textContent).toBe( |
| 91 | + 'INBOX,IMPORTANT' |
| 92 | + ) |
| 93 | + }) |
| 94 | + |
| 95 | + it('keeps the switch outside the field label and ignores clicks on the title', () => { |
| 96 | + act(() => root.render(<Harness />)) |
| 97 | + expect(container.querySelector('[role="radiogroup"]')?.closest('label')).toBeNull() |
| 98 | + act(() => container.querySelector('label')?.click()) |
| 99 | + expect(radio('Selector').checked).toBe(true) |
| 100 | + }) |
| 101 | + |
| 102 | + it('prevents mode changes while submission disables the fields', () => { |
| 103 | + act(() => root.render(<Harness disabled />)) |
| 104 | + expect(radio('Selector').disabled).toBe(true) |
| 105 | + expect(radio('Manual input').disabled).toBe(true) |
| 106 | + act(() => radio('Manual input').click()) |
| 107 | + expect(radio('Selector').checked).toBe(true) |
| 108 | + }) |
| 109 | +}) |
0 commit comments