-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
184 additions
and
4,223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const camelCase = require('lodash/camelCase'); | ||
const upperFirst = require('lodash/upperFirst'); | ||
|
||
function resolveCwd(...args) { | ||
args.unshift(process.cwd()); | ||
return path.join(...args); | ||
} | ||
|
||
const pkg = require(resolveCwd('package.json')); | ||
|
||
const framework = pkg.name; | ||
|
||
const fixedDateComponentList = ['config-provider', 'time-picker', 'date-picker', 'table', 'form', 'calendar']; // 需要在测试阶段固定日期的组件,table中因为有filter例子 渲染datepicker需要固定 | ||
|
||
// TODO 过滤掉一些导致挂掉的demo | ||
const filterCom = ['table']; | ||
const filterDemo = { | ||
table: ['virtual-scroll'], | ||
}; | ||
|
||
const CONFIG = { | ||
'tdesign-mobile-react': { | ||
sourcePath: path.resolve(__dirname, resolveCwd('src')), | ||
targetPath: path.resolve(__dirname, resolveCwd('src')), | ||
defaultTemplate: 'import { mount } from \'@vue/test-utils\';', | ||
}, | ||
'tdesign-mobile-vue': { | ||
sourcePath: path.resolve(__dirname, resolveCwd('src')), | ||
targetPath: path.resolve(__dirname, resolveCwd(`src`)), | ||
defaultTemplate: 'import { mount } from \'@vue/test-utils\';', | ||
}, | ||
}; | ||
|
||
const { sourcePath, targetPath, defaultTemplate } = CONFIG[framework]; | ||
|
||
const data = `/** | ||
* 该文件为由脚本 \`npm run test:demo\` 自动生成,如需修改,执行脚本命令即可。请勿手写直接修改,否则会被覆盖 | ||
*/ | ||
${defaultTemplate} | ||
{{ HERE IS DEMO LIST }} | ||
`; | ||
|
||
function filterFileFunction(files) { | ||
// 过滤非 vue jsx 文件 | ||
const fileType = ['vue', 'jsx'] | ||
return files.filter(item => { | ||
return fileType.includes(item.substr(item.lastIndexOf('.') + 1, item.length)) | ||
}) | ||
} | ||
|
||
function getKeyFunction(component) { | ||
const newComponent = upperFirst(camelCase(component)); | ||
|
||
return ` | ||
describe('${newComponent}', () => { | ||
Object.keys(mapper).forEach((demoName) => { | ||
it(\`${newComponent} \${demoName} demo works fine\`, () => { | ||
const wrapper = mount(mapper[demoName]); | ||
expect(wrapper.element).toMatchSnapshot(); | ||
}); | ||
}); | ||
});`; | ||
} | ||
|
||
function outputOneComponentTestFile(component, demoFiles) { | ||
const outputPath = `${targetPath}/${component}/__test__`; | ||
const imports = []; | ||
const demos = ['\nconst mapper = {']; | ||
|
||
demoFiles.forEach((demo) => { | ||
if (filterCom.includes(component) && filterDemo[component].includes(demo.replace('.vue', ''))) return; | ||
|
||
const name = camelCase(demo); | ||
imports.push(`import ${name} from '@/${component}/demos/${demo}';`); | ||
demos.push(` ${name},`); | ||
}); | ||
if (fixedDateComponentList.includes(component)) { | ||
imports.unshift('import MockDate from \'mockdate\';\n'); | ||
imports.push('\nMockDate.set(\'2020-12-28\');'); | ||
} | ||
|
||
demos.push('};'); | ||
const keyData = [imports.join('\n'), demos.join('\n'), getKeyFunction(component)].join('\n'); | ||
const testFileData = data.replace('{{ HERE IS DEMO LIST }}', keyData); | ||
fs.mkdir(outputPath, { recursive: true }, (err) => { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
fs.writeFile(`${outputPath}/demo.test.tsx`, testFileData, (writeErr) => { | ||
if (writeErr) { | ||
return console.error(writeErr); | ||
} | ||
return console.log(`test file: ${outputPath} has been created.`); | ||
}); | ||
}); | ||
} | ||
|
||
function main() { | ||
fs.readdir(sourcePath, (err, files) => { | ||
if (err) { | ||
console.log('Error', err); | ||
} else { | ||
files.forEach((componentFolder) => { | ||
const demoPath = `${sourcePath}/${componentFolder}/demos`; | ||
fs.readdir(demoPath, (err1, demoFiles) => { | ||
if (err1) { | ||
console.log('Error', err1); | ||
} else { | ||
if (['icon', 'local-provider'].includes(componentFolder)) return; | ||
const finalDemoFiles = filterFileFunction(demoFiles); | ||
outputOneComponentTestFile(componentFolder, finalDemoFiles); | ||
} | ||
}); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,153 +1,4 @@ | ||
module.exports = { | ||
"": "93.84%", | ||
"actionSheet": "24.48%", | ||
"actionSheet/demos": "0%", | ||
"actionSheet/style": "0%", | ||
"avatar": "91.17%", | ||
"avatarGroup": "35.89%", | ||
"avatarGroup/style": "0%", | ||
"avatar/demos": "0%", | ||
"avatar/style": "0%", | ||
"backTop": "93.93%", | ||
"backTop/demos": "0%", | ||
"backTop/style": "0%", | ||
"badge": "88.57%", | ||
"badge/demos": "0%", | ||
"badge/style": "0%", | ||
"button": "91.17%", | ||
"buttonGroup": "83.33%", | ||
"buttonGroup/style": "0%", | ||
"button/demos": "0%", | ||
"button/style": "0%", | ||
"cell": "100%", | ||
"cellGroup": "92.85%", | ||
"cellGroup/style": "0%", | ||
"cell/demos": "0%", | ||
"cell/style": "0%", | ||
"checkGroup": "13.97%", | ||
"checkGroup/style": "0%", | ||
"checkbox": "35.41%", | ||
"checkbox/demos": "0%", | ||
"checkbox/style": "0%", | ||
"collapse": "27.1%", | ||
"collapse/demos": "0%", | ||
"collapse/style": "0%", | ||
"countDown": "73.68%", | ||
"countDown/demos": "0%", | ||
"countDown/style": "0%", | ||
"dateTimePicker": "12.3%", | ||
"dateTimePicker/demos": "0%", | ||
"dateTimePicker/style": "0%", | ||
"dialog": "24.13%", | ||
"dialog/demos": "0%", | ||
"dialog/style": "0%", | ||
"divider": "70%", | ||
"divider/style": "0%", | ||
"drawer": "42.85%", | ||
"drawer/demos": "0%", | ||
"drawer/style": "0%", | ||
"dropdownMenu": "0%", | ||
"dropdownMenu/demos": "0%", | ||
"dropdownMenu/style": "0%", | ||
"fab": "57.14%", | ||
"fab/style": "0%", | ||
"grid": "41.07%", | ||
"grid/demos": "0%", | ||
"grid/style": "0%", | ||
"image": "31.37%", | ||
"imageViewer": "8.62%", | ||
"imageViewer/demos": "0%", | ||
"imageViewer/style": "0%", | ||
"image/demos": "0%", | ||
"image/style": "0%", | ||
"indexes": "23.89%", | ||
"indexes/demos": "0%", | ||
"indexes/style": "0%", | ||
"input": "29.82%", | ||
"input/demos": "0%", | ||
"input/style": "0%", | ||
"list": "26.49%", | ||
"list/demos": "0%", | ||
"list/style": "0%", | ||
"loading": "39.13%", | ||
"loading/demos": "0%", | ||
"loading/icon": "57.14%", | ||
"loading/style": "0%", | ||
"mask": "76.92%", | ||
"mask/demos": "0%", | ||
"mask/style": "0%", | ||
"message": "32.75%", | ||
"message/demos": "0%", | ||
"message/style": "0%", | ||
"navbar": "50%", | ||
"navbar/demos": "0%", | ||
"navbar/style": "0%", | ||
"noticeBar": "0%", | ||
"noticeBar/demos": "0%", | ||
"noticeBar/style": "0%", | ||
"picker": "12.65%", | ||
"picker/demos": "0%", | ||
"picker/style": "0%", | ||
"popup": "34.88%", | ||
"popup/demos": "0%", | ||
"popup/style": "0%", | ||
"progress": "54.54%", | ||
"progress/style": "0%", | ||
"radio": "32.72%", | ||
"radioGroup": "48%", | ||
"radioGroup/style": "0%", | ||
"radio/demos": "0%", | ||
"radio/style": "0%", | ||
"rate": "30%", | ||
"rate/demos": "0%", | ||
"rate/style": "0%", | ||
"search": "28.94%", | ||
"search/demos": "0%", | ||
"search/style": "0%", | ||
"shared": "66.66%", | ||
"shared/useChildSlots": "10.52%", | ||
"shared/useCountDown": "21.05%", | ||
"shared/useDefault": "7.89%", | ||
"shared/useEmitEvent": "33.33%", | ||
"shared/useInterval": "0%", | ||
"shared/useToggle": "14.28%", | ||
"skeleton": "31.25%", | ||
"skeleton/demos": "0%", | ||
"skeleton/style": "0%", | ||
"slider": "10.28%", | ||
"slider/demos": "0%", | ||
"slider/style": "0%", | ||
"stepper": "35.55%", | ||
"stepper/demos": "0%", | ||
"stepper/style": "0%", | ||
"steps": "32.46%", | ||
"steps/demos": "0%", | ||
"steps/style": "0%", | ||
"sticky": "29.78%", | ||
"sticky/demos": "0%", | ||
"sticky/style": "0%", | ||
"style": "0%", | ||
"swipeCell": "15.38%", | ||
"swipeCell/demos": "0%", | ||
"swipeCell/style": "0%", | ||
"swiper": "14.28%", | ||
"swiper/demos": "0%", | ||
"swiper/style": "0%", | ||
"switch": "51.72%", | ||
"switch/demos": "0%", | ||
"switch/style": "0%", | ||
"tabBar": "40.9%", | ||
"tabBar/demos": "0%", | ||
"tabBar/style": "0%", | ||
"tabs": "29%", | ||
"tabs/demos": "0%", | ||
"tabs/style": "0%", | ||
"tag": "39.7%", | ||
"tag/demos": "0%", | ||
"tag/style": "0%", | ||
"textarea": "23.07%", | ||
"textarea/style": "0%", | ||
"toast": "34.32%", | ||
"toast/demos": "0%", | ||
"toast/style": "0%" | ||
"": "100%", | ||
"checkbox": "17.64%" | ||
} |
Oops, something went wrong.