Skip to content

Commit 7e65a27

Browse files
chore(docs): adds explanation on scoped styles
1 parent adb85dd commit 7e65a27

15 files changed

+615
-368
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules/
33
!.gitignore
44
!.editorconfig
55
!.travis.yml
6+
!.prettierrc
67
coverage/

.prettierrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"printWidth": 80,
3+
"bracketSpacing": true,
4+
"semi": true,
5+
"singleQuote": false,
6+
"tabWidth": 2,
7+
"trailingComma": "es5",
8+
"useTabs": false,
9+
"proseWrap": "always"
10+
}

__test__/array-merge.test.ts

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,49 @@
1-
import { VNodeData, VNodeDirective } from "vue"
2-
import { mergeData } from "../src/index"
1+
import { VNodeData, VNodeDirective } from "vue";
2+
import { mergeData } from "../src/index";
33

44
it("should execute array merge on class, style, directive properties", () => {
5-
let vd1: VNodeData = {
6-
class: ["a", { b: true, c: false }],
7-
style: ["display:block;", { color: "red", fontSize: "16px" }],
8-
}
9-
let vd2: VNodeData = {
10-
class: ["d", { e: true, f: false }],
11-
style: "position:absolute;",
12-
}
5+
function inserted(el: any) {
6+
el.focus();
7+
}
8+
let vd1: VNodeData = {
9+
class: ["a", { b: true, c: false }],
10+
style: ["display:block;", { color: "red", fontSize: "16px" }],
11+
};
12+
let vd2: VNodeData = {
13+
class: ["d", { e: true, f: false }],
14+
style: "position:absolute;",
15+
directives: [{ focus: { inserted } }],
16+
};
1317

14-
let actual = mergeData(vd1, vd2)
15-
let expected = {
16-
class: ["d", { e: true, f: false }, "a", { b: true, c: false }],
17-
style: ["position:absolute;", "display:block;", { color: "red", fontSize: "16px" }],
18-
}
18+
let actual = mergeData(vd1, vd2);
19+
let expected = {
20+
class: ["d", { e: true, f: false }, "a", { b: true, c: false }],
21+
style: [
22+
"position:absolute;",
23+
"display:block;",
24+
{ color: "red", fontSize: "16px" },
25+
],
26+
directives: [{ focus: { inserted } }],
27+
};
1928

20-
// Check values recursively
21-
expect(actual).toEqual(expected)
22-
// Check that root object refs do not match
23-
expect(actual).not.toBe(expected)
24-
// Check that level 1 object refs do not match
25-
expect(actual.class).not.toBe(vd1.class)
26-
expect(actual.class).not.toBe(vd2.class)
27-
expect(actual.style).not.toBe(vd1.style)
28-
expect(actual.style).not.toBe(vd2.style)
29-
})
29+
// Check values recursively
30+
expect(actual).toEqual(expected);
31+
// Check that root object refs do not match
32+
expect(actual).not.toBe(expected);
33+
// Check that level 1 object refs do not match
34+
expect(actual.class).not.toBe(vd1.class);
35+
expect(actual.class).not.toBe(vd2.class);
36+
expect(actual.style).not.toBe(vd1.style);
37+
expect(actual.style).not.toBe(vd2.style);
38+
});
3039

3140
it("should init types to array", () => {
32-
let test = mergeData({ class: "string" })
33-
expect(Array.isArray(test.class)).toBe(true)
41+
let test = mergeData({ class: "string" });
42+
expect(Array.isArray(test.class)).toBe(true);
3443

35-
test = mergeData({ class: { string: true } })
36-
expect(Array.isArray(test.class)).toBe(true)
44+
test = mergeData({ class: { string: true } });
45+
expect(Array.isArray(test.class)).toBe(true);
3746

38-
test = mergeData({ class: ["string"] })
39-
expect(Array.isArray(test.class)).toBe(true)
40-
})
47+
test = mergeData({ class: ["string"] });
48+
expect(Array.isArray(test.class)).toBe(true);
49+
});

__test__/handler-merge.test.ts

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,75 @@
1-
import { VNodeData, VNodeDirective } from "vue"
2-
import { mergeData } from "../src/index"
1+
import { VNodeData, VNodeDirective } from "vue";
2+
import { mergeData } from "../src/index";
33

44
it("should not mutate original object (issue #2)", () => {
5-
let def1 = { on: { click() {} } }
6-
let def2 = { on: { click() {} } }
5+
let def1 = { on: { click() {} } };
6+
let def2 = { on: { click() {} } };
77

8-
let onclick1 = def1.on.click
9-
let onclick2 = def2.on.click
8+
let onclick1 = def1.on.click;
9+
let onclick2 = def2.on.click;
1010

11-
let data = mergeData({}, def1, def2)
11+
let data = mergeData({}, def1, def2);
1212

13-
expect(def1.on).not.toBe(data.on)
14-
expect(def2.on).not.toBe(data.on)
13+
expect(def1.on).not.toBe(data.on);
14+
expect(def2.on).not.toBe(data.on);
1515

16-
expect(def1.on.click).toBe(onclick1)
17-
expect(def2.on.click).toBe(onclick2)
16+
expect(def1.on.click).toBe(onclick1);
17+
expect(def2.on.click).toBe(onclick2);
1818

19-
expect(data.on.click).toContain(onclick1)
20-
expect(data.on.click).toContain(onclick2)
21-
})
19+
expect(data.on.click).toContain(onclick1);
20+
expect(data.on.click).toContain(onclick2);
21+
});
2222

2323
it("should set single handlers and concat multi", () => {
24-
let h1 = console.log
25-
let h2 = console.info
26-
let h3 = console.error
27-
let actual: VNodeData
24+
let h1 = console.log;
25+
let h2 = console.info;
26+
let h3 = console.error;
27+
let actual: VNodeData;
2828

29-
actual = mergeData({ class: ["btn", "text-center"] }, { on: { mouseup: h1 } })
30-
expect(actual).toMatchObject({ on: { mouseup: h1 } })
31-
expect(Array.isArray(actual.on.mouseup)).toBe(false)
29+
actual = mergeData(
30+
{ class: ["btn", "text-center"] },
31+
{ on: { mouseup: h1 } }
32+
);
33+
expect(actual).toMatchObject({ on: { mouseup: h1 } });
34+
expect(Array.isArray(actual.on.mouseup)).toBe(false);
3235

33-
actual = mergeData({ nativeOn: { mouseup: h1 } }, { class: ["btn", "text-center"] })
34-
expect(actual).toMatchObject({ nativeOn: { mouseup: h1 } })
35-
expect(Array.isArray(actual.nativeOn.mouseup)).toBe(false)
36+
actual = mergeData(
37+
{ nativeOn: { mouseup: h1 } },
38+
{ class: ["btn", "text-center"] }
39+
);
40+
expect(actual).toMatchObject({ nativeOn: { mouseup: h1 } });
41+
expect(Array.isArray(actual.nativeOn.mouseup)).toBe(false);
3642

37-
actual = mergeData({ on: { mouseup: h1 } }, { on: { mouseup: h2 } }, { on: { mouseup: h3 } })
38-
expect(Array.isArray(actual.on.mouseup)).toBe(true)
39-
expect(actual.on.mouseup).toContain(h1)
40-
expect(actual.on.mouseup).toContain(h2)
41-
expect(actual.on.mouseup).toContain(h3)
42-
})
43+
actual = mergeData(
44+
{ on: { mouseup: h1 } },
45+
{ on: { mouseup: h2 } },
46+
{ on: { mouseup: h3 } }
47+
);
48+
expect(Array.isArray(actual.on.mouseup)).toBe(true);
49+
expect(actual.on.mouseup).toContain(h1);
50+
expect(actual.on.mouseup).toContain(h2);
51+
expect(actual.on.mouseup).toContain(h3);
52+
});
4353

4454
it("should call the right-most argument first", () => {
45-
let first = 0
46-
let factory = n => () => {
47-
if (!first) {
48-
first = n
49-
}
50-
}
55+
let first = 0;
56+
let factory = n => () => {
57+
if (!first) {
58+
first = n;
59+
}
60+
};
5161

52-
let actual = mergeData(
53-
{ on: { click: factory(1) } },
54-
{ on: { click: factory(2) } },
55-
{ on: { click: factory(3) } },
56-
{ on: { click: factory(4) } }
57-
)
62+
let actual = mergeData(
63+
{ on: { click: factory(1) } },
64+
{ on: { click: factory(2) } },
65+
{ on: { click: factory(3) } },
66+
{ on: { click: factory(4) } }
67+
);
5868

59-
expect(Array.isArray(actual.on.click)).toBe(true)
60-
for (const fn of actual.on.click) {
61-
fn()
62-
}
63-
expect(first).toBe(4)
64-
expect(actual).toMatchSnapshot("merge-4")
65-
})
69+
expect(Array.isArray(actual.on.click)).toBe(true);
70+
for (const fn of actual.on.click) {
71+
fn();
72+
}
73+
expect(first).toBe(4);
74+
expect(actual).toMatchSnapshot("merge-4");
75+
});

__test__/kitchen-sink.test.ts

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
1-
import { VNodeData, VNodeDirective } from "vue"
2-
import { mergeData } from "../src/index"
1+
import { VNodeData, VNodeDirective } from "vue";
2+
import { mergeData } from "../src/index";
33

44
it("should handle multiple arguments", () => {
5-
// Pre-define functions so they compare equal
6-
function click() {}
7-
function mouseup() {}
5+
// Pre-define functions so they compare equal
6+
function click() {}
7+
function mouseup() {}
88

9-
let expected: VNodeData = {
10-
staticClass: "btn ml-auto",
11-
class: [{ "text-center": true }, "btn-block", { "btn-primary": true }],
12-
on: {
13-
click: [click, click],
14-
mouseup: [mouseup, mouseup],
15-
},
16-
}
9+
let expected: VNodeData = {
10+
staticClass: "btn ml-auto",
11+
class: [{ "text-center": true }, "btn-block", { "btn-primary": true }],
12+
on: {
13+
click: [click, click],
14+
mouseup: [mouseup, mouseup],
15+
},
16+
};
1717

18-
let actual = mergeData(
19-
{ staticClass: "ml-auto" },
20-
{ staticClass: "btn", class: { "btn-primary": true } },
21-
{ class: ["btn-block"] },
22-
{ on: { click, mouseup } },
23-
{ on: { click, mouseup } },
24-
{ class: { "text-center": true } }
25-
)
18+
let actual = mergeData(
19+
{ staticClass: "ml-auto" },
20+
{ staticClass: "btn", class: { "btn-primary": true } },
21+
{ class: ["btn-block"] },
22+
{ on: { click, mouseup } },
23+
{ on: { click, mouseup } },
24+
{ class: { "text-center": true } }
25+
);
2626

27-
expect(actual).toEqual(expected)
28-
})
27+
expect(actual).toEqual(expected);
28+
});
2929

3030
it("should work like in the example", () => {
31-
let onClick1 = e => alert("💥")
32-
let onClick2 = e => alert("👍")
31+
let onClick1 = e => alert("💥");
32+
let onClick2 = e => alert("👍");
3333

34-
let componentData: VNodeData = {
35-
staticClass: "fn-component", // concatenates all static classes
36-
class: {
37-
active: true,
38-
"special-class": false,
39-
},
40-
attrs: {
41-
id: "my-functional-component",
42-
},
43-
on: {
44-
click: onClick1,
45-
},
46-
}
47-
// <my-btn variant="primary" type="submit" id="form-submit-btn" @click="onClick">Submit</my-btn>
48-
let templateData: VNodeData = {
49-
attrs: {
50-
id: "form-submit-btn",
51-
type: "submit",
52-
},
53-
on: { click: onClick2 },
54-
}
34+
let componentData: VNodeData = {
35+
staticClass: "fn-component", // concatenates all static classes
36+
class: {
37+
active: true,
38+
"special-class": false,
39+
},
40+
attrs: {
41+
id: "my-functional-component",
42+
},
43+
on: {
44+
click: onClick1,
45+
},
46+
};
47+
// <my-btn variant="primary" type="submit" id="form-submit-btn" @click="onClick">Submit</my-btn>
48+
let templateData: VNodeData = {
49+
attrs: {
50+
id: "form-submit-btn",
51+
type: "submit",
52+
},
53+
on: { click: onClick2 },
54+
};
5555

56-
expect(mergeData(templateData, componentData)).toEqual({
57-
staticClass: "fn-component",
58-
class: expect.arrayContaining([
59-
{
60-
active: true,
61-
"special-class": false,
62-
},
63-
]),
64-
attrs: {
65-
id: "my-functional-component",
66-
type: "submit",
67-
},
68-
on: { click: expect.arrayContaining([onClick1, onClick2]) },
69-
})
70-
})
56+
expect(mergeData(templateData, componentData)).toEqual({
57+
staticClass: "fn-component",
58+
class: expect.arrayContaining([
59+
{
60+
active: true,
61+
"special-class": false,
62+
},
63+
]),
64+
attrs: {
65+
id: "my-functional-component",
66+
type: "submit",
67+
},
68+
on: { click: expect.arrayContaining([onClick1, onClick2]) },
69+
});
70+
});

__test__/nullable-objects.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { VNodeData, VNodeDirective } from "vue"
2-
import { mergeData } from "../src/index"
1+
import { VNodeData, VNodeDirective } from "vue";
2+
import { mergeData } from "../src/index";
33

44
it("should handle nested nullable objects as initial state", () => {
5-
let testData: VNodeData[] = [{ on: undefined }, { staticClass: "foo" }]
6-
let actual: VNodeData
7-
let boom = () => (actual = mergeData(...testData))
5+
let testData: VNodeData[] = [{ on: undefined }, { staticClass: "foo" }];
6+
let actual: VNodeData;
7+
let boom = () => (actual = mergeData(...testData));
88

9-
expect(boom).not.toThrowError()
10-
expect(actual.staticClass).toBe("foo")
11-
expect(actual.on).toEqual({})
12-
})
9+
expect(boom).not.toThrowError();
10+
expect(actual.staticClass).toBe("foo");
11+
expect(actual.on).toEqual({});
12+
});

0 commit comments

Comments
 (0)