-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
67 lines (52 loc) · 1.44 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
import test from 'ava';
import jsdom from 'jsdom';
const {JSDOM} = jsdom;
const {window} = new JSDOM('');
const {document} = (new JSDOM('')).window;
global.window = window;
global.document = document;
Object.defineProperty(window.HTMLElement.prototype, 'dataset', {
get: () => null
});
const dataset = require('.');
function toDashed(name) {
return name.replace(/([A-Z])/g, u => {
return `-${u.toLowerCase()}`;
});
}
test('get values', t => {
const elem = document.createElement('div');
elem.setAttribute('data-foo', 'bar');
elem.setAttribute('data-a-b', 'c');
t.is(dataset(elem).foo, 'bar');
t.is(dataset(elem).aB, 'c');
});
test('set values', t => {
const elem = document.createElement('div');
let undef;
elem.setAttribute('data-undef', 'test');
elem.setAttribute('data-a-b', 'c');
elem.setAttribute('data-foo', 'bar');
dataset(elem).undef = undef;
dataset(elem).aB = 'c';
dataset(elem).foo = 'baz';
t.is(elem.getAttribute('data-undef'), null);
t.is(elem.getAttribute('data-a-b'), 'c');
t.is(elem.getAttribute('data-foo'), 'baz');
});
test('get multiple values', t => {
const elem = document.createElement('div');
const attrs = {
foo: 'bar',
aB: 'c',
true: 'true'
};
for (const key in attrs) {
if (Object.prototype.hasOwnProperty.call(attrs, key)) {
const name = toDashed(key);
elem.setAttribute(`data-${name}`, attrs[key]);
}
}
const result = Object.assign({}, dataset(elem));
t.deepEqual(result, attrs);
});