-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
152 lines (133 loc) · 3.67 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const test = require("tape");
const flattenObjSass = require("./index");
test("empty object yields empty string", function (t) {
t.equal(flattenObjSass({}), "");
t.end();
});
test("single property with numeric value", function (t) {
t.equal(flattenObjSass({ single: 1 }), "$single: 1; ");
t.end();
});
test("single property with boolean value", function (t) {
t.equal(flattenObjSass({ single: true }), "$single: true; ");
t.equal(flattenObjSass({ single: false }), "$single: false; ");
t.end();
});
test("single property with string value", function (t) {
t.equal(flattenObjSass({ single: "foo" }), "$single: foo; ");
t.equal(flattenObjSass({ single: "15px" }), "$single: 15px; ");
t.end();
});
test("unquoted string value containing whitespace yields unquoted string value", function (t) {
t.equal(
flattenObjSass({ color: "rgb(204, 102, 153)" }),
"$color: rgb(204, 102, 153); "
);
t.end();
});
test("quoted string value containing whitespace yields quoted string value", function (t) {
t.equal(
flattenObjSass({ single: "'hello world'" }),
"$single: 'hello world'; "
);
t.equal(
flattenObjSass({ single: '"hello world"' }),
'$single: "hello world"; '
);
t.end();
});
test("multiple properties", function (t) {
t.equal(flattenObjSass({ first: 1, second: 2 }), "$first: 1; $second: 2; ");
t.end();
});
test("nested object", function (t) {
t.equal(flattenObjSass({ object: { one: 1 } }), "$object-one: 1; ");
t.equal(
flattenObjSass({ object: { one: { two: 2 } } }),
"$object-one-two: 2; "
);
t.equal(
flattenObjSass({ object: { one: { two: { three: 3 } } } }),
"$object-one-two-three: 3; "
);
t.end();
});
test("nested empty object yields empty string", function (t) {
t.equal(flattenObjSass({ object: {} }), "");
t.end();
});
test("nested array", function (t) {
t.equal(flattenObjSass({ array: [1, 2, 3] }), "$array: (1,2,3); ");
t.end();
});
test("nested empty array", function (t) {
t.equal(flattenObjSass({ array: [] }), "$array: (); ");
t.end();
});
test("mixed array", function (t) {
t.equal(
flattenObjSass({ array: ["1rem", 2, "1px solid red"] }),
"$array: (1rem,2,1px solid red); "
);
t.end();
});
test("prefix option", function (t) {
t.equal(
flattenObjSass({ black: "#000", brand: "#6699cc" }, "$color-"),
"$color-black: #000; $color-brand: #6699cc; "
);
t.end();
});
test("transform option", function (t) {
const actual = flattenObjSass(
{
transform: 1,
thatKey: 2,
complex: {
a: [1, 2, 3],
b: "#fff",
},
},
"$transform-",
(key, val) => {
if (typeof val === "number") {
val = val * 100;
}
if (key === "thatKey") {
val = val / 100;
}
if (Array.isArray(val)) {
val = val.map((n) => n * 100);
}
return val;
}
);
const expected =
"$transform-transform: 100; $transform-thatKey: 200; $transform-complex-a: (100,200,300); $transform-complex-b: #fff; ";
t.equal(actual, expected);
t.end();
});
test("unescaped special characters in property names are escaped", function (t) {
const actual = flattenObjSass({
primary: {
"30%": "#27ae604c",
"60%": "#27ae6099",
},
});
const expected = "$primary-30\\%: #27ae604c; $primary-60\\%: #27ae6099; ";
t.equal(actual, expected);
t.end();
});
test("already escaped special characters in property names are preserved", function (t) {
const actual = flattenObjSass({
icon: {
"\\@": "#000000",
"\\$": "#dddddd",
"\\+": "#eeeeee",
},
});
const expected =
"$icon-\\@: #000000; $icon-\\$: #dddddd; $icon-\\+: #eeeeee; ";
t.equal(actual, expected);
t.end();
});