|
| 1 | +/** |
| 2 | + * Copyright (C) 2026 Percona LLC |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +import { readFileSync } from 'node:fs'; |
| 19 | +import path from 'node:path'; |
| 20 | +import { fileURLToPath } from 'node:url'; |
| 21 | +import { describe, expect, it } from 'vitest'; |
| 22 | +import { BRAND_ICONS, pascalCase, resolveNavIcon } from './appNavIcons.codegen'; |
| 23 | + |
| 24 | +const SPEC_PATH = path.resolve( |
| 25 | + path.dirname(fileURLToPath(import.meta.url)), |
| 26 | + '../../api/specs/sep.json', |
| 27 | +); |
| 28 | +const NAV_ICON_KEYS: string[] = JSON.parse(readFileSync(SPEC_PATH, 'utf8')).components.schemas |
| 29 | + .nav_icons__NavIcon.enum; |
| 30 | + |
| 31 | +describe('pascalCase', () => { |
| 32 | + it('joins kebab segments into a single PascalCase token', () => { |
| 33 | + expect(pascalCase('support-agent')).toBe('SupportAgent'); |
| 34 | + expect(pascalCase('bar-chart')).toBe('BarChart'); |
| 35 | + expect(pascalCase('code')).toBe('Code'); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('resolveNavIcon', () => { |
| 40 | + it('brands exactly the three non-MUI keys', () => { |
| 41 | + expect(Object.keys(BRAND_ICONS).sort()).toEqual(['mongo', 'mysql', 'postgresql']); |
| 42 | + }); |
| 43 | + |
| 44 | + it('routes brand keys to the @percona/percona-ui barrel', () => { |
| 45 | + expect(resolveNavIcon('mysql')).toEqual({ |
| 46 | + module: '@percona/percona-ui', |
| 47 | + importName: 'MySqlIcon', |
| 48 | + }); |
| 49 | + expect(resolveNavIcon('mongo')).toEqual({ |
| 50 | + module: '@percona/percona-ui', |
| 51 | + importName: 'MongoIcon', |
| 52 | + }); |
| 53 | + expect(resolveNavIcon('postgresql')).toEqual({ |
| 54 | + module: '@percona/percona-ui', |
| 55 | + importName: 'PostgreSqlIcon', |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('routes non-brand keys to the MUI subpath with an Icon binding', () => { |
| 60 | + expect(resolveNavIcon('support-agent')).toEqual({ |
| 61 | + module: '@mui/icons-material/SupportAgent', |
| 62 | + importName: 'SupportAgentIcon', |
| 63 | + }); |
| 64 | + expect(resolveNavIcon('code')).toEqual({ |
| 65 | + module: '@mui/icons-material/Code', |
| 66 | + importName: 'CodeIcon', |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('does not treat inherited Object.prototype keys as brand keys', () => { |
| 71 | + const resolved = resolveNavIcon('toString'); |
| 72 | + expect(resolved.module).not.toBe('@percona/percona-ui'); |
| 73 | + expect(typeof resolved.importName).toBe('string'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('resolves every NavIcon vocabulary key to an import source', () => { |
| 77 | + for (const key of NAV_ICON_KEYS) { |
| 78 | + const resolved = resolveNavIcon(key); |
| 79 | + expect(resolved.module).toBeTruthy(); |
| 80 | + expect(resolved.importName).toBeTruthy(); |
| 81 | + } |
| 82 | + }); |
| 83 | +}); |
0 commit comments