Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.14 KB | None | 0 0
  1. describe('numericInput: test spec', function () {
  2.  
  3. var $scope,
  4. $compile,
  5. $filter,
  6. inputElement,
  7. templateDefault = '<input type="tel" class="form-control" data-ng-model="model" data-numeric-input required />',
  8. templateDefaultWithMaxLength = '<input type="tel" class="form-control" data-ng-model="model" data-numeric-input max-length="2" required />',
  9. templateDefaultWithMinAndLengthToEnableMin = '<input type="tel" class="form-control" data-ng-model="model" min="10000" length-to-enable-min-validation="4" data-numeric-input required />',
  10. templateDefaultWithMinMax = '<input type="tel" class="form-control" data-ng-model="model" min="100" max="1000" data-numeric-input required />',
  11. templateDefaultWithMinAndmustLargeThanMin = '<input type="tel" class="form-control" data-ng-model="model" min="100" must-large-than-min="true" data-numeric-input required />',
  12. templateAllowDecimal = '<input type="tel" class="form-control" data-ng-model="model" data-numeric-input allow-decimal="true" required />',
  13. templateAllowDecimalWithMinMax = '<input type="tel" class="form-control" data-ng-model="model" min="100" max="1000" data-numeric-input allow-decimal="true" required />';
  14. var initDefault = function (model) {
  15. $scope.model = model;
  16. inputElement = $compile(templateDefault)($scope);
  17. $scope.$apply();
  18. };
  19.  
  20. var initDefaultWithMinAndmustLargeThanMin = function (model) {
  21. $scope.model = model;
  22. inputElement = $compile(templateDefaultWithMinAndmustLargeThanMin)($scope);
  23. $scope.$apply();
  24. };
  25.  
  26. var intDefaultWithMaxLength = function (model) {
  27. $scope.model = model;
  28. inputElement = $compile(templateDefaultWithMaxLength)($scope);
  29. $scope.$apply();
  30. };
  31.  
  32. var initDefaultWithMinMax = function (model) {
  33. $scope.model = model;
  34. inputElement = $compile(templateDefaultWithMinMax)($scope);
  35. $scope.$apply();
  36. };
  37.  
  38. var initefaultWithMinAndLengthToEnableMin = function (model) {
  39. $scope.model = model;
  40. inputElement = $compile(templateDefaultWithMinAndLengthToEnableMin)($scope);
  41. $scope.$apply();
  42. };
  43.  
  44. var initAllowDecimalWithMinMax = function (model) {
  45. $scope.model = model;
  46. inputElement = $compile(templateAllowDecimalWithMinMax)($scope);
  47. $scope.$apply();
  48. };
  49.  
  50. var initAllowDecimal = function (model) {
  51. $scope.model = model;
  52. inputElement = $compile(templateAllowDecimal)($scope);
  53. $scope.$apply();
  54. };
  55.  
  56. beforeEach(function () {
  57. module('app.common');
  58. inject(function ($injector) {
  59. $scope = $injector.get('$rootScope').$new();
  60. $compile = $injector.get('$compile');
  61. $filter = $injector.get('$filter');
  62. });
  63. });
  64.  
  65. it('test for default config: input 1234 should be auto-formatting to 1,234', function () {
  66. initDefault();
  67.  
  68. inputElement.val('1234');
  69. inputElement.triggerHandler('input');
  70.  
  71. var ngModel = inputElement.controller('ngModel');
  72.  
  73. expect(ngModel.$viewValue).toEqual('1,234');
  74. expect(inputElement.val()).toEqual('1,234');
  75. });
  76.  
  77. it('test for default config: input 1234. should be auto-formatting to 1,234', function () {
  78. initDefault();
  79.  
  80. inputElement.val('1234.');
  81. inputElement.triggerHandler('input');
  82.  
  83. var ngModel = inputElement.controller('ngModel');
  84.  
  85. expect(ngModel.$viewValue).toEqual('1,234');
  86. expect(inputElement.val()).toEqual('1,234');
  87. });
  88.  
  89. it('test for default config: input 0 should be auto-formatting to 0 with min error', function () {
  90. initDefault();
  91.  
  92. inputElement.val('0');
  93. inputElement.triggerHandler('input');
  94.  
  95. var ngModel = inputElement.controller('ngModel');
  96. expect(ngModel.$viewValue).toEqual('0');
  97. expect(inputElement.val()).toEqual('0');
  98. expect(ngModel.$error.min).toEqual(true);
  99. });
  100.  
  101. it('test for default config: input . should be auto-formatting to 0 with min error', function () {
  102. initDefault();
  103.  
  104. inputElement.val('.');
  105. inputElement.triggerHandler('input');
  106.  
  107. var ngModel = inputElement.controller('ngModel');
  108. expect(ngModel.$viewValue).toEqual('0');
  109. expect(inputElement.val()).toEqual('0');
  110. expect(ngModel.$error.min).toEqual(true);
  111. });
  112.  
  113. it('test for default config With min 1000 and lengthToEnableMin 4: input 999 should be auto-formatting to 999 with no min error', function () {
  114. initefaultWithMinAndLengthToEnableMin();
  115.  
  116. inputElement.val('999');
  117. inputElement.triggerHandler('input');
  118.  
  119. var ngModel = inputElement.controller('ngModel');
  120. expect(ngModel.$viewValue).toEqual('999');
  121. expect(inputElement.val()).toEqual('999');
  122. expect(ngModel.$error.min).toEqual(undefined);
  123. });
  124.  
  125. it('test for default config with min 100 and max 1000: input 99 should be auto-formatting to 99 with min error', function () {
  126. initDefaultWithMinMax();
  127.  
  128. inputElement.val('99');
  129. inputElement.triggerHandler('input');
  130.  
  131. var ngModel = inputElement.controller('ngModel');
  132. expect(ngModel.$viewValue).toEqual('99');
  133. expect(inputElement.val()).toEqual('99');
  134. expect(ngModel.$error.min).toEqual(true);
  135. });
  136.  
  137. it('test for default config with min 100 and max 1000: input 100 should be auto-formatting to 100 with no min error', function () {
  138. initDefaultWithMinMax();
  139.  
  140. inputElement.val('100');
  141. inputElement.triggerHandler('input');
  142.  
  143. var ngModel = inputElement.controller('ngModel');
  144. expect(ngModel.$viewValue).toEqual('100');
  145. expect(inputElement.val()).toEqual('100');
  146. expect(ngModel.$error.min).toEqual(false);
  147. });
  148.  
  149. it('test for default config with min 100 and max 1000: input 10001 should be auto-formatting to 10,001 with max error', function () {
  150. initDefaultWithMinMax();
  151.  
  152. inputElement.val('10001');
  153. inputElement.triggerHandler('input');
  154.  
  155. var ngModel = inputElement.controller('ngModel');
  156. expect(ngModel.$viewValue).toEqual('10,001');
  157. expect(inputElement.val()).toEqual('10,001');
  158. expect(ngModel.$error.max).toEqual(true);
  159. });
  160.  
  161. it('test for allow decimal config: input . should be auto-formatting to 0. with numeric error', function () {
  162. initAllowDecimal();
  163.  
  164. inputElement.val('.');
  165. inputElement.triggerHandler('input');
  166.  
  167. var ngModel = inputElement.controller('ngModel');
  168. expect(ngModel.$viewValue).toEqual('0.');
  169. expect(inputElement.val()).toEqual('0.');
  170. expect(ngModel.$error.numeric).toEqual(true);
  171. });
  172.  
  173. it('test for allow decimal config: input 1234. should be auto-formatting to 1,234. with numeric error', function () {
  174. initAllowDecimal();
  175.  
  176. inputElement.val('1234.');
  177. inputElement.triggerHandler('input');
  178.  
  179. var ngModel = inputElement.controller('ngModel');
  180. expect(ngModel.$viewValue).toEqual('1,234.');
  181. expect(inputElement.val()).toEqual('1,234.');
  182. expect(ngModel.$error.numeric).toEqual(true);
  183. });
  184.  
  185. it('test for allow decimal config: input 1234.5 should be auto-formatting to 1,234.5', function () {
  186. initAllowDecimal();
  187.  
  188. inputElement.val('1234.5');
  189. inputElement.triggerHandler('input');
  190.  
  191. var ngModel = inputElement.controller('ngModel');
  192. expect(ngModel.$viewValue).toEqual('1,234.5');
  193. expect(inputElement.val()).toEqual('1,234.5');
  194. });
  195.  
  196. it('test for allow decimal config: input 1234.567 should be auto-formatting to 1,234.57', function () {
  197. initAllowDecimal();
  198.  
  199. inputElement.val('1234.567');
  200. inputElement.triggerHandler('input');
  201.  
  202. var ngModel = inputElement.controller('ngModel');
  203. expect(ngModel.$viewValue).toEqual('1,234.57');
  204. expect(inputElement.val()).toEqual('1,234.57');
  205. });
  206.  
  207.  
  208. it('test for allow decimal config with min 100 and max 1000: input 99.99 should be auto-formatting to 99.99 with min error', function () {
  209. initAllowDecimalWithMinMax();
  210.  
  211. inputElement.val('99.99');
  212. inputElement.triggerHandler('input');
  213.  
  214. var ngModel = inputElement.controller('ngModel');
  215. expect(ngModel.$viewValue).toEqual('99.99');
  216. expect(inputElement.val()).toEqual('99.99');
  217. expect(ngModel.$error.min).toEqual(true);
  218. });
  219.  
  220. it('test for allow decimal config with min 100 and max 1000: input 100.11 should be auto-formatting to 100.11 with no min error', function () {
  221. initAllowDecimalWithMinMax();
  222.  
  223. inputElement.val('100.11');
  224. inputElement.triggerHandler('input');
  225.  
  226. var ngModel = inputElement.controller('ngModel');
  227. expect(ngModel.$viewValue).toEqual('100.11');
  228. expect(inputElement.val()).toEqual('100.11');
  229. expect(ngModel.$error.min).toEqual(false);
  230. });
  231.  
  232. it('test for allow decimal config with min 100 and max 1000: input 1000.11 should be auto-formatting to 1,000.11 with max error', function () {
  233. initAllowDecimalWithMinMax();
  234.  
  235. inputElement.val('1000.11');
  236. inputElement.triggerHandler('input');
  237.  
  238. var ngModel = inputElement.controller('ngModel');
  239. expect(ngModel.$viewValue).toEqual('1,000.11');
  240. expect(inputElement.val()).toEqual('1,000.11');
  241. expect(ngModel.$error.max).toEqual(true);
  242. });
  243.  
  244. it('test for default config with max length 2: input 13 and then input 134 should be auto-formatting to last valid value', function () {
  245. intDefaultWithMaxLength();
  246.  
  247. inputElement.val('13');
  248. inputElement.triggerHandler('input');
  249.  
  250. inputElement.val('134');
  251. inputElement.triggerHandler('input');
  252.  
  253. var ngModel = inputElement.controller('ngModel');
  254.  
  255. expect(ngModel.$viewValue).toEqual('13');
  256. expect(inputElement.val()).toEqual('13');
  257. });
  258.  
  259.  
  260. it('test for default config with min 100 and Must Large Than Min: input 100 should be auto-formatting 100 and min error is true', function () {
  261. initDefaultWithMinAndmustLargeThanMin();
  262.  
  263. inputElement.val('100');
  264. inputElement.triggerHandler('input');
  265.  
  266. var ngModel = inputElement.controller('ngModel');
  267.  
  268. expect(ngModel.$viewValue).toEqual('100');
  269. expect(inputElement.val()).toEqual('100');
  270. expect(ngModel.$error.min).toEqual(true);
  271. });
  272.  
  273. // start test with initial value
  274. it('test for default config: initial model value is 1000 should be auto-formatting to 1,000', function () {
  275. initDefault(1000);
  276.  
  277. var ngModel = inputElement.controller('ngModel');
  278. expect(inputElement.val()).toEqual('1,000');
  279. expect(ngModel.$viewValue).toEqual('1,000');
  280. expect(ngModel.$modelValue).toEqual(1000);
  281. });
  282.  
  283. it('test for default config: initial model value is "1000" should be auto-formatting to 1,000', function () {
  284. initDefault('1000');
  285. var ngModel = inputElement.controller('ngModel');
  286. expect(inputElement.val()).toEqual('1,000');
  287. expect(ngModel.$viewValue).toEqual('1,000');
  288. expect(ngModel.$modelValue).toEqual('1000');
  289. });
  290.  
  291. it('test for default config: initial model value is null should be auto-formatting to empty string', function () {
  292. initDefault(null);
  293. var ngModel = inputElement.controller('ngModel');
  294. expect(inputElement.val()).toEqual('');
  295. expect(ngModel.$viewValue).toEqual(undefined);
  296. expect(ngModel.$modelValue).toEqual(null);
  297. });
  298.  
  299. it('test for default config: initial model value is undefined should be auto-formatting to empty string', function () {
  300. initDefault(undefined);
  301. var ngModel = inputElement.controller('ngModel');
  302. expect(inputElement.val()).toEqual('');
  303. expect(ngModel.$viewValue).toEqual(undefined);
  304. expect(ngModel.$modelValue).toEqual(undefined);
  305. });
  306.  
  307. it('test for default config: initial model value is "abc" should be auto-formatting to empty string', function () {
  308. initDefault('abc');
  309. var ngModel = inputElement.controller('ngModel');
  310. expect(inputElement.val()).toEqual('');
  311. expect(ngModel.$viewValue).toEqual(undefined);
  312. expect(ngModel.$modelValue).toEqual('abc');
  313. });
  314. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement