form_test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. require('models/form');
  20. var form,
  21. field,
  22. formField,
  23. resultCases = [
  24. {
  25. text: Em.I18n.t('form.saveError'),
  26. result: -1
  27. },
  28. {
  29. text: Em.I18n.t('form.saveSuccess'),
  30. result: 1
  31. },
  32. {
  33. text: '',
  34. result: 0
  35. }
  36. ],
  37. displayTypeCases = [
  38. {
  39. type: 'checkbox',
  40. classString: 'Checkbox'
  41. },
  42. {
  43. type: 'select',
  44. classString: 'Select'
  45. },
  46. {
  47. type: 'textarea',
  48. classString: 'TextArea'
  49. },
  50. {
  51. type: 'password',
  52. classString: 'TextField'
  53. },
  54. {
  55. type: 'hidden',
  56. classString: 'TextField'
  57. }
  58. ],
  59. hiddenCases = [
  60. {
  61. displayType: 'password',
  62. type: 'hidden',
  63. value: false
  64. },
  65. {
  66. displayType: 'hidden',
  67. type: 'hidden',
  68. value: true
  69. }
  70. ],
  71. expectError = function (message) {
  72. formField.validate();
  73. expect(formField.get('errorMessage')).to.equal(message);
  74. };
  75. describe('App.Form', function () {
  76. beforeEach(function () {
  77. form = App.Form.create({
  78. fieldsOptions: [
  79. {
  80. name: 'field0',
  81. value: 'value0',
  82. isRequired: false
  83. }
  84. ]
  85. });
  86. field = form.get('fields').objectAt(0);
  87. });
  88. describe('#fields', function () {
  89. it('should get data from formFields', function () {
  90. var fields = form.get('fields');
  91. expect(fields).to.have.length(1);
  92. expect(field.get('name')).to.equal('field0');
  93. });
  94. });
  95. describe('#field', function () {
  96. it('should get data from formFields', function () {
  97. var field0 = form.get('field.field0');
  98. expect(form.get('field')).to.not.be.empty;
  99. expect(field0.get('name')).to.equal('field0');
  100. expect(field0.get('form')).to.eql(form);
  101. });
  102. });
  103. describe('#getField', function () {
  104. it('should get field0', function () {
  105. expect(form.getField('field0')).to.eql(form.get('field.field0'));
  106. });
  107. it('should be empty', function () {
  108. form.set('fields', []);
  109. expect(form.getField()).to.be.empty;
  110. });
  111. });
  112. describe('#isValid', function () {
  113. it('should be true', function () {
  114. field.set('isRequired', false);
  115. expect(form.isValid()).to.be.true;
  116. });
  117. it('should be false', function () {
  118. field.setProperties({
  119. isRequired: true,
  120. value: ''
  121. });
  122. expect(form.isValid()).to.be.false;
  123. });
  124. });
  125. describe('#updateValues', function () {
  126. it('should update field0 value', function () {
  127. form.set('object', Em.Object.create({field0: 'value0upd'}));
  128. expect(field.get('value')).to.equal('value0upd');
  129. });
  130. it('should empty password value', function () {
  131. field.set('displayType', 'password');
  132. form.set('object', Em.Object.create());
  133. expect(field.get('value')).to.be.empty;
  134. });
  135. it('should clear values', function () {
  136. form.set('object', []);
  137. expect(field.get('value')).to.be.empty;
  138. });
  139. });
  140. describe('#clearValues', function () {
  141. it('should clear values', function () {
  142. var field0 = form.get('fields').objectAt(0);
  143. field0.set('value', 'value0');
  144. form.clearValues();
  145. expect(field0.get('value')).to.be.empty;
  146. });
  147. });
  148. describe('#resultText', function () {
  149. resultCases.forEach(function (item) {
  150. it('should be ' + item.text, function () {
  151. form.set('result', item.result);
  152. expect(form.get('resultText')).to.equal(item.text);
  153. });
  154. });
  155. });
  156. });
  157. describe('App.FormField', function () {
  158. beforeEach(function () {
  159. formField = App.FormField.create();
  160. });
  161. describe('#isValid', function () {
  162. it('should be true', function () {
  163. expect(formField.get('isValid')).to.be.true;
  164. });
  165. it('should be false', function () {
  166. formField.set('errorMessage', 'error');
  167. expect(formField.get('isValid')).to.be.false;
  168. });
  169. });
  170. describe('#viewClass', function () {
  171. displayTypeCases.forEach(function (item) {
  172. it('should be ' + item.classString, function () {
  173. formField.set('displayType', item.type);
  174. expect(formField.get('viewClass').toString()).to.contain(item.classString);
  175. });
  176. });
  177. });
  178. describe('#validate', function () {
  179. it('should return error message', function () {
  180. formField.set('isRequired', true);
  181. expectError('This is required');
  182. });
  183. it('should return empty error message', function () {
  184. formField.set('isRequired', false);
  185. expectError('');
  186. formField.set('value', 'value');
  187. expectError('');
  188. });
  189. });
  190. describe('#isHiddenField', function () {
  191. hiddenCases.forEach(function (item) {
  192. it('should be ' + item.value, function () {
  193. formField.setProperties(item);
  194. expect(formField.get('isHiddenField')).to.equal(item.value);
  195. });
  196. });
  197. });
  198. });