-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathutil.test.ts
More file actions
657 lines (637 loc) · 22.6 KB
/
Copy pathutil.test.ts
File metadata and controls
657 lines (637 loc) · 22.6 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import { Diff, DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT } from 'diff-match-patch'
import { JSDOM } from 'jsdom'
import {
areArraysEqual,
areNodesEqual,
charForNodeName,
cleanUpNodeMarkers,
diffText,
getAncestors,
isComment,
isDocument,
isDocumentFragment,
isElement,
isText,
never,
} from './util'
const window = new JSDOM('').window
const document = window.document
const text = document.createTextNode('text')
const identicalText = document.createTextNode('text')
const differentText = document.createTextNode('different text')
const span = document.createElement('SPAN')
const identicalSpan = document.createElement('SPAN')
const differentAttributeNamesSpan = document.createElement('SPAN')
const differentAttributeValuesSpan = document.createElement('SPAN')
const differentChildNodesSpan = document.createElement('SPAN')
const video = document.createElement('VIDEO')
const comment = document.createComment('comment')
const identicalComment = document.createComment('comment')
const differentComment = document.createComment('different comment')
const fragment = document.createDocumentFragment()
const anotherFragment = document.createDocumentFragment()
const pChar = charForNodeName('P')
const ulChar = charForNodeName('UL')
const liChar = charForNodeName('LI')
span.setAttribute('data-a', 'a')
span.setAttribute('data-b', 'b')
identicalSpan.setAttribute('data-b', 'b')
identicalSpan.setAttribute('data-a', 'a')
differentAttributeNamesSpan.setAttribute('data-a', 'a')
differentAttributeNamesSpan.setAttribute('data-b', 'b')
differentAttributeNamesSpan.setAttribute('data-c', 'c')
differentAttributeValuesSpan.setAttribute('data-a', 'different a')
differentAttributeValuesSpan.setAttribute('data-b', 'different b')
differentChildNodesSpan.setAttribute('data-a', 'a')
differentChildNodesSpan.setAttribute('data-b', 'b')
differentChildNodesSpan.appendChild(document.createTextNode('different'))
describe('isText', () => {
test('return true given a text node', () => {
expect(isText(text)).toBe(true)
})
test('return false given a SPAN', () => {
expect(isText(span)).toBe(false)
})
test('return false given a document', () => {
expect(isText(document)).toBe(false)
})
test('return false given a document fragment', () => {
expect(isText(fragment)).toBe(false)
})
test('return false given a comment', () => {
expect(isText(comment)).toBe(false)
})
})
describe('isElement', () => {
test('return false given a text node', () => {
expect(isElement(text)).toBe(false)
})
test('return true given a SPAN', () => {
expect(isElement(span)).toBe(true)
})
test('return false given a document', () => {
expect(isElement(document)).toBe(false)
})
test('return false given a document fragment', () => {
expect(isElement(fragment)).toBe(false)
})
test('return false given a comment', () => {
expect(isElement(comment)).toBe(false)
})
})
describe('isDocument', () => {
test('return false given a text node', () => {
expect(isDocument(text)).toBe(false)
})
test('return false given a SPAN', () => {
expect(isDocument(span)).toBe(false)
})
test('return true given a document', () => {
expect(isDocument(document)).toBe(true)
expect(isDocument(new JSDOM('').window.document)).toBe(true)
})
test('return false given a document fragment', () => {
expect(isDocument(fragment)).toBe(false)
})
test('return false given a comment', () => {
expect(isDocument(comment)).toBe(false)
})
})
describe('isDocumentFragment', () => {
test('return false given a text node', () => {
expect(isDocumentFragment(text)).toBe(false)
})
test('return false given a SPAN', () => {
expect(isDocumentFragment(span)).toBe(false)
})
test('return false given a document', () => {
expect(isDocumentFragment(document)).toBe(false)
})
test('return true given a document fragment', () => {
expect(isDocumentFragment(fragment)).toBe(true)
})
test('return false given a comment', () => {
expect(isDocumentFragment(comment)).toBe(false)
})
})
describe('isComment', () => {
test('return false given a text node', () => {
expect(isComment(text)).toBe(false)
})
test('return false given a SPAN', () => {
expect(isComment(span)).toBe(false)
})
test('return false given a document', () => {
expect(isComment(document)).toBe(false)
})
test('return true given a document fragment', () => {
expect(isComment(fragment)).toBe(false)
})
test('return true given a comment', () => {
expect(isComment(comment)).toBe(true)
})
})
describe('areArraysEqual', () => {
describe('default comparator', () => {
test('empty arrays', () => {
expect(areArraysEqual([], [])).toBe(true)
})
test('different length', () => {
expect(areArraysEqual([1], [1, 2])).toBe(false)
})
test('different item types', () => {
expect(areArraysEqual([1, 2], [1, '2'])).toBe(false)
})
test('identical arrays', () => {
expect(areArraysEqual([1, '2', text], [1, '2', text])).toBe(true)
})
test('the same nodes', () => {
expect(areArraysEqual([text, text], [text, text])).toBe(true)
})
test('identical nodes', () => {
expect(areArraysEqual([text, text], [text, identicalText])).toBe(
false,
)
})
test('different nodes', () => {
expect(areArraysEqual([text, text], [text, differentText])).toBe(
false,
)
})
})
describe('node comparator', () => {
test('the same nodes', () => {
expect(
areArraysEqual([text, text], [text, text], areNodesEqual),
).toBe(true)
})
test('identical nodes', () => {
expect(
areArraysEqual(
[text, text],
[text, identicalText],
areNodesEqual,
),
).toBe(true)
})
test('different nodes', () => {
expect(
areArraysEqual(
[text, text],
[text, differentText],
areNodesEqual,
),
).toBe(false)
})
})
})
describe.each<[string, (() => string[]) | undefined]>([
['native', window.Element.prototype.getAttributeNames],
['undefined', undefined],
])(
'areNodesEqual (getAttributeNames: %s)',
(_: string, customGetAttributeNames: any) => {
const originalGetAttributeNames =
window.Element.prototype.getAttributeNames
beforeAll(() => {
window.Element.prototype.getAttributeNames = customGetAttributeNames
})
afterAll(() => {
window.Element.prototype.getAttributeNames = originalGetAttributeNames
})
describe('shallow', () => {
test('the same node', () => {
expect(areNodesEqual(text, text)).toBe(true)
})
test('different node types', () => {
expect(areNodesEqual(text, span)).toBe(false)
})
test('different node names', () => {
expect(areNodesEqual(video, span)).toBe(false)
})
test('different comment nodes', () => {
expect(areNodesEqual(comment, differentComment)).toBe(false)
})
test('identical comment nodes', () => {
expect(areNodesEqual(comment, identicalComment)).toBe(true)
})
test('different text nodes', () => {
expect(areNodesEqual(text, differentText)).toBe(false)
})
test('identical text nodes', () => {
expect(areNodesEqual(text, identicalText)).toBe(true)
})
test('elements with different attribute names', () => {
expect(areNodesEqual(span, differentAttributeNamesSpan)).toBe(
false,
)
})
test('elements with different attribute values', () => {
expect(areNodesEqual(span, differentAttributeValuesSpan)).toBe(
false,
)
})
test('elements with different attribute names being ignored', () => {
expect(
areNodesEqual(
span,
differentAttributeNamesSpan,
true,
true,
),
).toBe(true)
})
test('elements with different attribute values being ignored', () => {
expect(
areNodesEqual(
span,
differentAttributeValuesSpan,
true,
true,
),
).toBe(true)
})
test('elements with different childNodes', () => {
expect(areNodesEqual(span, differentChildNodesSpan)).toBe(true)
})
test('identical elements', () => {
expect(areNodesEqual(span, identicalSpan)).toBe(true)
})
test('document fragments', () => {
expect(areNodesEqual(fragment, anotherFragment)).toBe(true)
})
})
describe('deep', () => {
const rootNode = document.createDocumentFragment()
const div = document.createElement('DIV')
const p = document.createElement('P')
const em = document.createElement('EM')
const strong = document.createElement('STRONG')
rootNode.append(div, p)
p.append(em, strong)
em.textContent = 'em'
strong.textContent = 'strong'
test('identical nodes', () => {
expect(
areNodesEqual(
rootNode.cloneNode(true),
rootNode.cloneNode(true),
true,
),
).toBe(true)
})
test('extraneous child', () => {
const differentRootNode = rootNode.cloneNode(true)
;((differentRootNode.lastChild as Node)
.lastChild as Node).appendChild(
document.createTextNode('different'),
)
expect(
areNodesEqual(
rootNode.cloneNode(true),
differentRootNode,
true,
),
).toBe(false)
})
test('child with a different attribute', () => {
const differentRootNode = rootNode.cloneNode(true)
;((differentRootNode.lastChild as Node)
.lastChild as Element).setAttribute('data-a', 'a')
expect(
areNodesEqual(
rootNode.cloneNode(true),
differentRootNode,
true,
),
).toBe(false)
})
})
},
)
describe('getAncestors', () => {
const node1 = document.createDocumentFragment()
const node2 = document.createElement('DIV')
const node3 = document.createTextNode('test')
node1.append(node2)
node2.append(node3)
const testData: Array<[Node, Node | undefined | null, Node[]]> = [
[node1, undefined, []],
[node2, undefined, [node1]],
[node3, undefined, [node2, node1]],
[node1, null, []],
[node2, null, [node1]],
[node3, null, [node2, node1]],
[node1, node1, []],
[node2, node1, [node1]],
[node3, node1, [node2, node1]],
[node1, node2, []],
[node2, node2, []],
[node3, node2, [node2]],
[node1, node3, []],
[node2, node3, [node1]],
[node3, node3, []],
]
testData.forEach(([node, rootNode, ancestors]) => {
test(`node: ${node.nodeName}; root: ${rootNode &&
rootNode.nodeName}`, () => {
expect(getAncestors(node, rootNode)).toStrictEqual(ancestors)
})
})
})
describe('never', () => {
test('default message', () => {
expect(() => never()).toThrowError(
'visual-dom-diff: Should never happen',
)
})
test('custom message', () => {
expect(() => never('Custom message')).toThrowError('Custom message')
})
})
describe('diffText', () => {
test('empty inputs', () => {
expect(diffText('', '')).toStrictEqual([])
})
test('identical inputs', () => {
expect(diffText('test', 'test')).toStrictEqual([[DIFF_EQUAL, 'test']])
})
test('insert into empty', () => {
expect(diffText('', 'test')).toStrictEqual([[DIFF_INSERT, 'test']])
})
test('delete all', () => {
expect(diffText('test', '')).toStrictEqual([[DIFF_DELETE, 'test']])
})
test('different letter case', () => {
expect(diffText('test', 'Test')).toStrictEqual([
[DIFF_DELETE, 't'],
[DIFF_INSERT, 'T'],
[DIFF_EQUAL, 'est'],
])
})
test('different whitespace', () => {
expect(diffText('start end', 'start end')).toStrictEqual([
[DIFF_EQUAL, 'start '],
[DIFF_INSERT, ' '],
[DIFF_EQUAL, 'end'],
])
})
test('word added', () => {
expect(diffText('start end', 'start add end')).toStrictEqual([
[DIFF_EQUAL, 'start '],
[DIFF_INSERT, 'add '],
[DIFF_EQUAL, 'end'],
])
})
test('word removed', () => {
expect(diffText('start remove end', 'start end')).toStrictEqual([
[DIFF_EQUAL, 'start '],
[DIFF_DELETE, 'remove '],
[DIFF_EQUAL, 'end'],
])
})
test('word replaced', () => {
expect(diffText('start remove end', 'start add end')).toStrictEqual([
[DIFF_EQUAL, 'start '],
[DIFF_DELETE, 'remove'],
[DIFF_INSERT, 'add'],
[DIFF_EQUAL, ' end'],
])
})
test('word added with a node marker', () => {
expect(
diffText(
`${pChar}start${pChar}end`,
`${pChar}start${pChar}add${pChar}end`,
),
).toStrictEqual([
[DIFF_EQUAL, `${pChar}start`],
[DIFF_INSERT, `${pChar}add`],
[DIFF_EQUAL, `${pChar}end`],
])
})
test('word removed with a node marker', () => {
expect(
diffText(
`${pChar}start${pChar}remove${pChar}end`,
`${pChar}start${pChar}end`,
),
).toStrictEqual([
[DIFF_EQUAL, `${pChar}start`],
[DIFF_DELETE, `${pChar}remove`],
[DIFF_EQUAL, `${pChar}end`],
])
})
test('word replaced in text with node markers', () => {
expect(
diffText(
`${pChar}start${pChar}remove${pChar}end`,
`${pChar}start${pChar}add${pChar}end`,
),
).toStrictEqual([
[DIFF_EQUAL, `${pChar}start${pChar}`],
[DIFF_DELETE, 'remove'],
[DIFF_INSERT, 'add'],
[DIFF_EQUAL, `${pChar}end`],
])
})
test('semantic diff', () => {
expect(diffText('mouse', 'sofas')).toStrictEqual([
[DIFF_DELETE, 'mouse'],
[DIFF_INSERT, 'sofas'],
])
})
describe('skip node markers when running diff_cleanupSemantic', () => {
test('equal node markers only', () => {
expect(
diffText(
'\uE000\uE001\uE002\uE003',
'\uE000\uE001\uE002\uE003',
),
).toStrictEqual([[DIFF_EQUAL, '\uE000\uE001\uE002\uE003']])
})
test('equal node markers with prefix', () => {
expect(
diffText(
'a\uE000\uE001\uE002\uE003',
'a\uE000\uE001\uE002\uE003',
),
).toStrictEqual([[DIFF_EQUAL, 'a\uE000\uE001\uE002\uE003']])
})
test('equal node markers with suffix', () => {
expect(
diffText(
'\uE000\uE001\uE002\uE003z',
'\uE000\uE001\uE002\uE003z',
),
).toStrictEqual([[DIFF_EQUAL, '\uE000\uE001\uE002\uE003z']])
})
test('equal node markers with prefix and suffix', () => {
expect(
diffText(
'a\uE000\uE001\uE002\uE003z',
'a\uE000\uE001\uE002\uE003z',
),
).toStrictEqual([[DIFF_EQUAL, 'a\uE000\uE001\uE002\uE003z']])
})
test('equal prefix only', () => {
expect(diffText('prefix', 'prefix')).toStrictEqual([
[DIFF_EQUAL, 'prefix'],
])
})
test('changed letter within text', () => {
expect(diffText('prefixAsuffix', 'prefixBsuffix')).toStrictEqual([
[DIFF_EQUAL, 'prefix'],
[DIFF_DELETE, 'A'],
[DIFF_INSERT, 'B'],
[DIFF_EQUAL, 'suffix'],
])
})
test('changed node within text', () => {
expect(
diffText('prefix\uE000suffix', 'prefix\uE001suffix'),
).toStrictEqual([
[DIFF_EQUAL, 'prefix'],
[DIFF_DELETE, '\uE000'],
[DIFF_INSERT, '\uE001'],
[DIFF_EQUAL, 'suffix'],
])
})
test('multiple changed letters around equal letter', () => {
expect(diffText('abc!def', '123!456')).toStrictEqual([
[DIFF_DELETE, 'abc!def'],
[DIFF_INSERT, '123!456'],
])
})
test('multiple changed node markers around equal letter', () => {
expect(
diffText(
'\uE000\uE001\uE002!\uE003\uE004\uE005',
'\uE006\uE007\uE008!\uE009\uE00A\uE00B',
),
).toStrictEqual([
[DIFF_DELETE, '\uE000\uE001\uE002!\uE003\uE004\uE005'],
[DIFF_INSERT, '\uE006\uE007\uE008!\uE009\uE00A\uE00B'],
])
})
test('multiple changed letters around equal node marker', () => {
expect(diffText('abc\uE000def', '123\uE000456')).toStrictEqual([
[DIFF_DELETE, 'abc'],
[DIFF_INSERT, '123'],
[DIFF_EQUAL, '\uE000'],
[DIFF_DELETE, 'def'],
[DIFF_INSERT, '456'],
])
})
test('multiple changed node markers around equal node marker', () => {
expect(
diffText(
'\uE000\uE001\uE002\uF000\uE003\uE004\uE005',
'\uE006\uE007\uE008\uF000\uE009\uE00A\uE00B',
),
).toStrictEqual([
[DIFF_DELETE, '\uE000\uE001\uE002'],
[DIFF_INSERT, '\uE006\uE007\uE008'],
[DIFF_EQUAL, '\uF000'],
[DIFF_DELETE, '\uE003\uE004\uE005'],
[DIFF_INSERT, '\uE009\uE00A\uE00B'],
])
})
test.each<string>([
'!',
'\u0000',
'\uDFFF',
'\uF900',
'\uFFFF',
'\uDFFF\uF900',
])(
'identical text without node markers inside changed text (%#)',
string => {
expect(
diffText(`abcdef${string}ghijkl`, `123456${string}7890-=`),
).toStrictEqual([
[DIFF_DELETE, `abcdef${string}ghijkl`],
[DIFF_INSERT, `123456${string}7890-=`],
])
},
)
test.each<string>([
'\uE000',
'\uEFEF',
'\uF8FF',
'\uE000\uF8FF',
'\uE000!\uF8FF',
])(
'identical text with node markers inside changed text (%#)',
string => {
expect(
diffText(`abcdef${string}ghijkl`, `123456${string}7890-=`),
).toStrictEqual([
[DIFF_DELETE, 'abcdef'],
[DIFF_INSERT, '123456'],
[DIFF_EQUAL, string],
[DIFF_DELETE, 'ghijkl'],
[DIFF_INSERT, '7890-='],
])
},
)
})
})
describe('cleanUpNodeMarkers', () => {
test('cleans up multiple node markers in delete', () => {
const diff: Diff[] = [
[DIFF_EQUAL, `abc${pChar}${ulChar}${liChar}${liChar}`],
[DIFF_DELETE, `${pChar}${ulChar}${liChar}${liChar}`],
[DIFF_EQUAL, `${pChar}${ulChar}${liChar}${liChar}xyz`],
]
cleanUpNodeMarkers(diff)
expect(diff).toStrictEqual([
[DIFF_EQUAL, `abc`],
[DIFF_DELETE, `${pChar}${ulChar}${liChar}${liChar}`],
[
DIFF_EQUAL,
`${pChar}${ulChar}${liChar}${liChar}${pChar}${ulChar}${liChar}${liChar}xyz`,
],
])
})
test('cleans up multiple node markers in insert', () => {
const diff: Diff[] = [
[DIFF_EQUAL, `abc${pChar}${ulChar}${liChar}${liChar}`],
[DIFF_INSERT, `${pChar}${ulChar}${liChar}${liChar}`],
[DIFF_EQUAL, `${pChar}${ulChar}${liChar}${liChar}xyz`],
]
cleanUpNodeMarkers(diff)
expect(diff).toStrictEqual([
[DIFF_EQUAL, `abc`],
[DIFF_INSERT, `${pChar}${ulChar}${liChar}${liChar}`],
[
DIFF_EQUAL,
`${pChar}${ulChar}${liChar}${liChar}${pChar}${ulChar}${liChar}${liChar}xyz`,
],
])
})
test('cleans up a node marker in delete and removes a redundant diff item', () => {
const diff: Diff[] = [
[DIFF_EQUAL, `${pChar}`],
[DIFF_DELETE, `abc${pChar}`],
[DIFF_EQUAL, `${pChar}xyz`],
]
cleanUpNodeMarkers(diff)
expect(diff).toStrictEqual([
[DIFF_DELETE, `${pChar}abc`],
[DIFF_EQUAL, `${pChar}${pChar}xyz`],
])
})
test('cleans up a node marker in insert and removes a redundant diff item', () => {
const diff: Diff[] = [
[DIFF_EQUAL, `${pChar}`],
[DIFF_INSERT, `abc${pChar}`],
[DIFF_EQUAL, `${pChar}xyz`],
]
cleanUpNodeMarkers(diff)
expect(diff).toStrictEqual([
[DIFF_INSERT, `${pChar}abc`],
[DIFF_EQUAL, `${pChar}${pChar}xyz`],
])
})
})