| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 5 |
<title>MVC DOH Test</title> |
| 6 |
|
| 7 |
<style> |
| 8 |
@import "../../../../dojo/resources/dojo.css"; |
| 9 |
@import "../../../../dijit/tests/css/dijitTests.css"; |
| 10 |
@import "../css/app-format.css"; |
| 11 |
</style> |
| 12 |
|
| 13 |
|
| 14 |
<link id="themeStyles" rel="stylesheet" href="../../../../dijit/themes/claro/claro.css"> |
| 15 |
|
| 16 |
|
| 17 |
<script src="../../../../dojo/dojo.js" type="text/javascript" data-dojo-config="parseOnLoad: 0, isDebug: 1, mvc: {debugBindings: 1}"></script> |
| 18 |
<script type="text/javascript" src="./helpers.js"></script> |
| 19 |
|
| 20 |
<script type="text/javascript" > |
| 21 |
var lookup, loanModel, validHousingPercentConverter = { |
| 22 |
format: function(value){ |
| 23 |
return value <= 33; |
| 24 |
} |
| 25 |
}, nonZeroRelevanceConverter = { |
| 26 |
format: function(value){ |
| 27 |
return value <= 0; |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
require([ |
| 32 |
"doh/runner", |
| 33 |
"dojo/_base/declare", |
| 34 |
"dojo/dom", |
| 35 |
"dojo/_base/array", |
| 36 |
"dojo/_base/lang", |
| 37 |
"dojo/request/xhr", |
| 38 |
"dojo/parser", |
| 39 |
"dojo/when", |
| 40 |
"dijit/registry", |
| 41 |
"dojox/charting/Chart", |
| 42 |
"dojox/charting/themes/PlotKit/blue", |
| 43 |
"dojox/mvc/at", |
| 44 |
"dojox/mvc/computed", |
| 45 |
"dojo/Stateful", |
| 46 |
"dojox/mvc/StatefulSeries", |
| 47 |
"dijit/form/TextBox", |
| 48 |
"dijit/form/Button", |
| 49 |
"dijit/form/ValidationTextBox", |
| 50 |
"dijit/form/NumberTextBox", |
| 51 |
"dojox/charting/plot2d/Pie", |
| 52 |
"dojox/mvc/_TextBoxExtensions", |
| 53 |
"dojox/mvc/Group", |
| 54 |
"dojox/mvc/ModelRefController", |
| 55 |
"dojox/mvc/Output", |
| 56 |
"dojo/domReady!" |
| 57 |
], function(doh, declare, ddom, array, lang, dxhr, parser, when, registry, Chart, blue, at, computed, Stateful, StatefulSeries){ |
| 58 |
|
| 59 |
lookup = function(model){ |
| 60 |
if(isNaN(model.get("Zip"))){ return; } |
| 61 |
dxhr.get("../zips/" + model.get("Zip") + ".json", { |
| 62 |
content: {postalcode: model.get("Zip"), country: model.get("County")}, |
| 63 |
preventCache: true, |
| 64 |
handleAs: "json" |
| 65 |
}).then(function(data){ |
| 66 |
model.set("City", data.postalcodes[0].placeName); |
| 67 |
model.set("County", data.postalcodes[0].adminName2); |
| 68 |
model.set("State", data.postalcodes[0].adminCode1); |
| 69 |
model.set("isZipValid", true); |
| 70 |
}, function(e){ |
| 71 |
model.set("City", ""); |
| 72 |
model.set("County", ""); |
| 73 |
model.set("State", ""); |
| 74 |
model.set("isZipValid", false); |
| 75 |
}); |
| 76 |
}; |
| 77 |
|
| 78 |
loanModel = new Stateful({ |
| 79 |
Name: "John Doe", |
| 80 |
Street: "", |
| 81 |
City: "", |
| 82 |
County: "", |
| 83 |
State: "", |
| 84 |
Zip: "", |
| 85 |
Country: "US", |
| 86 |
BaseIncome: 50000, |
| 87 |
BonusIncome: 10000, |
| 88 |
Mortgage: 1000, |
| 89 |
Taxes: 500, |
| 90 |
OtherHousing: 1200 |
| 91 |
}); |
| 92 |
|
| 93 |
computed(loanModel, "TotalIncome", function(BaseIncome, BonusIncome){ |
| 94 |
return BaseIncome + BonusIncome; |
| 95 |
}, at(loanModel, "BaseIncome"), at(loanModel, "BonusIncome")); |
| 96 |
|
| 97 |
computed(loanModel, "TotalHousing", function(Mortgage, Taxes, OtherHousing){ |
| 98 |
return Mortgage + Taxes + OtherHousing; |
| 99 |
}, at(loanModel, "Mortgage"), at(loanModel, "Taxes"), at(loanModel, "OtherHousing")); |
| 100 |
|
| 101 |
computed(loanModel, "HousingPercent", function(TotalHousing, TotalIncome){ |
| 102 |
return Math.round(TotalHousing / TotalIncome * 100); |
| 103 |
}, at(loanModel, "TotalHousing"), at(loanModel, "TotalIncome")); |
| 104 |
|
| 105 |
computed(loanModel, "HousingPercentError", function(HousingPercent){ |
| 106 |
return HousingPercent > 33 ? "Housing should be less than 1/3 total expenses!" : ""; |
| 107 |
}, at(loanModel, "HousingPercent")); |
| 108 |
|
| 109 |
computed(loanModel, "HousingPercentDisabled", function(HousingPercent){ |
| 110 |
return HousingPercent <= 0 ? "disabled" : null; |
| 111 |
}, at(loanModel, "HousingPercent")); |
| 112 |
|
| 113 |
when(parser.parse(), function(){ |
| 114 |
new Chart("expenseChart") |
| 115 |
.setTheme(blue) |
| 116 |
.addPlot("default", {type: "Pie"}) |
| 117 |
.addSeries("default", new StatefulSeries([at(loanModel, "Mortgage"), at(loanModel, "Taxes"), at(loanModel, "OtherHousing")])).render(); |
| 118 |
|
| 119 |
// should be able to verify all of the inputs |
| 120 |
doh.register("Check values and bindings", [ |
| 121 |
{ |
| 122 |
name: "Initial", |
| 123 |
runTest: function(){ |
| 124 |
doh.is("5", registry.byId("percentHousing").get('value'),"percentHousing should be 5"); |
| 125 |
doh.is(true, registry.byId("percentHousing").isValid(), "isValid()","percentHousing should be valid"); |
| 126 |
doh.is("1000", registry.byId("housing").get('value'),"housing should be 1000"); |
| 127 |
doh.is("2700", registry.byId("totalHousing").get('value'),"totalHousing should be 2700"); |
| 128 |
} |
| 129 |
}, { |
| 130 |
name: "setZipTest", |
| 131 |
runTest: function(){ |
| 132 |
registry.byId("zipInput").focus(); |
| 133 |
registry.byId("zipInput").set('value', "11111"); |
| 134 |
var dfd = new doh.Deferred(); |
| 135 |
setTimeout(function(){ |
| 136 |
try{ |
| 137 |
registry.byId("cityInput").focus(); |
| 138 |
}catch(e){ |
| 139 |
return dfd.errback(e); |
| 140 |
} |
| 141 |
setTimeout(function(){ |
| 142 |
try{ |
| 143 |
doh.t(/^\s*$/.test(registry.byId("cityInput").get('value')), "bad zip should not display the city"); |
| 144 |
doh.is(false, registry.byId("zipInput").isValid(), "zipInput should be invalid"); |
| 145 |
// now try a good one |
| 146 |
registry.byId("zipInput").focus(); |
| 147 |
registry.byId("zipInput").set('value', "10706"); |
| 148 |
registry.byId("cityInput").focus(); |
| 149 |
}catch(e){ |
| 150 |
return dfd.errback(e); |
| 151 |
} |
| 152 |
setTimeout(function(){ |
| 153 |
try{ |
| 154 |
doh.is("Hastings On Hudson", registry.byId("cityInput").get('value'),"good zip 10706 should display the city"); |
| 155 |
return dfd.callback(true); |
| 156 |
}catch(e){ |
| 157 |
return dfd.errback(e); |
| 158 |
} |
| 159 |
}, 2000); |
| 160 |
}, 2000); |
| 161 |
}, 1000); |
| 162 |
return dfd; |
| 163 |
}, |
| 164 |
timeout: 10000 |
| 165 |
}, { |
| 166 |
name: "setMortgageTest", |
| 167 |
runTest: function(){ |
| 168 |
registry.byId("housing").focus(); |
| 169 |
registry.byId("housing").set('value', "10000"); |
| 170 |
registry.byId("totalHousing").focus(); |
| 171 |
var dfd = new doh.Deferred(); |
| 172 |
setTimeout(function(){ |
| 173 |
try{ |
| 174 |
doh.is("20", registry.byId("percentHousing").get('value'),"percentHousing should be 5"); |
| 175 |
doh.is(true, registry.byId("percentHousing").isValid(), "isValid()","percentHousing should be valid"); |
| 176 |
doh.is("10000", registry.byId("housing").get('value'),"housing should be 10000"); |
| 177 |
doh.is("11700", registry.byId("totalHousing").get('value'),"totalHousing should be 11700"); |
| 178 |
dfd.callback(true); |
| 179 |
}catch(e){ |
| 180 |
dfd.errback(e); |
| 181 |
} |
| 182 |
}, 500); |
| 183 |
return dfd; |
| 184 |
} |
| 185 |
}, { |
| 186 |
name: "setMortgageTest2", |
| 187 |
runTest: function(){ |
| 188 |
registry.byId("housing").focus(); |
| 189 |
registry.byId("housing").set('value', "20000"); |
| 190 |
registry.byId("totalHousing").focus(); |
| 191 |
var dfd = new doh.Deferred(); |
| 192 |
setTimeout(function(){ |
| 193 |
try{ |
| 194 |
doh.is("36", registry.byId("percentHousing").get('value'),"percentHousing should be 5"); |
| 195 |
doh.is(false, registry.byId("percentHousing").isValid(), "isValid()","percentHousing should not be valid"); |
| 196 |
doh.is("20000", registry.byId("housing").get('value'),"housing should be 10000"); |
| 197 |
doh.is("21700", registry.byId("totalHousing").get('value'),"totalHousing should be 11700"); |
| 198 |
return dfd.callback(true); |
| 199 |
}catch(e){ |
| 200 |
return dfd.errback(e); |
| 201 |
} |
| 202 |
}, 500); |
| 203 |
return dfd; |
| 204 |
} |
| 205 |
}, { |
| 206 |
name: "setHousingZero", |
| 207 |
runTest: function(){ |
| 208 |
registry.byId("housing").focus(); |
| 209 |
registry.byId("housing").set('value', "0"); |
| 210 |
registry.byId("taxes").focus(); |
| 211 |
registry.byId("taxes").set('value', "0"); |
| 212 |
registry.byId("otherHousing").focus(); |
| 213 |
registry.byId("otherHousing").set('value', "0"); |
| 214 |
registry.byId("totalHousing").focus(); |
| 215 |
var dfd = new doh.Deferred(); |
| 216 |
setTimeout(function(){ |
| 217 |
try{ |
| 218 |
doh.is("0", registry.byId("housing").get('value'),"housing should be 0"); |
| 219 |
doh.is("0", registry.byId("taxes").get('value'),"taxes should be 0"); |
| 220 |
doh.is("0", registry.byId("otherHousing").get('value'),"otherHousing should be 0"); |
| 221 |
doh.is("0", registry.byId("totalHousing").get('value'),"totalHousing should be 0"); |
| 222 |
doh.is(true, registry.byId("totalHousing").get("disabled"), "disabled()","totalHousing should be disabled"); |
| 223 |
doh.is(true, registry.byId("percentHousing").get("disabled"), "disabled()","percentHousing should disabled"); |
| 224 |
doh.is("0", registry.byId("percentHousing").get('value'),"percentHousing should be 5"); |
| 225 |
doh.is(true, registry.byId("percentHousing").isValid(), "isValid()","percentHousing should be valid"); |
| 226 |
return dfd.callback(true); |
| 227 |
}catch(e){ |
| 228 |
return dfd.errback(e); |
| 229 |
} |
| 230 |
}, 500); |
| 231 |
return dfd; |
| 232 |
} |
| 233 |
}, { |
| 234 |
name: "setHousingBack", |
| 235 |
runTest: function(){ |
| 236 |
registry.byId("housing").focus(); |
| 237 |
registry.byId("housing").set('value', "1000"); |
| 238 |
registry.byId("taxes").focus(); |
| 239 |
registry.byId("taxes").set('value', "500"); |
| 240 |
registry.byId("otherHousing").focus(); |
| 241 |
registry.byId("otherHousing").set('value', "1200"); |
| 242 |
registry.byId("totalHousing").focus(); |
| 243 |
var dfd = new doh.Deferred(); |
| 244 |
setTimeout(function(){ |
| 245 |
try{ |
| 246 |
doh.is("5", registry.byId("percentHousing").get('value'),"percentHousing should be 5"); |
| 247 |
doh.is(true, registry.byId("percentHousing").isValid(), "isValid()","percentHousing should be valid"); |
| 248 |
doh.is("1000", registry.byId("housing").get('value'),"housing should be 1000"); |
| 249 |
doh.is("2700", registry.byId("totalHousing").get('value'),"totalHousing should be 2700"); |
| 250 |
doh.is(false, registry.byId("totalHousing").get("disabled"), "disabled()","totalHousing should be disabled"); |
| 251 |
doh.is(false, registry.byId("percentHousing").get("disabled"), "disabled()","percentHousing should disabled"); |
| 252 |
doh.is(true, registry.byId("percentHousing").isValid(), "isValid()","percentHousing should be valid"); |
| 253 |
return dfd.callback(true); |
| 254 |
}catch(e){ |
| 255 |
return dfd.errback(e); |
| 256 |
} |
| 257 |
}, 500); |
| 258 |
return dfd; |
| 259 |
} |
| 260 |
} |
| 261 |
]); |
| 262 |
|
| 263 |
doh.run(); |
| 264 |
}); |
| 265 |
}); |
| 266 |
</script> |
| 267 |
</head> |
| 268 |
<body class="claro" style="background-image: url(../images/validating_form_pattern.png)"> |
| 269 |
<script type="dojo/require">at: "dojox/mvc/at"</script> |
| 270 |
<h1 class="testTitle">doh_mvc_loan-stateful.html automated tests (non-robot)</h1> |
| 271 |
<div id="wrapper"> |
| 272 |
<div id="addressLookupController" class="dijitDisplayNone" data-dojo-type="dojox.mvc.ModelRefController" |
| 273 |
data-dojo-props="model: loanModel, ownProps: {_setZipAttr: 1, _setCountyAttr: 1}"> |
| 274 |
<script type="dojo/method" event="_setZipAttr" args="value"> |
| 275 |
var changed = this.model.Zip != value; |
| 276 |
this.model._changeAttrValue("Zip", value); |
| 277 |
if(changed){ |
| 278 |
lookup(this.model); |
| 279 |
} |
| 280 |
return this; |
| 281 |
</script> |
| 282 |
<script type="dojo/method" event="_setCountyAttr" args="value"> |
| 283 |
var changed = this.model.County != value; |
| 284 |
this.model._changeAttrValue("County", value); |
| 285 |
if(changed){ |
| 286 |
lookup(this.model); |
| 287 |
} |
| 288 |
return this; |
| 289 |
</script> |
| 290 |
</div> |
| 291 |
<div id="header"> |
| 292 |
<div id="navigation"> |
| 293 |
</div> |
| 294 |
<div id="headerInsert"> |
| 295 |
<h1>Big Red Loan</h1> |
| 296 |
<h2>The one to choose when you're in the red...</h2> |
| 297 |
</div> |
| 298 |
</div> |
| 299 |
<div id="main"> |
| 300 |
<div id="leftNav"></div> |
| 301 |
<div id="mainContent" data-dojo-type="dojox.mvc.Group" data-dojo-props="target: at('widget:addressLookupController')"> |
| 302 |
<h3>Borrower information</h3> |
| 303 |
<hr size="6" class="rule"/> |
| 304 |
<div class="spacer"></div> |
| 305 |
<div class="row"> |
| 306 |
<div class="cell"><label for="borrowerInput">Name:</label></div> |
| 307 |
<div class="cell"> |
| 308 |
<input id="borrowerInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'Name')"/> |
| 309 |
</div> |
| 310 |
</div> |
| 311 |
<div class="row"> |
| 312 |
<div class="cell"><label for="zipInput">Zipcode (try 10706):</label></div> |
| 313 |
<div class="cell"> |
| 314 |
<input id="zipInput" data-dojo-type="dijit.form.NumberTextBox" |
| 315 |
data-dojo-props="value: at('rel:', 'Zip'), valid: at('rel:', 'isZipValid')" |
| 316 |
constraints="{pattern:'#'}" regExp="\d{5}" required="true" invalidMessage="Invalid Zip Code."/> |
| 317 |
</div> |
| 318 |
</div> |
| 319 |
<div class="row"> |
| 320 |
<div class="cell"><label for="cityInput">City:</label></div> |
| 321 |
<div class="cell"> |
| 322 |
<input id="cityInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'City')"/> |
| 323 |
</div> |
| 324 |
</div> |
| 325 |
<div class="row"> |
| 326 |
<div class="cell"><label for="countyInput">County:</label></div> |
| 327 |
<div class="cell"> |
| 328 |
<input id="countyInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'County')"/> |
| 329 |
</div> |
| 330 |
</div> |
| 331 |
<div class="row"> |
| 332 |
<div class="cell"><label for="stateInput">State:</label></div> |
| 333 |
<div class="cell"> |
| 334 |
<input id="stateInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'State')"/> |
| 335 |
</div> |
| 336 |
</div> |
| 337 |
<div class="spacer"></div> |
| 338 |
<h3>Expenses</h3> |
| 339 |
<hr size="6" class="rule"/> |
| 340 |
<div class="spacer"></div> |
| 341 |
<table style="width:900px;"> |
| 342 |
<tr style="width:900px;"> |
| 343 |
<td style="width:400px;"> |
| 344 |
<div class="row"> |
| 345 |
<div class="cell"><label for="housing">Mortgage:</label></div> |
| 346 |
<div class="cell"> |
| 347 |
<input id="housing" data-dojo-type="dijit.form.NumberTextBox" data-dojo-props="value: at('rel:', 'Mortgage')"/> |
| 348 |
</div> |
| 349 |
</div> |
| 350 |
<div class="row"> |
| 351 |
<div class="cell"> |
| 352 |
<label>Real Estate Taxes:</label> |
| 353 |
</div> |
| 354 |
<div class="cell"> |
| 355 |
<input id="taxes" data-dojo-type="dijit.form.NumberTextBox" data-dojo-props="value: at('rel:', 'Taxes')"/> |
| 356 |
</div> |
| 357 |
</div> |
| 358 |
<div class="row"> |
| 359 |
<div class="cell"><label>Other Housing:</label></div> |
| 360 |
<div class="cell"> |
| 361 |
<input id="otherHousing" type="text" data-dojo-type="dijit.form.NumberTextBox" data-dojo-props="value: at('rel:', 'OtherHousing')"/> |
| 362 |
</div> |
| 363 |
</div> |
| 364 |
<div class="row"> |
| 365 |
<div class="cell"><label for="transportation">Total Housing:</label></div> |
| 366 |
<div class="cell"> |
| 367 |
<input id="totalHousing" data-dojo-type="dijit.form.NumberTextBox" |
| 368 |
data-dojo-props="value: at('rel:', 'TotalHousing'), |
| 369 |
disabled: at('rel:', 'TotalHousing').direction(at.from).transform(nonZeroRelevanceConverter)"/> |
| 370 |
</div> |
| 371 |
</div> |
| 372 |
</td> |
| 373 |
<td style="width:200px;"> |
| 374 |
<div id="expenseChart" width="150" height="150" style="display:block; width: 150px; height: 150px;"></div> |
| 375 |
</td> |
| 376 |
</tr> |
| 377 |
</table> |
| 378 |
<h3>Income</h3> |
| 379 |
<hr size="6" class="rule"/> |
| 380 |
<div class="spacer"></div> |
| 381 |
<div class="row"> |
| 382 |
<div class="cell"><label for="baseIncome">Base Income:</label></div> |
| 383 |
<div class="cell"> |
| 384 |
<input id="baseIncome" type="text" data-dojo-type="dijit.form.NumberTextBox" data-dojo-props="value: at('rel:', 'BaseIncome')"/> |
| 385 |
</div> |
| 386 |
</div> |
| 387 |
<div class="row"> |
| 388 |
<div class="cell"><label>Bonus Income:</label></div> |
| 389 |
<div class="cell"> |
| 390 |
<input id="bonusIncome" type="text" data-dojo-type="dijit.form.NumberTextBox" data-dojo-props="value: at('rel:', 'BonusIncome')"/> |
| 391 |
</div> |
| 392 |
</div> |
| 393 |
<div class="row"> |
| 394 |
<div class="cell"><label for="total">Total Income:</label></div> |
| 395 |
<div class="cell"> |
| 396 |
<input id="total" data-dojo-type="dijit.form.NumberTextBox" data-dojo-props="value: at('rel:', 'TotalIncome')"/> |
| 397 |
</div> |
| 398 |
</div> |
| 399 |
<h3>Analysis</h3> |
| 400 |
<hr size="6" class="rule"/> |
| 401 |
<div class="spacer"></div> |
| 402 |
<div class="row"> |
| 403 |
<div class="cell"><label for="percentHousing">Percent housing (under 33%):</label></div> |
| 404 |
<div class="cell"> |
| 405 |
<input id="percentHousing" data-dojo-type="dijit.form.NumberTextBox" |
| 406 |
data-dojo-props="value: at('rel:', 'HousingPercent'), |
| 407 |
valid: at('rel:', 'HousingPercent').direction(at.from).transform(validHousingPercentConverter), |
| 408 |
disabled: at('rel:', 'HousingPercent').direction(at.from).transform(nonZeroRelevanceConverter)" |
| 409 |
invalidMessage="Housing should be less than 1/3 total expenses!"/> |
| 410 |
</div> |
| 411 |
</div> |
| 412 |
</div> |
| 413 |
</div> |
| 414 |
</div> |
| 415 |
</body> |
| 416 |
</html> |