This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.test.ts
241 lines (217 loc) · 7.82 KB
/
index.test.ts
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
import { JSDOM } from 'jsdom';
import MutationObserverApplier from './index';
import { getCSSStyleSheet } from './lib/stylesheets';
test('adds attributes', () => {
const initialDom = '<!doctype html><html><body><p>Hello world!</p></body></html>';
const dom = new JSDOM(initialDom);
const { window: { document } } = dom;
const mod = new MutationObserverApplier(dom.serialize());
const targetNode = document.querySelector('p');
targetNode.setAttribute('style', 'color: red;');
const mutations = [{
type: 'attributes',
target: targetNode,
addedNodes: [],
removedNodes: [],
previousSibling: null,
nextSibling: null,
attributeName: 'style',
attributeNamespace: null,
}];
const serializedMutations = mod.serializeMutations(mutations);
mod.applyMutations(serializedMutations);
expect(mod.DOM).toBe(dom.serialize());
window.close();
mod.tearDown();
});
test('edits attributes', () => {
const initialDom = '<!doctype html><html><body><p>Hello world!</p></body></html>';
const dom = new JSDOM(initialDom);
const { window: { document } } = dom;
const targetNode = document.querySelector('p');
targetNode.setAttribute('style', 'color: red;');
const mod = new MutationObserverApplier(dom.serialize());
targetNode.setAttribute('style', 'color: green;');
const mutations = [{
type: 'attributes',
target: targetNode,
addedNodes: [],
removedNodes: [],
previousSibling: null,
nextSibling: null,
attributeName: 'style',
attributeNamespace: null,
}];
const serializedMutations = mod.serializeMutations(mutations);
mod.applyMutations(serializedMutations);
expect(mod.DOM).toBe(dom.serialize());
window.close();
mod.tearDown();
});
test('removes attributes', () => {
const initialDom = '<!doctype html><html><body><p>Hello world!</p></body></html>';
const dom = new JSDOM(initialDom);
const { window: { document } } = dom;
const targetNode = document.querySelector('p');
targetNode.setAttribute('style', 'color: red;');
const mod = new MutationObserverApplier(dom.serialize());
targetNode.removeAttribute('style');
const mutations = [{
type: 'attributes',
target: targetNode,
addedNodes: [],
removedNodes: [],
previousSibling: null,
nextSibling: null,
attributeName: 'style',
attributeNamespace: null,
}];
const serializedMutations = mod.serializeMutations(mutations);
mod.applyMutations(serializedMutations);
expect(mod.DOM).toBe(dom.serialize());
window.close();
mod.tearDown();
});
test('modifies character data', () => {
const initialDom = '<!doctype html><html><body><h1>Welcome to the coolest website!</h1><p>Well, it\'s actually nothing more than another test website...</p>';
const dom = new JSDOM(initialDom);
const { window: { document } } = dom;
const targetNode = document.querySelector('p');
const mod = new MutationObserverApplier(dom.serialize());
targetNode.firstChild.nodeValue = 'Well, it is actually just another test website... Don\'t act so surprised!';
const mutations = [{
type: 'characterData',
target: targetNode.firstChild,
addedNodes: [],
removedNodes: [],
previousSibling: null,
nextSibling: null,
attributeName: null,
attributeNamespace: null,
}];
const serializedMutations = mod.serializeMutations(mutations);
mod.applyMutations(serializedMutations);
expect(mod.DOM).toBe(dom.serialize());
window.close();
mod.tearDown();
});
test('adds elements to the DOM tree', () => {
const initialDom = '<!doctype html><html><head><title>Cool Website</title></head><body><h1>Shopping List</h1><ul><li>Coffee</li><li>Tea</li><li>Milk</li></ul></body></html>';
const dom = new JSDOM(initialDom);
const { window: { document } } = dom;
const targetNode = document.querySelector('ul');
const mod = new MutationObserverApplier(dom.serialize());
const addedNode = document.createElement('li');
const addedTextNode = document.createTextNode('Chocolate');
addedNode.appendChild(addedTextNode);
targetNode.appendChild(addedNode);
const mutations = [
{
type: 'childList',
target: targetNode,
addedNodes: [addedNode],
removedNodes: [],
previousSibling: addedNode.previousSibling,
nextSibling: addedNode.nextSibling,
attributeName: null,
attributeNamespace: null,
},
{
type: 'childList',
target: addedNode,
addedNodes: [addedTextNode],
removedNodes: [],
previousSibling: addedTextNode.previousSibling,
nextSibling: addedTextNode.nextSibling,
attributeName: null,
attributeNamespace: null,
}
];
const serializedMutations = mod.serializeMutations(mutations);
mod.applyMutations(serializedMutations);
expect(mod.DOM).toBe(dom.serialize());
window.close();
mod.tearDown();
});
test('removes elements from the DOM tree', () => {
const initialDom = '<!doctype html><html><head><title>Cool Website</title></head><body><h1>Shopping List</h1><ul><li>Coffee</li><li>Tea</li><li>Milk</li></ul></body></html>';
const dom = new JSDOM(initialDom);
const { window: { document } } = dom;
const targetNode = document.querySelector('ul');
const mod = new MutationObserverApplier(dom.serialize());
const removedNode = targetNode.childNodes[1];
const previousSibling = removedNode.previousSibling;
const nextSibling = removedNode.nextSibling;
targetNode.removeChild(removedNode);
const mutations = [{
type: 'childList',
target: targetNode,
addedNodes: [],
removedNodes: [removedNode],
previousSibling,
nextSibling,
attributeName: null,
attributeNamespace: null,
}];
const serializedMutations = mod.serializeMutations(mutations);
mod.applyMutations(serializedMutations);
expect(mod.DOM).toBe(dom.serialize());
window.close();
mod.tearDown();
});
test('adds stylesheets', () => {
const initialDom = '<!doctype html><html><body><p>Hello world!</p></body></html>';
const dom = new JSDOM(initialDom);
const { window: { document } } = dom;
const mod = new MutationObserverApplier(dom.serialize());
const targetNode = document.querySelector('p');
const styleElement = document.createElement('style');
document.head.appendChild(styleElement);
const styleSheet = styleElement.sheet;
styleSheet.insertRule('p { color: red; }', 0);
const mutations = [{
type: 'childList',
target: document.head,
addedNodes: [styleElement],
removedNodes: [],
previousSibling: styleElement.previousSibling,
nextSibling: styleElement.nextSibling,
attributeName: null,
attributeNamespace: null,
}];
const serializedMutations = mod.serializeMutations(mutations);
mod.applyMutations(serializedMutations);
expect(mod.styleSheets).toStrictEqual([getCSSStyleSheet(styleElement)]);
expect(mod.DOM).toBe(dom.serialize());
window.close();
mod.tearDown();
});
test('removes stylesheets', () => {
const initialDom = '<!doctype html><html><body><p>Hello world!</p></body></html>';
const dom = new JSDOM(initialDom);
const { window: { document } } = dom;
const targetNode = document.querySelector('p');
const styleElement = document.createElement('style');
document.head.appendChild(styleElement);
const styleSheet = styleElement.sheet;
styleSheet.insertRule('p { color: red; }', 0);
const mod = new MutationObserverApplier(dom.serialize());
mod.styleSheets = [getCSSStyleSheet(styleElement)];
const mutations = [{
type: 'childList',
target: document.head,
addedNodes: [],
removedNodes: [styleElement],
previousSibling: styleElement.previousSibling,
nextSibling: styleElement.nextSibling,
attributeName: null,
attributeNamespace: null,
}];
const serializedMutations = mod.serializeMutations(mutations);
mod.applyMutations(serializedMutations);
expect(mod.styleSheets).toStrictEqual([]);
styleElement.remove();
expect(mod.DOM).toBe(dom.serialize());
window.close();
mod.tearDown();
});