-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Text.test.ts
64 lines (57 loc) · 1.83 KB
/
Text.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
import Window from '../../../src/window/Window.js';
import Document from '../../../src/nodes/document/Document.js';
import DOMException from '../../../src/exception/DOMException.js';
import Text from '../../../src/nodes/text/Text.js';
import { beforeEach, describe, it, expect } from 'vitest';
describe('Text', () => {
let window: Window;
let document: Document;
beforeEach(() => {
window = new Window();
document = window.document;
});
describe('constructor()', () => {
it('Creates a new Text node.', () => {
const node = new window.Text('test');
expect(node).toBeInstanceOf(Text);
expect(node.data).toBe('test');
});
});
describe('get nodeName()', () => {
it('Returns "#text".', () => {
const node = document.createTextNode('test');
expect(node).toBeInstanceOf(Text);
expect(node.nodeName).toBe('#text');
});
});
describe('toString()', () => {
it('Returns "[object Text]".', () => {
const node = document.createTextNode('test');
expect(node.toString()).toBe('[object Text]');
});
});
describe('cloneNode()', () => {
it('Clones the node.', () => {
const node = document.createTextNode('test');
const clone = node.cloneNode();
expect(clone.data).toBe(node.data);
});
});
describe('splitText()', () => {
it('Splits the text node.', () => {
const node = document.createTextNode('test');
document.body.append(node);
const result = node.splitText(2);
expect(node.textContent).toBe('te');
expect(result).toBeInstanceOf(Text);
expect(result.textContent).toBe('st');
expect(node.nextSibling).toBe(result);
expect(result.previousSibling).toBe(node);
});
it('Throws on invalid index.', () => {
const node = document.createTextNode('test');
expect(() => node.splitText(-1)).toThrow(DOMException);
expect(() => node.splitText(5)).toThrow(DOMException);
});
});
});