Permalink
Browse files

added test cases for form widget

  • Loading branch information...
1 parent 3704883 commit 5de69bc518d8d5186efb55f600b1763f58d3b54a Omer Saeed committed Jul 30, 2012
Showing with 80 additions and 0 deletions.
  1. +65 −0 src/widgets/form/test.js
  2. +15 −0 src/widgets/form/testtmpl.mtpl
View
@@ -0,0 +1,65 @@
+/*global test, asyncTest, ok, equal, deepEqual, start, module, strictEqual, notStrictEqual, raises*/
+define([
+ 'mesh/request',
+ './../messagelist',
+ './../form',
+ 'tmpl!./testtmpl.mtpl',
+ './../../test/api/v1/targetvolumeprofile'
+], function(Request, MessageList, Form, template,
+ TargetVolumeProfile) {
+
+ var origAjax = Request.ajax(function(args) {
+ var data = args.data;
+ window.lastAjaxArgs = args;
+ if (/some-invalid-chars/.test(JSON.parse(data).name)) {
+ args.error({
+ getResponseHeader: function() {return 'application/json';},
+ responseText: JSON.stringify([[{
+ token: 'servererror',
+ message: 'something went wrong server-side'
+ }], {
+ name: [{
+ token: 'nonempty',
+ message: 'name must not be empty'
+ }]
+ }])
+ });
+ } else {
+ args.success({resources: []}, 200, {});
+ }
+ });
+
+ asyncTest('updates to model are propagated to bound widgets', function() {
+
+ var myFrm = Form.extend({
+ nodeTemplate: template,
+
+ defaults: {
+ modelClass: TargetVolumeProfile,
+ collection: null,
+ bindings: [
+ {
+ widget: 'name',
+ field: 'name'
+ }
+ ],
+ widgetize: true
+ }
+ });
+
+ var frm1 = myFrm().appendTo($('body'));
+ var frm2 = myFrm().appendTo($('body'));
+ frm1.getModel().set('name', 'DudeA');
+ frm2.getModel().set('name', 'DudeB');
+ vals1 = frm1.getBoundValues();
+ vals2 = frm2.getBoundValues();
+ setTimeout(function() {
+ equal(vals1.name, 'DudeA');
+ equal(vals2.name, 'DudeB');
+// equal(vals1.name, vals2.name);
+ start();
+ }, 0);
+ });
+
+ start();
+});
@@ -0,0 +1,15 @@
+<form>
+ <div class=messagelist></div>
+
+ <fieldset>
+
+ <label data-for=name>Name:</label>
+ <input name=name type=text />
+ <div class=messagelist data-for=name></div>
+
+ <label data-for=name>Volume ID:</label>
+ <input type=number name=volume_id />
+ <div class=messagelist data-for=volume_id></div>
+
+ </fieldset>
+</form>

0 comments on commit 5de69bc

Please sign in to comment.