Skip to content

Commit fdd9f62

Browse files
committed
test: 新增单元测试
1 parent 29f21c5 commit fdd9f62

File tree

5 files changed

+96
-21
lines changed

5 files changed

+96
-21
lines changed

.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ module.exports = {
1515
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
1616
},
1717
overrides: [
18+
{
19+
files: [
20+
'**/__tests__/*.{j,t}s?(x)',
21+
'**/tests/unit/**/*.spec.{j,t}s?(x)'
22+
],
23+
env: {
24+
jest: true
25+
}
26+
},
1827
{
1928
files: [
2029
'**/__tests__/*.{j,t}s?(x)',

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,23 @@
22
"name": "json-tree",
33
"version": "1.0.0",
44
"description": "JSON tree view for Vue",
5-
"main": "lib/JsonTree.common.js",
5+
"author": "",
66
"scripts": {
7-
"lib": "vue-cli-service build --target lib --name JsonTree --dest lib src/index.js",
8-
"publish": "npm run lib && npm publish --registry https://www.npmjs.com/",
9-
"docs:dev": "vuepress dev docs",
7+
"test:unit": "vue-cli-service test:unit",
8+
"deploy": "bash scripts/deploy.sh",
109
"docs:build": "vuepress build docs",
11-
"deploy": "bash deploy.sh"
12-
},
13-
"repository": {
14-
"type": "git",
15-
"url": "git+https://github.com/mathink12/json-tree.git"
16-
},
17-
"keywords": [
18-
"JSON",
19-
"tree",
20-
"vue",
21-
"view"
22-
],
23-
"author": "",
24-
"license": "MIT",
25-
"bugs": {
26-
"url": "https://github.com/mathink12/json-tree/issues"
10+
"docs:dev": "vuepress dev docs",
11+
"lib": "vue-cli-service build --target lib --name JsonTree --dest lib src/index.js",
12+
"publish": "npm run lib && npm publish --registry https://www.npmjs.com/"
2713
},
28-
"homepage": "https://github.com/mathink12/json-tree#readme",
14+
"main": "lib/JsonTree.common.js",
2915
"devDependencies": {
3016
"@vue/cli-plugin-babel": "^4.5.9",
3117
"@vue/cli-plugin-eslint": "^4.5.9",
3218
"@vue/cli-plugin-unit-jest": "^4.5.9",
3319
"@vue/cli-service": "^4.5.9",
3420
"@vue/eslint-config-standard": "^5.1.2",
21+
"@vue/test-utils": "^1.0.3",
3522
"@vuepress/plugin-back-to-top": "^1.7.1",
3623
"async-validator": "^1.11.5",
3724
"babel-eslint": "^10.1.0",
@@ -50,13 +37,28 @@
5037
"vue-template-compiler": "^2.6.12",
5138
"vuepress": "^1.7.1"
5239
},
40+
"bugs": {
41+
"url": "https://github.com/mathink12/json-tree/issues"
42+
},
5343
"gitHooks": {
5444
"pre-commit": "lint-staged"
5545
},
46+
"homepage": "https://github.com/mathink12/json-tree#readme",
47+
"keywords": [
48+
"JSON",
49+
"tree",
50+
"vue",
51+
"view"
52+
],
53+
"license": "MIT",
5654
"lint-staged": {
5755
"*.{js,jsx,vue}": [
5856
"vue-cli-service lint",
5957
"git add"
6058
]
59+
},
60+
"repository": {
61+
"type": "git",
62+
"url": "git+https://github.com/mathink12/json-tree.git"
6163
}
6264
}
File renamed without changes.

tests/unit/showLine.spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Prop: showLine(是否显示连接线)
2+
import { mount } from '@vue/test-utils'
3+
import JsonTree from '@/index.js'
4+
5+
// 使用这个方法生成的 json 结构数据
6+
// 根据 JsonTree 的默认项
7+
// 将会全部展开(默认展开两级), 并显示 3 条连接线
8+
const genJsonData = () => {
9+
return {
10+
name: 'Tom',
11+
age: 2,
12+
nums: [1, 1, 2, 3, 5, 8],
13+
friends: ['Jerry', 'Tuffy', 'Spike', 'Tyke']
14+
}
15+
}
16+
17+
const factory = (propsData) => {
18+
return mount(JsonTree, {
19+
propsData: {
20+
jsonData: genJsonData(),
21+
...propsData
22+
}
23+
})
24+
}
25+
26+
// 只要值类型是 object 或 array 就会生成连接线
27+
// 连接线是否显示是通过 'display: none' 来控制的
28+
describe('JsonTree.vue props:showLine', () => {
29+
it('默认情况下显示的连接线个数正确', () => {
30+
const wrapper = factory()
31+
32+
// 根据 json 数据的结构, 会生成相应个数的连接线
33+
// 默认情况下显示连接线
34+
expect(wrapper.findAll('.json-tree__connector-line').length).toBe(3)
35+
})
36+
37+
it('通过 showLine prop 控制连接线的显示', async () => {
38+
const wrapper = factory({ showLine: false })
39+
40+
expect(wrapper.findAll('.json-tree__connector-line').length).toBe(0)
41+
42+
await wrapper.setProps({ showLine: true })
43+
expect(wrapper.findAll('.json-tree__connector-line').length).toBe(3)
44+
})
45+
46+
// TODO: 连接线的可见性
47+
})

0 commit comments

Comments
 (0)