-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
HTMLScriptElement.test.ts
216 lines (175 loc) · 7.04 KB
/
HTMLScriptElement.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
import Window from '../../../src/window/Window';
import Document from '../../../src/nodes/document/Document';
import HTMLScriptElement from '../../../src/nodes/html-script-element/HTMLScriptElement';
import ScriptUtility from '../../../src/nodes/html-script-element/ScriptUtility';
describe('HTMLScriptElement', () => {
let window: Window;
let document: Document;
beforeEach(() => {
window = new Window();
document = window.document;
});
afterEach(() => {
jest.restoreAllMocks();
});
for (const property of ['type', 'charset', 'lang']) {
describe(`get ${property}()`, () => {
it(`Returns the "${property}" attribute.`, () => {
const element = document.createElement('script');
element.setAttribute(property, 'test');
expect(element[property]).toBe('test');
});
});
describe(`set ${property}()`, () => {
it(`Sets the attribute "${property}".`, () => {
const element = document.createElement('script');
element[property] = 'test';
expect(element.getAttribute(property)).toBe('test');
});
});
}
for (const property of ['async', 'defer']) {
describe(`get ${property}()`, () => {
it(`Returns "true" if the "${property}" attribute is defined.`, () => {
const element = document.createElement('script');
element.setAttribute(property, '');
expect(element[property]).toBe(true);
});
});
describe(`set ${property}()`, () => {
it(`Sets the "${property}" attribute to an empty string if set to "true".`, () => {
const element = document.createElement('script');
element[property] = true;
expect(element.getAttribute(property)).toBe('');
});
});
}
describe('get src()', () => {
it('Returns the "src" attribute.', () => {
const element = <HTMLScriptElement>document.createElement('script');
element.setAttribute('src', 'test');
expect(element.src).toBe('test');
});
});
describe('set src()', () => {
it('Sets the attribute "src".', () => {
const element = <HTMLScriptElement>document.createElement('script');
element.src = 'test';
expect(element.getAttribute('src')).toBe('test');
});
it('Loads and evaluates an external script when the attribute "src" is set and the element is connected to DOM.', () => {
const element = <HTMLScriptElement>document.createElement('script');
let loadedElement = null;
jest.spyOn(ScriptUtility, 'loadExternalScript').mockImplementation(async (element) => {
loadedElement = element;
});
document.body.appendChild(element);
element.async = true;
element.src = 'test';
expect(loadedElement).toBe(element);
});
it('Does not evaluate script if the element is not connected to DOM.', () => {
const element = <HTMLScriptElement>document.createElement('script');
let loadedElement = null;
jest.spyOn(ScriptUtility, 'loadExternalScript').mockImplementation(async (element) => {
loadedElement = element;
});
element.async = true;
element.src = 'test';
expect(loadedElement).toBe(null);
});
});
describe('get text()', () => {
it('Returns the data of text nodes.', () => {
const element = <HTMLScriptElement>document.createElement('script');
const text = document.createTextNode('test');
element.appendChild(text);
expect(element.text).toBe('test');
});
it('Replaces all child nodes with a text node.', () => {
const element = <HTMLScriptElement>document.createElement('script');
const text = document.createTextNode('test');
element.appendChild(text);
element.text = 'test2';
expect(element.text).toBe('test2');
});
});
describe('set isConnected()', () => {
it('Evaluates the text content as code when appended to an element that is connected to the document.', () => {
const element = <HTMLScriptElement>document.createElement('script');
element.text = 'globalThis.test = "test";';
document.body.appendChild(element);
expect(window['test']).toBe('test');
});
it('Evaluates the text content as code when inserted before an element that is connected to the document.', () => {
const element = <HTMLScriptElement>document.createElement('script');
const div1 = document.createElement('div');
const div2 = document.createElement('div');
element.text = 'globalThis.test = "test";';
div1.appendChild(element);
document.body.appendChild(div2);
document.body.insertBefore(div1, div2);
document.body.appendChild(element);
expect(window['test']).toBe('test');
});
it('Does not evaluate types that are not supported.', () => {
const div = document.createElement('div');
const element = <HTMLScriptElement>document.createElement('script');
element.type = 'application/json';
element.textContent = '{"key": "value"}';
div.appendChild(element);
expect(element.textContent).toBe('{"key": "value"}');
});
it('Does not evaluate code when added as innerHTML.', () => {
const div = document.createElement('div');
div.innerHTML = '<script>globalThis.test = "test";</script>';
document.body.appendChild(div);
expect(window['test']).toBe(undefined);
});
it('Does not evaluate code when added as outerHTML.', () => {
const div = document.createElement('div');
document.body.appendChild(div);
div.outerHTML = '<script>globalThis.test = "test";</script>';
expect(window['test']).toBe(undefined);
});
it('Does not evaluate code if the element is not connected to DOM.', () => {
const div = document.createElement('div');
const element = <HTMLScriptElement>document.createElement('script');
element.text = 'window.test = "test";';
div.appendChild(element);
expect(window['test']).toBe(undefined);
});
it('Evaluates the text content as code when using document.write().', () => {
document.write('<script>globalThis.test = "test";</script>');
expect(window['test']).toBe('test');
});
it('Evaluates the text content as code when using DOMParser.parseFromString().', () => {
const domParser = new window.DOMParser();
domParser.parseFromString('<script>globalThis.test = "test";</script>', 'text/html');
expect(window['test']).toBe('test');
});
it('Loads and evaluates an external script when "src" attribute has been set, but does not evaluate text content.', () => {
const element = <HTMLScriptElement>document.createElement('script');
let loadedElement = null;
jest.spyOn(ScriptUtility, 'loadExternalScript').mockImplementation(async (element) => {
loadedElement = element;
});
element.src = 'test';
element.text = 'globalThis.test = "test";';
document.body.appendChild(element);
expect(window['test']).toBe(undefined);
expect(loadedElement).toBe(element);
});
it('Does not load external scripts when "src" attribute has been set if the element is not connected to DOM.', () => {
const element = <HTMLScriptElement>document.createElement('script');
let loadedElement = null;
jest.spyOn(ScriptUtility, 'loadExternalScript').mockImplementation(async (element) => {
loadedElement = element;
});
element.src = 'test';
element.text = 'globalThis.test = "test";';
expect(window['test']).toBe(undefined);
expect(loadedElement).toBe(null);
});
});
});