-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
56 lines (41 loc) · 1.6 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
'use strict';
var test = require('tape');
var objToAttrs = require('./');
test('obj-to-attrs', function (t) {
t.plan(13);
// Test basic attributes and boolean attributes
t.equal(objToAttrs({ value: 'test' }), 'value="test"');
t.equal(objToAttrs({ value: 'test', foo: 'bar' }), 'value="test" foo="bar"');
t.equal(objToAttrs({ dataSomething: 'a' }), 'data-something="a"');
t.equal(objToAttrs({ checked: true }), 'checked');
t.equal(objToAttrs({ checked: true, value: 'a' }), 'checked value="a"');
// Test data helper
t.equal(objToAttrs({ data: { foo: 'bar' }}), 'data-foo="bar"');
// Test setting options
t.equal(objToAttrs({ data: { foo: 'bar' }, checked: false }, {
assignment: ' => ', quote: "'", separator: ', '
}), "data-foo => 'bar', checked");
// Test adding custom helpers
objToAttrs.addHelper('upper', function (val) {
return val.toUpperCase();
});
t.equal(objToAttrs({ upper: 'test' }), 'TEST');
// Test removing custom helpers
objToAttrs.removeHelper('upper');
t.equal(objToAttrs({ upper: 'test' }), 'upper="test"');
// Test quote escaping
t.equal(objToAttrs({ foo: '"bar\'' }), 'foo=""bar'"');
// More random tests
t.equal(objToAttrs({
width: 100, height: 100, style: 'font-color: red', dataFoo: 'bar'
}), 'width="100" height="100" style="font-color: red" data-foo="bar"');
var obj = { width: 100, height: 100 };
t.equal(
objToAttrs(obj, { assignment: ' = ', quote: "'", separator: ' ' }),
"width = '100' height = '100'"
);
t.equal(
objToAttrs({ value: 'test', data: { foo: 'bar', hello: 'world' }}),
'value="test" data-foo="bar" data-hello="world"'
);
});