-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
91 lines (67 loc) · 2 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
var test = require("prova");
var patcher = require('./');
var fruits = ['apple', 'grape', 'orange'];
var moreFruits = ['apple', 'grape', 'cherry', 'melon', 'orange'];
test('Basic DOM diffing and patching', function (t) {
t.plan(3);
clear();
var patch = patcher(document.body, basic('hi'));
t.equal(document.body.innerHTML.trim(), '<h1>hi</h1>');
var root = document.querySelector('basic');
patch(basic('yo'));
t.equal(document.body.innerHTML.trim(), '<h1>yo</h1>');
t.equal(document.querySelector('basic'), document.querySelector('basic'));
});
test('Lists', function (t) {
t.plan(9);
clear();
var patch = patcher(document.body, list(fruits));
t.equal(document.body.innerHTML.trim(), list(fruits));
Array.prototype.forEach.call(document.querySelectorAll('li'), function (el) {
el.style.color = 'purple';
t.equal(el.style.color, 'purple');
});
patch(list(moreFruits));
Array.prototype.forEach.call(document.querySelectorAll('li'), function (el, ind) {
if (ind > 2) { // cherry, melon or orange
t.notOk(el.style.color);
return;
};
t.equal(el.style.color, 'purple');
});
});
test('Countback example', function (t) {
t.plan(2);
clear();
var n = 3;
var patch = patcher(document.body, render);
var timer = setInterval(patch, 1000);
function render () {
if (n == 1) {
clearInterval(timer);
}
if (n < 3) {
t.equal(document.body.innerHTML.trim(), countback(n + 1));
}
return countback(n--);
}
});
function basic (content) {
return '<h1>' + content + '</h1>';
}
function list (content) {
var html = '<ul class="foo">';
html += content.map(function (el) {
return '<li class="bar">' + el + '</li>';
}).join('\n');
html += '</ul>';
return html;
}
function countback (n) {
var size = 48 - (n * 5);
var colors = ['red', 'blue', 'green'];
return '<h1 data-n="' + n + '" style="font-size: ' + size + 'px; color: ' + colors[n - 1] + ';">' + n + ' </h1>';
}
function clear () {
document.body.innerHTML = '';
}