import { test, expect } from 'vitest'; import { render, screen } from '@testing-library/react'; import CategoryItem from './CategoryItem'; const ITEM_TITLE = 'Bananas'; const ITEM_HREF = '#foo'; const ITEM_CONTENT = 'A banana is an elongated, edible fruit—botanically a berry—produced by several kinds of large treelike herbaceous flowering plants in the genus Musa.'; test('category item renders correctly', () => { render( {ITEM_CONTENT} ); const categoryItem = screen.getByTestId('category-item'); const title = screen.getByRole('heading'); const link = screen.getByRole('link'); const content = categoryItem.querySelector('p'); expect(categoryItem).toHaveClass('ds_category-item'); expect(categoryItem.tagName).toEqual('DIV'); expect(title).toHaveClass('ds_category-item__title'); expect(title.tagName).toEqual('H2'); expect(title.textContent).toEqual(ITEM_TITLE); expect(title.parentElement).toEqual(categoryItem); expect(link).toHaveClass('ds_category-item__link'); expect(link).toHaveAttribute('href', ITEM_HREF); expect(link.textContent).toEqual(ITEM_TITLE); expect(link.parentElement).toEqual(title); expect(content).toHaveClass('ds_category-item__summary'); expect(content?.textContent).toEqual(ITEM_CONTENT); expect(content?.parentElement).toEqual(categoryItem); }); test('custom heading level', () => { render( {ITEM_CONTENT} ); const title = screen.getByRole('heading'); expect(title.tagName).toEqual('H3'); }); test('custom element', () => { render( {ITEM_CONTENT} ); const categoryItem = screen.getByTestId('category-item'); expect(categoryItem.tagName).toEqual('ARTICLE'); }); test('custom link component', () => { render( ( )}> {ITEM_CONTENT} ); const link = screen.getByRole('link'); expect(link?.tagName).toEqual('SPAN'); expect(link?.textContent).toEqual(ITEM_TITLE); }); test('passing additional props', () => { render( ); const categoryItem = screen.getByTestId('category-item'); expect(categoryItem?.dataset.test).toEqual('foo'); }); test('passing additional CSS classes', () => { render( ); const categoryItem = screen.getByTestId('category-item'); expect(categoryItem).toHaveClass('foo'); });