/** * Test if the nodes are inserted correctly in the tree.
*/ function testTreeItemInsertedCorrectly(tree, doc) {
is(
tree.root.children.children.length,
2, "Number of top level elements match"
);
is(
tree.root.children.firstChild.lastChild.children.length,
3, "Number of first second level elements match"
);
is(
tree.root.children.lastChild.lastChild.children.length,
1, "Number of second second level elements match"
);
ok(tree.root.items.has("level1"), "Level1 top level element exists");
is(
tree.root.children.firstChild.dataset.id,
JSON.stringify(["level1"]), "Data id of first top level element matches"
);
is(
tree.root.children.firstChild.firstChild.textContent, "Level 1", "Text content of first top level element matches"
);
ok(tree.root.items.has("level1.1"), "Level1.1 top level element exists");
is(
tree.root.children.firstChild.nextSibling.dataset.id,
JSON.stringify(["level1.1"]), "Data id of second top level element matches"
);
is(
tree.root.children.firstChild.nextSibling.firstChild.textContent, "level1.1", "Text content of second top level element matches"
);
// Adding a new non text item in the tree. const node = doc.createElement("div");
node.textContent = "Foo Bar";
node.className = "foo bar";
tree.add([
{
id: "level1.2",
node,
attachment: {
foo: "bar",
},
},
]);
is(
tree.root.children.children.length,
3, "Number of top level elements match after update"
);
ok(tree.root.items.has("level1.2"), "New level node got added");
ok(
tree.attachments.has(JSON.stringify(["level1.2"])), "Attachment is present for newly added node"
); // The item should be added before level1 and level 1.1 as lexical sorting
is(
tree.root.children.firstChild.dataset.id,
JSON.stringify(["level1.2"]), "Data id of last top level element matches"
);
is(
tree.root.children.firstChild.firstChild.firstChild,
node, "Newly added node is inserted at the right location"
);
}
/** * Populate the unsorted tree.
*/ function populateUnsortedTree(tree) {
tree.sorted = false;
/** * Test if the nodes are inserted correctly in the unsorted tree.
*/ function testUnsortedTreeItemInsertedCorrectly(tree) {
ok(tree.root.items.has("g-1"), "g-1 top level element exists");
is(
tree.root.children.firstChild.lastChild.children.length,
3, "Number of children for g-1 matches"
);
is(
tree.root.children.firstChild.dataset.id,
JSON.stringify(["g-1"]), "Data id of g-1 matches"
);
is(
tree.root.children.firstChild.firstChild.textContent, "g-1", "Text content of g-1 matches"
);
is(
tree.root.children.firstChild.lastChild.firstChild.dataset.id,
JSON.stringify(["g-1", "d-2"]), "Data id of d-2 matches"
);
is(
tree.root.children.firstChild.lastChild.firstChild.textContent, "d-2.1", "Text content of d-2 matches"
);
is(
tree.root.children.firstChild.lastChild.firstChild.nextSibling.textContent, "b-2.2", "Text content of b-2 matches"
);
is(
tree.root.children.firstChild.lastChild.lastChild.textContent, "a-2.3", "Text content of a-2 matches"
);
}
/** * Tests if the API exposed by TreeWidget works properly
*/ function testAPI(tree, doc) {
info("Testing TreeWidget API"); // Check if selectItem and selectedItem setter works as expected // Nothing should be selected beforehand
ok(!doc.querySelector(".theme-selected"), "Nothing is selected");
tree.selectItem(["level1"]); const node = doc.querySelector(".theme-selected");
ok(!!node, "Something got selected");
is(
node.parentNode.dataset.id,
JSON.stringify(["level1"]), "Correct node selected"
);
tree.selectItem(["level1", "level2"]); const node2 = doc.querySelector(".theme-selected");
ok(!!node2, "Something is still selected");
isnot(node, node2, "Newly selected node is different from previous");
is(
node2.parentNode.dataset.id,
JSON.stringify(["level1", "level2"]), "Correct node selected"
);
// test if selectedItem getter works
is(tree.selectedItem.length, 2, "Correct length of selected item");
is(tree.selectedItem[0], "level1", "Correct selected item");
is(tree.selectedItem[1], "level2", "Correct selected item");
// test if isSelected works
ok(tree.isSelected(["level1", "level2"]), "isSelected works");
tree.selectedItem = ["level1"]; const node3 = doc.querySelector(".theme-selected");
ok(!!node3, "Something is still selected");
isnot(node2, node3, "Newly selected node is different from previous");
is(node3, node, "First and third selected nodes should be same");
is(
node3.parentNode.dataset.id,
JSON.stringify(["level1"]), "Correct node selected"
);
// test if selectedItem getter works
is(tree.selectedItem.length, 1, "Correct length of selected item");
is(tree.selectedItem[0], "level1", "Correct selected item");
// test if clear selection works
tree.clearSelection();
ok(
!doc.querySelector(".theme-selected"), "Nothing selected after clear selection call"
);
// test if collapseAll/expandAll work
ok(!!doc.querySelectorAll("[expanded]").length, "Some nodes are expanded");
tree.collapseAll();
is(
doc.querySelectorAll("[expanded]").length,
0, "Nothing is expanded after collapseAll call"
);
tree.expandAll();
is(
doc.querySelectorAll("[expanded]").length,
13, "All tree items expanded after expandAll call"
);
// test if selectNextItem and selectPreviousItem work
tree.selectedItem = ["level1", "level2"];
ok(tree.isSelected(["level1", "level2"]), "Correct item selected");
tree.selectNextItem();
ok(
tree.isSelected(["level1", "level2", "level3"]), "Correct item selected after selectNextItem call"
);
tree.selectNextItem();
ok(
tree.isSelected(["level1", "level2-1"]), "Correct item selected after second selectNextItem call"
);
tree.selectNextItem();
ok(
tree.isSelected(["level1", "level2-1", "level3-1"]), "Correct item selected after third selectNextItem call"
);
// test if clearing the tree works
is(
doc.querySelectorAll("[level='1']").length,
3, "Correct number of top level items before clearing"
);
tree.clear();
is(
doc.querySelectorAll("[level='1']").length,
0, "No top level item after clearing the tree"
);
}
¤ Dauer der Verarbeitung: 0.1 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.