Skip to content

Commit efc3b09

Browse files
committed
Add testing
1 parent 2a0db28 commit efc3b09

File tree

8 files changed

+12314
-7054
lines changed

8 files changed

+12314
-7054
lines changed

package-lock.json

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

package.json

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
"build:ssr": "cross-env NODE_ENV=production rollup --config build/rollup.config.js --format umd",
3636
"build:es": "cross-env NODE_ENV=production rollup --config build/rollup.config.js --format es",
3737
"build:unpkg": "cross-env NODE_ENV=production rollup --config build/rollup.config.js --format iife",
38-
"postbuild": "rimraf ./dist/types/dev ./dist/types/src/entry.d.ts"
38+
"postbuild": "rimraf ./dist/types/dev ./dist/types/src/entry.d.ts",
39+
"test": "jest",
40+
"test:watch": "jest --watch"
3941
},
4042
"devDependencies": {
4143
"@babel/core": "^7.14.6",
@@ -47,12 +49,17 @@
4749
"@rollup/plugin-commonjs": "^14.0.0",
4850
"@rollup/plugin-node-resolve": "^9.0.0",
4951
"@rollup/plugin-replace": "^2.4.2",
52+
"@types/jest": "^29.5.14",
5053
"@vue/cli-plugin-babel": "^4.5.13",
5154
"@vue/cli-plugin-typescript": "^4.5.13",
5255
"@vue/cli-service": "^4.5.13",
5356
"@vue/compiler-sfc": "^3.0.11",
57+
"@vue/test-utils": "^2.4.0",
58+
"@vue/vue3-jest": "^29.2.6",
5459
"@zerollup/ts-transform-paths": "^1.7.18",
5560
"cross-env": "^7.0.3",
61+
"jest": "^29.5.0",
62+
"jest-environment-jsdom": "^29.5.0",
5663
"minimist": "^1.2.5",
5764
"postcss": "^8.2.10",
5865
"rimraf": "^3.0.2",
@@ -62,6 +69,7 @@
6269
"rollup-plugin-terser": "^7.0.2",
6370
"rollup-plugin-typescript2": "^0.31.0",
6471
"rollup-plugin-vue": "^6.0.0",
72+
"ts-jest": "^29.1.2",
6573
"ttypescript": "^1.5.12",
6674
"typescript": "4.6.4",
6775
"vue": "^3.0.5"
@@ -71,5 +79,28 @@
7179
},
7280
"engines": {
7381
"node": ">=12"
82+
},
83+
"jest": {
84+
"testEnvironment": "jsdom",
85+
"transform": {
86+
"^.+\\.vue$": "@vue/vue3-jest",
87+
"^.+\\.(ts|tsx)$": "ts-jest"
88+
},
89+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
90+
"moduleFileExtensions": [
91+
"vue",
92+
"js",
93+
"jsx",
94+
"ts",
95+
"tsx",
96+
"json",
97+
"node"
98+
],
99+
"moduleNameMapper": {
100+
"^@/(.*)$": "<rootDir>/src/$1"
101+
},
102+
"testEnvironmentOptions": {
103+
"customExportConditions": ["node", "node-addons"]
104+
}
74105
}
75106
}

src/style.css

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,37 @@
33
color: white;
44
}
55

6-
.vue3-tree-vue .tree-item:hover .on-item-hover{
7-
display: block;
6+
.vue3-tree-vue .tree-item:hover .on-item-hover {
7+
display: block;
88
}
99

10-
.vue3-tree-vue .tree-item .on-item-hover{
11-
display: none;
10+
.vue3-tree-vue .tree-item .on-item-hover {
11+
display: none;
1212
}
1313

14-
.vue3-tree-vue .tree-item-node:hover li > .on-item-hover{
15-
display: block;
14+
.vue3-tree-vue .tree-item-node:hover li>.on-item-hover {
15+
display: block;
1616
}
1717

18-
.vue3-tree-vue .tree-item-node li > .on-item-hover{
19-
display: none;
18+
.vue3-tree-vue .tree-item-node li>.on-item-hover {
19+
display: none;
2020
}
2121

2222

2323
.vue3-tree-vue .no-guide .tree-item-node {
24-
border: none !important;
24+
border: none !important;
2525
}
26+
2627
.vue3-tree-vue .no-guide .tree-item-node::before {
27-
border: none !important;
28+
border: none !important;
2829
}
2930

3031
.vue3-tree-vue .no-guide .guide-line {
31-
border: none !important;
32+
border: none !important;
3233
}
3334

3435
.vue3-tree-vue .no-guide .guide-line::before {
35-
border: none !important;
36+
border: none !important;
3637
}
3738

3839

@@ -42,7 +43,7 @@ border: none !important;
4243
* Add a dashed border to the left of all but the last child in a parent node.
4344
* There will be a gap between the bottom of this tree-item-node and the top of the next sibling.
4445
*/
45-
.vue3-tree-vue .node-child .tree-item-node-parent .tree-item-node:not(:last-child) {
46+
.vue3-tree-vue .node-child .tree-item-node-parent .tree-item-node:not(:last-child) {
4647
position: relative;
4748
border-left: 1px dashed rgb(192, 192, 192);
4849
border-radius: 0;
@@ -53,31 +54,38 @@ border: none !important;
5354
* use the ::before pseudo-element to create a dashed line that extends down to the next sibling.
5455
* This will be applied to all but the last child in a parent node, and fill in the gap mentioned above.
5556
*/
56-
.vue3-tree-vue .node-child .tree-item-node-parent .tree-item-node:not(:last-child)::before {
57+
.vue3-tree-vue .node-child .tree-item-node-parent .tree-item-node:not(:last-child)::before {
5758
content: "";
5859
position: absolute;
59-
top: 100%; /* Extend below the actual element */
60-
left: -1px; /* Match the border-left width */
61-
height: 1em; /* Adjust the desired height */
60+
top: 100%;
61+
/* Extend below the actual element */
62+
left: -1px;
63+
/* Match the border-left width */
64+
height: 1em;
65+
/* Adjust the desired height */
6266
border-left: 1px dashed rgb(192, 192, 192);
6367
}
6468

6569

66-
.vue3-tree-vue .tree-item-node:only-child .tree-view-item{
67-
position: relative; /* Needed to position the ::before pseudo-element */
70+
.vue3-tree-vue .tree-item-node:only-child .tree-view-item {
71+
position: relative;
72+
/* Needed to position the ::before pseudo-element */
6873
}
6974

7075
/**
7176
* Create Vertical Guideline for only-children
7277
* Since we rely on the previous sibling to fill in the gap by extending down,
7378
* only-child elements will need to extend up to fill in the gap.
7479
*/
75-
.vue3-tree-vue .node-child .tree-item-node-parent .tree-item-node:only-child > .tree-view-item > .guide-line::before {
80+
.vue3-tree-vue .node-child .tree-item-node-parent .tree-item-node:only-child>.tree-view-item>.guide-line::before {
7681
content: "";
7782
position: absolute;
78-
bottom: 50%; /* Extend above the actual element */
79-
left: -1px; /* Match the border-left width */
80-
height: 1em; /* Extend 20px above the element */
83+
bottom: 50%;
84+
/* Extend above the actual element */
85+
left: -1px;
86+
/* Match the border-left width */
87+
height: 1em;
88+
/* Extend 20px above the element */
8189
border-left: 1px dashed rgb(192, 192, 192);
8290
}
8391

@@ -174,7 +182,7 @@ border: none !important;
174182
box-sizing: border-box;
175183
position: relative;
176184
display: block;
177-
transform: scale(var(--ggs,1));
185+
transform: scale(var(--ggs, 1));
178186
width: 22px;
179187
height: 22px;
180188
border: 2px solid transparent;

src/tree-item.ts

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,49 @@ export default defineComponent({
3131
const checkbox = ref<HTMLInputElement>();
3232
const parent = computed<TreeViewItem>(() => attrs.parent as TreeViewItem);
3333
const treeState = inject<TreeState>(_TREE_STATE_PROVIDER_INJECT_KEY)!;
34-
const setCheckboxState = () => checkbox.value!.checked = props.item.checked!;
34+
35+
const setCheckboxState = () => {
36+
if (checkbox.value && props.item.checked !== undefined) {
37+
checkbox.value.checked = props.item.checked;
38+
}
39+
};
3540

3641
onMounted(() => {
3742
treeState.trackNode(props.item, parent.value);
3843
if (props.item.checked) {
39-
setCheckboxState();
40-
updateParentCheckState(props.item!, treeState);
44+
nextTick(() => {
45+
setCheckboxState();
46+
updateParentCheckState(props.item!, treeState);
47+
});
4148
}
42-
else
43-
{
44-
props.item.checked = parent.value?.checked;
49+
else if (parent.value?.checked !== undefined) {
50+
props.item.checked = parent.value.checked;
4551
}
4652
});
4753

4854
const updateCheckState = () => {
49-
props.item.checked = checkbox.value?.checked;
50-
props.item.indeterminate = false;
51-
52-
updateParentCheckState(props.item!, treeState);
53-
updateChildrenCheckState(props.item!, treeState);
54-
treeState.emitItemCheckedChange();
55+
if (checkbox.value) {
56+
props.item.checked = checkbox.value.checked;
57+
props.item.indeterminate = false;
58+
59+
updateParentCheckState(props.item!, treeState);
60+
updateChildrenCheckState(props.item!, treeState);
61+
treeState.emitItemCheckedChange();
62+
}
5563
};
5664

5765
watch(
5866
() => props.item.indeterminate,
59-
() => checkbox.value!.indeterminate = props.item.indeterminate
67+
() => {
68+
if (checkbox.value && props.item.indeterminate !== undefined) {
69+
checkbox.value.indeterminate = props.item.indeterminate;
70+
}
71+
}
6072
);
6173

6274
watch(
6375
() => props.item.checked,
64-
() => setCheckboxState()
76+
() => nextTick(setCheckboxState)
6577
);
6678

6779
watch(
@@ -74,7 +86,6 @@ export default defineComponent({
7486
() => props.item.children?.forEach(child => treeState?.trackNode(child, props.item))
7587
)
7688

77-
7889
const isRenaming = ref(false);
7990
const renameBox = ref<HTMLInputElement>();
8091

@@ -99,23 +110,27 @@ export default defineComponent({
99110
/// expanded/collapsed events.
100111

101112
if (shouldSet) {
102-
props.item.expanded = !props.item.expanded;
103-
if (props.item.expanded)
104-
treeState.emitItemExpanded(props.item);
105-
else
106-
treeState.emitItemCollapsed(props.item);
113+
props.item.expanded = !props.item.expanded;
114+
}
115+
116+
if (props.item.expanded) {
117+
treeState?.emitItemExpanded(props.item);
118+
}
119+
else {
120+
treeState?.emitItemCollapsed(props.item);
107121
}
108122
}
109123

110124
return {
111-
toggleExpand,
125+
checkbox,
126+
parent,
112127
treeState,
113-
updateCheckState,
114128
isRenaming,
129+
renameBox,
130+
updateCheckState,
115131
beginRenaming,
116132
finishRenaming,
117-
parent,
118-
checkbox
133+
toggleExpand
119134
}
120135
}
121136
})

src/tree-item.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="d-flex align-items-center" @contextmenu.prevent="$emit('onContextMenu', { item, event: $event })">
33
<div class="guide-line" v-if="parent != null && !hideGuideLines"></div>
44

5-
<div @click="toggleExpand()" v-show="lazyLoad || item.children && item.children.length > 0">
5+
<div @click="toggleExpand()" v-if="lazyLoad || (item.children && item.children.length > 0)">
66
<slot name="expander" v-bind="item">
77
<span class="chevron-right" :class="{'rotate-90' : item.expanded }"></span>
88
</slot>

0 commit comments

Comments
 (0)