forked from json-schema-form/angular-schema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema-validate.directive.spec.js
175 lines (146 loc) · 4.49 KB
/
schema-validate.directive.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* eslint-disable quotes, no-var */
/* disabling quotes makes it easier to copy tests into the example app */
chai.should();
var runSync = function(scope, tmpl) {
var directiveScope = tmpl.isolateScope();
sinon.stub(directiveScope, 'resolveReferences', function(schema, form) {
directiveScope.render(schema, form);
});
scope.$apply();
};
describe('schema-validate.directive.js', function() {
var tmpl;
var exampleSchema;
beforeEach(module('schemaForm'));
beforeEach(
module(function($sceProvider) {
$sceProvider.enabled(false);
exampleSchema = {
"type": "object",
"title": "Person",
"properties": {
"name": {
"title": "Name",
"type": "string",
"minLength": 10,
},
"email": {
"type": "string",
"maxLength": 255,
"format": "email",
"email": true,
},
},
};
})
);
tv4.defineError('EMAIL', 10001, 'Invalid email address');
tv4.defineKeyword('email', function(data, value, schema) {
if (schema.email) {
if (/^\S+@\S+$/.test(data)) {
return null;
}
return {
code: 10001,
};
}
return null;
});
it('should validate the form on event [ノಠ益ಠ]ノ彡┻━┻', function() {
tmpl = angular.element('<form name="testform" sf-schema="schema" sf-form="form" sf-model="obj"></form>');
inject(function($compile, $rootScope) {
var scope = $rootScope.$new();
scope.obj = { "name": "Freddy" };
scope.schema = exampleSchema;
scope.form = [
"*",
{
"type": "button",
"style": "validate",
"onClick": "validate_all()",
},
];
scope.validate_all = function() {
scope.$broadcast('schemaFormValidate');
};
$compile(tmpl)(scope);
runSync(scope, tmpl);
var form = tmpl.eq(0).controller('form');
form.$valid.should.be.true;
scope.validate_all.should.not.have.beenCalled;
tmpl.find('button.validate').click();
scope.validate_all.should.have.beenCalledOnce;
form.$valid.should.be.false;
});
});
it('should process custom tv4 errors', function() {
tmpl = angular.element('<form name="testform" sf-schema="schema" sf-form="form" sf-model="obj"></form>');
inject(function($compile, $rootScope) {
var scope = $rootScope.$new();
scope.obj = { "email": "NULL" };
scope.schema = exampleSchema;
scope.form = [
{
"key": "email",
"placeholder": "Enter contact email",
"feedback": false,
},
{
"type": "button",
"style": "validate",
"onClick": "validate_all()",
},
];
scope.validate_all = function() {
scope.$broadcast('schemaFormValidate');
};
$compile(tmpl)(scope);
runSync(scope, tmpl);
var form = tmpl.eq(0).controller('form');
form.$valid.should.be.true;
scope.validate_all.should.not.have.beenCalled;
angular.element(tmpl.find('#testform-email')).val('invalid').trigger('input');
tmpl.find('button.validate').click();
scope.validate_all.should.have.beenCalledOnce;
form.$valid.should.be.false;
form.$error['tv4-10001'].should.be.false;
});
});
it('should allow custom tv4 error default message to be set', function() {
// TODO test message rename
// app.config(['sfErrorMessageProvider', function(sfErrorMessageProvider) {
// sfErrorMessageProvider.setDefaultMessage(10001, 'Whoa! Can you double check that email address for me?');
// }]);
tmpl = angular.element(
'<div>' +
'<form name="testform" sf-schema="schema" sf-form="form" sf-model="obj"></form>' +
'{{obj}}' +
'</div>'
);
inject(function($compile, $rootScope) {
var scope = $rootScope.$new();
scope.obj = { "email": "NULL" };
scope.schema = exampleSchema;
scope.form = [
{
"key": "email",
"placeholder": "Enter contact email",
"feedback": false,
},
{
"type": "submit",
"style": "btn-info",
"title": "OK",
},
];
$compile(tmpl)(scope);
tmpl.find('form').each(function() {
runSync(scope, $(this));
});
var form = tmpl.find('form').eq(0).controller('form');
form.$valid.should.be.true;
tmpl.find('input.btn-info').click();
// TODO form.$valid.should.be.false;
});
});
});