import '@testing-library/jest-dom'
import { axe, toHaveNoViolations } from 'jest-axe'
import { vi } from 'vitest'
import {
createElementTest,
BaseTestConfig,
setupConsoleMocking,
restoreConsoleMocking,
} from '../../tests/test-framework'
import { CustomElementFor } from '../../tests/component-registry'
import './icon'
import { PktIcon } from './icon'
expect.extend(toHaveNoViolations)
export interface IconTestConfig extends BaseTestConfig {
name?: string
path?: string
'aria-hidden'?: string
'aria-label'?: string
}
// Use shared framework
export const createIconTest = async (config: IconTestConfig = {}) => {
const { container, element } = await createElementTest<
CustomElementFor<'pkt-icon'>,
IconTestConfig
>('pkt-icon', config)
return {
container,
icon: element,
}
}
// Cleanup after each test
afterEach(() => {
document.body.innerHTML = ''
// Clean up sessionStorage after tests
sessionStorage.clear()
// Reset global variables
delete (window as any).pktFetch
delete (window as any).pktIconPath
// Restore console mocking
restoreConsoleMocking()
})
// Mock fetch for icon loading
const mockFetch = vi.fn()
const mockSvgContent =
''
beforeEach(() => {
// Setup console mocking to suppress error logs during tests
setupConsoleMocking()
// Setup default mocks
mockFetch.mockResolvedValue({
ok: true,
text: () => Promise.resolve(mockSvgContent),
})
window.pktFetch = mockFetch
window.pktIconPath = 'https://test-cdn.example.com/icons/'
})
describe('PktIcon', () => {
describe('Rendering and basic functionality', () => {
test('renders without errors', async () => {
const { icon } = await createIconTest()
expect(icon).toBeInTheDocument()
await icon.updateComplete
expect(icon.classList.contains('pkt-icon')).toBe(true)
})
test('renders with default structure', async () => {
const { icon } = await createIconTest({
name: 'arrow-right',
})
await icon.updateComplete
expect(icon).toBeInTheDocument()
expect(icon.classList.contains('pkt-icon')).toBe(true)
})
test('renders nothing when no name is provided', async () => {
const { icon } = await createIconTest()
await icon.updateComplete
// Should render nothing meaningful when no name is provided (only Lit template comments)
expect(icon.innerHTML).not.toContain('