|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import { axe } from 'jest-axe'; |
| 3 | + |
| 4 | +import { VanillaExtractThemeProvider } from '../../styles/themeContext'; |
| 5 | +import { UnstyledLink } from '../Link'; |
| 6 | +import { BadgeVanilla, COLORS } from './Badge.vanilla'; |
| 7 | + |
| 8 | +describe('BadgeVanilla', () => { |
| 9 | + const renderWithTheme = (ui: React.ReactElement) => { |
| 10 | + return render(<VanillaExtractThemeProvider>{ui}</VanillaExtractThemeProvider>); |
| 11 | + }; |
| 12 | + |
| 13 | + it('should render as span by default', () => { |
| 14 | + const { container } = renderWithTheme(<BadgeVanilla>Badge</BadgeVanilla>); |
| 15 | + const badge = container.firstChild; |
| 16 | + |
| 17 | + expect(badge?.nodeName).toBe('SPAN'); |
| 18 | + expect(badge).toHaveTextContent('Badge'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should render as button when interactive', () => { |
| 22 | + const { container } = renderWithTheme(<BadgeVanilla interactive>Badge</BadgeVanilla>); |
| 23 | + const badge = container.firstChild; |
| 24 | + |
| 25 | + expect(badge?.nodeName).toBe('BUTTON'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should render as link with asChild and interactive', () => { |
| 29 | + const { getByRole } = renderWithTheme( |
| 30 | + <BadgeVanilla asChild interactive> |
| 31 | + <UnstyledLink href="https://traefik.io">Link Badge</UnstyledLink> |
| 32 | + </BadgeVanilla>, |
| 33 | + ); |
| 34 | + |
| 35 | + expect(getByRole('link')).toBeInTheDocument(); |
| 36 | + expect(getByRole('link')).toHaveTextContent('Link Badge'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should render with custom className', () => { |
| 40 | + const { container } = renderWithTheme( |
| 41 | + <BadgeVanilla className="custom-badge">Badge</BadgeVanilla>, |
| 42 | + ); |
| 43 | + const badge = container.firstChild as HTMLElement; |
| 44 | + |
| 45 | + expect(badge.className).toContain('custom-badge'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should apply variant prop', () => { |
| 49 | + const variants = ['neon', 'green', 'blue'] as const; |
| 50 | + |
| 51 | + variants.forEach((variant) => { |
| 52 | + const { container } = renderWithTheme(<BadgeVanilla variant={variant}>Badge</BadgeVanilla>); |
| 53 | + expect(container.firstChild).toBeInTheDocument(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should apply size prop', () => { |
| 58 | + const sizes = ['small', 'large'] as const; |
| 59 | + |
| 60 | + sizes.forEach((size) => { |
| 61 | + const { container } = renderWithTheme(<BadgeVanilla size={size}>Badge</BadgeVanilla>); |
| 62 | + expect(container.firstChild).toBeInTheDocument(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should render with all available colors', () => { |
| 67 | + COLORS.forEach((color) => { |
| 68 | + const { container } = renderWithTheme( |
| 69 | + <BadgeVanilla css={{ color: color }}>{color} Badge</BadgeVanilla>, |
| 70 | + ); |
| 71 | + expect(container.firstChild).toBeInTheDocument(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should apply alphaBg prop', () => { |
| 76 | + const { container } = renderWithTheme(<BadgeVanilla alphaBg>Badge</BadgeVanilla>); |
| 77 | + expect(container.firstChild).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should apply borderless prop', () => { |
| 81 | + const { container } = renderWithTheme(<BadgeVanilla borderless>Badge</BadgeVanilla>); |
| 82 | + expect(container.firstChild).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should combine multiple variant props', () => { |
| 86 | + const { container } = renderWithTheme( |
| 87 | + <BadgeVanilla variant="neon" size="small" alphaBg borderless> |
| 88 | + Combined Badge |
| 89 | + </BadgeVanilla>, |
| 90 | + ); |
| 91 | + expect(container.firstChild).toBeInTheDocument(); |
| 92 | + expect(container.firstChild).toHaveTextContent('Combined Badge'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should apply custom styles via style prop', () => { |
| 96 | + const { container } = renderWithTheme( |
| 97 | + <BadgeVanilla style={{ backgroundColor: 'red', padding: '10px' }}>Badge</BadgeVanilla>, |
| 98 | + ); |
| 99 | + const badge = container.firstChild as HTMLElement; |
| 100 | + |
| 101 | + expect(badge.style.backgroundColor).toBe('red'); |
| 102 | + expect(badge.style.padding).toBe('10px'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should apply CSS prop styles', () => { |
| 106 | + const { container } = renderWithTheme( |
| 107 | + <BadgeVanilla css={{ padding: '20px', margin: '10px' }}>Badge</BadgeVanilla>, |
| 108 | + ); |
| 109 | + const badge = container.firstChild as HTMLElement; |
| 110 | + |
| 111 | + expect(badge.style.padding).toBe('20px'); |
| 112 | + expect(badge.style.margin).toBe('10px'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should merge style and css props correctly', () => { |
| 116 | + const { container } = renderWithTheme( |
| 117 | + <BadgeVanilla |
| 118 | + css={{ padding: '20px', margin: '10px' }} |
| 119 | + style={{ backgroundColor: 'blue', padding: '30px' }} |
| 120 | + > |
| 121 | + Badge |
| 122 | + </BadgeVanilla>, |
| 123 | + ); |
| 124 | + const badge = container.firstChild as HTMLElement; |
| 125 | + |
| 126 | + expect(badge.style.backgroundColor).toBe('blue'); |
| 127 | + // style prop should override css prop |
| 128 | + expect(badge.style.padding).toBe('30px'); |
| 129 | + expect(badge.style.margin).toBe('10px'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should forward ref correctly', () => { |
| 133 | + const ref = { current: null }; |
| 134 | + renderWithTheme(<BadgeVanilla ref={ref}>Badge</BadgeVanilla>); |
| 135 | + |
| 136 | + expect(ref.current).toBeInstanceOf(HTMLSpanElement); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should forward ref correctly for interactive badge', () => { |
| 140 | + const ref = { current: null }; |
| 141 | + renderWithTheme( |
| 142 | + <BadgeVanilla ref={ref} interactive> |
| 143 | + Badge |
| 144 | + </BadgeVanilla>, |
| 145 | + ); |
| 146 | + |
| 147 | + expect(ref.current).toBeInstanceOf(HTMLButtonElement); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should pass through HTML attributes', () => { |
| 151 | + const { container } = renderWithTheme( |
| 152 | + <BadgeVanilla data-testid="test-badge" aria-label="Test Badge"> |
| 153 | + Badge |
| 154 | + </BadgeVanilla>, |
| 155 | + ); |
| 156 | + const badge = container.firstChild as HTMLElement; |
| 157 | + |
| 158 | + expect(badge.getAttribute('data-testid')).toBe('test-badge'); |
| 159 | + expect(badge.getAttribute('aria-label')).toBe('Test Badge'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should handle onClick for interactive badge', () => { |
| 163 | + const handleClick = jest.fn(); |
| 164 | + const { container } = renderWithTheme( |
| 165 | + <BadgeVanilla interactive onClick={handleClick}> |
| 166 | + Clickable Badge |
| 167 | + </BadgeVanilla>, |
| 168 | + ); |
| 169 | + const badge = container.firstChild as HTMLButtonElement; |
| 170 | + |
| 171 | + badge.click(); |
| 172 | + expect(handleClick).toHaveBeenCalledTimes(1); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should have no accessibility violations for static badge', async () => { |
| 176 | + const { container } = renderWithTheme(<BadgeVanilla>Badge</BadgeVanilla>); |
| 177 | + const results = await axe(container); |
| 178 | + |
| 179 | + expect(results).toHaveNoViolations(); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should have no accessibility violations for interactive badge', async () => { |
| 183 | + const { container } = renderWithTheme(<BadgeVanilla interactive>Badge</BadgeVanilla>); |
| 184 | + const results = await axe(container); |
| 185 | + |
| 186 | + expect(results).toHaveNoViolations(); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should work with light theme', () => { |
| 190 | + const { container } = render( |
| 191 | + <VanillaExtractThemeProvider defaultTheme="light"> |
| 192 | + <BadgeVanilla>Light Badge</BadgeVanilla> |
| 193 | + </VanillaExtractThemeProvider>, |
| 194 | + ); |
| 195 | + |
| 196 | + expect(container.firstChild).toBeInTheDocument(); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should work with dark theme', () => { |
| 200 | + const { container } = render( |
| 201 | + <VanillaExtractThemeProvider defaultTheme="dark"> |
| 202 | + <BadgeVanilla>Dark Badge</BadgeVanilla> |
| 203 | + </VanillaExtractThemeProvider>, |
| 204 | + ); |
| 205 | + |
| 206 | + expect(container.firstChild).toBeInTheDocument(); |
| 207 | + }); |
| 208 | +}); |
0 commit comments