|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import type { ReactNode } from 'react' |
| 5 | +import { renderToStaticMarkup } from 'react-dom/server' |
| 6 | +import { describe, expect, it, vi } from 'vitest' |
| 7 | +import type { Fact } from '@/lib/compare/data' |
| 8 | + |
| 9 | +vi.mock('@sim/emcn', () => ({ |
| 10 | + cn: (...values: Array<string | false | null | undefined>) => values.filter(Boolean).join(' '), |
| 11 | + Tooltip: { |
| 12 | + Root: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 13 | + Trigger: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 14 | + Content: ({ children }: { children: ReactNode }) => ( |
| 15 | + <span data-testid='source-tooltip'>{children}</span> |
| 16 | + ), |
| 17 | + }, |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@sim/emcn/icons', () => ({ |
| 21 | + Check: () => <svg data-icon='check' />, |
| 22 | + X: () => <svg data-icon='x' />, |
| 23 | +})) |
| 24 | + |
| 25 | +import { FactValue } from '@/app/(landing)/comparisons/components/fact-value/fact-value' |
| 26 | + |
| 27 | +const PRIMARY_SOURCE = { |
| 28 | + url: 'https://primary.example/compliance', |
| 29 | + label: 'Primary compliance source', |
| 30 | + asOf: '2026-09-04', |
| 31 | +} |
| 32 | + |
| 33 | +function createFact(overrides: Partial<Fact> = {}): Fact { |
| 34 | + return { |
| 35 | + value: 'Complete compliance statement', |
| 36 | + detail: 'Supporting qualification', |
| 37 | + shortValue: 'Compact compliance summary', |
| 38 | + confidence: 'verified', |
| 39 | + sources: [ |
| 40 | + PRIMARY_SOURCE, |
| 41 | + { |
| 42 | + url: 'https://secondary.example/compliance', |
| 43 | + label: 'Secondary compliance source', |
| 44 | + asOf: '2026-09-04', |
| 45 | + }, |
| 46 | + ], |
| 47 | + ...overrides, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function withoutScreenReaderText(markup: string): string { |
| 52 | + return markup.replace(/<span class="sr-only">.*?<\/span>/, '') |
| 53 | +} |
| 54 | + |
| 55 | +function screenReaderText(markup: string): string { |
| 56 | + const match = markup.match(/<span class="sr-only">(.*?)<\/span>/) |
| 57 | + expect(match).not.toBeNull() |
| 58 | + return match?.[1] ?? '' |
| 59 | +} |
| 60 | + |
| 61 | +describe('FactValue', () => { |
| 62 | + it('shows shortValue while exposing the complete value and detail once to screen readers', () => { |
| 63 | + const markup = renderToStaticMarkup(<FactValue fact={createFact()} />) |
| 64 | + const visibleMarkup = withoutScreenReaderText(markup) |
| 65 | + const accessibleText = screenReaderText(markup) |
| 66 | + |
| 67 | + expect(visibleMarkup).toContain('Compact compliance summary') |
| 68 | + expect(visibleMarkup).not.toContain('Complete compliance statement') |
| 69 | + expect(accessibleText).toBe('Complete compliance statement. Supporting qualification') |
| 70 | + expect(accessibleText.match(/Complete compliance statement/g)).toHaveLength(1) |
| 71 | + expect(accessibleText.match(/Supporting qualification/g)).toHaveLength(1) |
| 72 | + }) |
| 73 | + |
| 74 | + it('falls back to value and renders a fact without detail', () => { |
| 75 | + const markup = renderToStaticMarkup( |
| 76 | + <FactValue |
| 77 | + fact={createFact({ |
| 78 | + value: 'Fallback visible value', |
| 79 | + shortValue: undefined, |
| 80 | + detail: undefined, |
| 81 | + sources: [], |
| 82 | + })} |
| 83 | + /> |
| 84 | + ) |
| 85 | + |
| 86 | + expect(withoutScreenReaderText(markup)).toContain('Fallback visible value') |
| 87 | + expect(screenReaderText(markup)).toBe('Fallback visible value') |
| 88 | + }) |
| 89 | + |
| 90 | + it.each([ |
| 91 | + ['Statement.', 'Statement. Detail'], |
| 92 | + ['Statement!', 'Statement! Detail'], |
| 93 | + ['Statement?', 'Statement? Detail'], |
| 94 | + ['Statement.)', 'Statement.) Detail'], |
| 95 | + ['Statement.”', 'Statement.” Detail'], |
| 96 | + ['Statement', 'Statement. Detail'], |
| 97 | + ])('joins %j and detail without duplicate punctuation', (value, expected) => { |
| 98 | + const markup = renderToStaticMarkup( |
| 99 | + <FactValue fact={createFact({ value, detail: 'Detail', sources: [] })} /> |
| 100 | + ) |
| 101 | + |
| 102 | + expect(screenReaderText(markup)).toBe(expected) |
| 103 | + }) |
| 104 | + |
| 105 | + it('uses the URL and label from sources[0] for the visible source link', () => { |
| 106 | + const markup = renderToStaticMarkup(<FactValue fact={createFact()} />) |
| 107 | + const visibleMarkup = withoutScreenReaderText(markup) |
| 108 | + |
| 109 | + expect(visibleMarkup).toContain(`href="${PRIMARY_SOURCE.url}"`) |
| 110 | + expect(visibleMarkup).toContain(`aria-label="${PRIMARY_SOURCE.label} (opens source)"`) |
| 111 | + expect(visibleMarkup).not.toContain('https://secondary.example/compliance') |
| 112 | + expect(visibleMarkup).not.toContain('Secondary compliance source') |
| 113 | + expect(visibleMarkup).toContain('Checked 2026-09-04') |
| 114 | + }) |
| 115 | + |
| 116 | + it.each(['unknown', 'estimated'] as const)( |
| 117 | + 'preserves negation without a definitive icon for %s claims', |
| 118 | + (confidence) => { |
| 119 | + const markup = renderToStaticMarkup( |
| 120 | + <FactValue |
| 121 | + fact={createFact({ |
| 122 | + value: 'No: self-hosted deployment', |
| 123 | + shortValue: undefined, |
| 124 | + confidence, |
| 125 | + })} |
| 126 | + /> |
| 127 | + ) |
| 128 | + expect(markup).not.toContain('data-icon=') |
| 129 | + expect(withoutScreenReaderText(markup)).toContain('No: self-hosted deployment') |
| 130 | + expect(markup).toContain(confidence === 'unknown' ? '(unverified)' : '(estimate)') |
| 131 | + } |
| 132 | + ) |
| 133 | +}) |
0 commit comments