|
| 1 | +import { cleanup, render, screen, within } from '@testing-library/react' |
| 2 | +import { afterEach, describe, expect, it } from 'vitest' |
| 3 | +import { CompareCard } from '@/components/CompareCard' |
| 4 | + |
| 5 | +afterEach(() => { |
| 6 | + cleanup() |
| 7 | +}) |
| 8 | + |
| 9 | +describe('CompareCard', () => { |
| 10 | + it('renders use case heading, name, summary, strengths list, and gaps list', () => { |
| 11 | + render( |
| 12 | + <CompareCard |
| 13 | + useCase="I want an example use case." |
| 14 | + name="Example Tool" |
| 15 | + summary="One-line summary for the tool." |
| 16 | + strengths={['Strength A', 'Strength B']} |
| 17 | + gaps={['Gap A']} |
| 18 | + />, |
| 19 | + ) |
| 20 | + |
| 21 | + expect( |
| 22 | + screen.getByRole('heading', { level: 2, name: 'I want an example use case.' }), |
| 23 | + ).toBeInTheDocument() |
| 24 | + expect(screen.getByText('Example Tool')).toBeInTheDocument() |
| 25 | + expect(screen.getByText('One-line summary for the tool.')).toBeInTheDocument() |
| 26 | + expect(screen.getByRole('heading', { level: 3, name: 'What it does well' })).toBeInTheDocument() |
| 27 | + expect( |
| 28 | + screen.getByRole('heading', { level: 3, name: 'What it does not cover' }), |
| 29 | + ).toBeInTheDocument() |
| 30 | + expect(screen.getByText('Strength A')).toBeInTheDocument() |
| 31 | + expect(screen.getByText('Strength B')).toBeInTheDocument() |
| 32 | + expect(screen.getByText('Gap A')).toBeInTheDocument() |
| 33 | + }) |
| 34 | + |
| 35 | + it('renders Learn more link with href, target, rel, and aria-label when url is provided', () => { |
| 36 | + render( |
| 37 | + <CompareCard |
| 38 | + useCase="Use case" |
| 39 | + name="Agent Safehouse" |
| 40 | + summary="Summary" |
| 41 | + strengths={[]} |
| 42 | + gaps={[]} |
| 43 | + url="https://agent-safehouse.dev" |
| 44 | + />, |
| 45 | + ) |
| 46 | + |
| 47 | + const link = screen.getByRole('link', { name: 'Learn more about Agent Safehouse' }) |
| 48 | + expect(link).toHaveAttribute('href', 'https://agent-safehouse.dev') |
| 49 | + expect(link).toHaveAttribute('target', '_blank') |
| 50 | + expect(link).toHaveAttribute('rel', 'noopener noreferrer') |
| 51 | + }) |
| 52 | + |
| 53 | + it('does not render Learn more when url is omitted', () => { |
| 54 | + render( |
| 55 | + <CompareCard useCase="Use case" name="Tool" summary="Summary" strengths={[]} gaps={[]} />, |
| 56 | + ) |
| 57 | + |
| 58 | + expect(screen.queryByRole('link', { name: /Learn more about/ })).not.toBeInTheDocument() |
| 59 | + }) |
| 60 | + |
| 61 | + it('renders tracked CTA when highlight and trackedCta are provided', () => { |
| 62 | + render( |
| 63 | + <CompareCard |
| 64 | + useCase="Use case" |
| 65 | + name="Shield" |
| 66 | + summary="Summary" |
| 67 | + strengths={[]} |
| 68 | + gaps={[]} |
| 69 | + highlight |
| 70 | + trackedCta={{ |
| 71 | + href: 'https://app.multicorn.ai/signup', |
| 72 | + eventName: 'test_click', |
| 73 | + label: 'Sign up for Shield', |
| 74 | + }} |
| 75 | + />, |
| 76 | + ) |
| 77 | + |
| 78 | + expect(screen.getByRole('link', { name: 'Sign up for Shield' })).toHaveAttribute( |
| 79 | + 'href', |
| 80 | + 'https://app.multicorn.ai/signup', |
| 81 | + ) |
| 82 | + }) |
| 83 | + |
| 84 | + it('does not render CTA when highlight is false and trackedCta is omitted', () => { |
| 85 | + render( |
| 86 | + <CompareCard |
| 87 | + useCase="Use case" |
| 88 | + name="Tool" |
| 89 | + summary="Summary" |
| 90 | + strengths={[]} |
| 91 | + gaps={[]} |
| 92 | + highlight={false} |
| 93 | + />, |
| 94 | + ) |
| 95 | + |
| 96 | + expect(screen.queryByRole('link', { name: /Sign up/ })).not.toBeInTheDocument() |
| 97 | + }) |
| 98 | + |
| 99 | + it('renders strength and gap items in order', () => { |
| 100 | + const { container } = render( |
| 101 | + <CompareCard |
| 102 | + useCase="Use case" |
| 103 | + name="Tool" |
| 104 | + summary="Summary" |
| 105 | + strengths={['first strength', 'second strength', 'third strength']} |
| 106 | + gaps={['first gap', 'second gap']} |
| 107 | + />, |
| 108 | + ) |
| 109 | + |
| 110 | + const article = container.querySelector('article') |
| 111 | + expect(article).not.toBeNull() |
| 112 | + if (article === null) { |
| 113 | + return |
| 114 | + } |
| 115 | + const lists = article.querySelectorAll('ul') |
| 116 | + expect(lists).toHaveLength(2) |
| 117 | + |
| 118 | + const strengthItems = within(lists[0] as HTMLElement).getAllByRole('listitem') |
| 119 | + expect(strengthItems.map((li) => li.textContent)).toEqual([ |
| 120 | + 'first strength', |
| 121 | + 'second strength', |
| 122 | + 'third strength', |
| 123 | + ]) |
| 124 | + |
| 125 | + const gapItems = within(lists[1] as HTMLElement).getAllByRole('listitem') |
| 126 | + expect(gapItems.map((li) => li.textContent)).toEqual(['first gap', 'second gap']) |
| 127 | + }) |
| 128 | +}) |
0 commit comments