-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
181 lines (150 loc) · 5.03 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* @jest-environment jsdom */
const { patch, T, N } = require('./index.js');
const clean = () => (document.body.innerHTML = "");
describe('empty case', () => {
it('do nothing', () => {
let r1 = patch(null, null, document.body);
expect(r1).toBeNull();
});
});
describe('creating nodes', () => {
afterEach(clean);
it('text node', () => {
let r1 = patch(null, T('text'), document.body);
expect(r1.s).toBe('text');
expect(r1.n).not.toBeNull();
});
it('node', () => {
let r1 = patch(null, N('div', {}, {}, []), document.body);
expect(r1.n.nodeName).toBe('DIV');
expect(r1.n.children.length).toBe(0);
});
});
describe('deleting nodes', () => {
afterEach(clean);
it('text node', () => {
let r1 = patch(null, T('text'), document.body);
r1 = patch(r1, null, document.body);
expect(r1).toBeNull();
});
it('node', () => {
let r1 = patch(null, N('div', {}, {}, []), document.body);
r1 = patch(r1, null, document.body);
expect(r1).toBeNull();
});
it('children node', () => {
let n1 = N('div', {}, {}, [T('a')]);
let n2 = N('div', {}, {}, []);
let r1 = patch(null, n1, document.body);
r1 = patch(r1, n2, document.body);
expect(r1.c.length).toBe(0);
});
});
describe('updating nodes', () => {
afterEach(clean);
it('text node', () => {
let r1 = patch(null, T('a'), document.body);
r1 = patch(r1, T('b'), document.body);
expect(r1.s).toBe('b');
expect(r1.n.textContent).toBe('b');
});
describe('rendering inner nodes', () => {
it('update text inner node', () => {
const n = (text) => N('div', {}, {}, [text]);
let r1 = patch(null, n(T('a')), document.body);
const text = r1.c[0].n;
expect(r1.c.length).toBe(1);
expect(text.textContent).toBe('a');
r1 = patch(r1, n(T('b')), document.body);
expect(text.textContent).toBe('b');
});
it('changing inner node from div to p', () => {
const n = (t) => N('div', {}, {}, [
N(t, {}, {}, [
T('text')
]),
]);
let r1 = patch(null, n('div'), document.body);
let a = r1.c[0].n;
expect(a.nodeName).toBe('DIV');
patch(r1, n('p'), document.body);
a = r1.c[0].n;
expect(document.body.firstChild.firstChild.nodeName).toBe(a.nodeName);
});
it('changing inner node from text to node', () => {
const a = N('div', {}, {}, [
T('abc')
]);
const b = N('div', {}, {}, [
N('div', {}, {}, [])
]);
let r1 = patch(null, a, document.body);
r1 = patch(r1, b, document.body);
expect(document.body.firstChild.firstChild.nodeName).toBe('DIV');
});
it('changing inner node from node to text', () => {
const a = N('div', {}, {}, [
N('div', {}, {}, [])
]);
const b = N('div', {}, {}, [
T('abc')
]);
let r1 = patch(null, a, document.body);
r1 = patch(r1, b, document.body);
expect(document.body.firstChild.firstChild.nodeName).toBe('#text');
});
});
});
describe('managing attributes', () => {
afterEach(clean);
it('setting up an attribute', () => {
const n = N('button', { 'class': 'btn' }, {}, [T('text')]);
let r1 = patch(null, n, document.body);
expect(document.querySelector('.btn')).not.toBeNull();
});
it('removing an attribute', () => {
const n1 = N('button', { 'class': 'btn' }, {}, [T('text')]);
const n2 = N('button', { 'class': null }, {}, [T('text')]);
let r1 = patch(null, n1, document.body);
r1 = patch(r1, n2, document.body);
expect(document.querySelector('.btn')).toBeNull();
});
it('replacing attribute', () => {
const n1 = N('button', { 'class': 'btn' }, {}, [T('text')]);
const n2 = N('button', { 'class': 'btn2' }, {}, [T('text')]);
let r1 = patch(null, n1, document.body);
r1 = patch(r1, n2, document.body);
expect(document.querySelector('.btn')).toBeNull();
expect(document.querySelector('.btn2')).not.toBeNull();
});
});
describe('managing listeners', () => {
afterEach(clean);
it('setting up a listener', () => {
const f = jest.fn();
const n = N('button', { 'class': 'btn' }, { 'click': f }, [T('text')]);
let r1 = patch(null, n, document.body);
document.querySelector('.btn').click();
expect(f).toHaveBeenCalled();
});
it('disabling a listener', () => {
const f = jest.fn();
const n1 = N('button', { 'class': 'btn' }, { 'click': f }, [T('text')]);
const n2 = N('button', { 'class': 'btn' }, { 'click': null }, [T('text')]);
let r1 = patch(null, n1, document.body);
r1 = patch(r1, n2, document.body);
document.querySelector('.btn').click();
expect(f).not.toHaveBeenCalled();
});
it('replacing listener', () => {
const f = jest.fn();
const g = jest.fn();
const n1 = N('button', { 'class': 'btn' }, { 'click': f }, [T('text')]);
const n2 = N('button', { 'class': 'btn' }, { 'click': g }, [T('text')]);
let r1 = patch(null, n1, document.body);
r1 = patch(r1, n2, document.body);
document.querySelector('.btn').click();
expect(f).not.toHaveBeenCalled();
expect(g).toHaveBeenCalled();
});
});