import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import '../summary-box/usa-summary-box.ts'; import type { USASummaryBox } from '../summary-box/usa-summary-box.js'; import { testComponentAccessibility, USWDS_A11Y_CONFIG, } from '../../../__tests__/accessibility-utils.js'; import { waitForUpdate, testPropertyChanges, validateComponentJavaScript, } from '../../../__tests__/test-utils.js'; describe('USASummaryBox', () => { let element: USASummaryBox; beforeEach(() => { element = document.createElement('usa-summary-box') as USASummaryBox; document.body.appendChild(element); }); afterEach(() => { element.remove(); }); describe('Properties', () => { it('should have default properties', () => { expect(element.heading).toBe(''); expect(element.content).toBe(''); expect(element.headingLevel).toBe('h3'); }); it('should update heading property', async () => { await testPropertyChanges( element, 'heading', ['Summary', 'Important Information', 'Federal Guidelines'], (el, value) => { expect(el.heading).toBe(value); } ); }); it('should update content property', async () => { element.content = '

Test content

'; await waitForUpdate(element); expect(element.content).toBe('

Test content

'); }); it('should update headingLevel property', async () => { await testPropertyChanges( element, 'headingLevel', ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'], (el, value) => { expect(el.headingLevel).toBe(value); } ); }); }); describe('Rendering', () => { it('should render summary box container with correct classes', async () => { await waitForUpdate(element); const summaryBox = element.querySelector('.usa-summary-box'); expect(summaryBox).toBeTruthy(); expect(summaryBox?.classList.contains('usa-summary-box')).toBe(true); const body = element.querySelector('.usa-summary-box__body'); expect(body).toBeTruthy(); expect(body?.classList.contains('usa-summary-box__body')).toBe(true); const text = element.querySelector('.usa-summary-box__text'); expect(text).toBeTruthy(); expect(text?.classList.contains('usa-summary-box__text')).toBe(true); }); it('should render heading with correct level and class', async () => { element.heading = 'Test Heading'; element.headingLevel = 'h2'; await waitForUpdate(element); const heading = element.querySelector('h2.usa-summary-box__heading'); expect(heading).toBeTruthy(); expect(heading?.textContent).toBe('Test Heading'); expect(heading?.classList.contains('usa-summary-box__heading')).toBe(true); }); it('should render all heading levels correctly', async () => { const headingLevels = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] as const; for (const level of headingLevels) { element.heading = `Test ${level.toUpperCase()}`; element.headingLevel = level; await waitForUpdate(element); const heading = element.querySelector(`${level}.usa-summary-box__heading`); expect(heading).toBeTruthy(); expect(heading?.textContent).toBe(`Test ${level.toUpperCase()}`); } }); it('should default to h3 for invalid heading level', async () => { element.heading = 'Test Heading'; element.headingLevel = 'invalid' as any; await waitForUpdate(element); const heading = element.querySelector('h3.usa-summary-box__heading'); expect(heading).toBeTruthy(); expect(heading?.textContent).toBe('Test Heading'); }); it('should not render heading when heading property is empty', async () => { element.heading = ''; await waitForUpdate(element); const heading = element.querySelector('.usa-summary-box__heading'); expect(heading).toBeFalsy(); }); it('should render content via property', async () => { element.content = '

Property content

'; await waitForUpdate(element); const textContainer = element.querySelector('.usa-summary-box__text'); expect(textContainer?.innerHTML).toContain('

Property content

'); }); it('should render slot content when no property content', async () => { const slotParagraph = document.createElement('p'); slotParagraph.textContent = 'Slot content'; element.appendChild(slotParagraph); await waitForUpdate(element); expect(element.textContent).toContain('Slot content'); }); it('should prioritize content property over slot content', async () => { // Set content property first element.content = '

Property content

'; // Add slot content const slotParagraph = document.createElement('p'); slotParagraph.textContent = 'Slot content'; element.appendChild(slotParagraph); await waitForUpdate(element); const textContainer = element.querySelector('.usa-summary-box__text'); expect(textContainer?.innerHTML).toContain('Property content'); // Slot content should not be rendered when property content exists expect(textContainer?.innerHTML).not.toContain('Slot content'); }); it('should handle complex HTML content', async () => { const complexContent = `

Important: This is a complex summary.

`; element.content = complexContent; await waitForUpdate(element); const textContainer = element.querySelector('.usa-summary-box__text'); expect(textContainer?.innerHTML).toContain('Important:'); expect(textContainer?.innerHTML).toContain('