-
Notifications
You must be signed in to change notification settings - Fork 7
/
css-ns.spec.js
490 lines (422 loc) · 14.7 KB
/
css-ns.spec.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
var assert = require('chai').assert; // @see http://chaijs.com/api/assert/
var cssNs = require('./css-ns');
var React = require('react');
var ReactDOMServer = require('react-dom/server');
function assertEqualHtml(Component, expectedHtml) {
assert.deepEqual(
ReactDOMServer.renderToStaticMarkup(React.createElement(Component)),
expectedHtml
);
}
// Enable this to repeat the test suite a few times
// for (var i = 0; i < 100; i++)
describe('css-ns', function() {
describe('createOptions()', function() {
it('accepts a string', function() {
assert.deepEqual(
cssNs.createOptions('MyComponent').namespace,
'MyComponent'
);
});
it('accepts a file path', function() {
assert.deepEqual(
cssNs.createOptions('/path/to/MyComponent.jsx').namespace,
'MyComponent'
);
});
it('accepts a relative file path', function() {
assert.deepEqual(
cssNs.createOptions('../MyComponent.jsx').namespace,
'MyComponent'
);
});
it('accepts a full Windows file path', function() {
assert.deepEqual(
cssNs.createOptions('C:\\Users\\path\\to\\MyComponent.jsx').namespace,
'MyComponent'
);
});
it('accepts a full Windows file path with whitespace', function() {
assert.deepEqual(
cssNs.createOptions('C:\\Program Files\\path\\to\\MyComponent.js').namespace,
'MyComponent'
);
});
it('accepts underscores in file names', function() {
assert.deepEqual(
cssNs.createOptions('../my_component.jsx').namespace,
'my_component'
);
});
it('processes options only once', function() {
assert.deepEqual(
cssNs.createOptions(cssNs.createOptions('MyComponent')).namespace,
'MyComponent'
);
});
it('accepts an object', function() {
assert.deepEqual(
cssNs.createOptions({ namespace: 'MyComponent' }).namespace,
'MyComponent'
);
});
});
describe('nsAuto()', function() {
it('handles falsy input', function() {
assert.equal(cssNs.nsAuto('MyComponent', null), null);
});
it('handles class list input', function() {
assert.equal(cssNs.nsAuto('Foo', 'bar'), 'Foo-bar');
});
it('handles React element input', function() {
var MyComponent = function() {
return cssNs.nsAuto(
{ namespace: 'MyComponent', React: React },
React.createElement('div', { className: 'row' })
);
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-row"></div>'
);
});
});
describe('nsString()', function() {
it('prefixes a single class', function() {
assert.equal(cssNs.nsString('Foo', 'bar'), 'Foo-bar');
});
it('prefixes multiple classes', function() {
assert.equal(cssNs.nsString('Foo', 'bar baz'), 'Foo-bar Foo-baz');
});
it('tolerates exotic classNames and whitespace', function() {
// ...not that using these would be a good idea for other reasons, but we won't judge!
assert.equal(cssNs.nsString('Foo', ' bar-baz lol{wtf$why%would__ANYONE"do.this} '), 'Foo-bar-baz Foo-lol{wtf$why%would__ANYONE"do.this}');
});
it('supports an include option', function() {
var options = {
namespace: 'Foo',
include: /^b/ // only prefix classes that start with b
};
assert.equal(
cssNs.nsString(options, 'bar AnotherComponent car'),
'Foo-bar AnotherComponent car'
);
});
it('supports an exclude option', function() {
var options = {
namespace: 'Foo',
exclude: /^([A-Z]|icon)/ // ignore classes that start with caps or "icon"
};
assert.equal(
cssNs.nsString(options, 'bar AnotherComponent iconInfo baz'),
'Foo-bar AnotherComponent iconInfo Foo-baz'
);
});
it('supports both include and exclude at the same time', function() {
var options = {
namespace: 'Foo',
include: /^[a-z]/, // include classes that start with lower-case
exclude: /^icon/ // ...but still ignore the "icon" prefix
};
assert.equal(
cssNs.nsString(options, 'bar iconInfo baz'),
'Foo-bar iconInfo Foo-baz'
);
});
it('supports escaping', function() {
var options = {
namespace: 'Foo',
escape: '~'
};
assert.equal(cssNs.nsString(options, '~bar'), 'bar');
});
it('supports an exotic escape string', function() {
var options = {
namespace: 'Foo',
escape: '@]]£20as+d09a+s+fsdkjnf'
};
assert.equal(cssNs.nsString(options, '@]]£20as+d09a+s+fsdkjnfbar car'), 'bar Foo-car');
});
it('supports escaping with the default option', function() {
var options = {
namespace: 'Foo'
};
assert.equal(cssNs.nsString(options, '=bar car'), 'bar Foo-car');
});
it('supports a self option', function() {
var options = {
namespace: 'Foo',
self: /^__ns__$/
};
assert.equal(cssNs.nsString(options, '__ns__ bar'), 'Foo Foo-bar');
});
it('supports a glue option', function() {
var options = {
namespace: 'Foo',
glue: '___'
};
assert.equal(cssNs.nsString(options, 'bar'), 'Foo___bar');
});
describe('with prefix', function() {
it('prefixes a single class', function() {
assert.equal(cssNs.nsString({ prefix: 'app-', namespace: 'Foo' }, 'bar'), 'app-Foo-bar');
});
it('prefixes multiple classes', function() {
assert.equal(cssNs.nsString({ prefix: 'app-', namespace: 'Foo' }, 'bar baz'), 'app-Foo-bar app-Foo-baz');
});
it('tolerates exotic classNames and whitespace', function() {
// ...not that using these would be a good idea for other reasons, but we won't judge!
assert.equal(cssNs.nsString({ prefix: 'app-', namespace: 'Foo' }, ' bar-baz lol{wtf$why%would__ANYONE"do.this} '), 'app-Foo-bar-baz app-Foo-lol{wtf$why%would__ANYONE"do.this}');
});
it('supports an include option', function() {
var options = {
prefix: 'app-',
namespace: 'Foo',
include: /^b/ // only prefix classes that start with b
};
assert.equal(
cssNs.nsString(options, 'bar AnotherComponent car'),
'app-Foo-bar AnotherComponent car'
);
});
it('supports an exclude option', function() {
var options = {
prefix: 'app-',
namespace: 'Foo',
exclude: /^([A-Z]|icon)/ // ignore classes that start with caps or "icon"
};
assert.equal(
cssNs.nsString(options, 'bar AnotherComponent iconInfo baz'),
'app-Foo-bar AnotherComponent iconInfo app-Foo-baz'
);
});
it('supports both include and exclude at the same time', function() {
var options = {
prefix: 'app-',
namespace: 'Foo',
include: /^[a-z]/, // include classes that start with lower-case
exclude: /^icon/ // ...but still ignore the "icon" prefix
};
assert.equal(
cssNs.nsString(options, 'bar iconInfo baz'),
'app-Foo-bar iconInfo app-Foo-baz'
);
});
it('supports a self option', function() {
var options = {
prefix: 'app-',
namespace: 'Foo',
self: /^__ns__$/
};
assert.equal(cssNs.nsString(options, '__ns__ bar'), 'app-Foo app-Foo-bar');
});
it('supports a glue option', function() {
var options = {
prefix: 'app-',
namespace: 'Foo',
glue: '___'
};
assert.equal(cssNs.nsString(options, 'bar'), 'app-Foo___bar');
});
it('automatically ignores pre-prefixed classes', function() {
var options = {
prefix: 'app-',
namespace: 'Foo'
};
assert.equal(
cssNs.nsString(options, 'bar app-AnotherComponent app-car'),
'app-Foo-bar app-AnotherComponent app-car'
);
});
});
});
describe('nsArray()', function() {
it('prefixes classes', function() {
assert.equal(cssNs.nsArray('Foo', [ 'bar', 'baz' ]), 'Foo-bar Foo-baz');
});
it('tolerates falsy values', function() {
assert.equal(cssNs.nsArray('Foo', [ 'bar', null, 'baz', false ]), 'Foo-bar Foo-baz');
});
});
describe('nsObject()', function() {
it('prefixes classes', function() {
assert.equal(cssNs.nsObject('Foo', { bar: true, baz: true }), 'Foo-bar Foo-baz');
});
it('tolerates falsy values', function() {
assert.equal(cssNs.nsObject('Foo', { bar: true, ignoreThis: null, baz: true, alsoThis: false }), 'Foo-bar Foo-baz');
});
});
describe('createReact()', function() {
it('creates an ns-bound React instance', function() {
var nsReact = cssNs.createReact({ namespace: 'MyComponent', React: React });
var MyComponent = function() {
return nsReact.createElement('div', { className: 'row' });
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-row"></div>'
);
});
it('prefixes classNames on components as well', function() {
// This is a bit of an edge case, since for a component, a prop called "className" holds no special value.
// But if you're using a prop with that name it's highly likely this is the behaviour you want.
var nsReact = cssNs.createReact({ namespace: 'MyComponent', React: React });
var MyChildComponent = function(props) {
return nsReact.createElement('div', { className: props.className });
// ^ this won't get double-namespaced, but only because we ignore uppercased classes by default; otherwise it would
};
var MyComponent = function() {
return nsReact.createElement(MyChildComponent, { className: 'parentInjectedName' })
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-parentInjectedName"></div>'
);
});
});
describe('nsReactElement()', function() {
var nsReactElement = cssNs.nsReactElement.bind(null, { // bind default options
namespace: 'MyComponent',
React: React
});
it('prefixes a single className', function() {
var MyComponent = function() {
return nsReactElement(
React.createElement('div', { className: 'row' })
);
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-row"></div>'
);
});
it('ignores text nodes', function() {
var MyComponent = function() {
return nsReactElement(
React.createElement('div', { className: 'row' }, 'Hello World')
);
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-row">Hello World</div>'
);
});
it('supports array input', function() {
var MyComponent = function() {
return nsReactElement(
React.createElement('div', { className: [ 'row' ] })
);
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-row"></div>'
);
});
it('supports object input', function() {
var MyComponent = function() {
return nsReactElement(
React.createElement('div', { className: { row: true } })
);
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-row"></div>'
);
});
it('prefixes classNames recursively', function() {
var MyComponent = function() {
return nsReactElement(
React.createElement('div', { className: 'row' },
React.createElement('div', { className: 'column' })
)
);
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-row"><div class="MyComponent-column"></div></div>'
);
});
it('allows elements without a className', function() {
var MyComponent = function() {
return nsReactElement(
React.createElement('div', { className: 'row' },
React.createElement('section', null,
React.createElement('div', { className: 'column' })
)
)
);
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-row"><section><div class="MyComponent-column"></div></section></div>'
);
});
it('respects component boundaries', function() {
var MyChildComponent = function() {
return React.createElement('div', { className: 'protected' });
};
var MyComponent = function() {
return nsReactElement(
React.createElement('div', { className: 'container' },
React.createElement(MyChildComponent, null)
)
);
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-container"><div class="protected"></div></div>'
);
});
it('respects ownership of children across components', function() {
var MyChildComponent = function(props) {
return React.createElement('div', { className: 'protected' }, props.children);
};
var MyComponent = function() {
return nsReactElement(
React.createElement(MyChildComponent, null,
React.createElement('div', { className: 'owned' }) // it doesn't matter that "owned" is a child of MyChildComponent, it's still OWNED by MyComponent
)
);
};
assertEqualHtml(
MyComponent,
'<div class="protected"><div class="MyComponent-owned"></div></div>'
);
});
it('works with nested components', function() {
var MyChildComponent = function() {
return cssNs.nsReactElement(
{ namespace: 'MyChildComponent', React: React },
React.createElement('div', { className: 'protected' })
);
};
var MyComponent = function() {
return nsReactElement(
React.createElement('div', { className: 'container' },
React.createElement(MyChildComponent, null)
)
);
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-container"><div class="MyChildComponent-protected"></div></div>'
);
});
it('prefixes classNames on components as well', function() {
// This is a bit of an edge case, since for a component, a prop called "className" holds no special value.
// But if you're using a prop with that name it's highly likely this is the behaviour you want.
var MyChildComponent = function(props) {
return React.createElement('div', { className: props.className });
};
var MyComponent = function() {
return nsReactElement(
React.createElement(MyChildComponent, { className: 'parentInjectedName' })
);
};
assertEqualHtml(
MyComponent,
'<div class="MyComponent-parentInjectedName"></div>'
);
});
});
});