forked from OptimalBPM/angular-schema-form-dynamic-select
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.js
More file actions
291 lines (266 loc) · 13.5 KB
/
tests.js
File metadata and controls
291 lines (266 loc) · 13.5 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* jshint expr: true */
chai.should();
describe('Schema form', function () {
describe('directive', function () {
beforeEach(module('templates'));
beforeEach(module('schemaForm'));
beforeEach(module('mgcrea.ngStrap'));
beforeEach(
// We don't need no sanitation. We don't need no thought control.
// (nicklasb: I don't totally understand this Floyd reference, so I better leave it in there :-)
module(function ($sceProvider) {
$sceProvider.enabled(false);
})
);
// Populate a given scope with test data.
var assignToScope = function (scope, http) {
scope.callBackSD = function (options) {
return [
{value: 'value1', name: 'text1'},
{value: 'value2', name: 'text2'},
{value: 'value3', name: 'Select dynamic!'}
];
// Note: Options is a reference to the original instance, if you change a value,
// that change will persist when you use this form instance again.
};
scope.callBackMSD = function (options) {
return [
{value: 'value1', name: 'text1'},
{value: 'value2', name: 'text2'},
{value: 'value3', name: 'Multiple select dynamic!'}
];
// Note: Options is a reference to the original instance, if you change a value,
// that change will persist when you use this form instance again.
};
scope.callBackMSDAsync = function (options) {
// Node that we got the url from the options. Not necessary, but then the same callback function can be used
// by different selects with different parameters.
return http.get(options.urlOrWhateverOptionIWant);
};
scope.stringOptionsCallback = function (options) {
// Here you can manipulate the form options used in a http_post or http_get
// For example, you can use variables to build the URL or set the parameters, here we just set the url.
options.httpPost.url = "test/testdata.json";
// Note: This is a copy of the form options, edits here will not persist but are only used in this request.
return options;
};
scope.schema = {
type: 'object',
title: 'Select',
properties: {
select: {
title: 'Single Select Static',
type: 'string',
description: 'Only single item is allowed'
},
multiselect: {
title: 'Multi Select Static',
type: 'array',
items: {
type: "string"
},
maxItems: 2,
description: 'Multi single items are allowed. (select three for maxItems error)'
},
selectDynamic: {
title: 'Single Select Dynamic',
type: 'string',
items: {
type: "string"
},
description: 'This data is loaded from the $scope.callBackSD function. (and laid out using css-options)'
},
multiselectDynamic: {
title: 'Multi Select Dynamic',
type: 'array',
items: {
type: "string"
},
description: 'This data is loaded from the $scope.callBackMSD function. (referenced by name)'
},
multiselectDynamicHttpPost: {
title: 'Multi Select Dynamic HTTP Post',
type: 'array',
items: {
type: "string"
},
description: 'This data is asynchronously loaded using a HTTP post. ' +
'(specifies parameter in form, options.url in a named callback)'
},
multiselectDynamicHttpGet: {
title: 'Multi Select Dynamic HTTP Get',
type: 'array',
items: {
type: "string"
},
description: 'This data is asynchronously loaded using a HTTP get. ' +
'(Set the URL at options.url)'
},
multiselectDynamicHttpGetMapped: {
title: 'Multi Select Dynamic HTTP Get Mapped data',
type: 'array',
items: {
type: "string"
},
description: 'This data is as above, but remapped from a nodeId/nodeName array of objects. ' +
'(See app.js: "map" : {valueProperty: "nodeId", textProperty: "nodeName"})'
},
multiselectDynamicAsync: {
title: 'Multi Select Dynamic Async',
type: 'array',
items: {
type: "string"
},
description: 'This data is asynchrously loaded using a async call. ' +
'(specify options.async.call)'
}
},
required: ['select', 'multiselect']
};
scope.testResponse = [
{value: "json-value1", name: "json-name1"},
{value: "json-value2", name: "json-name2"},
{value: "json-value3", name: "json-name3"}
];
scope.testResponseMapped = [
{"nodeId": "1", "nodeName": "Node 1"},
{"nodeId": "2", "nodeName": "Node 2"},
{"nodeId": "3", "nodeName": "Node 3"}
];
scope.testResponseMappedCmp = [
{"nodeId": "1", "nodeName": "Node 1", "value": "1", "name": "Node 1"},
{"nodeId": "2", "nodeName": "Node 2", "value": "2", "name": "Node 2"},
{"nodeId": "3", "nodeName": "Node 3", "value": "3", "name": "Node 3"}
];
scope.form = [
{
"key": 'select',
"type": 'strapselect',
"titleMap": [
{"value": 'value1', "name": 'text1'},
{"value": 'value2', "name": 'text2'},
{"value": 'value3', "name": 'text3'}
]
},
{
"key": 'multiselect',
"type": 'strapmultiselect',
"titleMap": [
{"value": 'value1', "name": 'text1'},
{"value": 'value2', "name": 'text2'},
{"value": 'value3', "name": 'long very very long label3'}
]
},
{
"key": "selectDynamic",
"type": 'strapselectdynamic',
"htmlClass": "col-lg-3 col-md-3",
"labelHtmlClass": "bigger",
"fieldHtmlClass": "tilted",
"options": {
"callback": scope.callBackSD
}
},
{
"key": "multiselectDynamic",
"type": 'strapmultiselectdynamic',
placeholder: "not set yet(this text is defined using the placeholder option)",
"options": {
"callback": "callBackMSD"
}
},
{
"key": "multiselectDynamicHttpPost",
"type": 'strapmultiselectdynamic',
"title": 'Multi Select Dynamic HTTP Post (title is from form.options, overriding the schema.title)',
"options": {
"httpPost": {
"optionsCallback": "stringOptionsCallback",
"parameter": {"myparam": "Hello"}
}
}
},
{
"key": "multiselectDynamicHttpGet",
"type": 'strapmultiselectdynamic',
"options": {
"httpGet": {
"url": "test/testdata.json"
}
}
},
{
"key": "multiselectDynamicHttpGetMapped",
"type": 'strapmultiselectdynamic',
"options": {
"httpGet": {
"url": "test/testdata_mapped.json"
},
"map": {valueProperty: "nodeId", nameProperty: "nodeName"}
}
},
{
"key": "multiselectDynamicAsync",
"type": 'strapmultiselectdynamic',
"onChange": function () {
alert("You changed this value! (this was the onChange event in action)");
},
"options": {
"asyncCallback": scope.callBackMSDAsync,
"urlOrWhateverOptionIWant": "test/testdata.json"
}
}
];
scope.model = {};
scope.model.select = 'value1';
scope.model.multiselect = ['value2', 'value1'];
scope.model.multiselectDynamicHttpPost = null;
};
it('should load the correct items into each type of select', function () {
inject(function ($compile, $rootScope, schemaForm, $http, $httpBackend, $timeout, $document) {
var scope = $rootScope.$new();
// Load example data
assignToScope(scope, $http);
// Create a template
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="model"></form>');
// Add http mocks
$httpBackend.whenGET("test/testdata.json").respond(200, scope.testResponse);
$httpBackend.whenGET("test/testdata_mapped.json").respond(200, scope.testResponseMapped);
$httpBackend.whenPOST("test/testdata.json", {"myparam": "Hello"}).respond(200, scope.testResponse);
// Compile the template
$compile(tmpl)(scope);
// Do an update, this triggers all items to be loaded
$rootScope.$apply();
// Attempt to click one of the selects, doesn't work as Karma seem to not work that way
tmpl.children().eq(7).children().eq(0).children().eq(1).click();
// Tell the mock to respond to requests
$httpBackend.flush();
// Wait for all getting done before checking.
$timeout(function () {
// Find HTML elements in the response, find its scope, and then deep compare with known results.
// Single Select Dynamic
expect(JSON.stringify(angular.element(tmpl.children().eq(2).children().eq(0).children().eq(1)).scope().form.titleMap)).
to.equal(JSON.stringify(scope.callBackSD()), "Single Select Dynamic test failed.");
// Multi Select Dynamic
expect(JSON.stringify(angular.element(tmpl.children().eq(3).children().eq(0).children().eq(1)).scope().form.titleMap)).
to.equal(JSON.stringify(scope.callBackMSD()), "Multi Select Dynamic test failed.");
// Multi Select Dynamic HTTP Post
expect(JSON.stringify(angular.element(tmpl.children().eq(4).children().eq(0).children().eq(1)).scope().form.titleMap)).
to.equal(JSON.stringify(scope.testResponse), "Multi Select Dynamic HTTP Post test failed.");
// Multi Select Dynamic HTTP Get
expect(JSON.stringify(angular.element(tmpl.children().eq(5).children().eq(0).children().eq(1)).scope().form.titleMap)).
to.equal(JSON.stringify(scope.testResponse), "Multi Select Dynamic HTTP Get test failed.");
// Multi Select Dynamic HTTP Get Mapped
expect(JSON.stringify(angular.element(tmpl.children().eq(6).children().eq(0).children().eq(1)).scope().form.titleMap)).
to.equal(JSON.stringify(scope.testResponseMappedCmp), "Multi Select Dynamic HTTP Get Mapped test failed.");
// Multi Select Dynamic Async
expect(JSON.stringify(angular.element(tmpl.children().eq(7).children().eq(0).children().eq(1)).scope().form.titleMap)).
to.equal(JSON.stringify(scope.testResponse), "Multi Select Dynamic Async test failed.");
}
);
// Angular doesn't like async unit tests, tell it to call the checks above
$timeout.flush()
});
});
});
});