| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 |
"http://www.w3.org/TR/html4/strict.dtd"> |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
<html lang="en"> |
| 11 |
<head> |
| 12 |
<title>dijit.Tree against dijit.tree.ForestStoreModel Automated Tests</title> |
| 13 |
|
| 14 |
<style type="text/css"> |
| 15 |
@import "../../../dojo/resources/dnd.css"; |
| 16 |
@import "../../../dojo/tests/dnd/dndDefault.css"; |
| 17 |
</style> |
| 18 |
|
| 19 |
<script type="text/javascript" src="../boilerplate.js"></script> |
| 20 |
|
| 21 |
<script type="text/javascript"> |
| 22 |
require([ |
| 23 |
"doh/runner", |
| 24 |
"dojo/data/ItemFileWriteStore", |
| 25 |
"dojo/dom", |
| 26 |
"dojo/dom-attr", |
| 27 |
"dojo/dom-class", |
| 28 |
"dijit/Tree", |
| 29 |
"dijit/tree/ForestStoreModel", |
| 30 |
"dijit/tree/TreeStoreModel", |
| 31 |
"dijit/tree/dndSource", |
| 32 |
"dijit/tests/helpers", |
| 33 |
"dojo/domReady!" |
| 34 |
], function(doh, ItemFileWriteStore, dom, domAttr, domClass, Tree, ForestStoreModel, TreeStoreModel, dndSource, helpers){ |
| 35 |
|
| 36 |
doh.register("setup", function(){ |
| 37 |
myStore = new ItemFileWriteStore({url:'../_data/countries.json'}); |
| 38 |
doh.t(myStore, "store created"); |
| 39 |
|
| 40 |
myModel = new ForestStoreModel({ |
| 41 |
store: myStore, |
| 42 |
query: {type:'continent'}, |
| 43 |
rootId: "earth", |
| 44 |
rootLabel: "Earth", |
| 45 |
childrenAttrs: ["children"] |
| 46 |
}); |
| 47 |
doh.t(myModel, "model created"); |
| 48 |
}); |
| 49 |
doh.register("paths", [ |
| 50 |
function create(){ |
| 51 |
var d = new doh.Deferred(); |
| 52 |
|
| 53 |
myTree = new Tree({ |
| 54 |
id: "myTree", |
| 55 |
model: myModel, |
| 56 |
persist: false, // persist==true is too hard to test |
| 57 |
dndController: dndSource |
| 58 |
}); |
| 59 |
doh.t(myTree, "tree created"); |
| 60 |
|
| 61 |
dom.byId("container").appendChild(myTree.domNode); |
| 62 |
myTree.startup(); |
| 63 |
|
| 64 |
myTree.onLoadDeferred.then(d.getTestCallback(function(){ |
| 65 |
// Give the tree time to load, and the do checks that it |
| 66 |
// loaded correctly |
| 67 |
doh.t(myTree.rootNode, "root node exists"); |
| 68 |
doh.t(myTree.rootNode.isExpanded, "root node is expanded"); |
| 69 |
|
| 70 |
var children = myTree.rootNode.getChildren(); |
| 71 |
doh.is(6, children.length, "six children"); |
| 72 |
doh.is("Africa", children[0].label, "first child"); |
| 73 |
doh.f(children[0].isExpanded, "first child not expanded"); |
| 74 |
doh.is("South America", children[5].label, "last child"); |
| 75 |
|
| 76 |
// Last child has special CSS for drawing the grid lines |
| 77 |
doh.f(domClass.contains(children[3].domNode, "dijitTreeIsLast"), "middle node doesn't have dijitTreeIsLast"); |
| 78 |
doh.t(domClass.contains(children[5].domNode, "dijitTreeIsLast"), "last node has dijitTreeIsLast"); |
| 79 |
})); |
| 80 |
return d; |
| 81 |
}, |
| 82 |
|
| 83 |
{ |
| 84 |
name: "createWithPath", |
| 85 |
timeout: 10000, |
| 86 |
runTest: function(){ |
| 87 |
var d = new doh.Deferred(); |
| 88 |
myTree2 = new Tree({ |
| 89 |
id: "myTree2", |
| 90 |
model: myModel, |
| 91 |
persist: false, // persist==true is too hard to test |
| 92 |
dndController: dndSource, |
| 93 |
path: ["earth", "EU", "IT"] |
| 94 |
}); |
| 95 |
doh.t(myTree2, "myTree2 created"); |
| 96 |
|
| 97 |
dom.byId("container2").appendChild(myTree2.domNode); |
| 98 |
myTree2.startup(); |
| 99 |
|
| 100 |
myTree2.onLoadDeferred.then(d.getTestCallback(function(){ |
| 101 |
doh.t(myTree2.rootNode, "root node exists"); |
| 102 |
doh.t(myTree2.rootNode.isExpanded, "root node is expanded"); |
| 103 |
doh.t(myTree2.rootNode.getChildren()[3].isExpanded, "europe node is expanded"); |
| 104 |
doh.is(myTree2.rootNode.getChildren()[3].getChildren()[3], myTree2.selectedNode, "selected correct node"); |
| 105 |
})); |
| 106 |
|
| 107 |
return d; |
| 108 |
} |
| 109 |
}, |
| 110 |
function copyPath(){ |
| 111 |
var d = new doh.Deferred(); |
| 112 |
|
| 113 |
myTree.set("path", myTree2.get("path")).then(d.getTestCallback(function(){ |
| 114 |
doh.t(myTree.rootNode.isExpanded, "root node is expanded"); |
| 115 |
doh.t(myTree.rootNode.getChildren()[3].isExpanded, "europe node is expanded"); |
| 116 |
doh.is(myTree.rootNode.getChildren()[3].getChildren()[3], myTree.get("selectedNode"), "selected correct node"); |
| 117 |
})); |
| 118 |
|
| 119 |
return d; |
| 120 |
}, |
| 121 |
|
| 122 |
{ |
| 123 |
name: "copyPathByIds", |
| 124 |
timeout: 5000, |
| 125 |
runTest: function(){ |
| 126 |
var d = new doh.Deferred(); |
| 127 |
|
| 128 |
myTree.set("path", ["earth", "NA", "CA", "Ottawa"]).then(d.getTestErrback(function(){ |
| 129 |
var path = dojo.map(myTree.get("path"), function(item){ return myTree.model.getIdentity(item); }); |
| 130 |
doh.is(["earth", "NA", "CA", "Ottawa"], path, "path got set on myTree"); |
| 131 |
|
| 132 |
myTree2.set("path", path).then(d.getTestCallback(function(){ |
| 133 |
doh.t(myTree2.rootNode.isExpanded, "root node is expanded"); |
| 134 |
doh.t(myTree2.rootNode.getChildren()[4].isExpanded, "north america node is expanded"); |
| 135 |
doh.t(myTree2.rootNode.getChildren()[4].getChildren()[1].isExpanded, "canada node is expanded"); |
| 136 |
doh.is(myTree2.rootNode.getChildren()[4].getChildren()[1].getChildren()[0], myTree2.get("selectedNode"), "selected correct node"); |
| 137 |
})); |
| 138 |
})); |
| 139 |
|
| 140 |
return d; |
| 141 |
} |
| 142 |
}, |
| 143 |
|
| 144 |
function setPathToNull(){ |
| 145 |
var d = new doh.Deferred(); |
| 146 |
|
| 147 |
myTree2.set("path", []).then(d.getTestCallback(function(){ |
| 148 |
doh.is(null, myTree2.get("selectedNode"), "no selected node"); |
| 149 |
})); |
| 150 |
return d; |
| 151 |
}, |
| 152 |
|
| 153 |
function setPathToRoot(){ |
| 154 |
var d = new doh.Deferred(); |
| 155 |
|
| 156 |
myTree2.set("path", ["earth"]).then(d.getTestCallback(function(){ |
| 157 |
doh.is(myTree2.rootNode, myTree2.get("selectedNode"), "selected root node"); |
| 158 |
})); |
| 159 |
return d; |
| 160 |
}, |
| 161 |
|
| 162 |
{ |
| 163 |
name: "setPaths", |
| 164 |
timeout: 5000, |
| 165 |
runTest: function() { |
| 166 |
var d = new doh.Deferred(); |
| 167 |
|
| 168 |
myTree2.set("paths", [ |
| 169 |
["earth", "AF", "KE", "Nairobi"], |
| 170 |
["earth", "NA", "MX", "Guadalajara"] |
| 171 |
]).then(d.getTestCallback(function () { |
| 172 |
var ids = dojo.map(myTree2.selectedItems, function (x) { |
| 173 |
return myTree2.model.getIdentity(x); |
| 174 |
}).sort(); |
| 175 |
doh.is(["Guadalajara", "Nairobi"], ids); |
| 176 |
})); |
| 177 |
return d; |
| 178 |
} |
| 179 |
} |
| 180 |
]); |
| 181 |
|
| 182 |
doh.register("data store", [ |
| 183 |
function itemUpdate(){ |
| 184 |
// Test that Tree noticed when data store items change, and updates accordingly |
| 185 |
|
| 186 |
var item = myTree.rootNode.getChildren()[3].item; |
| 187 |
myStore.setValue(item, "name", "EU"); |
| 188 |
|
| 189 |
doh.is("EU", helpers.innerText(myTree.rootNode.getChildren()[3].labelNode), "label changed"); |
| 190 |
}, |
| 191 |
|
| 192 |
function topLevelItemDelete(){ |
| 193 |
// Delete a top level item. ForestStoreModel needs to realize that the top level |
| 194 |
// children have changed and notify Tree |
| 195 |
|
| 196 |
// Remove "South America" |
| 197 |
var item = myTree.rootNode.getChildren()[5].item; |
| 198 |
myStore.deleteItem(item); |
| 199 |
|
| 200 |
var children = myTree.rootNode.getChildren(); |
| 201 |
doh.is(5, children.length, "five children"); |
| 202 |
doh.is("North America", children[4].label, "last child"); |
| 203 |
doh.t(domClass.contains(children[4].domNode, "dijitTreeIsLast"), |
| 204 |
"North america has become the last node so it gets the CSS class for that"); |
| 205 |
}, |
| 206 |
|
| 207 |
function openANode(){ |
| 208 |
var d = new doh.Deferred(); |
| 209 |
|
| 210 |
var asia = myTree.rootNode.getChildren()[1]; |
| 211 |
|
| 212 |
doh.is(0, asia.getChildren().length, "no children loaded yet"); |
| 213 |
|
| 214 |
myTree._onExpandoClick({node: asia}); |
| 215 |
|
| 216 |
setTimeout(d.getTestCallback(function(){ |
| 217 |
// Give the tree time to load the children, and the do checks that it |
| 218 |
// loaded correctly |
| 219 |
|
| 220 |
var children = asia.getChildren(); |
| 221 |
doh.is(4, children.length, "four children"); |
| 222 |
doh.is("China", children[0].label, "first child"); |
| 223 |
doh.is("Mongolia", children[3].label, "last child"); |
| 224 |
}), 750); |
| 225 |
return d; |
| 226 |
}, |
| 227 |
|
| 228 |
function selectAnItem(){ |
| 229 |
myTree.set('selectedItem','CN'); |
| 230 |
doh.is('CN',myTree.model.getIdentity(myTree.get('selectedItem'))); |
| 231 |
}, |
| 232 |
|
| 233 |
function nestedItemDelete(){ |
| 234 |
// Delete a nested item |
| 235 |
|
| 236 |
// Remove "China" |
| 237 |
var asia = myTree.rootNode.getChildren()[1], |
| 238 |
china = asia.getChildren()[0]; |
| 239 |
myStore.deleteItem(china.item); |
| 240 |
|
| 241 |
var children = asia.getChildren(); |
| 242 |
doh.is(3, children.length, "three children"); |
| 243 |
}, |
| 244 |
|
| 245 |
function topLevelItemInsert(){ |
| 246 |
// Create a new top level item as last child. |
| 247 |
// ForestStoreModel needs to realize that the top level children have changed and notify Tree. |
| 248 |
|
| 249 |
myStore.newItem({ |
| 250 |
id: 'PA', |
| 251 |
name:'Pacifica', |
| 252 |
type:'continent', |
| 253 |
children: [] |
| 254 |
}); |
| 255 |
|
| 256 |
var children = myTree.rootNode.getChildren(); |
| 257 |
doh.is(6, children.length, "six children"); |
| 258 |
doh.is("Pacifica", children[5].label, "last child"); |
| 259 |
doh.f(domClass.contains(children[4].domNode, "dijitTreeIsLast"), |
| 260 |
"North America no longer last child"); |
| 261 |
doh.t(domClass.contains(children[5].domNode, "dijitTreeIsLast"), |
| 262 |
"Pacifica is last child"); |
| 263 |
}, |
| 264 |
|
| 265 |
function topLevelItemModify(){ |
| 266 |
// Modify a top level item so it's no longer top level. |
| 267 |
// ForestStoreModel needs to realize that the top level children have changed and notify Tree. |
| 268 |
|
| 269 |
myStore.fetchItemByIdentity({ |
| 270 |
identity: 'PA', |
| 271 |
onItem: function(item){ |
| 272 |
// in real life we would need to give the item a parent, |
| 273 |
// but this is enough for testing |
| 274 |
myStore.setValue(item, 'type', 'country'); |
| 275 |
} |
| 276 |
}); |
| 277 |
|
| 278 |
var children = myTree.rootNode.getChildren(); |
| 279 |
doh.is(5, children.length, "five children again"); |
| 280 |
}, |
| 281 |
|
| 282 |
function nestedItemModify(){ |
| 283 |
// Modify a nested item so it matches the query for top level items in the tree. |
| 284 |
// ForestStoreModel needs to realize that the top level children have changed and notify Tree. |
| 285 |
|
| 286 |
myStore.fetchItemByIdentity({ |
| 287 |
identity: 'PA', |
| 288 |
onItem: function(item){ |
| 289 |
// in real life we would need to give the item a parent, |
| 290 |
// but this is enough for testing |
| 291 |
myStore.setValue(item, 'type', 'continent'); |
| 292 |
} |
| 293 |
}); |
| 294 |
|
| 295 |
var children = myTree.rootNode.getChildren(); |
| 296 |
doh.is(6, children.length, "six children again"); |
| 297 |
}, |
| 298 |
|
| 299 |
function destroyTree(){ |
| 300 |
// Just running this to make sure we don't get an exception |
| 301 |
console.log("destroying tree"); |
| 302 |
myTree.destroy(); |
| 303 |
myTree2.destroy(); |
| 304 |
} |
| 305 |
]); |
| 306 |
|
| 307 |
doh.register("delete selected node", [ |
| 308 |
{ |
| 309 |
name: "create", |
| 310 |
timeout: 10000, |
| 311 |
runTest: function() { |
| 312 |
var d = new doh.Deferred(); |
| 313 |
|
| 314 |
myTree = new Tree({ |
| 315 |
id: "myTree", |
| 316 |
model: myModel, |
| 317 |
persist: false, // persist==true is too hard to test |
| 318 |
path: ["earth", "EU", "IT"] |
| 319 |
}); |
| 320 |
doh.t(myTree, "tree created"); |
| 321 |
|
| 322 |
dom.byId("container").appendChild(myTree.domNode); |
| 323 |
myTree.startup(); |
| 324 |
|
| 325 |
myTree.onLoadDeferred.then(d.getTestCallback(function () { |
| 326 |
var item = myTree.get("selectedItem"); |
| 327 |
doh.is("IT", myStore.getValue(item, "id")); |
| 328 |
})); |
| 329 |
return d; |
| 330 |
} |
| 331 |
}, |
| 332 |
|
| 333 |
function deleteSelectedItem(){ |
| 334 |
myStore.deleteItem(myTree.get("selectedItem")); |
| 335 |
}, |
| 336 |
|
| 337 |
function selectNewItem(){ |
| 338 |
myTree.set("path", ["earth", "EU", "FR"]); |
| 339 |
var item = myTree.get("selectedItem"); |
| 340 |
doh.is("FR", myStore.getValue(item, "id")); |
| 341 |
} |
| 342 |
]); |
| 343 |
|
| 344 |
// Make sure that Tree doesn't have spurious lang="" dir="" on nodes |
| 345 |
doh.register("nobidi", { |
| 346 |
name: "noLangDir", |
| 347 |
timeout: 10000, |
| 348 |
runTest: function () { |
| 349 |
var d = new doh.Deferred(); |
| 350 |
myStore = new dojo.data.ItemFileWriteStore({url: '../_data/countries.json'}); |
| 351 |
doh.t(myStore, "store created"); |
| 352 |
|
| 353 |
myModel = new ForestStoreModel({ |
| 354 |
store: myStore, |
| 355 |
query: {type: 'continent'}, |
| 356 |
rootId: "earth", |
| 357 |
rootLabel: "Earth", |
| 358 |
childrenAttrs: ["children"] |
| 359 |
}); |
| 360 |
doh.t(myModel, "model created"); |
| 361 |
|
| 362 |
myTree = new Tree({ |
| 363 |
id: "noDirTree", |
| 364 |
model: myModel, |
| 365 |
persist: false, // persist==true is too hard to test |
| 366 |
dndController: dndSource |
| 367 |
}); |
| 368 |
doh.t(myTree, "tree created"); |
| 369 |
|
| 370 |
dom.byId("container").appendChild(myTree.domNode); |
| 371 |
myTree.startup(); |
| 372 |
|
| 373 |
myTree.onLoadDeferred.then(d.getTestCallback(function(){ |
| 374 |
// Give the tree time to load, and the do checks that it |
| 375 |
// loaded correctly |
| 376 |
doh.t(myTree.rootNode, "root node exists"); |
| 377 |
doh.t(myTree.rootNode.isExpanded, "root node is expanded"); |
| 378 |
doh.f(domAttr.has(myTree.rootNode, "lang"), "no (empty) lang attribute on root TreeNode"); |
| 379 |
doh.f(domAttr.has(myTree.rootNode, "dir"), "no (empty) dir attribute on root TreeNode"); |
| 380 |
|
| 381 |
var children = myTree.rootNode.getChildren(); |
| 382 |
doh.f(domAttr.has(children[2], "lang"), "no (empty) lang attribute on child TreeNode"); |
| 383 |
doh.f(domAttr.has(children[2], "dir"), "no (empty) dir attribute on child TreeNode"); |
| 384 |
})); |
| 385 |
return d; |
| 386 |
} |
| 387 |
}); |
| 388 |
|
| 389 |
doh.run(); |
| 390 |
}); |
| 391 |
</script> |
| 392 |
|
| 393 |
</head> |
| 394 |
<body class="claro" role="main"> |
| 395 |
|
| 396 |
<h1 class="testTitle">Dijit.Tree / dijit.tree.ForestStoreModel Automated Tests</h1> |
| 397 |
<div id="container"> </div> |
| 398 |
<div id="container2"> </div> |
| 399 |
</body> |
| 400 |
</html> |