fix existing test expectations and add new tests for HTMLOptionsCollection

This commit is contained in:
Alexandrov Sergey 2016-09-22 21:12:15 +03:00
parent cf284cc035
commit 2173545cdb
8 changed files with 128 additions and 149 deletions

View file

@ -0,0 +1,89 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title id='title'>HTMLOptionsCollection</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<div id="log"></div>
<select id="selly">
<option id="id1" name="name1">1</option>
<option id="id2" name="name2">2</option>
<option id="id3" name="name3">3</option>
<option id="id4" name="name4">4</option>
<optgroup id="og1">
<option name="nameonly">n1</option>
<option id="id5">5</option>
</optgroup>
<optgroup id="og2">
<option name="nameonly">n2</option>
<option id="id6">6</option>
</optgroup>
</select>
<script>
var selly;
setup(function() {
selly = document.getElementById('selly');
});
test(function () {
var option = document.getElementById('id1');
var optgroup = document.getElementById('og1');
selly.options.add(option, option);
selly.options.add(optgroup, optgroup);
assert_equals(selly.children.length, 6);
assert_equals(selly.length, 8);
}, "if before and node are the same element nothing should be done");
test(function () {
var o1 = document.createElement("option");
o1.value = "a";
var o2 = document.createElement("option");
o2.value = "b";
var o3 = document.createElement("option");
o3.value = "c";
var optgroup = document.getElementById('og1');
selly.options.add(o1, null);
selly.options.add(o2, optgroup);
selly.options.add(o3, 0);
var elarray = [];
for (var i = 0; i < selly.length; i++) {
elarray.push(selly[i].value);
}
assert_array_equals(elarray, ["c", "1", "2", "3", "4", "b", "n1", "5", "n2", "6", "a"]);
}, "add method should add option elements correctly");
test(function () {
var og1 = document.createElement("optgroup");
var o1 = document.createElement("option");
o1.value = "a";
o1.appendChild(og1);
var og2 = document.createElement("optgroup");
var o2 = document.createElement("option");
o2.value = "b";
o2.appendChild(og2);
var og3 = document.createElement("optgroup");
var o3 = document.createElement("option");
o3.value = "c";
o3.appendChild(og3);
var optgroup = document.getElementById('og1');
selly.options.add(og1, null);
selly.options.add(og2, optgroup);
selly.options.add(og3, 0);
var elarray = [];
for (var i = 0; i < selly.length; i++) {
elarray.push(selly[i].value);
}
assert_array_equals(elarray, ["c", "1", "2", "3", "4", "b", "n1", "5", "n2", "6", "a"]);
}, "add method should add option groups correctly");
</script>
</body>
</html>

View file

@ -67,4 +67,33 @@ test(function () {
assert_equals(selly.children.length, 4,
"Number of children should have changed");
}, "Setting a length lower than the old length trims nodes from the end");
test(function () {
var opts = selly.options;
opts[3] = null;
assert_equals(selly[3], undefined,
"previously set node is now undefined");
assert_equals(selly.length, 3,
"Number of nodes in collection is correctly changed");
assert_equals(selly.children.length, 3,
"Number of children should have changed");
}, "Setting element to null by index removed the element");
test(function () {
var opts = selly.options;
var new_option = document.createElement("option");
var replace_option = new_option.cloneNode(true);
new_option.value = "-1";
replace_option.value = "a";
opts[5] = new_option;
opts[0] = replace_option;
var elarray = [];
for (var i = 0; i < selly.length; i++) {
elarray.push(selly[i].value);
}
assert_array_equals(elarray, ["a", "2", "3", "", "", "-1"]);
}, "Setting element by index should correctly append and replace elements");
</script>