summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/forms/the-selectlist-element/selectlist-value-selectedOption.tentative.html
blob: 20e72ac1dcb5549cb3c08a62652a14e99486111a (plain)
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
<!DOCTYPE html>
<title>HTMLSelectListElement Test: value and selectedOption</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<selectlist id="selectList0"></selectlist>

<selectlist id="selectList1">
  <option>one</option>
  <option>two</option>
  <div>I'm a div with no part attr</div>
  <option id="selectList1-option3">three</option>
  <option>four</option>
</selectlist>

<selectlist id="selectList2">
  <div behavior="option">one</div>
  <div behavior="option">two</div>
  <div>I'm a div with no part attr</div>
  <div behavior="option">three</div>
  <div behavior="option">four</div>
</selectlist>

<selectlist id="selectList3">
  <div>I'm a div with no part attr</div>
  <option id="selectList3-child1">one</option>
  <option id="selectList3-child2">two</option>
  <option id="selectList3-child3">three</option>
</selectlist>

<selectlist id="selectList4">
  <div slot="button" behavior="button">
    <div behavior="selected-value" id="selectList4-custom-selected-value">Default custom selected-value text</div>
  </div>
  <option id="selectList4-option1">one</option>
  <option id="selectList4-option2">two</option>
</selectlist>

<selectlist id="selectList5">
  <div slot="button" behavior="button">
    <div behavior="selected-value" id="selectList5-custom-selected-value">Default custom selected-value text</div>
  </div>
  <div popover slot="listbox" behavior="listbox">
    <option id="selectList5-option1">one</option>
    <option id="selectList5-option2">two</option>
  </div>
</selectlist>

<selectlist id="selectList6">
  <option id="selectList6-option1">one</option>
  <option id="selectList6-option2" selected>two</option>
  <option id="selectList6-option3">three</option>
</selectlist>

<selectlist id="selectList7">
  <option id="selectList7-option1">one</option>
  <option id="selectList7-option2" selected value="test">two</option>
  <option>three</option>
</selectlist>

<script>

test(() => {
  const selectList0 = document.getElementById("selectList0");
  assert_equals(selectList0.value, "");
  assert_equals(selectList0.selectedOption, null);
  selectList0.value = "something";
  assert_equals(selectList0.value, "", "If there is no matching option, selectlist should be cleared");
  assert_equals(selectList0.selectedOption, null);
}, "Test that HTMLSelectList with no options has empty string for value and null for selectedOption");

test(() => {
  const selectList1 = document.getElementById("selectList1");
  assert_equals(selectList1.value, "one", "value should start with the text of the first option part");

  selectList1.value = "three";
  assert_equals(selectList1.value, "three", "value can be set to the text of an option part");
  assert_equals(selectList1.selectedOption, document.getElementById("selectList1-option3"));

  selectList1.value = "I'm a div with no part attr";
  assert_equals(selectList1.value, "", "If there is no matching option selectlist should be cleared");
  assert_equals(selectList1.selectedOption, null);
}, "Test value and selectedOption with HTMLOptionElement element option parts");

test(() => {
  const selectList1 = document.getElementById("selectList1");
  selectList1.value = "one";
  assert_equals(selectList1.value, "one");

  selectList1.value = null;
  assert_equals(selectList1.value, "");
  assert_equals(selectList1.selectedOption, null);
}, "Test value and selectedOption when value is null");

test(() => {
  const selectList1 = document.getElementById("selectList1");
  selectList1.value = "one";
  assert_equals(selectList1.value, "one");

  selectList1.value = undefined;
  assert_equals(selectList1.value, "");
  assert_equals(selectList1.selectedOption, null);
}, "Test value and selectedOption when value is undefined");

test(() => {
  const selectList2 = document.getElementById("selectList2");
  assert_equals(selectList2.value, "", "Non-HTMLOptionElements shouldn't be treated as option parts");
  assert_equals(selectList2.selectedOption, null);

  selectList2.value = "three";
  assert_equals(selectList2.value, "", "value can't be set when there are no option parts'");
  assert_equals(selectList2.selectedOption, null);
}, "Test value with non-HTMLOptionElement elements labeled as parts");

test(() => {
  const selectList3 = document.getElementById("selectList3");
  assert_equals(selectList3.value, "one", "value should start with the text of the first option part");
  assert_equals(selectList3.selectedOption, document.getElementById("selectList3-child1"));

  document.getElementById("selectList3-child3").remove();
  assert_equals(selectList3.value, "one", "Removing a non-selected option should not change the value");
  assert_equals(selectList3.selectedOption, document.getElementById("selectList3-child1"));

  document.getElementById("selectList3-child1").remove();
  assert_equals(selectList3.value, "two", "When the selected option is removed, the new first option should become selected");
  assert_equals(selectList3.selectedOption, document.getElementById("selectList3-child2"));

  document.getElementById("selectList3-child2").remove();
  assert_equals(selectList3.value, "", "When all options are removed, value should be the empty string");
  assert_equals(selectList3.selectedOption, null);
}, "Test that value and selectedOption are updated when options are removed");

test(() => {
  const selectList4 = document.getElementById("selectList4");
  let customSelectedValuePart = document.getElementById("selectList4-custom-selected-value");
  assert_equals(selectList4.value, "one", "value should start with the text of the first option part");
  assert_equals(selectList4.selectedOption, document.getElementById("selectList4-option1"));
  assert_equals(customSelectedValuePart.innerText, "one", "Custom selected value part should be set to initial value of selectlist");

  selectList4.value = "two";
  assert_equals(customSelectedValuePart.innerText, "two", "Custom selected value part should be updated when value of selectlist changes");
  assert_equals(selectList4.selectedOption, document.getElementById("selectList4-option2"));
}, "Test that slotted-in selected-value part is updated to value of selectlist");

test(() => {
  const selectList5 = document.getElementById("selectList5");
  let customSelectedValuePart = document.getElementById("selectList5-custom-selected-value");
  assert_equals(selectList5.value, "one", "value should start with the text of the first option part");
  assert_equals(selectList5.selectedOption, document.getElementById("selectList5-option1"));
  assert_equals(customSelectedValuePart.innerText, "one", "Custom selected value part should be set to initial value of selectlist");

  selectList5.value = "two";
  assert_equals(customSelectedValuePart.innerText, "two", "Custom selected value part should be updated when value of selectlist changes");
  assert_equals(selectList5.selectedOption, document.getElementById("selectList5-option2"));
}, "Test that option parts in a slotted-in listbox are reflected in the value property");

test(() => {
  let selectList = document.createElement('selectlist');
  assert_equals(selectList.value, "");
  let option = document.createElement('option');
  option.innerText = "one";
  selectList.appendChild(option);
  assert_equals(selectList.value, "one");
  assert_equals(selectList.selectedOption, option);

  let newOption = document.createElement('option');
  newOption.innerText = 'two';
  selectList.appendChild(newOption);
  selectList.value = "two";
  assert_equals(selectList.value, "two");
  assert_equals(selectList.selectedOption, newOption);

  option.click();
  assert_equals(selectList.value, "one");
  assert_equals(selectList.selectedOption, option);
}, "Test that value and selectedOption are correctly updated");

test(() => {
  const selectList = document.getElementById("selectList6");
  let selectListOption1 = document.getElementById("selectList6-option1");

  assert_equals(selectList.value, "two");
  assert_equals(selectList.selectedOption, document.getElementById("selectList6-option2"));
  assert_false(selectListOption1.selected);
  selectListOption1.selected = true;
  assert_equals(selectList.value, "one");
  assert_equals(selectList.selectedOption, selectListOption1);

  let newOption = document.createElement("option");
  newOption.innerText = "four";
  newOption.selected = true;
  selectList.appendChild(newOption);
  assert_equals(selectList.value, "four");
  assert_equals(selectList.selectedOption, newOption);
  assert_false(selectListOption1.selected);

  selectList.value = "three";
  assert_equals(selectList.selectedOption, document.getElementById("selectList6-option3"));
  assert_false(newOption.selected);
}, "Test that HTMLOption.selected updates selectlist.value and selectlist.selectedOption");

test(() => {
  const selectList = document.getElementById("selectList7");
  let selectListOption1 = document.getElementById("selectList7-option1");

  assert_equals(selectList.value, "test");
  assert_equals(selectList.selectedOption, document.getElementById("selectList7-option2"));
  assert_false(selectListOption1.selected);
  selectListOption1.selected = true;
  assert_equals(selectList.value, "one");
  assert_equals(selectList.selectedOption, selectListOption1);

  selectListOption1.value = "new test";
  assert_equals(selectList.value, "new test");
  assert_equals(selectList.selectedOption, selectListOption1);
  selectListOption1.removeAttribute("value");
  assert_equals(selectList.value, "one");
  assert_equals(selectList.selectedOption, selectListOption1);
  selectListOption1.value = "";
  assert_equals(selectList.value, "");
  assert_equals(selectList.selectedOption, selectListOption1);
}, "Test that HTMLOption.value updates selectlist.value");

</script>