-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.ts
225 lines (210 loc) · 7.7 KB
/
index.test.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { describe, test, expect } from "vitest";
import bem, { setup } from "./index";
const testCases = [
{
description: "default",
tested: () => bem("block"),
expectations: [
"block",
"block block--mod1",
"block block--mod1 block--mod2",
"block__element",
"block__element block__element--mod1",
"block__element block__element--mod1 block__element--mod2",
],
},
{
description: "`elementDelimiter` option",
tested: () => bem("block", { elementDelimiter: "_" }),
expectations: [
"block",
"block block--mod1",
"block block--mod1 block--mod2",
"block_element",
"block_element block_element--mod1",
"block_element block_element--mod1 block_element--mod2",
],
},
{
description: "`modifierDelimiter` option",
tested: () => bem("block", { modifierDelimiter: "-" }),
expectations: [
"block",
"block block-mod1",
"block block-mod1 block-mod2",
"block__element",
"block__element block__element-mod1",
"block__element block__element-mod1 block__element-mod2",
],
},
{
description: "`namespace` option",
tested: () => bem("block", { namespace: "ns" }),
expectations: [
"ns-block",
"ns-block ns-block--mod1",
"ns-block ns-block--mod1 ns-block--mod2",
"ns-block__element",
"ns-block__element ns-block__element--mod1",
"ns-block__element ns-block__element--mod1 ns-block__element--mod2",
],
},
{
description: "`namespace` option (array)",
tested: () => bem("block", { namespace: ["ns1", "ns2"] }),
expectations: [
"ns1-ns2-block",
"ns1-ns2-block ns1-ns2-block--mod1",
"ns1-ns2-block ns1-ns2-block--mod1 ns1-ns2-block--mod2",
"ns1-ns2-block__element",
"ns1-ns2-block__element ns1-ns2-block__element--mod1",
"ns1-ns2-block__element ns1-ns2-block__element--mod1 ns1-ns2-block__element--mod2",
],
},
{
description: "`namespace` option (empty array)",
tested: () => bem("block", { namespace: [] }),
expectations: [
"block",
"block block--mod1",
"block block--mod1 block--mod2",
"block__element",
"block__element block__element--mod1",
"block__element block__element--mod1 block__element--mod2",
],
},
{
description: "`namespaceDelimiter` option",
tested: () => bem("block", { namespace: "ns", namespaceDelimiter: "---" }),
expectations: [
"ns---block",
"ns---block ns---block--mod1",
"ns---block ns---block--mod1 ns---block--mod2",
"ns---block__element",
"ns---block__element ns---block__element--mod1",
"ns---block__element ns---block__element--mod1 ns---block__element--mod2",
],
},
{
description: "`namespaceDelimiter` option without `namespace` option",
tested: () => bem("block", { namespaceDelimiter: "---" }),
expectations: [
"block",
"block block--mod1",
"block block--mod1 block--mod2",
"block__element",
"block__element block__element--mod1",
"block__element block__element--mod1 block__element--mod2",
],
},
{
description: "`setup()`",
tested: () => {
setup({
elementDelimiter: "_",
modifierDelimiter: "-",
namespace: "ns",
namespaceDelimiter: "---",
strict: true,
});
return bem("block");
},
expectations: [
"ns---block",
"ns---block ns---block-mod1",
"ns---block ns---block-mod1 ns---block-mod2",
"ns---block_element",
"ns---block_element ns---block_element-mod1",
"ns---block_element ns---block_element-mod1 ns---block_element-mod2",
],
},
];
testCases.forEach(({ description, tested, expectations }) => {
describe(description, () => {
const b = tested();
const [block, blockMod1, blockMod2, element, elementMod1, elementMod2] = expectations;
test("returns block", () => {
expect(b()).toEqual(block);
expect(b({ mod1: false })).toEqual(block);
expect(b({ mod1: null })).toEqual(block);
expect(b({ mod1: undefined })).toEqual(block);
expect(b({ mod1: false, mod2: false, mod3: null, mod4: undefined })).toEqual(block);
expect(b([])).toEqual(block);
expect(b([""])).toEqual(block);
expect(b([null])).toEqual(block);
expect(b([undefined])).toEqual(block);
expect(b(["", null, undefined])).toEqual(block);
});
test("returns block with modifier", () => {
expect(b({ mod1: true })).toEqual(blockMod1);
expect(b({ mod1: true, mod2: false, mod3: null, mod4: undefined })).toEqual(blockMod1);
expect(b(["mod1"])).toEqual(blockMod1);
expect(b(["mod1", "", null, undefined])).toEqual(blockMod1);
});
test("returns block with multiple modifiers", () => {
expect(b({ mod1: true, mod2: true })).toEqual(blockMod2);
expect(b({ mod1: true, mod2: true, mod3: false })).toEqual(blockMod2);
expect(b(["mod1", "mod2"])).toEqual(blockMod2);
expect(b(["mod1", "mod2", null])).toEqual(blockMod2);
});
test("returns block with element", () => {
expect(b("element")).toEqual(element);
expect(b("element", {})).toEqual(element);
expect(b("element", { mod: false })).toEqual(element);
expect(b("element", [""])).toEqual(element);
});
test("returns block with element and modifier", () => {
expect(b("element", { mod1: true })).toEqual(elementMod1);
expect(b("element", { mod1: true, mod2: false })).toEqual(elementMod1);
expect(b("element", ["mod1"])).toEqual(elementMod1);
expect(b("element", ["mod1", null])).toEqual(elementMod1);
});
test("returns block with element and multiple modifiers", () => {
expect(b("element", { mod1: true, mod2: true })).toEqual(elementMod2);
expect(b("element", { mod1: true, mod2: true, mod3: false })).toEqual(elementMod2);
expect(b("element", ["mod1", "mod2"])).toEqual(elementMod2);
});
});
});
describe("invalid arguments", () => {
const b = bem("invalid", {
namespaceDelimiter: "-",
elementDelimiter: "__",
modifierDelimiter: "--",
});
const expectedError = (subject: string, value: string): string =>
`The ${subject} ("${value}") must not use the characters contained within the delimiters ("-", "__", "--").`;
test("element is invalid", () => {
expect(() => b("element--")).toThrow(expectedError("element", "element--"));
expect(() => b("element_")).toThrow(expectedError("element", "element_"));
expect(() => b("---element")).toThrow(expectedError("element", "---element"));
expect(() => b("-_element")).toThrow(expectedError("element", "-_element"));
expect(() => b("ele-me_nt")).toThrow(expectedError("element", "ele-me_nt"));
});
test("modifier is invalid", () => {
expect(() => b(["modifier--"])).toThrow(expectedError("modifier", "modifier--"));
expect(() => b(["modifier_"])).toThrow(expectedError("modifier", "modifier_"));
expect(() => b(["---modifier"])).toThrow(expectedError("modifier", "---modifier"));
expect(() => b(["-_modifier"])).toThrow(expectedError("modifier", "-_modifier"));
expect(() => b(["mod-ifi_er"])).toThrow(expectedError("modifier", "mod-ifi_er"));
});
});
// `setup()` test must be at last
describe("`setup()` additional case", () => {
test("overrides options which was setup", () => {
const b = bem("block", {
elementDelimiter: ":",
modifierDelimiter: "/",
namespace: "n",
namespaceDelimiter: "=",
strict: false,
});
expect(b("element:", { mod: true })).toEqual("n=block:element: n=block:element:/mod");
});
test("has no effect when empty options are passed", () => {
setup({});
expect(bem("block")("element", { mod: true })).toEqual(
"ns---block_element ns---block_element-mod",
);
});
});