import * as fs from 'fs'; import * as path from 'path'; describe('uni-app 框架自带原生 tabBar 规范测试 (TDD Red -> Green)', () => { const rootDir = path.resolve(__dirname, '../../../mpuniapp'); const pagesJsonPath = path.join(rootDir, 'pages.json'); it('1. pages.json 中必须配置合法且符合 uni-app 原生规范的 tabBar', () => { expect(fs.existsSync(pagesJsonPath)).toBe(true); const content = fs.readFileSync(pagesJsonPath, 'utf-8'); // 去除 json 中的注释 const cleanJson = content.replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm, '$1'); const config = JSON.parse(cleanJson); expect(config.tabBar).toBeDefined(); expect(Array.isArray(config.tabBar.list)).toBe(true); expect(config.tabBar.list.length).toBeGreaterThanOrEqual(2); expect(config.tabBar.list.length).toBeLessThanOrEqual(5); // 关键规范:tabBar.list[0].pagePath 必须与 pages[0].path 一致,原生 tabBar 才能在首屏正常加载 expect(config.tabBar.list[0].pagePath).toBe(config.pages[0].path); expect(['工作台', '名片']).toContain(config.tabBar.list[0].text); // 校验颜色 Token expect(config.tabBar.color.toLowerCase()).toBe('#667085'); expect(config.tabBar.selectedColor.toLowerCase()).toBe('#0b5cff'); }); it('2. tabBar 中声明的所有图标资源文件必须真实存在', () => { const content = fs.readFileSync(pagesJsonPath, 'utf-8'); const cleanJson = content.replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm, '$1'); const config = JSON.parse(cleanJson); for (const item of config.tabBar.list) { if (item.iconPath) { const iconFile = path.join(rootDir, item.iconPath); expect(fs.existsSync(iconFile)).toBe(true); } if (item.selectedIconPath) { const selIconFile = path.join(rootDir, item.selectedIconPath); expect(fs.existsSync(selIconFile)).toBe(true); } } }); });