|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | + |
| 5 | +import type { MosaicAppearance } from '../appearance'; |
| 6 | +import { Icon } from '../components/icon'; |
| 7 | +import { MosaicProvider } from '../MosaicProvider'; |
| 8 | + |
| 9 | +const wrap = (ui: React.ReactElement, appearance?: MosaicAppearance) => |
| 10 | + render(<MosaicProvider appearance={appearance}>{ui}</MosaicProvider>); |
| 11 | + |
| 12 | +describe('Icon', () => { |
| 13 | + it('renders the default glyph for a known name', () => { |
| 14 | + const { container } = wrap(<Icon name='chevron-right' />); |
| 15 | + const svg = container.querySelector('svg[data-cl-slot="icon"]'); |
| 16 | + expect(svg).not.toBeNull(); |
| 17 | + expect(svg?.querySelector('path')).not.toBeNull(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('renders the override instead of the default glyph', () => { |
| 21 | + const appearance: MosaicAppearance = { |
| 22 | + icons: { 'chevron-right': props => <span {...props} data-testid='override' /> }, |
| 23 | + }; |
| 24 | + const { getByTestId, container } = wrap(<Icon name='chevron-right' />, appearance); |
| 25 | + expect(getByTestId('override')).not.toBeNull(); |
| 26 | + // The default svg glyph is not rendered when overridden. |
| 27 | + expect(container.querySelector('svg')).toBeNull(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('applies Mosaic styling (className) and the slot attr to the override', () => { |
| 31 | + const appearance: MosaicAppearance = { |
| 32 | + icons: { 'chevron-right': props => <span {...props} data-testid='override' /> }, |
| 33 | + }; |
| 34 | + const { getByTestId } = wrap(<Icon name='chevron-right' size='lg' />, appearance); |
| 35 | + const el = getByTestId('override'); |
| 36 | + expect(el.className).toBeTruthy(); |
| 37 | + expect(el.getAttribute('data-cl-slot')).toBe('icon'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('applies appearance.elements.icon styling to an overridden glyph (consistent with the built-in)', () => { |
| 41 | + const appearance: MosaicAppearance = { |
| 42 | + elements: { icon: { opacity: 0.5 } }, |
| 43 | + icons: { 'chevron-right': props => <span {...props} data-testid='override' /> }, |
| 44 | + }; |
| 45 | + const { getByTestId } = wrap(<Icon name='chevron-right' />, appearance); |
| 46 | + expect(getByTestId('override').className).toBeTruthy(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('falls through to the default when a different name is overridden', () => { |
| 50 | + const appearance: MosaicAppearance = { |
| 51 | + icons: { 'chevron-left': props => <span {...props} data-testid='override' /> }, |
| 52 | + }; |
| 53 | + const { container, queryByTestId } = wrap(<Icon name='chevron-right' />, appearance); |
| 54 | + expect(queryByTestId('override')).toBeNull(); |
| 55 | + expect(container.querySelector('svg[data-cl-slot="icon"]')).not.toBeNull(); |
| 56 | + }); |
| 57 | +}); |
0 commit comments