|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { |
| 3 | + ValidOcticon, |
| 4 | + VALID_OCTICONS, |
| 5 | + OCTICON_COMPONENTS, |
| 6 | + isValidOcticon, |
| 7 | + getOcticonComponent, |
| 8 | +} from '../lib/octicons' |
| 9 | +import { CopilotIcon, BugIcon, RocketIcon } from '@primer/octicons-react' |
| 10 | + |
| 11 | +describe('octicons reference', () => { |
| 12 | + describe('VALID_OCTICONS', () => { |
| 13 | + test('contains expected octicon names', () => { |
| 14 | + // Test that we have the expected number of octicons and they're all defined |
| 15 | + expect(VALID_OCTICONS.length).toBeGreaterThan(0) |
| 16 | + expect(VALID_OCTICONS).toEqual(expect.arrayContaining(['bug', 'rocket', 'copilot'])) |
| 17 | + }) |
| 18 | + |
| 19 | + test('all octicons are strings', () => { |
| 20 | + VALID_OCTICONS.forEach((octicon) => { |
| 21 | + expect(typeof octicon).toBe('string') |
| 22 | + }) |
| 23 | + }) |
| 24 | + }) |
| 25 | + |
| 26 | + describe('OCTICON_COMPONENTS', () => { |
| 27 | + test('has components for all valid octicons', () => { |
| 28 | + VALID_OCTICONS.forEach((octicon) => { |
| 29 | + expect(OCTICON_COMPONENTS[octicon]).toBeDefined() |
| 30 | + expect(typeof OCTICON_COMPONENTS[octicon]).toBe('object') |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + test('maps specific octicons to correct components', () => { |
| 35 | + expect(OCTICON_COMPONENTS.bug).toBe(BugIcon) |
| 36 | + expect(OCTICON_COMPONENTS.rocket).toBe(RocketIcon) |
| 37 | + expect(OCTICON_COMPONENTS.copilot).toBe(CopilotIcon) |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + describe('isValidOcticon', () => { |
| 42 | + test('returns true for valid octicons', () => { |
| 43 | + expect(isValidOcticon('bug')).toBe(true) |
| 44 | + expect(isValidOcticon('rocket')).toBe(true) |
| 45 | + expect(isValidOcticon('shield-lock')).toBe(true) |
| 46 | + }) |
| 47 | + |
| 48 | + test('returns false for invalid octicons', () => { |
| 49 | + expect(isValidOcticon('invalid-octicon')).toBe(false) |
| 50 | + expect(isValidOcticon('pizza')).toBe(false) |
| 51 | + expect(isValidOcticon('')).toBe(false) |
| 52 | + }) |
| 53 | + |
| 54 | + test('returns false for null or undefined', () => { |
| 55 | + expect(isValidOcticon(null)).toBe(false) |
| 56 | + expect(isValidOcticon(undefined as any)).toBe(false) |
| 57 | + }) |
| 58 | + |
| 59 | + test('provides correct type narrowing', () => { |
| 60 | + const testOcticon: string | null = 'bug' |
| 61 | + |
| 62 | + if (isValidOcticon(testOcticon)) { |
| 63 | + // This should compile without type errors |
| 64 | + const validOcticon: ValidOcticon = testOcticon |
| 65 | + expect(validOcticon).toBe('bug') |
| 66 | + } |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + describe('getOcticonComponent', () => { |
| 71 | + test('returns correct component for valid octicons', () => { |
| 72 | + expect(getOcticonComponent('bug')).toBe(BugIcon) |
| 73 | + expect(getOcticonComponent('rocket')).toBe(RocketIcon) |
| 74 | + expect(getOcticonComponent('copilot')).toBe(CopilotIcon) |
| 75 | + }) |
| 76 | + |
| 77 | + test('returns CopilotIcon as fallback for undefined', () => { |
| 78 | + expect(getOcticonComponent(undefined)).toBe(CopilotIcon) |
| 79 | + }) |
| 80 | + |
| 81 | + test('returns CopilotIcon as fallback for invalid octicons', () => { |
| 82 | + // TypeScript should prevent this, but test runtime behavior |
| 83 | + expect(getOcticonComponent('invalid' as ValidOcticon)).toBe(CopilotIcon) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + describe('type safety', () => { |
| 88 | + test('ValidOcticon type includes all expected values', () => { |
| 89 | + // This test ensures the type system prevents invalid octicons at compile time |
| 90 | + // Test a few key octicons to verify the type works correctly |
| 91 | + const testOcticons: ValidOcticon[] = ['bug', 'rocket', 'copilot'] |
| 92 | + |
| 93 | + testOcticons.forEach((octicon) => { |
| 94 | + expect(VALID_OCTICONS.includes(octicon)).toBe(true) |
| 95 | + }) |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + describe('consistency checks', () => { |
| 100 | + test('OCTICON_COMPONENTS keys match VALID_OCTICONS', () => { |
| 101 | + const componentKeys = Object.keys(OCTICON_COMPONENTS) |
| 102 | + const validOcticonsSet = new Set(VALID_OCTICONS) |
| 103 | + |
| 104 | + componentKeys.forEach((key) => { |
| 105 | + expect(validOcticonsSet.has(key as ValidOcticon)).toBe(true) |
| 106 | + }) |
| 107 | + |
| 108 | + expect(componentKeys).toHaveLength(VALID_OCTICONS.length) |
| 109 | + }) |
| 110 | + |
| 111 | + test('no duplicate octicons in VALID_OCTICONS', () => { |
| 112 | + const octiconsSet = new Set(VALID_OCTICONS) |
| 113 | + expect(octiconsSet.size).toBe(VALID_OCTICONS.length) |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + describe('single source of truth', () => { |
| 118 | + test('VALID_OCTICONS is derived from OCTICON_COMPONENTS', () => { |
| 119 | + const componentKeys = Object.keys(OCTICON_COMPONENTS).sort() |
| 120 | + const validOcticons = [...VALID_OCTICONS].sort() |
| 121 | + |
| 122 | + expect(validOcticons).toEqual(componentKeys) |
| 123 | + }) |
| 124 | + |
| 125 | + test('ValidOcticon type matches OCTICON_COMPONENTS keys', () => { |
| 126 | + // This test ensures the type system is correctly derived from the object |
| 127 | + const testOcticon: ValidOcticon = 'bug' |
| 128 | + expect(OCTICON_COMPONENTS[testOcticon]).toBeDefined() |
| 129 | + |
| 130 | + // Type check - this should compile without errors |
| 131 | + const allKeys: ValidOcticon[] = Object.keys(OCTICON_COMPONENTS) as ValidOcticon[] |
| 132 | + expect(allKeys.length).toBeGreaterThan(0) |
| 133 | + }) |
| 134 | + |
| 135 | + test('adding new octicon only requires updating OCTICON_COMPONENTS', () => { |
| 136 | + // This test documents the single source of truth approach |
| 137 | + // If you add a new octicon to OCTICON_COMPONENTS: |
| 138 | + // 1. ValidOcticon type automatically includes it |
| 139 | + // 2. VALID_OCTICONS array automatically includes it |
| 140 | + // 3. All validation functions work with it |
| 141 | + |
| 142 | + const componentCount = Object.keys(OCTICON_COMPONENTS).length |
| 143 | + const validOcticonsCount = VALID_OCTICONS.length |
| 144 | + |
| 145 | + expect(componentCount).toBe(validOcticonsCount) |
| 146 | + }) |
| 147 | + }) |
| 148 | +}) |
0 commit comments