Skip to content

Commit 7df6121

Browse files
Improving tests
1 parent 65504a0 commit 7df6121

File tree

4 files changed

+111
-7
lines changed

4 files changed

+111
-7
lines changed

src/core/componentBuilderHelper.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function isHtmlAttribute(value) {
66
return ["id", "class"].includes(value) || value.startsWith("data-");
77
}
88

9-
function getComponentAttributes($attrs, componentData) {
9+
function getComponentAttributes({ $attrs, componentData }) {
1010
const attributes = Object.entries($attrs)
1111
.filter(([key, _]) => isHtmlAttribute(key))
1212
.reduce((res, [key, value]) => {
@@ -29,8 +29,9 @@ function getSortableOption({ $attrs, callBackBuilder }) {
2929
draggable: ">*"
3030
};
3131
Object.entries($attrs)
32+
.filter(([key, _]) => !isHtmlAttribute(key))
3233
.map(([key, value]) => [camelize(key), value])
33-
.filter(([key, _]) => !isHtmlAttribute(key) && !isReadOnlyEvent(key))
34+
.filter(([key, _]) => !isReadOnlyEvent(key))
3435
.forEach(([key, value]) => {
3536
options[key] = value;
3637
});

src/util/string.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ function cached(fn) {
77
}
88

99
const regex = /-(\w)/g;
10-
const camelize = cached(str =>
11-
str.replace(regex, (_, c) => (c ? c.toUpperCase() : ""))
12-
);
10+
const camelize = cached(str => str.replace(regex, (_, c) => c.toUpperCase()));
1311

1412
const capitalize = str => str.replace(/^\w/, c => c.toUpperCase());
1513

src/vuedraggable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const draggableComponent = defineComponent({
143143
},
144144

145145
render() {
146-
const { $slots, $attrs, tag } = this;
146+
const { $slots, $attrs, tag, componentData } = this;
147147
const defaultSlots = getSlot($slots, "default") || [];
148148
this.transitionMode = isTransition(defaultSlots);
149149
const { children, headerOffset, footerOffset } = computeChildrenAndOffsets(
@@ -152,7 +152,7 @@ const draggableComponent = defineComponent({
152152
);
153153
this.headerOffset = headerOffset;
154154
this.footerOffset = footerOffset;
155-
const attributes = getComponentAttributes($attrs, this.componentData);
155+
const attributes = getComponentAttributes({ $attrs, componentData });
156156
this.defaultSlots = defaultSlots;
157157
const realRoot =
158158
isHtmlTag(tag) || isTransitionName(tag) ? tag : resolveComponent(tag);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import {
2+
getComponentAttributes,
3+
getSortableOption,
4+
} from "@/core/componentBuilderHelper";
5+
6+
describe("getComponentAttributes", () => {
7+
test.each([
8+
[{ $attrs: {}, componentData: {} }, {}],
9+
[
10+
{
11+
$attrs: {},
12+
componentData: {
13+
attrs: {
14+
value: 89,
15+
},
16+
},
17+
},
18+
{
19+
value: 89,
20+
},
21+
],
22+
[
23+
{
24+
$attrs: {},
25+
componentData: {
26+
props:{
27+
prop1: "value"
28+
},
29+
attrs: {
30+
value: 89,
31+
},
32+
},
33+
},
34+
{
35+
value: 89,
36+
prop1: "value"
37+
},
38+
],
39+
[
40+
{
41+
$attrs: {
42+
filtered: true,
43+
id: 68,
44+
"data-application": "app",
45+
class: "my-class",
46+
other: "will be filtered"
47+
},
48+
componentData: {
49+
attrs: {
50+
value: 89,
51+
},
52+
},
53+
},
54+
{
55+
id: 68,
56+
class: "my-class",
57+
"data-application": "app",
58+
value: 89,
59+
},
60+
],
61+
])("for %o returns %o", (value, expected) => {
62+
const actual = getComponentAttributes(value);
63+
expect(actual).toEqual(expected);
64+
});
65+
});
66+
67+
describe("getSortableOption", () => {
68+
test.each([
69+
[{ $attrs: {}, callBackBuilder: {} }, { draggable: ">*" }],
70+
[{ $attrs: { onStart: 23 }, callBackBuilder: {} }, { draggable: ">*" }],
71+
[{ $attrs: { onEnd: 23 }, callBackBuilder: {} }, { draggable: ">*" }],
72+
[
73+
{
74+
$attrs: { id: "id", class: "class", "data-app": "app" },
75+
callBackBuilder: {},
76+
},
77+
{ draggable: ">*" },
78+
],
79+
[
80+
{
81+
$attrs: { value: "43" },
82+
callBackBuilder: {},
83+
},
84+
{ value: "43", draggable: ">*" },
85+
],
86+
[
87+
{
88+
$attrs: {
89+
value: "43",
90+
"ghost-class": "phantom",
91+
draggable: ".draggable",
92+
},
93+
callBackBuilder: {},
94+
},
95+
{
96+
value: "43",
97+
ghostClass: "phantom",
98+
draggable: ".draggable",
99+
},
100+
],
101+
])("for %o returns %o", (value, expected) => {
102+
const actual = getSortableOption(value);
103+
expect(actual).toEqual(expected);
104+
});
105+
});

0 commit comments

Comments
 (0)