|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | | -import { normalizeCodeBlockLanguages, EMPTY_VALUE, type CodeBlockLanguageSupport } from '../plugins/codemirror' |
| 2 | +import { getCodeBlockLanguageSelectData, normalizeCodeBlockLanguages, EMPTY_VALUE, type CodeBlockLanguageSupport } from '../plugins/codemirror' |
3 | 3 |
|
4 | 4 | describe('normalizeCodeBlockLanguages', () => { |
5 | 5 | describe('record format', () => { |
@@ -68,6 +68,18 @@ describe('normalizeCodeBlockLanguages', () => { |
68 | 68 | const result = normalizeCodeBlockLanguages([{ name: 'JavaScript', alias: ['js'] }]) |
69 | 69 | expect(result.keyMap.javascript).toBe('js') |
70 | 70 | }) |
| 71 | + |
| 72 | + it('maps empty-string canonical to EMPTY_VALUE sentinel', () => { |
| 73 | + const result = normalizeCodeBlockLanguages([ |
| 74 | + { name: 'Plain text', alias: [''] }, |
| 75 | + { name: 'JavaScript', alias: ['js'] } |
| 76 | + ]) |
| 77 | + expect(result.items).toEqual([ |
| 78 | + { value: EMPTY_VALUE, label: 'Plain text' }, |
| 79 | + { value: 'js', label: 'JavaScript' } |
| 80 | + ]) |
| 81 | + expect(result.keyMap['']).toBe(EMPTY_VALUE) |
| 82 | + }) |
71 | 83 | }) |
72 | 84 |
|
73 | 85 | describe('supportMap', () => { |
@@ -107,4 +119,58 @@ describe('normalizeCodeBlockLanguages', () => { |
107 | 119 | expect(result.keyMap).toEqual({}) |
108 | 120 | }) |
109 | 121 | }) |
| 122 | + |
| 123 | + describe('unknown language', () => { |
| 124 | + it('returns undefined from keyMap for an unknown language (record format)', () => { |
| 125 | + const result = normalizeCodeBlockLanguages({ js: 'JavaScript' }) |
| 126 | + expect(result.keyMap.brainfuck).toBeUndefined() |
| 127 | + expect(Object.hasOwn(result.keyMap, 'brainfuck')).toBe(false) |
| 128 | + }) |
| 129 | + |
| 130 | + it('returns undefined from keyMap for an unknown language (array format)', () => { |
| 131 | + const result = normalizeCodeBlockLanguages([{ name: 'JavaScript', alias: ['js'], extensions: ['js', 'mjs'] }]) |
| 132 | + expect(result.keyMap.brainfuck).toBeUndefined() |
| 133 | + expect(Object.hasOwn(result.keyMap, 'brainfuck')).toBe(false) |
| 134 | + }) |
| 135 | + |
| 136 | + it('returns undefined from supportMap for an unknown language', () => { |
| 137 | + const mockSupport: CodeBlockLanguageSupport = { extension: [] } |
| 138 | + const result = normalizeCodeBlockLanguages([{ name: 'JavaScript', alias: ['js'], support: mockSupport }]) |
| 139 | + expect(result.supportMap.brainfuck).toBeUndefined() |
| 140 | + }) |
| 141 | + |
| 142 | + it('does not include an unknown language in items', () => { |
| 143 | + const result = normalizeCodeBlockLanguages([{ name: 'JavaScript', alias: ['js'] }]) |
| 144 | + expect(result.items.find((item) => item.value === 'brainfuck')).toBeUndefined() |
| 145 | + }) |
| 146 | + }) |
| 147 | +}) |
| 148 | + |
| 149 | +describe('getCodeBlockLanguageSelectData', () => { |
| 150 | + it('returns configured items for a known language', () => { |
| 151 | + const normalized = normalizeCodeBlockLanguages([{ name: 'JavaScript', alias: ['js'] }]) |
| 152 | + expect(getCodeBlockLanguageSelectData(normalized, 'js')).toEqual({ |
| 153 | + value: 'js', |
| 154 | + items: [{ value: 'js', label: 'JavaScript' }] |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + it('adds a temporary item for an unknown language', () => { |
| 159 | + const normalized = normalizeCodeBlockLanguages([{ name: 'JavaScript', alias: ['js'] }]) |
| 160 | + expect(getCodeBlockLanguageSelectData(normalized, 'brainfuck')).toEqual({ |
| 161 | + value: 'brainfuck', |
| 162 | + items: [ |
| 163 | + { value: 'js', label: 'JavaScript' }, |
| 164 | + { value: 'brainfuck', label: 'brainfuck' } |
| 165 | + ] |
| 166 | + }) |
| 167 | + }) |
| 168 | + |
| 169 | + it('keeps the empty-language sentinel intact', () => { |
| 170 | + const normalized = normalizeCodeBlockLanguages([{ name: 'Plain text', alias: [''] }]) |
| 171 | + expect(getCodeBlockLanguageSelectData(normalized, '')).toEqual({ |
| 172 | + value: EMPTY_VALUE, |
| 173 | + items: [{ value: EMPTY_VALUE, label: 'Plain text' }] |
| 174 | + }) |
| 175 | + }) |
110 | 176 | }) |
0 commit comments