|
1 | 1 | /* eslint no-unused-vars:0, max-len:0 */
|
2 |
| -import _ from 'lodash'; |
3 |
| -import angular from 'angular-fix'; |
| 2 | +import _ from 'lodash' |
| 3 | +import angular from 'angular-fix' |
4 | 4 |
|
5 |
| -import testUtils from '../test.utils.js'; |
| 5 | +import testUtils from '../test.utils.js' |
6 | 6 |
|
7 |
| -const {shouldWarnWithLog} = testUtils; |
| 7 | +const {shouldWarnWithLog} = testUtils |
8 | 8 |
|
9 | 9 | describe(`formly-custom-validation`, function() {
|
10 |
| - let $compile, $timeout, $q, scope, $log, formlyConfig; |
11 |
| - const formTemplate = `<form name="myForm">TEMPLATE</form>`; |
12 |
| - beforeEach(window.module('formly')); |
| 10 | + let $compile, $timeout, $q, scope, $log, formlyConfig |
| 11 | + const formTemplate = `<form name="myForm">TEMPLATE</form>` |
| 12 | + beforeEach(window.module('formly')) |
13 | 13 | beforeEach(inject((_$compile_, _$timeout_, _$q_, $rootScope, _$log_, _formlyConfig_) => {
|
14 |
| - $compile = _$compile_; |
15 |
| - $timeout = _$timeout_; |
16 |
| - $q = _$q_; |
17 |
| - scope = $rootScope.$new(); |
18 |
| - scope.options = {validation: {}, validators: {}, asyncValidators: {}}; |
19 |
| - $log = _$log_; |
20 |
| - formlyConfig = _formlyConfig_; |
21 |
| - })); |
| 14 | + $compile = _$compile_ |
| 15 | + $timeout = _$timeout_ |
| 16 | + $q = _$q_ |
| 17 | + scope = $rootScope.$new() |
| 18 | + scope.options = {validation: {}, validators: {}, asyncValidators: {}} |
| 19 | + $log = _$log_ |
| 20 | + formlyConfig = _formlyConfig_ |
| 21 | + })) |
22 | 22 |
|
23 | 23 | describe(`using parsers`, () => {
|
24 | 24 | checkApi(formTemplate.replace(
|
25 | 25 | `TEMPLATE`, `<input ng-model="input" name="field" formly-custom-validation use-parsers />`
|
26 |
| - ), angular.version.minor >= 3); |
27 |
| - }); |
| 26 | + ), angular.version.minor >= 3) |
| 27 | + }) |
28 | 28 |
|
29 | 29 | describe(`using $validators`, () => {
|
30 | 30 | checkApi(formTemplate.replace(
|
31 | 31 | `TEMPLATE`, `<input ng-model="input" name="field" formly-custom-validation />`
|
32 |
| - )); |
33 |
| - }); |
| 32 | + )) |
| 33 | + }) |
34 | 34 |
|
35 | 35 | describe(`options.validation.messages`, () => {
|
36 | 36 | it(`should convert all strings to functions`, () => {
|
37 | 37 | scope.options.validation = {
|
38 | 38 | messages: {
|
39 |
| - isHello: `'"' + $viewValue + '" is not "hello"'` |
40 |
| - } |
41 |
| - }; |
| 39 | + isHello: `'"' + $viewValue + '" is not "hello"'`, |
| 40 | + }, |
| 41 | + } |
42 | 42 | $compile(formTemplate.replace(
|
43 | 43 | `TEMPLATE`, `<input ng-model="input" name="field" formly-custom-validation />`
|
44 |
| - ))(scope); |
| 44 | + ))(scope) |
45 | 45 |
|
46 |
| - expect(typeof scope.options.validation.messages.isHello).to.eq('function'); |
47 |
| - const field = scope.myForm.field; |
48 |
| - field.$setViewValue('sup'); |
49 |
| - expect(scope.options.validation.messages.isHello()).to.eq('"sup" is not "hello"'); |
50 |
| - }); |
51 |
| - }); |
| 46 | + expect(typeof scope.options.validation.messages.isHello).to.eq('function') |
| 47 | + const field = scope.myForm.field |
| 48 | + field.$setViewValue('sup') |
| 49 | + expect(scope.options.validation.messages.isHello()).to.eq('"sup" is not "hello"') |
| 50 | + }) |
| 51 | + }) |
52 | 52 |
|
53 | 53 | function checkApi(template, versionThreeOrBetterAndEmulating) {
|
54 |
| - const value = `hello`; |
| 54 | + const value = `hello` |
55 | 55 | describe(`validators`, () => {
|
56 |
| - const validate = doValidation.bind(null, template, 'hello', false); |
| 56 | + const validate = doValidation.bind(null, template, 'hello', false) |
57 | 57 | it(`should pass if returning a string that passes`, () => {
|
58 |
| - validate(`$viewValue === "${value}"`, true); |
59 |
| - }); |
| 58 | + validate(`$viewValue === "${value}"`, true) |
| 59 | + }) |
60 | 60 |
|
61 | 61 | it(`should fail if returning a string that fails`, () => {
|
62 |
| - validate(`$viewValue !== "${value}"`, false); |
63 |
| - }); |
| 62 | + validate(`$viewValue !== "${value}"`, false) |
| 63 | + }) |
64 | 64 |
|
65 | 65 | it(`should pass if it's a function that passes`, () => {
|
66 |
| - validate(viewValue => viewValue === value, true); |
67 |
| - }); |
| 66 | + validate(viewValue => viewValue === value, true) |
| 67 | + }) |
68 | 68 |
|
69 | 69 | it(`should fail if it's a function that fails`, () => {
|
70 |
| - validate(viewValue => viewValue !== value, false); |
71 |
| - }); |
72 |
| - }); |
| 70 | + validate(viewValue => viewValue !== value, false) |
| 71 | + }) |
| 72 | + }) |
73 | 73 |
|
74 | 74 | describe(`asyncValidators`, () => {
|
75 |
| - const validate = doValidation.bind(null, template, 'hello', true); |
| 75 | + const validate = doValidation.bind(null, template, 'hello', true) |
76 | 76 | it(`should pass if it's a function that returns a promise that resolves`, () => {
|
77 |
| - validate(() => $q.when(), true); |
78 |
| - }); |
| 77 | + validate(() => $q.when(), true) |
| 78 | + }) |
79 | 79 |
|
80 | 80 | it(`should fail if it's a function that returns a promise that rejects`, () => {
|
81 |
| - validate(() => $q.reject(), false); |
82 |
| - }); |
| 81 | + validate(() => $q.reject(), false) |
| 82 | + }) |
83 | 83 |
|
84 | 84 | it(`should be pending until the promise is resolved`, () => {
|
85 |
| - const deferred = $q.defer(); |
86 |
| - const deferred2 = $q.defer(); |
87 |
| - scope.options.asyncValidators.isHello = () => deferred.promise; |
88 |
| - scope.options.asyncValidators.isHey = () => deferred2.promise; |
89 |
| - $compile(template)(scope); |
90 |
| - const field = scope.myForm.field; |
91 |
| - scope.$digest(); |
92 |
| - field.$setViewValue(value); |
93 |
| - |
94 |
| - expect(field.$pending).to.exist; |
95 |
| - expect(field.$pending.isHello).to.be.true; |
96 |
| - expect(field.$pending.isHey).to.be.true; |
| 85 | + const deferred = $q.defer() |
| 86 | + const deferred2 = $q.defer() |
| 87 | + scope.options.asyncValidators.isHello = () => deferred.promise |
| 88 | + scope.options.asyncValidators.isHey = () => deferred2.promise |
| 89 | + $compile(template)(scope) |
| 90 | + const field = scope.myForm.field |
| 91 | + scope.$digest() |
| 92 | + field.$setViewValue(value) |
| 93 | + |
| 94 | + expect(field.$pending).to.exist |
| 95 | + expect(field.$pending.isHello).to.be.true |
| 96 | + expect(field.$pending.isHey).to.be.true |
97 | 97 |
|
98 | 98 | // because in angular 1.3 they do some interesting stuff with $pending, so can only test $pending in 1.2
|
99 | 99 | if (!versionThreeOrBetterAndEmulating) {
|
100 |
| - deferred.resolve(); |
101 |
| - scope.$digest(); |
| 100 | + deferred.resolve() |
| 101 | + scope.$digest() |
102 | 102 |
|
103 |
| - expect(field.$pending).to.exist; |
104 |
| - expect(field.$pending.isHey).to.be.true; |
105 |
| - expect(field.$pending.isHello).to.not.exist; |
| 103 | + expect(field.$pending).to.exist |
| 104 | + expect(field.$pending.isHey).to.be.true |
| 105 | + expect(field.$pending.isHello).to.not.exist |
106 | 106 |
|
107 |
| - deferred2.reject(); |
108 |
| - scope.$digest(); |
109 |
| - expect(field.$pending).to.not.exist; |
| 107 | + deferred2.reject() |
| 108 | + scope.$digest() |
| 109 | + expect(field.$pending).to.not.exist |
110 | 110 | }
|
111 |
| - }); |
112 |
| - }); |
| 111 | + }) |
| 112 | + }) |
113 | 113 | }
|
114 | 114 |
|
115 | 115 | function doValidation(template, value, isAsync, validator, pass) {
|
116 | 116 | if (isAsync) {
|
117 |
| - scope.options.asyncValidators.isHello = validator; |
| 117 | + scope.options.asyncValidators.isHello = validator |
118 | 118 | } else {
|
119 |
| - scope.options.validators.isHello = validator; |
| 119 | + scope.options.validators.isHello = validator |
120 | 120 | }
|
121 |
| - $compile(template)(scope); |
122 |
| - const field = scope.myForm.field; |
123 |
| - scope.$digest(); |
124 |
| - field.$setViewValue(value); |
125 |
| - scope.$digest(); |
126 |
| - expect(field.$valid).to.eq(pass); |
| 121 | + $compile(template)(scope) |
| 122 | + const field = scope.myForm.field |
| 123 | + scope.$digest() |
| 124 | + field.$setViewValue(value) |
| 125 | + scope.$digest() |
| 126 | + expect(field.$valid).to.eq(pass) |
127 | 127 | }
|
128 |
| -}); |
| 128 | +}) |
0 commit comments