form_test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /*eslint-disable mocha-cleanup/no-assertions-outside-it */
  21. var form,
  22. field,
  23. formField,
  24. resultCases = [
  25. {
  26. text: Em.I18n.t('form.saveError'),
  27. result: -1
  28. },
  29. {
  30. text: Em.I18n.t('form.saveSuccess'),
  31. result: 1
  32. },
  33. {
  34. text: '',
  35. result: 0
  36. }
  37. ],
  38. displayTypeCases = [
  39. {
  40. type: 'checkbox',
  41. classString: 'Checkbox'
  42. },
  43. {
  44. type: 'select',
  45. classString: 'Select'
  46. },
  47. {
  48. type: 'textarea',
  49. classString: 'TextArea'
  50. },
  51. {
  52. type: 'password',
  53. classString: 'TextField'
  54. },
  55. {
  56. type: 'hidden',
  57. classString: 'TextField'
  58. }
  59. ],
  60. hiddenCases = [
  61. {
  62. displayType: 'password',
  63. type: 'hidden',
  64. value: false
  65. },
  66. {
  67. displayType: 'hidden',
  68. type: 'hidden',
  69. value: true
  70. }
  71. ],
  72. expectError = function (message) {
  73. formField.validate();
  74. expect(formField.get('errorMessage')).to.equal(message);
  75. };
  76. /*eslint-enable mocha-cleanup/no-assertions-outside-it */
  77. describe('App.Form', function () {
  78. beforeEach(function () {
  79. form = App.Form.create({
  80. fieldsOptions: [
  81. {
  82. name: 'field0',
  83. value: 'value0',
  84. isRequired: false
  85. }
  86. ]
  87. });
  88. field = form.get('fields').objectAt(0);
  89. });
  90. describe('#fields', function () {
  91. it('should get data from formFields', function () {
  92. var fields = form.get('fields');
  93. expect(fields).to.have.length(1);
  94. expect(field.get('name')).to.equal('field0');
  95. });
  96. });
  97. describe('#field', function () {
  98. it('should get data from formFields', function () {
  99. var field0 = form.get('field.field0');
  100. expect(form.get('field')).to.not.be.empty;
  101. expect(field0.get('name')).to.equal('field0');
  102. expect(field0.get('form')).to.eql(form);
  103. });
  104. });
  105. describe('#getField', function () {
  106. it('should get field0', function () {
  107. expect(form.getField('field0')).to.eql(form.get('field.field0'));
  108. });
  109. it('should be empty', function () {
  110. form.set('fields', []);
  111. expect(form.getField()).to.be.empty;
  112. });
  113. });
  114. describe('#isValid', function () {
  115. it('should be true', function () {
  116. field.set('isRequired', false);
  117. expect(form.isValid()).to.be.true;
  118. });
  119. it('should be false', function () {
  120. field.setProperties({
  121. isRequired: true,
  122. value: ''
  123. });
  124. expect(form.isValid()).to.be.false;
  125. });
  126. });
  127. describe('#updateValues', function () {
  128. it('should update field0 value', function () {
  129. form.set('object', Em.Object.create({field0: 'value0upd'}));
  130. expect(field.get('value')).to.equal('value0upd');
  131. });
  132. it('should empty password value', function () {
  133. field.set('displayType', 'password');
  134. form.set('object', Em.Object.create());
  135. expect(field.get('value')).to.be.empty;
  136. });
  137. it('should clear values', function () {
  138. form.set('object', []);
  139. expect(field.get('value')).to.be.empty;
  140. });
  141. });
  142. describe('#clearValues', function () {
  143. it('should clear values', function () {
  144. var field0 = form.get('fields').objectAt(0);
  145. field0.set('value', 'value0');
  146. form.clearValues();
  147. expect(field0.get('value')).to.be.empty;
  148. });
  149. });
  150. describe('#resultText', function () {
  151. resultCases.forEach(function (item) {
  152. it('should be ' + item.text, function () {
  153. form.set('result', item.result);
  154. expect(form.get('resultText')).to.equal(item.text);
  155. });
  156. });
  157. });
  158. });
  159. describe('App.FormField', function () {
  160. beforeEach(function () {
  161. formField = App.FormField.create();
  162. });
  163. describe('#isValid', function () {
  164. it('should be true', function () {
  165. expect(formField.get('isValid')).to.be.true;
  166. });
  167. it('should be false', function () {
  168. formField.set('errorMessage', 'error');
  169. expect(formField.get('isValid')).to.be.false;
  170. });
  171. });
  172. describe('#viewClass', function () {
  173. displayTypeCases.forEach(function (item) {
  174. it('should be ' + item.classString, function () {
  175. formField.set('displayType', item.type);
  176. expect(formField.get('viewClass').toString()).to.contain(item.classString);
  177. });
  178. });
  179. });
  180. describe('#validate', function () {
  181. it('should return error message', function () {
  182. formField.set('isRequired', true);
  183. expectError('This is required');
  184. });
  185. it('should return empty error message', function () {
  186. formField.set('isRequired', false);
  187. expectError('');
  188. formField.set('value', 'value');
  189. expectError('');
  190. });
  191. });
  192. describe('#isHiddenField', function () {
  193. hiddenCases.forEach(function (item) {
  194. it('should be ' + item.value, function () {
  195. formField.setProperties(item);
  196. expect(formField.get('isHiddenField')).to.equal(item.value);
  197. });
  198. });
  199. });
  200. });