|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import { NumberBadge } from './NumberBadge'; |
| 4 | + |
| 5 | +describe('NumberBadge', () => { |
| 6 | + it('renders as a <span> element', () => { |
| 7 | + const { container } = render(<NumberBadge>5</NumberBadge>); |
| 8 | + expect(container.firstChild?.nodeName).toBe('SPAN'); |
| 9 | + }); |
| 10 | + |
| 11 | + it('always has base dsn-number-badge class', () => { |
| 12 | + const { container } = render(<NumberBadge>5</NumberBadge>); |
| 13 | + expect(container.firstChild).toHaveClass('dsn-number-badge'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('always has aria-hidden="true"', () => { |
| 17 | + const { container } = render(<NumberBadge>5</NumberBadge>); |
| 18 | + expect(container.firstChild).toHaveAttribute('aria-hidden', 'true'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('applies negative variant class by default', () => { |
| 22 | + const { container } = render(<NumberBadge>5</NumberBadge>); |
| 23 | + expect(container.firstChild).toHaveClass('dsn-number-badge--negative'); |
| 24 | + }); |
| 25 | + |
| 26 | + it.each(['positive', 'negative', 'warning', 'info', 'neutral'] as const)( |
| 27 | + 'applies variant modifier class for %s variant', |
| 28 | + (variant) => { |
| 29 | + const { container } = render( |
| 30 | + <NumberBadge variant={variant}>5</NumberBadge> |
| 31 | + ); |
| 32 | + expect(container.firstChild).toHaveClass(`dsn-number-badge--${variant}`); |
| 33 | + } |
| 34 | + ); |
| 35 | + |
| 36 | + it('renders children as display value', () => { |
| 37 | + const { getByText } = render(<NumberBadge>42</NumberBadge>); |
| 38 | + expect(getByText('42')).toBeTruthy(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('renders string children unchanged', () => { |
| 42 | + const { getByText } = render(<NumberBadge>99+</NumberBadge>); |
| 43 | + expect(getByText('99+')).toBeTruthy(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('shows maxCount+ when children number exceeds maxCount', () => { |
| 47 | + const { getByText } = render(<NumberBadge maxCount={99}>128</NumberBadge>); |
| 48 | + expect(getByText('99+')).toBeTruthy(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('shows actual count when children number equals maxCount', () => { |
| 52 | + const { getByText } = render(<NumberBadge maxCount={99}>99</NumberBadge>); |
| 53 | + expect(getByText('99')).toBeTruthy(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('shows actual count when children number is below maxCount', () => { |
| 57 | + const { getByText } = render(<NumberBadge maxCount={99}>5</NumberBadge>); |
| 58 | + expect(getByText('5')).toBeTruthy(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('does not apply maxCount truncation to non-numeric string children', () => { |
| 62 | + const { getByText } = render(<NumberBadge maxCount={99}>99+</NumberBadge>); |
| 63 | + expect(getByText('99+')).toBeTruthy(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('applies custom className', () => { |
| 67 | + const { container } = render( |
| 68 | + <NumberBadge className="custom">5</NumberBadge> |
| 69 | + ); |
| 70 | + expect(container.firstChild).toHaveClass('dsn-number-badge'); |
| 71 | + expect(container.firstChild).toHaveClass('custom'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('forwards ref', () => { |
| 75 | + const ref = { current: null as HTMLSpanElement | null }; |
| 76 | + render(<NumberBadge ref={ref}>5</NumberBadge>); |
| 77 | + expect(ref.current).toBeInstanceOf(HTMLSpanElement); |
| 78 | + }); |
| 79 | + |
| 80 | + it('spreads additional HTML attributes', () => { |
| 81 | + const { container } = render( |
| 82 | + <NumberBadge data-testid="badge">5</NumberBadge> |
| 83 | + ); |
| 84 | + expect(container.firstChild).toHaveAttribute('data-testid', 'badge'); |
| 85 | + }); |
| 86 | +}); |
0 commit comments