| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
<html> |
| 8 |
<head> |
| 9 |
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 10 |
<title>Dijit manager unit test</title> |
| 11 |
<style type="text/css"> |
| 12 |
@import "../../themes/claro/document.css"; |
| 13 |
@import "../css/dijitTests.css"; |
| 14 |
</style> |
| 15 |
<script type="text/javascript" src="../../../dojo/dojo.js" |
| 16 |
data-dojo-config="isDebug: true"></script> |
| 17 |
<script type="text/javascript" src="../_testCommon.js"></script> |
| 18 |
|
| 19 |
<script type="text/javascript"> |
| 20 |
dojo.require("doh.runner"); |
| 21 |
dojo.require("dojo.parser"); |
| 22 |
dojo.require("dijit.dijit"); |
| 23 |
|
| 24 |
dojo.ready(function(){ |
| 25 |
dojo.declare("foo", dijit._Widget, { |
| 26 |
name: "", |
| 27 |
attr1: 0, |
| 28 |
attr2: 0 |
| 29 |
}); |
| 30 |
|
| 31 |
dojo.declare("bar", dijit._Widget, { |
| 32 |
name: "", |
| 33 |
attr1: 0, |
| 34 |
attr2: 0 |
| 35 |
}); |
| 36 |
|
| 37 |
dojo.declare("Baz", dijit._Widget, { |
| 38 |
name: "", |
| 39 |
attr1: 1, |
| 40 |
attr2: 1 |
| 41 |
}); |
| 42 |
|
| 43 |
doh.register("parse", function(){ |
| 44 |
dojo.parser.parse(); |
| 45 |
}); |
| 46 |
|
| 47 |
doh.register("dijit._base.manager", |
| 48 |
[ |
| 49 |
function forEachTest(t){ |
| 50 |
var names=[]; |
| 51 |
dijit.registry.forEach(function(widget){ names.push(widget.name); }); |
| 52 |
t.is(names.join(" "), "bob is your uncle"); |
| 53 |
}, |
| 54 |
function filterTest(t){ |
| 55 |
var names=[]; |
| 56 |
dijit.registry. |
| 57 |
filter(function(widget){ return widget.attr1==10; }). |
| 58 |
forEach(function(widget){ names.push(widget.name); }); |
| 59 |
t.is(names.join(" "), "bob uncle"); |
| 60 |
}, |
| 61 |
function filterScope(t){ |
| 62 |
// testing filter with a forced 'this' in the cb function |
| 63 |
var names = [], |
| 64 |
obj = { |
| 65 |
check: function(widget, val){ |
| 66 |
// same as previous unit test |
| 67 |
return widget.attr1 == val; |
| 68 |
} |
| 69 |
}; |
| 70 |
dijit.registry |
| 71 |
.filter(function(w){ return this.check(w, 10); }, obj) |
| 72 |
.forEach(function(w){ names.push(w.name) }) |
| 73 |
; |
| 74 |
t.is(names.join(" "), "bob uncle"); |
| 75 |
}, |
| 76 |
function byId(t){ |
| 77 |
t.is(dijit.byId("three").name, "your"); |
| 78 |
t.f(dijit.byId("nonexistent")); |
| 79 |
}, |
| 80 |
function byClass(t){ |
| 81 |
var names=[]; |
| 82 |
dijit.registry. |
| 83 |
byClass("bar"). |
| 84 |
forEach(function(widget){ names.push(widget.name); }); |
| 85 |
t.is(names.join(" "), "your uncle"); |
| 86 |
}, |
| 87 |
function lengthTest(t){ |
| 88 |
// test existing length |
| 89 |
t.is(dijit.registry.byClass("bar").length, 2); |
| 90 |
// test adding: |
| 91 |
var n = new Baz(); |
| 92 |
t.is(dijit.registry.byClass("Baz").length, 1); |
| 93 |
// test destroying: |
| 94 |
n.destroy(); |
| 95 |
t.is(dijit.registry.byClass("Baz").length, 0); |
| 96 |
}, |
| 97 |
function nullLengthTest(t){ |
| 98 |
var ws = new dijit.WidgetSet(); |
| 99 |
var count = 0; |
| 100 |
|
| 101 |
var b = [new Baz(), new Baz()]; |
| 102 |
ws.add(b[0]); |
| 103 |
ws.add(b[1]); |
| 104 |
|
| 105 |
t.is(2, ws.length); |
| 106 |
|
| 107 |
ws.remove(b[0].id); |
| 108 |
t.is(1, ws.length); |
| 109 |
|
| 110 |
ws.remove(b[1].id); |
| 111 |
t.is(0, ws.length); |
| 112 |
|
| 113 |
// ensure we don't ever go below 0 |
| 114 |
ws.remove("invalidId"); |
| 115 |
t.is(0, ws.length); |
| 116 |
|
| 117 |
dojo.forEach(b, function(w){ w.destroy() }); |
| 118 |
|
| 119 |
}, |
| 120 |
function forEachScope(t){ |
| 121 |
var obj = { |
| 122 |
getIt: function(widget){ |
| 123 |
return widget.id; |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
dijit.registry.forEach(function(w){ |
| 128 |
t.is(w.id, this.getIt(w)); |
| 129 |
}, obj); |
| 130 |
}, |
| 131 |
function forEachReturn(t){ |
| 132 |
// also tests WidgetSet.map |
| 133 |
var ws = dijit.registry.forEach(function(){}).map(function(w){ |
| 134 |
return w.declaredClass.toUpperCase(); |
| 135 |
}); |
| 136 |
t.t(dojo.isArray(ws)); |
| 137 |
t.is(ws.length, dijit.registry.length); |
| 138 |
}, |
| 139 |
function forEachInc(t){ |
| 140 |
var x = 0; |
| 141 |
dijit.registry.forEach(function(w, i){ |
| 142 |
t.is(i, x++); |
| 143 |
}); |
| 144 |
}, |
| 145 |
function toArrayTest(t){ |
| 146 |
var ws = dijit.registry.byClass("bar"); |
| 147 |
var wa = ws.toArray(); |
| 148 |
|
| 149 |
t.t(dojo.isArray(wa)); |
| 150 |
t.is(ws.length, wa.length); |
| 151 |
|
| 152 |
var wda = dojo.map(wa, function(w){ |
| 153 |
return w.domNode; |
| 154 |
}); |
| 155 |
|
| 156 |
t.is(wda.length, ws.length); |
| 157 |
var wdas = wda.slice(-1); |
| 158 |
|
| 159 |
t.is(1, wdas.length); |
| 160 |
|
| 161 |
var w = dijit.byNode(wdas[0]); |
| 162 |
t.is(w.declaredClass, "bar"); |
| 163 |
}, |
| 164 |
function mapTest(t){ |
| 165 |
var ids = dijit.registry.map(function(w){ |
| 166 |
return w.id; |
| 167 |
}); |
| 168 |
t.is(ids.length, dijit.registry.length); |
| 169 |
dijit.registry.forEach(function(id){ |
| 170 |
t.t(dijit.byId(id)); |
| 171 |
}); |
| 172 |
}, |
| 173 |
function everyTest(t){ |
| 174 |
|
| 175 |
var a = new Baz(); |
| 176 |
var b = new Baz(); |
| 177 |
|
| 178 |
var hasid = dijit.registry.every(function(w){ |
| 179 |
return w.id; |
| 180 |
}); |
| 181 |
|
| 182 |
t.t(hasid); |
| 183 |
|
| 184 |
var hasattr1 = dijit.registry.every(function(w){ |
| 185 |
return w.attr1 > 0; |
| 186 |
}); |
| 187 |
|
| 188 |
t.t(hasattr1); |
| 189 |
|
| 190 |
var x = 0; |
| 191 |
var once = dijit.registry.every(function(w){ |
| 192 |
x++; |
| 193 |
return false; |
| 194 |
}); |
| 195 |
t.is(1, x); |
| 196 |
t.f(once); |
| 197 |
|
| 198 |
a.destroy(); |
| 199 |
b.destroy(); |
| 200 |
|
| 201 |
}, |
| 202 |
function someTest(t){ |
| 203 |
|
| 204 |
var a = new Baz(); |
| 205 |
var b = new Baz(); |
| 206 |
|
| 207 |
var x = 0; |
| 208 |
var hasid = dijit.registry.some(function(w){ |
| 209 |
x++; |
| 210 |
return w.id; |
| 211 |
}); |
| 212 |
|
| 213 |
t.is(1, x); |
| 214 |
t.t(hasid); |
| 215 |
|
| 216 |
var n = 0; |
| 217 |
var hasattr1 = dijit.registry.some(function(w){ |
| 218 |
n++; |
| 219 |
return false; |
| 220 |
}); |
| 221 |
t.is(n, dijit.registry.length); |
| 222 |
t.f(hasattr1); |
| 223 |
|
| 224 |
var l = 0; |
| 225 |
var once = dijit.registry.some(function(w){ |
| 226 |
l++; |
| 227 |
return true; |
| 228 |
}); |
| 229 |
t.is(1, l); |
| 230 |
t.t(once); |
| 231 |
|
| 232 |
a.destroy(); |
| 233 |
b.destroy(); |
| 234 |
|
| 235 |
}, |
| 236 |
function deleteTest(t){ |
| 237 |
var names=[]; |
| 238 |
dijit.byId("two").destroy(); |
| 239 |
dijit.byId("four").destroy(); |
| 240 |
dijit.registry.forEach(function(widget){ names.push(widget.name); }); |
| 241 |
t.is(names.join(" "), "bob your"); |
| 242 |
}, |
| 243 |
function getEnclosingWidgetTest(t){ |
| 244 |
t.is(dijit.getEnclosingWidget(dojo.byId("not-a-widget")), null); |
| 245 |
t.is(dijit.getEnclosingWidget(dojo.byId("three")).name, "your"); |
| 246 |
t.is(dijit.getEnclosingWidget(dojo.byId("three.one")).name, "your"); |
| 247 |
t.is(dijit.getEnclosingWidget(dojo.byId("three.one.one")).name, "your"); |
| 248 |
} |
| 249 |
] |
| 250 |
); |
| 251 |
|
| 252 |
doh.run(); |
| 253 |
}); |
| 254 |
|
| 255 |
</script> |
| 256 |
</head> |
| 257 |
<body> |
| 258 |
<h1>Dijit Manager Unit Test</h1> |
| 259 |
<div id="one" data-dojo-type="foo" data-dojo-props='name:"bob", attr1:10, attr2:10'></div> |
| 260 |
<div id="two" data-dojo-type="foo" data-dojo-props='name:"is", attr1:5, attr2:10'></div> |
| 261 |
<div id="three" data-dojo-type="bar" data-dojo-props='name:"your", attr1:5, attr2:5'> |
| 262 |
<div id="three.one"> |
| 263 |
<div id="three.one.one"></div> |
| 264 |
</div> |
| 265 |
</div> |
| 266 |
<div id="four" data-dojo-type="bar" data-dojo-props='name:"uncle", attr1:10, attr2:5'></div> |
| 267 |
<div id="not-a-widget"></div> |
| 268 |
</body> |
| 269 |
</html> |