|
| 1 | +import React from 'react'; |
| 2 | +import { Text } from 'react-native'; |
| 3 | +import { render } from '@testing-library/react-native'; |
| 4 | + |
| 5 | +import { Icon, resolveIconName } from './Icon'; |
| 6 | + |
| 7 | +const mockHasIcon = jest.fn(); |
| 8 | +const mockCustomIcon = jest.fn(() => <Text testID='custom-icon'>icon</Text>); |
| 9 | + |
| 10 | +jest.mock('../CustomIcon', () => ({ |
| 11 | + hasIcon: (...args: unknown[]) => mockHasIcon(...args), |
| 12 | + CustomIcon: (...props: Parameters<typeof mockCustomIcon>) => mockCustomIcon(...props) |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('../../theme', () => ({ |
| 16 | + useTheme: () => ({ |
| 17 | + colors: { |
| 18 | + fontDefault: '#000000', |
| 19 | + fontDanger: '#d00000', |
| 20 | + fontSecondaryInfo: '#0060d0', |
| 21 | + statusFontWarning: '#d09000', |
| 22 | + statusFontDanger: '#ff2020', |
| 23 | + surfaceTint: '#f2f2f2' |
| 24 | + } |
| 25 | + }) |
| 26 | +})); |
| 27 | + |
| 28 | +describe('UIKit Icon', () => { |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('resolveIconName', () => { |
| 34 | + it('returns original icon when available', () => { |
| 35 | + mockHasIcon.mockImplementation((name: string) => name === 'bell'); |
| 36 | + |
| 37 | + expect(resolveIconName('bell')).toBe('bell'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('resolves known alias when alias icon exists', () => { |
| 41 | + mockHasIcon.mockImplementation((name: string) => name === 'phone-off'); |
| 42 | + |
| 43 | + expect(resolveIconName('phone-end')).toBe('phone-off'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('falls back to info when icon and alias are unavailable', () => { |
| 47 | + mockHasIcon.mockReturnValue(false); |
| 48 | + |
| 49 | + expect(resolveIconName('unknown')).toBe('info'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('renders secondary variant color', () => { |
| 54 | + mockHasIcon.mockReturnValue(true); |
| 55 | + render(<Icon element={{ icon: 'bell', type: 'icon', variant: 'secondary' } as any} />); |
| 56 | + |
| 57 | + expect(mockCustomIcon).toHaveBeenCalledTimes(1); |
| 58 | + const firstCallArg = (mockCustomIcon.mock.calls[0] as any[])[0]; |
| 59 | + expect(firstCallArg).toEqual( |
| 60 | + expect.objectContaining({ |
| 61 | + name: 'bell', |
| 62 | + color: '#0060d0', |
| 63 | + size: 20 |
| 64 | + }) |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('uses framed danger color and frame background', () => { |
| 69 | + mockHasIcon.mockReturnValue(true); |
| 70 | + const { toJSON } = render(<Icon element={{ icon: 'bell', type: 'icon', variant: 'danger', framed: true } as any} />); |
| 71 | + |
| 72 | + expect(mockCustomIcon).toHaveBeenCalledTimes(1); |
| 73 | + const firstCallArg = (mockCustomIcon.mock.calls[0] as any[])[0]; |
| 74 | + expect(firstCallArg).toEqual( |
| 75 | + expect.objectContaining({ |
| 76 | + name: 'bell', |
| 77 | + color: '#ff2020', |
| 78 | + size: 20 |
| 79 | + }) |
| 80 | + ); |
| 81 | + expect(toJSON()).toMatchObject({ |
| 82 | + props: { |
| 83 | + style: expect.arrayContaining([expect.objectContaining({ backgroundColor: '#f2f2f2' })]) |
| 84 | + } |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments