forked from marko-js/marko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncVDOMBuilder-test.js
134 lines (111 loc) · 3.95 KB
/
AsyncVDOMBuilder-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
var AsyncVDOMBuilder = require('../runtime/vdom/AsyncVDOMBuilder');
var HTMLElement = require('../runtime/vdom/HTMLElement');
var expect = require('chai').expect;
describe('AsyncVDOMBuilder', function() {
it('sync', function() {
var out = new AsyncVDOMBuilder();
out.element('div', {}, 0);
var tree = out.getOutput();
expect(tree.childNodes.length).to.equal(1);
});
it('end, then listen for finish', function(done) {
var out = new AsyncVDOMBuilder();
out.element('div', {}, 0);
out.end();
out.on('finish', function(result) {
expect(result.getOutput().childNodes.length).to.equal(1);
done();
});
});
it('async', function(done) {
var out = new AsyncVDOMBuilder();
out.element('div', {}, 0);
var asyncOut = out.beginAsync();
setTimeout(function() {
asyncOut.element('span', {}, 0);
asyncOut.end();
}, 10);
out.element('section', {}, 0);
out.end();
out.on('finish', function(result) {
var tree = result.getOutput();
expect(tree.childNodes.length).to.equal(3);
expect(tree.firstChild.nodeName).to.equal('div');
expect(tree.firstChild.nextSibling.nodeName).to.equal('span');
expect(tree.firstChild.nextSibling.nextSibling.nodeName).to.equal('section');
done();
});
});
it('promise', function(done) {
const out = new AsyncVDOMBuilder();
out.element('div', {}, 0);
out.end().then((result) => {
expect(result.getOutput().childNodes.length).to.equal(1);
done();
}).catch(done);
});
it('async flush', function(done) {
var out = new AsyncVDOMBuilder();
out.on('update', function(result) {
expect(result.getOutput().childNodes.length).to.equal(1);
});
out.once('finish', function(result) {
expect(result.getOutput().childNodes.length).to.equal(2);
done();
});
out.element('div', {}, 0);
out.flush();
var asyncOut = out.beginAsync();
setTimeout(function() {
asyncOut.element('span', {}, 0);
asyncOut.end();
}, 10);
out.end();
});
it('for loop', function(done) {
var out = new AsyncVDOMBuilder();
out.once('finish', function(result) {
var tree = result.getOutput();
var header = tree.childNodes[0];
var list = tree.childNodes[1];
var paragraph = tree.childNodes[2];
expect(header.nodeName).to.equal('h1');
expect(list.nodeName).to.equal('ul');
expect(list.childNodes.length).to.equal(10);
expect(paragraph.nodeName).to.equal('p');
done();
});
out.element('h1', {}, 0);
out.beginElement('ul', {});
for(var i = 0; i < 10; i++) {
out.element('li', {}, 1).t(i);
}
out.endElement();
out.element('p', {}, 0);
out.end();
});
it('staticNode, text, comment', function(done) {
var staticNode = new HTMLElement('div', {}, 0, 'f891ea3');
var out = new AsyncVDOMBuilder();
out.node(staticNode);
out.text('Hello <em>World</em>');
out.comment('TODO: make this work');
out.end();
out.once('finish', function(result) {
var tree = result.getOutput();
expect(tree.childNodes[0].nodeName).to.equal('div');
expect(tree.childNodes[1].nodeValue).to.equal('Hello <em>World</em>');
expect(tree.childNodes[2].nodeValue).to.equal('TODO: make this work');
done();
});
});
// it('should handle timeouts correctly');
//
// it('should handle sync errors correctly');
//
// it('should handle timeout errors correctly');
//
// it('should avoid writes after end');
//
// it('globals');
});