-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoctre-dom.test.js
More file actions
400 lines (325 loc) · 14.1 KB
/
Copy pathdoctre-dom.test.js
File metadata and controls
400 lines (325 loc) · 14.1 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import { describe, test, expect, beforeEach } from 'vitest';
// ── Doctre.patch() prototype extensions ─────────────────────────
describe('Doctre.patch() — Element prototype extensions', () => {
test('freeze stores child nodes as frost JSON in dataset', () => {
const el = document.createElement('div');
el.innerHTML = '<span>hello</span>';
el.freeze();
expect(el.dataset.frozen).toBeDefined();
expect(el.dataset.frozen).toContain('span');
expect(el.dataset.frozen).toContain('hello');
// Children still present after freeze
expect(el.children.length).toBe(1);
});
test('freeze with custom dataName stores in that key', () => {
const el = document.createElement('div');
el.innerHTML = '<b>text</b>';
el.freeze('myTemplate');
expect(el.dataset.myTemplate).toBeDefined();
expect(el.dataset.frozen).toBeUndefined();
});
test('solid freezes then clears innerHTML', () => {
const el = document.createElement('div');
el.innerHTML = '<p>content</p>';
const returned = el.solid();
expect(el.dataset.frozen).toBeDefined();
expect(el.innerHTML).toBe('');
// Returns the element (chainable)
expect(returned).toBe(el);
});
test('hot restores frozen data as DocumentFragment', () => {
const el = document.createElement('div');
el.innerHTML = '<em>restored</em>';
el.solid();
const fragment = el.hot();
expect(fragment).toBeInstanceOf(DocumentFragment);
expect(fragment.firstChild.tagName).toBe('EM');
expect(fragment.firstChild.textContent).toBe('restored');
});
test('hot with matchReplacer performs token substitution', () => {
const el = document.createElement('div');
el.innerHTML = '<span>|name|</span>';
el.solid();
const fragment = el.hot({ name: 'World' });
expect(fragment.firstChild.textContent).toBe('World');
});
test('hot returns null when no frozen data', () => {
const el = document.createElement('div');
const result = el.hot();
expect(result).toBeNull();
});
test('hot returns null when frozen data is empty', () => {
const el = document.createElement('div');
el.dataset.frozen = '';
const result = el.hot();
expect(result).toBeNull();
});
test('worm appends hot content and returns NodeArray', () => {
const el = document.createElement('div');
el.innerHTML = '<i>item</i>';
el.solid();
const nodeArray = el.worm();
expect(nodeArray).toBeInstanceOf(NodeArray);
expect(nodeArray.length).toBeGreaterThan(0);
// Content is appended to the element
expect(el.children.length).toBe(1);
expect(el.children[0].tagName).toBe('I');
});
test('melt clears then appends (solid → melt roundtrip)', () => {
const el = document.createElement('div');
el.innerHTML = '<strong>original</strong>';
el.solid();
// Element is empty after solid
expect(el.innerHTML).toBe('');
// melt restores content
const nodeArray = el.melt();
expect(el.children.length).toBe(1);
expect(el.children[0].tagName).toBe('STRONG');
expect(el.children[0].textContent).toBe('original');
});
test('melt with matchReplacer replaces tokens', () => {
const el = document.createElement('div');
el.innerHTML = '<span>|greeting|</span>';
el.solid();
el.melt({ greeting: 'Hi' });
expect(el.children[0].textContent).toBe('Hi');
});
test('multiple melt calls from same frozen template', () => {
const el = document.createElement('div');
el.innerHTML = '<span>|val|</span>';
el.solid();
el.melt({ val: 'first' });
expect(el.textContent).toBe('first');
el.melt({ val: 'second' });
expect(el.textContent).toBe('second');
// frozen data persists
expect(el.dataset.frozen).toBeDefined();
});
test('burn returns fragment and deletes frozen data', () => {
const el = document.createElement('div');
el.innerHTML = '<div>temp</div>';
el.solid();
expect(el.dataset.frozen).toBeDefined();
const fragment = el.burn();
expect(fragment).toBeInstanceOf(DocumentFragment);
expect(el.dataset.frozen).toBeUndefined();
});
});
// ── Doctre.createElement / createElementBy ──────────────────────
describe('Doctre.createElement', () => {
test('creates element with tag name', () => {
const el = Doctre.createElement('div');
expect(el.tagName).toBe('DIV');
});
test('creates element with majorAttrs (class, id)', () => {
const el = Doctre.createElement('span', { class: 'foo bar', id: 'myId' });
expect(el.className).toBe('foo bar');
expect(el.id).toBe('myId');
});
test('creates element with string content', () => {
const el = Doctre.createElement('p', null, 'hello');
expect(el.textContent).toBe('hello');
});
test('creates element with style object', () => {
const el = Doctre.createElement('div', null, null, { color: 'red' });
expect(el.style.color).toBe('red');
});
test('creates element with data attributes', () => {
const el = Doctre.createElement('div', null, null, {}, {}, { itemId: '123' });
expect(el.dataset.itemId).toBe('123');
});
test('creates element with matchReplacer in content', () => {
// createElement signature: (tagName, majorAttrs, contentData, style, attrs, datas, matchReplacer)
const el = Doctre.createElement('div', null, '|msg|', {}, {}, {}, { msg: 'replaced' });
expect(el.textContent).toBe('replaced');
});
});
describe('Doctre.createElementBy (solidId)', () => {
test('parses solidId with tag.class#id', () => {
const el = Doctre.createElementBy('div.box.float#app');
expect(el.tagName).toBe('DIV');
expect(el.classList.contains('box')).toBe(true);
expect(el.classList.contains('float')).toBe(true);
expect(el.id).toBe('app');
});
test('parses solidId with name and type', () => {
const el = Doctre.createElementBy('input@email$text');
expect(el.tagName).toBe('INPUT');
expect(el.getAttribute('name')).toBe('email');
expect(el.getAttribute('type')).toBe('text');
});
test('creates element with content and solidId', () => {
const el = Doctre.createElementBy('span.label', 'Hello');
expect(el.textContent).toBe('Hello');
expect(el.classList.contains('label')).toBe(true);
});
});
// ── Doctre.createFragment ───────────────────────────────────────
describe('Doctre.createFragment', () => {
test('creates fragment from cold array with strings', () => {
const frag = Doctre.createFragment(['<b>bold</b>', '<i>italic</i>']);
expect(frag).toBeInstanceOf(DocumentFragment);
expect(frag.childNodes.length).toBe(2);
expect(frag.firstChild.tagName).toBe('B');
});
test('creates fragment from cold array with element arrays', () => {
const frag = Doctre.createFragment([['div', [], {}, {}, {}]]);
expect(frag.firstChild.tagName).toBe('DIV');
});
});
// ── Doctre coldify / stringify / live roundtrip ─────────────────
describe('Doctre coldify → stringify → live roundtrip', () => {
test('element survives coldify → stringify → parse roundtrip', () => {
const original = document.createElement('div');
original.className = 'container';
original.id = 'main';
const child = document.createElement('span');
child.textContent = 'content';
original.appendChild(child);
// Serialize to frost (JSON string)
const frost = Doctre.stringify(original, false, true);
expect(typeof frost).toBe('string');
// Restore from frost
const fragment = Doctre.parse(frost);
expect(fragment).toBeInstanceOf(DocumentFragment);
const restored = fragment.firstChild;
expect(restored.tagName).toBe('DIV');
expect(restored.className).toBe('container');
expect(restored.id).toBe('main');
expect(restored.firstChild.tagName).toBe('SPAN');
expect(restored.firstChild.textContent).toBe('content');
});
test('coldify preserves multiple children', () => {
const parent = document.createElement('ul');
for (const text of ['A', 'B', 'C']) {
const li = document.createElement('li');
li.textContent = text;
parent.appendChild(li);
}
const cold = Doctre.coldify(parent.childNodes, true);
expect(cold.length).toBe(3);
// Each item is a cold array starting with tag info
for (const item of cold) {
expect(Array.isArray(item)).toBe(true);
}
});
test('Node.coldify / Node.stringify prototype methods work', () => {
const el = document.createElement('b');
el.textContent = 'bold';
const cold = el.coldify();
expect(Array.isArray(cold)).toBe(true);
const frost = el.stringify();
expect(typeof frost).toBe('string');
expect(frost).toContain('b');
});
test('NodeList.coldify works on multiple nodes', () => {
const wrapper = document.createElement('div');
wrapper.innerHTML = '<span>a</span><span>b</span>';
const cold = wrapper.childNodes.coldify(true);
expect(cold.length).toBe(2);
});
test('Element.cold returns child cold array', () => {
const el = document.createElement('div');
el.innerHTML = '<p>text</p>';
const cold = el.cold(true);
expect(Array.isArray(cold)).toBe(true);
expect(cold.length).toBe(1);
});
test('Element.frozen returns frost string of children', () => {
const el = document.createElement('div');
el.innerHTML = '<i>text</i>';
const frost = el.frozen();
expect(typeof frost).toBe('string');
expect(frost).toContain('i');
expect(frost).toContain('text');
});
test('Element.takeCold returns cold and clears content', () => {
const el = document.createElement('div');
el.innerHTML = '<span>gone</span>';
const cold = el.takeCold(true);
expect(cold.length).toBe(1);
expect(el.innerHTML).toBe('');
});
test('Element.takeFrozen returns frost and clears content', () => {
const el = document.createElement('div');
el.innerHTML = '<b>gone</b>';
const frost = el.takeFrozen();
expect(frost).toContain('b');
expect(el.innerHTML).toBe('');
});
});
// ── Element.alive / alone ───────────────────────────────────────
describe('Element.alive / alone', () => {
test('alive appends frost content and returns NodeArray', () => {
const el = document.createElement('div');
el.innerHTML = '<p>existing</p>';
const frost = '[["span",[],{},{},{}]]';
const nodeArray = el.alive(frost);
expect(nodeArray).toBeInstanceOf(NodeArray);
// Both existing and new content
expect(el.children.length).toBe(2);
});
test('alone clears then appends (replace)', () => {
const el = document.createElement('div');
el.innerHTML = '<p>old</p>';
const frost = '[["span",["new"],{},{},{}]]';
el.alone(frost);
expect(el.children.length).toBe(1);
expect(el.children[0].tagName).toBe('SPAN');
});
});
// ── Doctre.getSolidId / packTagAndMajorAttrs ────────────────────
describe('Doctre.getSolidId', () => {
test('assembles solidId from components', () => {
expect(Doctre.getSolidId('div', 'a b', 'myId', 'myName', 'myType'))
.toBe('div.a.b#myId@myName$myType');
});
test('omits null parts', () => {
expect(Doctre.getSolidId('span', null, 'x'))
.toBe('span#x');
});
test('tag only', () => {
expect(Doctre.getSolidId('p'))
.toBe('p');
});
});
describe('Doctre.packTagAndMajorAttrs', () => {
test('extracts as object by default', () => {
const el = document.createElement('div');
el.className = 'foo';
el.id = 'bar';
const packed = Doctre.packTagAndMajorAttrs(el);
expect(packed.tagName).toBe('div');
expect(packed.class).toBe('foo');
expect(packed.id).toBe('bar');
});
test('extracts as solidId string when asSolidId=true', () => {
const el = document.createElement('input');
el.setAttribute('name', 'email');
el.setAttribute('type', 'text');
const solidId = Doctre.packTagAndMajorAttrs(el, true);
expect(solidId).toBe('input@email$text');
});
});
// ── NodeArray ───────────────────────────────────────────────────
describe('NodeArray', () => {
test('box creates NodeArray from DocumentFragment', () => {
const frag = document.createDocumentFragment();
frag.appendChild(document.createElement('div'));
frag.appendChild(document.createElement('span'));
// Note: after box, fragment's children are still there because
// box copies references, doesn't move nodes
const arr = NodeArray.box(frag);
expect(arr).toBeInstanceOf(NodeArray);
expect(arr).toBeInstanceOf(Array);
expect(arr.length).toBe(2);
expect(arr[0].tagName).toBe('DIV');
expect(arr[1].tagName).toBe('SPAN');
});
test('box creates NodeArray from NodeList', () => {
const wrapper = document.createElement('div');
wrapper.innerHTML = '<a>1</a><b>2</b><i>3</i>';
const arr = NodeArray.box(wrapper.childNodes);
expect(arr.length).toBe(3);
});
});