|
| 1 | +import type { ComponentProps, PropsWithChildren } from 'react' |
| 2 | +import { JSDOM } from 'jsdom' |
| 3 | +import { renderToStaticMarkup } from 'react-dom/server' |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const validation = vi.hoisted(() => ({ |
| 7 | + current: { isChecking: false, error: 'Use lowercase letters', isValid: false } as { |
| 8 | + isChecking: boolean |
| 9 | + error: string | null |
| 10 | + isValid: boolean |
| 11 | + }, |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@sim/emcn', () => ({ |
| 15 | + Input: (props: ComponentProps<'input'>) => <input {...props} />, |
| 16 | + Label: (props: ComponentProps<'label'>) => ( |
| 17 | + <label htmlFor={props.htmlFor} className={props.className}> |
| 18 | + {props.children} |
| 19 | + </label> |
| 20 | + ), |
| 21 | + cn: (...values: unknown[]) => values.filter(Boolean).join(' '), |
| 22 | + Tooltip: { |
| 23 | + Root: ({ children }: PropsWithChildren) => <>{children}</>, |
| 24 | + Trigger: ({ children }: PropsWithChildren) => <>{children}</>, |
| 25 | + Content: ({ children }: PropsWithChildren) => <>{children}</>, |
| 26 | + }, |
| 27 | +})) |
| 28 | +vi.mock('@sim/emcn/icons', () => ({ Check: () => null, TriangleAlert: () => null })) |
| 29 | +vi.mock('@sim/logger', () => ({ createLogger: () => ({}) })) |
| 30 | +vi.mock('@/components/ui', () => ({ GeneratedPasswordInput: () => null })) |
| 31 | +vi.mock('@/lib/core/config/deployment-shape', () => ({ useDeploymentShape: () => ({}) })) |
| 32 | +vi.mock('@/lib/core/utils/urls', () => ({ |
| 33 | + getBaseUrl: () => 'https://sim.ai', |
| 34 | + getEmailDomain: () => 'sim.ai', |
| 35 | +})) |
| 36 | +vi.mock('@/lib/messaging/email/validation', () => ({ validateAllowlistEntry: () => true })) |
| 37 | +vi.mock('@/lib/workflows/streaming/output-selector', () => ({ |
| 38 | + formatInternalOutputSelector: () => '', |
| 39 | +})) |
| 40 | +vi.mock( |
| 41 | + '@/app/workspace/[workspaceId]/w/[workflowId]/components/chat/components/output-select/output-select', |
| 42 | + () => ({ |
| 43 | + OutputSelect: () => null, |
| 44 | + }) |
| 45 | +) |
| 46 | +vi.mock('@/hooks/queries/chats', () => ({ |
| 47 | + useCreateChat: () => ({}), |
| 48 | + useDeleteChat: () => ({}), |
| 49 | + useRevealChatPassword: () => ({}), |
| 50 | + useUpdateChat: () => ({}), |
| 51 | +})) |
| 52 | +vi.mock('@/hooks/use-permission-config', () => ({ usePermissionConfig: () => ({}) })) |
| 53 | +vi.mock('./hooks', () => ({ useIdentifierValidation: () => validation.current })) |
| 54 | + |
| 55 | +import { IdentifierInput } from './chat' |
| 56 | + |
| 57 | +function renderIdentifier() { |
| 58 | + return new JSDOM(renderToStaticMarkup(<IdentifierInput value='bad path' onChange={vi.fn()} />)) |
| 59 | + .window.document |
| 60 | +} |
| 61 | + |
| 62 | +describe('deploy URL field error', () => { |
| 63 | + it('announces and associates the URL validation error with its input', () => { |
| 64 | + validation.current = { isChecking: false, error: 'Use lowercase letters', isValid: false } |
| 65 | + const document = renderIdentifier() |
| 66 | + const input = document.querySelector<HTMLInputElement>('#chat-url') |
| 67 | + const alert = document.querySelector<HTMLElement>('[role="alert"]') |
| 68 | + |
| 69 | + expect(alert?.textContent).toBe('Use lowercase letters') |
| 70 | + expect(alert?.className).toBe('mt-[6.5px] text-[var(--text-error)] text-caption') |
| 71 | + expect(input?.getAttribute('aria-invalid')).toBe('true') |
| 72 | + expect(input?.getAttribute('aria-describedby')).toBe(alert?.id) |
| 73 | + expect(alert?.id).toBeTruthy() |
| 74 | + expect(document.querySelector('label')?.htmlFor).toBe(input?.id) |
| 75 | + }) |
| 76 | + |
| 77 | + it('omits the error relationship when the URL is valid', () => { |
| 78 | + validation.current = { isChecking: false, error: null, isValid: true } |
| 79 | + const document = renderIdentifier() |
| 80 | + const input = document.querySelector<HTMLInputElement>('#chat-url') |
| 81 | + |
| 82 | + expect(document.querySelector('[role="alert"]')).toBeNull() |
| 83 | + expect(input?.getAttribute('aria-invalid')).toBe('false') |
| 84 | + expect(input?.hasAttribute('aria-describedby')).toBe(false) |
| 85 | + }) |
| 86 | +}) |
0 commit comments