Skip to content

Commit

Permalink
feat: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
irisSong committed Jan 14, 2025
1 parent 1b4e8ca commit 9c46294
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
26 changes: 26 additions & 0 deletions scripts/create-component-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,29 @@ function createDoc() {
})
})
}
function createTest() {
return new Promise((resolve, reject) => {
const nameLc = newCpt.name.toLowerCase()
const { name } = newCpt

let content = demoModel(name).test
const dirPath = path.join(__dirname, '../src/packages/' + nameLc)

const testFolderPath = path.join(dirPath, `__test__`)
const filePath = path.join(testFolderPath, `${nameLc}.spec.tsx`)

if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath)
}
if (!fs.existsSync(testFolderPath)) {
fs.mkdirSync(testFolderPath)
}
fs.writeFile(filePath, content, (err) => {
if (err) throw err
resolve(`doc.md文件成功`)
})
})
}
function createNew() {
createIndexJs()
.then(() => {
Expand All @@ -299,6 +322,9 @@ function createNew() {
.then(() => {
return createDoc()
})
.then(() => {
return createTest()
})
.then(() => {
return addToPackageJson()
})
Expand Down
56 changes: 34 additions & 22 deletions scripts/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ import { useTranslate } from '@/sites/assets/locale'
import Demo1 from './demos/h5/demo1'
const ${name}Demo = () => {
const [translated] = useTranslate({
'zh-CN': {
title: '基础用法',
},
'en-US': {
title: 'Basic Usage',
},
'zh-TW': {
title: '基礎用法',
},
})
const [translated] = useTranslate({
'zh-CN': {
title: '基础用法',
},
'en-US': {
title: 'Basic Usage',
},
'zh-TW': {
title: '基礎用法',
},
})
return (
<div className="demo">
<h2>{translated.title}</h2>
Expand All @@ -62,17 +62,17 @@ import Header from '@/sites/components/header'
import Demo1 from './demos/taro/demo1'
const ${name}Demo = () => {
const [translated] = useTranslate({
'zh-CN': {
title: '基础用法',
},
'en-US': {
title: 'Basic Usage',
},
'zh-TW': {
title: '基礎用法',
},
})
const [translated] = useTranslate({
'zh-CN': {
title: '基础用法',
},
'en-US': {
title: 'Basic Usage',
},
'zh-TW': {
title: '基礎用法',
},
})
return (
<>
<Header />
Expand Down Expand Up @@ -179,6 +179,18 @@ import { name } from '@nutui/nutui-react'
| 名称 | 说明 | 默认值 |
| --- | --- | --- |
| \--nutui-${name.toLowerCase()}-height | badge 的高度 | \`14px\` |
`,
test: `import React from 'react'
import { render } from '@testing-library/react'
import '@testing-library/jest-dom'
import { ${name} } from '../${name.toLowerCase()}'
test('should match snapshot', () => {
const { container } = render(
<${name}></${name}>
)
expect(container).toMatchSnapshot()
})
`,
}

Expand Down

0 comments on commit 9c46294

Please sign in to comment.