Skip to content

Commit 70295dc

Browse files
WIP: adding tests
1 parent f01e39b commit 70295dc

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

src/core/componentBuilderHelper.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
1-
import { capitalize } from "../util/string";
2-
import { camelize } from "../util/string";
1+
import { capitalize, camelize } from "../util/string";
32
import { events, isReadOnlyEvent } from "./sortableEvents";
43

54
function isHtmlAttribute(value) {
65
return ["id", "class"].includes(value) || value.startsWith("data-");
76
}
87

9-
function getComponentAttributes({ $attrs, componentData }) {
10-
const attributes = Object.entries($attrs)
11-
.filter(([key, _]) => isHtmlAttribute(key))
12-
.reduce((res, [key, value]) => {
13-
res[key] = value;
14-
return res;
15-
}, {});
8+
function project(entries) {
9+
return entries.reduce((res, [key, value]) => {
10+
res[key] = value;
11+
return res;
12+
}, {});
13+
}
1614

15+
function getComponentAttributes({ $attrs, componentData }) {
16+
const attributes = project(
17+
Object.entries($attrs).filter(([key, _]) => isHtmlAttribute(key))
18+
);
1719
if (!componentData) {
1820
return attributes;
1921
}
2022
const { on, props, attrs } = componentData;
2123
Object.entries(on || {}).forEach(([key, value]) => {
2224
attributes[`on${capitalize(key)}`] = value;
2325
});
24-
return { ...attributes, ...attrs, ...props };
26+
return {
27+
...attributes,
28+
...attrs,
29+
...props
30+
};
2531
}
2632

2733
function createSortableOption({ $attrs, callBackBuilder }) {
28-
const options = {
29-
draggable: ">*"
30-
};
31-
Object.entries($attrs)
32-
.filter(([key, _]) => !isHtmlAttribute(key))
33-
.map(([key, value]) => [camelize(key), value])
34-
.filter(([key, _]) => !isReadOnlyEvent(key))
35-
.forEach(([key, value]) => {
36-
options[key] = value;
37-
});
34+
const options = project(getValidSortableEntries($attrs));
3835
Object.entries(callBackBuilder).forEach(([eventType, eventBuilder]) => {
3936
events[eventType].forEach(event => {
4037
options[`on${event}`] = eventBuilder(event);
4138
});
4239
});
43-
return options;
40+
return {
41+
draggable: ">*",
42+
...options
43+
};
4444
}
4545

4646
function getValidSortableEntries(value) {
4747
return Object.entries(value)
48+
.filter(([key, _]) => !isHtmlAttribute(key))
4849
.map(([key, value]) => [camelize(key), value])
4950
.filter(([key, _]) => !isReadOnlyEvent(key));
5051
}

tests/unit/core/componentBuilderHelper.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {
22
getComponentAttributes,
33
createSortableOption,
4+
getValidSortableEntries
45
} from "@/core/componentBuilderHelper";
56

67
describe("getComponentAttributes", () => {
78
test.each([
9+
[{ $attrs: {} }, {}],
810
[{ $attrs: {}, componentData: {} }, {}],
911
[
1012
{
@@ -36,6 +38,17 @@ describe("getComponentAttributes", () => {
3638
prop1: "value",
3739
},
3840
],
41+
[
42+
{
43+
$attrs: {
44+
id: "value",
45+
value: 89,
46+
}
47+
},
48+
{
49+
id: "value",
50+
},
51+
],
3952
[
4053
{
4154
$attrs: {
@@ -58,6 +71,22 @@ describe("getComponentAttributes", () => {
5871
value: 89,
5972
},
6073
],
74+
[
75+
{
76+
$attrs: {
77+
},
78+
componentData: {
79+
on: {
80+
value: 89,
81+
input: 66
82+
},
83+
},
84+
},
85+
{
86+
onValue: 89,
87+
onInput: 66
88+
},
89+
],
6190
])("for %o returns %o", (value, expected) => {
6291
const actual = getComponentAttributes(value);
6392
expect(actual).toEqual(expected);
@@ -150,3 +179,17 @@ describe("createSortableOption", () => {
150179
expect(actual).toEqual(expected);
151180
});
152181
});
182+
183+
describe("getValidSortableEntries", () =>{
184+
test.each([
185+
[{newValue: 1}, [["newValue", 1]]],
186+
[{onStart: 1}, []],
187+
[{onStart: 1, newValue: 11}, [["newValue", 11]]],
188+
[{onStart: 1, id: "newId", attribute: "yes"}, [["attribute", "yes"]]],
189+
[{onStart: 1, "data-bind": "value", boolean: true}, [["boolean", true]]]
190+
])
191+
("for %o returns %o", (value, expected) => {
192+
const actual = getValidSortableEntries(value);
193+
expect(actual).toEqual(expected);
194+
});
195+
})

0 commit comments

Comments
 (0)