-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathconfig.test.ts
More file actions
166 lines (162 loc) · 6 KB
/
config.test.ts
File metadata and controls
166 lines (162 loc) · 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
import { Diff } from 'diff-match-patch'
import { JSDOM } from 'jsdom'
import { optionsToConfig } from './config'
import { diffText, isComment, isDocumentFragment, isText } from './util'
const document = new JSDOM('').window.document
const text = document.createTextNode('text')
const span = document.createElement('SPAN')
const div = document.createElement('DIV')
const video = document.createElement('VIDEO')
const comment = document.createComment('comment')
const fragment = document.createDocumentFragment()
describe('skipChildren', () => {
describe('without options', () => {
const config = optionsToConfig()
test('return true given a text node', () => {
expect(config.skipChildren(text)).toBe(true)
})
test('return true given a comment node', () => {
expect(config.skipChildren(comment)).toBe(true)
})
test('return false given a SPAN', () => {
expect(config.skipChildren(span)).toBe(false)
})
test('return true given a VIDEO', () => {
expect(config.skipChildren(video)).toBe(true)
})
test('return false given a DIV', () => {
expect(config.skipChildren(div)).toBe(false)
})
test('return false given a document fragment', () => {
expect(config.skipChildren(fragment)).toBe(false)
})
test('return false given a document', () => {
expect(config.skipChildren(document)).toBe(false)
})
})
describe('with options', () => {
const config = optionsToConfig({
skipChildren(node: Node): boolean | undefined {
return node.nodeName === 'SPAN'
? true
: node.nodeName === 'VIDEO'
? false
: isText(node) || isComment(node)
? false
: isDocumentFragment(node)
? true
: undefined
},
})
test('return true given a text node', () => {
expect(config.skipChildren(text)).toBe(true)
})
test('return true given a comment node', () => {
expect(config.skipChildren(comment)).toBe(true)
})
test('return true given a SPAN', () => {
expect(config.skipChildren(span)).toBe(true)
})
test('return false given a VIDEO', () => {
expect(config.skipChildren(video)).toBe(false)
})
test('return false given a DIV', () => {
expect(config.skipChildren(div)).toBe(false)
})
test('return true given a document fragment', () => {
expect(config.skipChildren(fragment)).toBe(true)
})
test('return false given a document', () => {
expect(config.skipChildren(document)).toBe(false)
})
})
})
describe('skipSelf', () => {
describe('without options', () => {
const config = optionsToConfig()
test('return false given a text node', () => {
expect(config.skipSelf(text)).toBe(false)
})
test('return true given a comment node', () => {
expect(config.skipSelf(comment)).toBe(true)
})
test('return true given a SPAN', () => {
expect(config.skipSelf(span)).toBe(true)
})
test('return false given a VIDEO', () => {
expect(config.skipSelf(video)).toBe(false)
})
test('return false given a DIV', () => {
expect(config.skipSelf(div)).toBe(false)
})
test('return true given a document fragment', () => {
expect(config.skipSelf(fragment)).toBe(true)
})
})
describe('with options', () => {
const config = optionsToConfig({
skipSelf(node: Node): boolean | undefined {
return isText(node)
? true
: isComment(node)
? false
: isDocumentFragment(node)
? false
: node.nodeName === 'SPAN'
? false
: node.nodeName === 'VIDEO'
? true
: undefined
},
})
test('return true given a text node', () => {
expect(config.skipSelf(text)).toBe(true)
})
test('return true given a comment node', () => {
expect(config.skipSelf(comment)).toBe(true)
})
test('return false given a SPAN', () => {
expect(config.skipSelf(span)).toBe(false)
})
test('return true given a VIDEO', () => {
expect(config.skipSelf(video)).toBe(true)
})
test('return false given a DIV', () => {
expect(config.skipSelf(div)).toBe(false)
})
test('return true given a document fragment', () => {
expect(config.skipSelf(fragment)).toBe(true)
})
})
})
describe('simple options', () => {
test('default', () => {
const config = optionsToConfig()
expect(config.addedClass).toBe('vdd-added')
expect(config.diffText).toBe(diffText)
expect(config.modifiedClass).toBe('vdd-modified')
expect(config.removedClass).toBe('vdd-removed')
expect(config.skipModified).toBe(false)
expect(config.ignoreAttributes).toBe(false)
})
test('override', () => {
const customDiffText = (
_oldText: string,
_newText: string,
): Diff[] => []
const config = optionsToConfig({
addedClass: 'ADDED',
diffText: customDiffText,
ignoreAttributes: true,
modifiedClass: 'MODIFIED',
removedClass: 'REMOVED',
skipModified: true,
})
expect(config.addedClass).toBe('ADDED')
expect(config.diffText).toBe(customDiffText)
expect(config.ignoreAttributes).toBe(true)
expect(config.modifiedClass).toBe('MODIFIED')
expect(config.removedClass).toBe('REMOVED')
expect(config.skipModified).toBe(true)
})
})