input-tester.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { NgZone } from '@angular/core';
  2. const lorem_ipsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi maximus nisl lobortis interdum condimentum. Cras volutpat, massa quis vehicula eleifend, turpis mauris sodales erat, ut varius ligula ipsum et turpis. Aliquam erat volutpat. Maecenas sodales pellentesque auctor. Suspendisse faucibus a erat sit amet pretium. Vestibulum nec tempus tellus. Mauris fringilla faucibus dui sed vestibulum. Curabitur porttitor consectetur nisl. Nulla porta, neque sed congue tempus, erat nunc rutrum diam, eu elementum sapien leo quis eros. Donec non convallis felis. Nam eu pharetra sapien.';
  3. export const TEXT_CORPUS = [
  4. ['hola', 'hola'],
  5. ['', ''],
  6. [' ', ' '],
  7. ['adiós', 'adiós'],
  8. ['hola y adiós', 'hola y adiós'],
  9. [lorem_ipsum, lorem_ipsum]
  10. ];
  11. export const NUMBER_CORPUS = [
  12. [-1, -1],
  13. [0, 0],
  14. [-123456789, -123456789],
  15. [1.1234, 1.1234],
  16. [123456789, 123456789],
  17. ['1.1234', 1.1234],
  18. ['123456789', 123456789],
  19. ['-123456789', -123456789]
  20. ];
  21. export const BOOLEAN_CORPUS = [
  22. [true, true],
  23. [false, false],
  24. ['', true],
  25. ['false', false],
  26. ['true', true],
  27. ];
  28. export const ANY_CORPUS = [
  29. [true, true],
  30. [false, false],
  31. [0, 0],
  32. ['', ''],
  33. [' ', ' '],
  34. ['hola', 'hola']
  35. ];
  36. export function commonInputTest(input, config) {
  37. // TODO test form register/deregister
  38. // TODO test item classes
  39. // TODO test disable
  40. const zone = new NgZone({ enableLongStackTrace: true });
  41. zone.run(() => {
  42. if (config.testItem === true && !input._item) {
  43. (void 0) /* assert */;
  44. }
  45. if (config.testForm === true && !input._form) {
  46. (void 0) /* assert */;
  47. }
  48. // Run tests before initialization
  49. testInput(input, config, false);
  50. input.ngAfterContentInit();
  51. input.ngAfterViewInit && input.ngAfterViewInit();
  52. // Run tests after initialization
  53. testInput(input, config, true);
  54. // Run tests without item
  55. if (config.testItem === true && !input._item) {
  56. input._item = undefined;
  57. testInput(input, config, true);
  58. }
  59. // Run tests without item
  60. if (config.testForm === true && !input._form) {
  61. input._form = undefined;
  62. testInput(input, config, true);
  63. }
  64. testInput(input, config, true);
  65. input.ngOnDestroy();
  66. (void 0) /* assert */;
  67. });
  68. }
  69. function testInput(input, config, isInit) {
  70. testState(input, config, isInit);
  71. testWriteValue(input, config, isInit);
  72. testNgModelChange(input, config, isInit);
  73. }
  74. function testState(input, config, isInit) {
  75. assertEqual(input._init, isInit, 'input must be init');
  76. assertEqual(input._isFocus, false, 'should not be focus');
  77. assertEqual(input.isFocus(), false, 'should not be focus');
  78. assertEqual(input.value, config.defaultValue, 'default value is wrong');
  79. if (isInit) {
  80. let blurCount = 0;
  81. let focusCount = 0;
  82. let onTouchedCalled = 0;
  83. const subBlur = input.ionBlur.subscribe((ev) => {
  84. assertEqual(ev, input, 'ionBlur argument is wrong');
  85. blurCount++;
  86. if (config.onFocusChange && config.onFocusChange(false) !== true) {
  87. (void 0) /* assert */;
  88. }
  89. });
  90. const subFocus = input.ionFocus.subscribe((ev) => {
  91. assertEqual(ev, input, 'ionFocus argument is wrong');
  92. focusCount++;
  93. if (config.onFocusChange && config.onFocusChange(true) !== true) {
  94. (void 0) /* assert */;
  95. }
  96. });
  97. input.registerOnTouched(() => {
  98. assertEqual(onTouchedCalled, 0, 'registerOnTouched: internal error');
  99. onTouchedCalled++;
  100. });
  101. input._fireBlur();
  102. assertEqual(blurCount, 0, 'blur should not have been emitted');
  103. assertEqual(onTouchedCalled, 0, 'touched should not have been called');
  104. input._fireFocus();
  105. assertEqual(input._isFocus, true, 'should be focus');
  106. assertEqual(input.isFocus(), true, 'should be focus');
  107. input._fireFocus();
  108. input._fireBlur();
  109. assertEqual(input._isFocus, false, 'should be not focus');
  110. assertEqual(input.isFocus(), false, 'should be not focus');
  111. assertEqual(onTouchedCalled, 1, 'touched should have been called');
  112. input._fireBlur(); // it should not crash
  113. assertEqual(focusCount, 1, 'ionFocus was not called correctly');
  114. assertEqual(blurCount, 1, 'ionBlur was not called correctly');
  115. subBlur.unsubscribe();
  116. subFocus.unsubscribe();
  117. }
  118. }
  119. function testWriteValue(input, config, isInit) {
  120. let test;
  121. let i;
  122. let ionChangeCalled = 0;
  123. let OnChangeCalled = 0;
  124. let OnTouchedCalled = 0;
  125. let ngModelValue;
  126. // Test ionChange
  127. let sub = input.ionChange.subscribe((ev) => {
  128. assertEqual(ionChangeCalled, 0, 'ionChange: internal error');
  129. assertEqual(ev, input, 'ionChange: ev is not the input');
  130. assertEqual(ev.value, test[1], 'ionChange: value does not match');
  131. assertEqual(ngModelValue, test[1], 'ionChange: ngmodel was not updated');
  132. ionChangeCalled++;
  133. });
  134. // Test registerOnChange
  135. input.registerOnChange((ev) => {
  136. assertEqual(OnChangeCalled, 0, 'registerOnChange: internal error');
  137. assertEqual(input.value, ev, 'registerOnChange: ev output does not match');
  138. assertEqual(input.value, test[1], 'registerOnChange: value does not match');
  139. ngModelValue = ev;
  140. OnChangeCalled++;
  141. });
  142. // Test registerOnTouched
  143. input.registerOnTouched(() => {
  144. assertEqual(OnTouchedCalled, 0, 'registerOnTouched: internal error');
  145. OnTouchedCalled++;
  146. });
  147. // Run corpus
  148. for (i = 0; i < config.corpus.length; i++) {
  149. test = config.corpus[i];
  150. input.value = test[0];
  151. assertEqual(input.value, test[1], 'loop: input/output does not match');
  152. if (isInit) {
  153. assertEqual(ionChangeCalled, 1, 'loop: ionChange error');
  154. if (config.onValueChange && config.onValueChange(test[1]) !== true) {
  155. (void 0) /* assert */;
  156. }
  157. }
  158. else {
  159. assertEqual(ionChangeCalled, 0, 'loop: ionChange error');
  160. }
  161. assertEqual(OnChangeCalled, 1, 'loop: OnChangeCalled was not called');
  162. assertEqual(OnTouchedCalled, 0, 'loop: OnTouchedCalled was called');
  163. OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;
  164. // Set same value (it should not redispatch)
  165. input.value = test[0];
  166. assertEqual(ionChangeCalled, 0, 'loop: ionChange should not be called');
  167. assertEqual(OnChangeCalled, 0, 'loop: OnChangeCalled should not be called');
  168. // TODO OnTouchedCalled?
  169. OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;
  170. }
  171. // Test undefined
  172. input.value = undefined;
  173. assertEqual(input.value, test[1], 'undefined should not change the value');
  174. assertEqual(ionChangeCalled, 0, 'undefined: ionChange should not be called');
  175. assertEqual(OnChangeCalled, 0, 'undefined: OnChangeCalled should not be called');
  176. assertEqual(OnTouchedCalled, 0, 'undefined: OnTouchedCalled should not be called');
  177. // Test null (reset)
  178. test = [null, config.defaultValue];
  179. input.value = null;
  180. assertEqual(input.value, config.defaultValue, 'null: wrong default value');
  181. assertEqual(OnChangeCalled, 1, 'null: OnChangeCalled was not called');
  182. assertEqual(OnTouchedCalled, 0, 'null: OnTouchedCalled was called');
  183. input.registerOnChange(null);
  184. input.registerOnTouched(null);
  185. sub.unsubscribe();
  186. }
  187. function testNgModelChange(input, config, isInit) {
  188. let test;
  189. let i;
  190. let ionChangeCalled = 0;
  191. let OnChangeCalled = 0;
  192. let OnTouchedCalled = 0;
  193. // Test ionChange
  194. let sub = input.ionChange.subscribe((ev) => {
  195. assertEqual(ionChangeCalled, 0, 'internal error');
  196. assertEqual(ev, input, 'ev output does not match');
  197. assertEqual(test[1], ev.value, 'value does not match');
  198. ionChangeCalled++;
  199. });
  200. // Test registerOnChange
  201. input.registerOnChange(() => {
  202. OnChangeCalled++;
  203. });
  204. // Test registerOnChange
  205. input.registerOnTouched(() => {
  206. OnTouchedCalled++;
  207. });
  208. // Run corpus
  209. for (i = 0; i < config.corpus.length; i++) {
  210. test = config.corpus[i];
  211. input.writeValue(test[0]);
  212. assertEqual(input.value, test[1], 'input/output does not match');
  213. if (isInit) {
  214. assertEqual(ionChangeCalled, 1, 'ionChange error');
  215. if (config.onValueChange && config.onValueChange(test[1]) !== true) {
  216. (void 0) /* assert */;
  217. }
  218. }
  219. else {
  220. assertEqual(ionChangeCalled, 0, 'ionChange error');
  221. }
  222. assertEqual(OnChangeCalled, 0, 'OnChangeCalled should not be called');
  223. assertEqual(OnTouchedCalled, 0, 'OnTouchedCalled should not be called');
  224. OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;
  225. // Set same value (it should not redispatch)
  226. input.writeValue(test[0]);
  227. input.value = test[0];
  228. assertEqual(ionChangeCalled, 0, 'ionChange should not be called');
  229. assertEqual(OnChangeCalled, 0, 'OnChangeCalled should not be called');
  230. // TODO OnTouchedCalled?
  231. OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;
  232. }
  233. input.registerOnChange(null);
  234. input.registerOnTouched(null);
  235. sub.unsubscribe();
  236. input.value = config.defaultValue;
  237. }
  238. function assertEqual(a, b, message) {
  239. if (!equal(a, b)) {
  240. (void 0) /* assert */;
  241. }
  242. }
  243. function equal(a, b) {
  244. if (a === b) {
  245. return true;
  246. }
  247. // return false;
  248. return JSON.stringify(a) === JSON.stringify(b);
  249. }
  250. //# sourceMappingURL=input-tester.js.map