form_test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. hiddenCases = [
  38. {
  39. displayType: 'password',
  40. type: 'hidden',
  41. value: false
  42. },
  43. {
  44. displayType: 'hidden',
  45. type: 'hidden',
  46. value: true
  47. }
  48. ];
  49. describe('App.Form', function () {
  50. beforeEach(function () {
  51. form = App.Form.create({
  52. fieldsOptions: [
  53. {
  54. name: 'field0',
  55. value: 'value0',
  56. isRequired: false
  57. }
  58. ]
  59. });
  60. field = form.get('fields').objectAt(0);
  61. });
  62. describe('#fields', function () {
  63. it('should get data from formFields', function () {
  64. var fields = form.get('fields');
  65. expect(fields).to.have.length(1);
  66. expect(field.get('name')).to.equal('field0');
  67. });
  68. });
  69. describe('#field', function () {
  70. it('should get data from formFields', function () {
  71. var field0 = form.get('field.field0');
  72. expect(form.get('field')).to.not.be.empty;
  73. expect(field0.get('name')).to.equal('field0');
  74. expect(field0.get('form')).to.eql(form);
  75. });
  76. });
  77. describe('#getField', function () {
  78. it('should get field0', function () {
  79. expect(form.getField('field0')).to.eql(form.get('field.field0'));
  80. });
  81. it('should be empty', function () {
  82. form.set('fields', []);
  83. expect(form.getField()).to.be.empty;
  84. });
  85. });
  86. describe('#isValid', function () {
  87. it('should be true', function () {
  88. field.set('isRequired', false);
  89. expect(form.isValid()).to.be.true;
  90. });
  91. it('should be false', function () {
  92. field.setProperties({
  93. isRequired: true,
  94. value: ''
  95. });
  96. expect(form.isValid()).to.be.false;
  97. });
  98. });
  99. describe('#updateValues', function () {
  100. it('should update field0 value', function () {
  101. form.set('object', Em.Object.create({field0: 'value0upd'}));
  102. expect(field.get('value')).to.equal('value0upd');
  103. });
  104. it('should empty password value', function () {
  105. field.set('displayType', 'password');
  106. form.set('object', Em.Object.create());
  107. expect(field.get('value')).to.be.empty;
  108. });
  109. it('should clear values', function () {
  110. form.set('object', []);
  111. expect(field.get('value')).to.be.empty;
  112. });
  113. });
  114. describe('#clearValues', function () {
  115. it('should clear values', function () {
  116. var field0 = form.get('fields').objectAt(0);
  117. field0.set('value', 'value0');
  118. form.clearValues();
  119. expect(field0.get('value')).to.be.empty;
  120. });
  121. });
  122. describe('#resultText', function () {
  123. resultCases.forEach(function (item) {
  124. it('should be ' + item.text, function () {
  125. form.set('result', item.result);
  126. expect(form.get('resultText')).to.equal(item.text);
  127. });
  128. });
  129. });
  130. });
  131. describe('App.FormField', function () {
  132. beforeEach(function () {
  133. formField = App.FormField.create();
  134. });
  135. describe('#isValid', function () {
  136. it('should be true', function () {
  137. expect(formField.get('isValid')).to.be.true;
  138. });
  139. it('should be false', function () {
  140. formField.set('errorMessage', 'error');
  141. expect(formField.get('isValid')).to.be.false;
  142. });
  143. });
  144. /*eslint-enable mocha-cleanup/asserts-limit */
  145. describe('#isHiddenField', function () {
  146. hiddenCases.forEach(function (item) {
  147. it('should be ' + item.value, function () {
  148. formField.setProperties(item);
  149. expect(formField.get('isHiddenField')).to.equal(item.value);
  150. });
  151. });
  152. });
  153. });