forked from capricorn86/happy-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLOptionElement.test.ts
117 lines (90 loc) · 3.36 KB
/
HTMLOptionElement.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
import Window from '../../../src/window/Window';
import Document from '../../../src/nodes/document/Document';
import IHTMLOptionElement from '../../../src/nodes/html-option-element/IHTMLOptionElement';
import IHTMLSelectElement from '../../../src/nodes/html-select-element/IHTMLSelectElement';
describe('HTMLOptionElement', () => {
let window: Window;
let document: Document;
let element: IHTMLOptionElement;
beforeEach(() => {
window = new Window();
document = window.document;
element = <IHTMLOptionElement>document.createElement('option');
});
describe('Object.prototype.toString', () => {
it('Returns `[object HTMLOptionElement]`', () => {
expect(Object.prototype.toString.call(element)).toBe('[object HTMLOptionElement]');
});
});
describe('get value()', () => {
it('Returns the attribute "value".', () => {
element.setAttribute('value', 'VALUE');
expect(element.value).toBe('VALUE');
});
it('Returns the text IDL value if no attribute is present.', () => {
element.removeAttribute('value');
element.textContent = 'TEXT VALUE';
expect(element.value).toBe('TEXT VALUE');
});
});
describe('set value()', () => {
it('Sets the attribute "value".', () => {
element.value = 'VALUE';
expect(element.getAttribute('value')).toBe('VALUE');
});
});
describe('get disabled()', () => {
it('Returns the attribute "disabled".', () => {
element.setAttribute('disabled', '');
expect(element.disabled).toBe(true);
});
});
describe('set disabled()', () => {
it('Sets the attribute "disabled".', () => {
element.disabled = true;
expect(element.getAttribute('disabled')).toBe('');
});
});
describe('get selected()', () => {
it('Returns the selected state of the option.', () => {
const select = <IHTMLSelectElement>document.createElement('select');
const option1 = <IHTMLOptionElement>document.createElement('option');
const option2 = <IHTMLOptionElement>document.createElement('option');
expect(option1.selected).toBe(false);
expect(option2.selected).toBe(false);
select.appendChild(option1);
select.appendChild(option2);
expect(option1.selected).toBe(true);
expect(option2.selected).toBe(false);
expect(option1.getAttribute('selected')).toBe(null);
expect(option2.getAttribute('selected')).toBe(null);
select.options.selectedIndex = 1;
expect(option1.selected).toBe(false);
expect(option2.selected).toBe(true);
expect(option1.getAttribute('selected')).toBe(null);
expect(option2.getAttribute('selected')).toBe(null);
select.options.selectedIndex = -1;
expect(option1.selected).toBe(false);
expect(option2.selected).toBe(false);
});
});
describe('set selected()', () => {
it('Sets the selected state of the option.', () => {
const select = <IHTMLSelectElement>document.createElement('select');
const option1 = <IHTMLOptionElement>document.createElement('option');
const option2 = <IHTMLOptionElement>document.createElement('option');
expect(option1.selected).toBe(false);
expect(option2.selected).toBe(false);
option1.selected = true;
expect(select.selectedIndex).toBe(-1);
select.appendChild(option1);
select.appendChild(option2);
option1.selected = true;
expect(select.selectedIndex).toBe(0);
option2.selected = true;
expect(select.selectedIndex).toBe(1);
option2.selected = false;
expect(select.selectedIndex).toBe(0);
});
});
});