-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
152 lines (128 loc) · 4.59 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 should = require('should');
const nodom = require('nodom');
global.document = new nodom.Document();
describe('Pakertaja', () => {
const p = require('./dist/pakertaja');
it('should be able to create DOM elements', () => {
const elements = {
'div': p('div'),
'a': p('a'),
'br': p('br')
};
Object.keys(elements).forEach(tagName => {
const element = elements[tagName];
should.exist(element);
should.exist(element.childNodes);
should.exist(element.style);
should.exist(element.nodeType);
should.exist(element.tagName);
should.strictEqual(element.tagName, tagName);
should.strictEqual(element.nodeType, 1);
element.childNodes.should.be.empty();
element.style.should.be.empty();
});
});
it('should be able to create text nodes', () => {
const node = p('text', 'Test.');
node.should.be.instanceOf(nodom.TextNode);
node.should.have.property('textContent', 'Test.');
});
it('should have shortcut function for creating text nodes', () => {
const node = p.text('Test.');
node.should.be.instanceOf(nodom.TextNode);
node.should.have.property('textContent', 'Test.');
});
it('should be able to set inner text', () => {
const elements1 = [
p('div', 'Hello!'),
p('div', { text: 'Hello!' }),
p('div', { text: () => 'Hello!' })
];
const elements2 = [
p('div', ''),
p('div', { text: '' }),
p('div', { text: null }),
p('div', { text: () => '' }),
p('div', { text: () => null })
];
elements1.forEach(element => {
should.exist(element.childNodes);
should.exist(element.childNodes[0]);
should.strictEqual(element.childNodes[0].nodeType, 3);
should.exist(element.textContent);
should.strictEqual(element.textContent, 'Hello!');
});
elements2.forEach(element => {
should.exist(element.textContent);
element.textContent.should.be.empty();
});
});
it('should be able to set inner HTML', () => {
const elements1 = [
p('div', { html: '<b></b>' }),
p('div', { html: () => '<b></b>' })
];
const elements2 = [
p('div', { html: '' }),
p('div', { html: () => '' }),
p('div', { html: null }),
p('div', { html: () => null })
];
elements1.forEach(element => {
should.exist(element.innerHTML);
element.innerHTML.should.not.be.empty();
should.strictEqual(element.innerHTML, '<b></b>');
});
elements2.forEach(element => {
should.exist(element.innerHTML);
element.innerHTML.should.be.empty();
});
});
it('should be able to set CSS rules', () => {
const elements = [
p('div', { style: { color: 'red' } }),
p('div', { style: () => ({ color: 'red' } )})
];
elements.forEach(element => {
element.style.should.not.be.empty();
element.style.color.should.not.be.empty();
});
});
it('should be able to set CSS rules as a string', () => {
should.strictEqual(p('div', { style: 'color: red' }).getAttribute('style'), 'color: red');
});
it('should be able to create child nodes', () => {
const element = p('div', p('div'), p('a'), p('div'));
should.exist(element.childNodes);
should.strictEqual(element.childNodes.length, 3);
should.exist(element.firstChild);
should.exist(element.firstChild.tagName);
should.strictEqual(element.firstChild.tagName, 'div');
should.strictEqual(element.getElementsByTagName('div').length, 2);
should.strictEqual(element.getElementsByTagName('a').length, 1);
});
it('should be able to set attributes', () => {
const element = p('a', { href: 'http://goatse.cx' });
should.exist(element.getAttribute);
should.strictEqual(element.getAttribute('href'), 'http://goatse.cx');
});
it('should be able to escape HTML entities', () => {
p.escape.should.be.Function();
should.strictEqual(p.escape('foo & bar'), 'foo & bar');
should.strictEqual(p.escape('foo < bar'), 'foo < bar');
should.strictEqual(p.escape('foo > bar'), 'foo > bar');
should.strictEqual(p.escape('foo " bar'), 'foo " bar');
should.strictEqual(p.escape('foo \' bar'), 'foo ' bar');
should.strictEqual(p.escape('foo / bar'), 'foo / bar');
});
it('should contain shortcut functions for node creation', () => {
['div', 'h1', 'p', 'a', 'strong'].forEach(tagName => {
should.exist(p[tagName]);
p[tagName].should.be.Function();
const element = p[tagName].call(p);
should.exist(element);
should.exist(element.tagName);
should.strictEqual(element.tagName, tagName);
});
});
});