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.

446 lines
13 KiB

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