Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

432 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. /**
  8. * Tests Ext.data.Store functionality
  9. * @author Ed Spencer
  10. */
  11. (function() {
  12. var suite = Ext.test.session.getSuite('Ext.form.BasicForm'),
  13. assert = Y.Assert;
  14. function buildForm(config) {
  15. config = config || {};
  16. Ext.applyIf(config, {
  17. });
  18. return new Ext.form.BasicForm(null, config);
  19. };
  20. var itemIdCount = 0;
  21. //creates a duck object simulating a form field
  22. function buildFormItem(options) {
  23. itemIdCount ++;
  24. options = options || {};
  25. Ext.applyIf(options, {
  26. valid: true,
  27. dirty: false,
  28. id : itemIdCount
  29. });
  30. return (function(itemId, config) {
  31. return Ext.apply(config || {}, {
  32. isFormField: true,
  33. itemId : itemId,
  34. getName: function() {
  35. return config.name || config.id;
  36. },
  37. getItemId: function() {
  38. return itemId;
  39. },
  40. validate: function() {
  41. return config.valid;
  42. },
  43. isDirty: function() {
  44. return config.dirty;
  45. },
  46. setValue: function() {
  47. return true;
  48. }
  49. });
  50. })(itemIdCount, options);
  51. };
  52. suite.add(new Y.Test.Case({
  53. name: 'initialization with an element',
  54. setUp: function() {
  55. //create a fake form element to test that initEl is called
  56. this.el = Ext.getBody().createChild({tag: 'form'});
  57. },
  58. tearDown: function() {
  59. Ext.destroy(this.el);
  60. },
  61. testItemsCreated: function() {
  62. var form = buildForm();
  63. assert.isTrue(form.items instanceof Ext.util.MixedCollection);
  64. },
  65. testInitsEl: function() {
  66. var BasicForm = Ext.form.BasicForm,
  67. proto = BasicForm.prototype;
  68. var wasCalled = false,
  69. oldInitEl = proto.initEl;
  70. proto.initEl = function() {
  71. wasCalled = true;
  72. };
  73. var form = new BasicForm(this.el, {});
  74. assert.isTrue(wasCalled);
  75. //cleanup
  76. proto.initEl = oldInitEl;
  77. },
  78. testGetEL: function() {
  79. var form = new Ext.form.BasicForm(this.el, {});
  80. assert.areEqual(this.el, form.getEl());
  81. },
  82. testGetsFormClass: function() {
  83. var form = new Ext.form.BasicForm(this.el, {});
  84. assert.isTrue(this.el.hasClass('x-form'));
  85. }
  86. }));
  87. suite.add(new Y.Test.Case({
  88. name: 'validations',
  89. setUp: function() {
  90. this.form = buildForm();
  91. this.validItem = buildFormItem({valid: true});
  92. this.invalidItem = buildFormItem({valid: false});
  93. },
  94. testAllValid: function() {
  95. this.form.add(this.validItem);
  96. assert.isTrue(this.form.isValid());
  97. },
  98. testSomeInvalid: function() {
  99. this.form.add(this.validItem, this.invalidItem);
  100. assert.isFalse(this.form.isValid());
  101. }
  102. }));
  103. suite.add(new Y.Test.Case({
  104. name: 'dirtiness',
  105. setUp: function() {
  106. this.form = buildForm();
  107. this.cleanItem = buildFormItem({dirty: false});
  108. this.dirtyItem = buildFormItem({dirty: true});
  109. },
  110. testAllClean: function() {
  111. this.form.add(this.cleanItem);
  112. assert.isFalse(this.form.isDirty());
  113. },
  114. testSomeDirty: function() {
  115. this.form.add(this.cleanItem, this.dirtyItem);
  116. assert.isTrue(this.form.isDirty());
  117. }
  118. }));
  119. suite.add(new Y.Test.Case({
  120. name: 'performing actions'
  121. }));
  122. suite.add(new Y.Test.Case({
  123. name: 'submission'
  124. }));
  125. suite.add(new Y.Test.Case({
  126. name: 'loading, resetting and updating records',
  127. setUp: function() {
  128. this.form = buildForm();
  129. this.item1 = buildFormItem({id: 'name'});
  130. this.item2 = buildFormItem({id: 'email'});
  131. this.item3 = buildFormItem({id: 'phone'});
  132. this.form.add(this.item1, this.item2, this.item3);
  133. },
  134. testReset: function() {
  135. var resetCount = 0;
  136. var reset = function() {
  137. resetCount ++;
  138. };
  139. this.item1.reset = reset;
  140. this.item2.reset = reset;
  141. this.item3.reset = reset;
  142. this.form.reset();
  143. assert.areEqual(3, resetCount);
  144. }
  145. }));
  146. suite.add(new Y.Test.Case({
  147. name: 'finding fields',
  148. setUp: function() {
  149. this.form = buildForm();
  150. this.item1 = buildFormItem({id: 'name', dataIndex: 'someDataIndex'});
  151. this.item2 = buildFormItem({id: 'email'});
  152. this.item3 = buildFormItem({id: 'phone', name: 'phone number'});
  153. this.form.add(this.item1, this.item2, this.item3);
  154. },
  155. testfindByItemId: function() {
  156. assert.areEqual(this.item3, this.form.findField(this.item3.itemId));
  157. },
  158. testFindById: function() {
  159. assert.areEqual(this.item2, this.form.findField('email'));
  160. },
  161. testFindByDataIndex: function() {
  162. assert.areEqual(this.item1, this.form.findField('someDataIndex'));
  163. },
  164. testFindByName: function() {
  165. assert.areEqual(this.item3, this.form.findField('phone number'));
  166. }
  167. }));
  168. suite.add(new Y.Test.Case({
  169. name: 'marking invalid',
  170. setUp: function() {
  171. this.form = buildForm();
  172. this.item1 = buildFormItem({id: 'name'});
  173. this.item2 = buildFormItem({id: 'email'});
  174. this.item3 = buildFormItem({id: 'phone'});
  175. this.form.add(this.item1, this.item2, this.item3);
  176. },
  177. testMarkInvalidWithObject: function() {
  178. var wasCalled = false, message;
  179. this.item1.markInvalid = function(msg) {
  180. wasCalled = true;
  181. message = msg;
  182. };
  183. this.form.markInvalid({
  184. name: 'is a bad name'
  185. });
  186. assert.isTrue(wasCalled);
  187. assert.areEqual('is a bad name', message);
  188. },
  189. testMarkInvalidWithArray: function() {
  190. var wasCalled = false, message;
  191. this.item2.markInvalid = function(msg) {
  192. wasCalled = true;
  193. message = msg;
  194. };
  195. this.form.markInvalid([{id: 'email', msg: 'is the wrong format'}]);
  196. assert.isTrue(wasCalled);
  197. assert.areEqual('is the wrong format', message);
  198. },
  199. testClearInvalid: function() {
  200. var clearCount = 0;
  201. var clearInvalid = function() {
  202. clearCount ++;
  203. };
  204. this.item1.clearInvalid = clearInvalid;
  205. this.item2.clearInvalid = clearInvalid;
  206. this.item3.clearInvalid = clearInvalid;
  207. this.form.clearInvalid();
  208. assert.areEqual(3, clearCount);
  209. }
  210. }));
  211. suite.add(new Y.Test.Case({
  212. name: 'setting values',
  213. setUp: function() {
  214. this.form = buildForm({
  215. trackResetOnLoad: true
  216. });
  217. this.item1 = buildFormItem({name: 'name', getValue: function() {return 'ed'; }});
  218. this.item2 = buildFormItem({name: 'email'});
  219. this.item3 = buildFormItem({name: 'phone', getValue: function() {return '333';}});
  220. this.form.add(this.item1, this.item2, this.item3);
  221. },
  222. testSetByObject: function() {
  223. var called = false, value;
  224. this.item1.setValue = function(val) {
  225. wasCalled = true;
  226. value = val;
  227. };
  228. this.form.setValues({name: 'my name'});
  229. assert.isTrue(wasCalled);
  230. assert.areEqual('my name', value);
  231. },
  232. testSetByArray: function() {
  233. var called = false, value;
  234. this.item1.setValue = function(val) {
  235. wasCalled = true;
  236. value = val;
  237. };
  238. this.form.setValues([{id: 'name', value: 'my name'}]);
  239. assert.isTrue(wasCalled);
  240. assert.areEqual('my name', value);
  241. },
  242. testSavesOriginalValue: function() {
  243. this.form.setValues({'phone': '444'});
  244. assert.areEqual('333', this.item3.originalValue);
  245. }
  246. }));
  247. suite.add(new Y.Test.Case({
  248. name: 'getting values',
  249. setUp: function() {
  250. this.form = buildForm();
  251. this.item1 = buildFormItem({name: 'name', getValue: function() {return 'Edward';}});
  252. this.item2 = buildFormItem({name: 'email', getValue: function() {return 'ed@extjs.com';}});
  253. this.item3 = buildFormItem({name: 'phone', getValue: function() {return '333';}});
  254. this.form.add(this.item1, this.item2, this.item3);
  255. },
  256. testGetFieldValues: function() {
  257. var values = this.form.getFieldValues();
  258. assert.areEqual('Edward', values.name);
  259. assert.areEqual('ed@extjs.com', values.email);
  260. assert.areEqual('333', values.phone);
  261. },
  262. testGetFieldValuesWithArray: function() {
  263. //adding a second item with the same name should result in an array being returned
  264. this.item4 = buildFormItem({
  265. name : 'name',
  266. getValue: function() {return 200;}
  267. });
  268. this.form.add(this.item4);
  269. var values = this.form.getFieldValues();
  270. assert.areEqual(2, values.name.length);
  271. assert.areEqual('Edward', values.name[0]);
  272. assert.areEqual(200, values.name[1]);
  273. },
  274. testGetDirtyFieldValues: function() {
  275. this.item1.isDirty = function() {return true;};
  276. var values = this.form.getFieldValues(true);
  277. assert.areEqual('Edward', values.name);
  278. assert.isUndefined(values.email);
  279. assert.isUndefined(values.phone);
  280. },
  281. testGetValuesForSubmission: function() {
  282. //pending
  283. }
  284. }));
  285. suite.add(new Y.Test.Case({
  286. name: 'adding and removing',
  287. setUp: function() {
  288. this.form = buildForm();
  289. this.item1 = buildFormItem({id: 'name'});
  290. this.item2 = buildFormItem({id: 'email'});
  291. this.item3 = buildFormItem({id: 'phone'});
  292. this.form.add(this.item1);
  293. },
  294. testAddField: function() {
  295. var count = this.form.items.getCount();
  296. this.form.add(this.item2);
  297. assert.areEqual(count + 1, this.form.items.getCount());
  298. },
  299. testAddMultipleFields: function() {
  300. var count = this.form.items.getCount();
  301. this.form.add(this.item2, this.item3);
  302. assert.areEqual(count + 2, this.form.items.getCount());
  303. },
  304. testAddReturnsForm: function() {
  305. assert.areEqual(this.form, this.form.add(this.item2));
  306. },
  307. testRemoveField: function() {
  308. var count = this.form.items.getCount();
  309. this.form.remove(this.item1);
  310. assert.areEqual(count - 1, this.form.items.getCount());
  311. },
  312. testRemoveReturnsForm: function() {
  313. assert.areEqual(this.form, this.form.remove(this.item1));
  314. }
  315. }));
  316. })();