/
dialog-field-refresh.service.spec.js
495 lines (422 loc) · 18.8 KB
/
dialog-field-refresh.service.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
describe('DialogFieldRefresh', function() {
beforeEach(function() {
module('app.states');
bard.inject('CollectionsApi', 'Notifications', 'AutoRefresh', 'DialogFieldRefresh');
});
describe('#refreshSingleDialogField with values not as an object', function() {
var dialog1 = {name: 'dialog1', default_value: 'name1'};
var dialog2 = {name: 'dialog2', default_value: 'name2'};
var allDialogFields = [dialog1, dialog2];
var collectionsApiSpy;
var triggerAutoRefreshSpy;
describe('when the API call is successful', function() {
var successResponse;
describe('when the dialogfield type is DialogFieldDateControl', function() {
beforeEach(function() {
successResponse = {
result: {
dialog1: {
type: 'DialogFieldDateControl',
options: 'options',
read_only: false,
required: false,
values: '2016-01-02T00:00:00+00:00Z'
}
}
};
triggerAutoRefreshSpy = sinon.stub(DialogFieldRefresh, 'triggerAutoRefresh');
collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse));
});
it('updates the attributes for the dialog field', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(dialog1.options).to.eq('options');
expect(dialog1.read_only).to.be.false;
expect(dialog1.required).to.be.false;
expect(dialog1.default_value).to.eq(new Date('2016-01-02T00:00:00+00:00Z'));
});
it('triggers an auto-refresh', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(triggerAutoRefreshSpy).to.have.been.called;
});
});
describe('when the dialogfield type is DialogFieldDateTimeControl', function() {
beforeEach(function() {
successResponse = {
result: {
dialog1: {
type: 'DialogFieldDateTimeControl',
options: 'options',
read_only: false,
required: false,
values: '2016-01-02T12:34:00+00:00Z'
}
}
};
triggerAutoRefreshSpy = sinon.stub(DialogFieldRefresh, 'triggerAutoRefresh');
collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse));
});
it('updates the attributes for the dialog field', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(dialog1.options).to.eq('options');
expect(dialog1.read_only).to.be.false;
expect(dialog1.required).to.be.false;
expect(dialog1.default_value).to.eq('Text');
expect(dialog1.default_value).to.eq(new Date('2016-01-02T12:34:00+00:00Z'));
});
it('triggers an auto-refresh', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(triggerAutoRefreshSpy).to.have.been.called;
});
});
describe('when the dialogfield type is DialogFieldCheckBox', function() {
beforeEach(function() {
successResponse = {
result: {
dialog1: {
type: 'DialogFieldCheckBox',
options: 'options',
read_only: false,
required: false,
values: 'f'
}
}
};
triggerAutoRefreshSpy = sinon.stub(DialogFieldRefresh, 'triggerAutoRefresh');
collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse));
});
it('updates the attributes for the dialog field', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(dialog1.options).to.eq('options');
expect(dialog1.read_only).to.be.false;
expect(dialog1.required).to.be.false;
expect(dialog1.default_value).to.eq('f');
});
it('triggers an auto-refresh', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(triggerAutoRefreshSpy).to.have.been.called;
});
});
describe('when the dialogfield type is anything else', function() {
beforeEach(function() {
successResponse = {
result: {
dialog1: {
data_type: 'string',
options: 'options',
read_only: false,
required: false,
values: 'Text'
}
}
};
triggerAutoRefreshSpy = sinon.stub(DialogFieldRefresh, 'triggerAutoRefresh');
collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse));
});
it('updates the attributes for the dialog field', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(dialog1.data_type).to.eq('string');
expect(dialog1.options).to.eq('options');
expect(dialog1.read_only).to.be.false;
expect(dialog1.required).to.be.false;
expect(dialog1.default_value).to.eq('Text');
});
it('triggers an auto-refresh', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(triggerAutoRefreshSpy).to.have.been.called;
});
});
});
});
describe('#refreshSingleDialogField with values as an object', function() {
var dialog1 = {name: 'dialog1', default_value: 'name1'};
var dialog2 = {name: 'dialog2', default_value: 'name2'};
var allDialogFields = [dialog1, dialog2];
var collectionsApiSpy;
var triggerAutoRefreshSpy;
describe('when the API call is successful', function() {
var successResponse;
beforeEach(function() {
successResponse = {
result: {
dialog1: {
default_value: 'new default value',
data_type: 'string',
options: 'options',
read_only: false,
required: false,
values: [['1', 'One'], ['2', 'Two']]
}
}
};
triggerAutoRefreshSpy = sinon.stub(DialogFieldRefresh, 'triggerAutoRefresh');
collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse));
});
describe('when the default value is not given', function() {
beforeEach(function() {
successResponse = {
result: {
dialog1: {
data_type: 'integer',
options: 'options',
read_only: false,
required: false,
values: [[1, 'One'], [2, 'Two']]
}
}
};
});
it('updates the attributes for the dialog field with the first value being default', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(dialog1.data_type).to.eq('integer');
expect(dialog1.options).to.eq('options');
expect(dialog1.read_only).to.be.false;
expect(dialog1.required).to.be.false;
var dialog1Values = JSON.stringify(dialog1.values);
var dialog1ExpectedValues = JSON.stringify([[1, 'One'], [2, 'Two']]);
expect(dialog1Values).to.eq(dialog1ExpectedValues);
expect(dialog1.default_value).to.eq('1');
});
});
describe('when the default value is given', function() {
it('updates the attributes for the dialog field', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(dialog1.data_type).to.eq('string');
expect(dialog1.options).to.eq('options');
expect(dialog1.read_only).to.be.false;
expect(dialog1.required).to.be.false;
var dialog1Values = JSON.stringify(dialog1.values);
var dialog1ExpectedValues = JSON.stringify([['1', 'One'], ['2', 'Two']]);
expect(dialog1Values).to.eq(dialog1ExpectedValues);
expect(dialog1.default_value).to.eq('new default value');
});
});
it('calls the API with the correct parameters', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(collectionsApiSpy).to.have.been.calledWith(
'the_url',
123,
{},
JSON.stringify({
action: 'refresh_dialog_fields',
resource: {
dialog_fields: {dialog1: 'name1', dialog2: 'name2'},
fields: ['dialog1']
}
})
);
});
it('triggers an auto-refresh', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(triggerAutoRefreshSpy).to.have.been.called;
});
});
describe('when the API call fails', function() {
var notificationsErrorSpy;
beforeEach(function() {
var errorResponse = 'oopsies';
notificationsErrorSpy = sinon.spy(Notifications, 'error');
collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.reject(errorResponse));
});
it('returns a notification error', function(done) {
DialogFieldRefresh.refreshSingleDialogField(allDialogFields, dialog1, 'the_url', 123);
done();
expect(notificationsErrorSpy).to.have.been.calledWith('There was an error refreshing this dialog: oopsies');
});
});
});
describe('#setupDialogData', function() {
var allDialogFields;
var autoRefreshableDialogFields;
beforeEach(function() {
allDialogFields = [];
autoRefreshableDialogFields = [];
});
describe('when the dialog field default value is blank but the values are not', function() {
var dialogField = {name: 'dialog1', values: 'test_value', default_value: ''};
var dialogs = [{dialog_tabs: [{dialog_groups: [{dialog_fields: [dialogField]}]}]}];
it('pushes the dialog fields into the all dialog fields array', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(allDialogFields.length).to.equal(1);
});
it('assigns the default value', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(dialogField.default_value).to.equal('test_value');
});
it('sets up the triggering of auto refresh', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(dialogField).to.respondTo('triggerAutoRefresh');
});
});
describe('when the dialog field has an object for values', function() {
describe('when there is no default value assigned', function() {
var dialogField = {name: 'dialog1', values: [[1, 'one'], [2, 'two']], default_value: null};
var dialogs = [{dialog_tabs: [{dialog_groups: [{dialog_fields: [dialogField]}]}]}];
it('pushes the dialog field into the all dialog fields array', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(allDialogFields.length).to.equal(1);
});
it('assigns the default value to the first value', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(dialogField.default_value).to.equal(1);
});
it('sets up the triggering of auto refresh', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(dialogField).to.respondTo('triggerAutoRefresh');
});
});
describe('when there is a default value assigned', function() {
var dialogField = {name: 'dialog1', values: [[1, 'one'], [2, 'two']], default_value: 2, data_type: "integer"};
var dialogs = [{dialog_tabs: [{dialog_groups: [{dialog_fields: [dialogField]}]}]}];
it('pushes the dialog field into the all dialog fields array', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(allDialogFields.length).to.equal(1);
});
it('assigns the default value', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(dialogField.default_value).to.equal(2);
});
it('sets up the triggering of auto refresh', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(dialogField).to.respondTo('triggerAutoRefresh');
});
});
});
describe('when the dialog field is auto refreshable', function() {
var dialogField = {auto_refresh: true, name: 'dialog1'};
var dialogs = [{dialog_tabs: [{dialog_groups: [{dialog_fields: [dialogField]}]}]}];
it('pushes the dialog field into the all dialog fields array', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(allDialogFields.length).to.equal(1);
});
it('sets up the triggering of auto refresh', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(dialogField).to.respondTo('triggerAutoRefresh');
});
it('pushes the dialog field name into the auto refreshable array', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(autoRefreshableDialogFields[0].name).to.equal('dialog1');
});
it('sets the refreshableFieldIndex', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(dialogField.refreshableFieldIndex).to.equal(0);
});
});
describe('when the dialog field triggers auto refreshes', function() {
var dialogField = {auto_refresh: false, trigger_auto_refresh: true, name: 'dialog1'};
var dialogs = [{dialog_tabs: [{dialog_groups: [{dialog_fields: [dialogField]}]}]}];
it('pushes the dialog field into the all dialog fields array', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(allDialogFields.length).to.equal(1);
});
it('sets up the triggering of auto refresh', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(dialogField).to.respondTo('triggerAutoRefresh');
});
it('pushes the dialog field name into the auto refreshable array', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(autoRefreshableDialogFields[0].name).to.equal('dialog1');
});
it('sets the refreshableFieldIndex', function() {
DialogFieldRefresh.setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields);
expect(dialogField.refreshableFieldIndex).to.equal(0);
});
});
});
describe('#triggerAutoRefresh', function() {
var autoRefreshSpy;
var dialogField = {};
beforeEach(function() {
autoRefreshSpy = sinon.stub(AutoRefresh, 'triggerAutoRefresh');
dialogField.name = 'dialogName';
});
describe('when the dialog field triggers auto refreshes', function() {
beforeEach(function() {
dialogField.trigger_auto_refresh = true;
dialogField.refreshableFieldIndex = 123;
});
describe('when the initial trigger is true', function() {
beforeEach(function() {
DialogFieldRefresh.triggerAutoRefresh(dialogField, true);
});
it('calls the auto refresh trigger function with a currentIndex of 0', function() {
expect(autoRefreshSpy).to.have.been.calledWith({
initializingIndex: 123,
currentIndex: 0
});
});
});
describe('when the initial trigger is not true', function() {
beforeEach(function() {
var autoRefreshOptions = {
initializingIndex: 123,
currentIndex: 321
};
DialogFieldRefresh.triggerAutoRefresh(dialogField, false, autoRefreshOptions);
});
it('calls the auto refresh trigger function and passes along the autoRefreshOptions', function() {
expect(autoRefreshSpy).to.have.been.calledWith({
initializingIndex: 123,
currentIndex: 321
});
});
});
});
describe('when the dialog field does not trigger auto refreshes', function() {
beforeEach(function() {
dialogField.trigger_auto_refresh = false;
});
describe('when the override is true', function() {
beforeEach(function() {
dialogField.triggerOverride = true;
});
describe('when the initial trigger is true', function() {
beforeEach(function() {
DialogFieldRefresh.triggerAutoRefresh(dialogField, true);
});
it('calls the auto refresh trigger function with a currentIndex of 0', function() {
expect(autoRefreshSpy).to.have.been.calledWith({
initializingIndex: 123,
currentIndex: 0
});
});
});
describe('when the initial trigger is not true', function() {
beforeEach(function() {
var autoRefreshOptions = {
initializingIndex: 123,
currentIndex: 321
};
DialogFieldRefresh.triggerAutoRefresh(dialogField, false, autoRefreshOptions);
});
it('calls the auto refresh trigger function and passes along the autoRefreshOptions', function() {
expect(autoRefreshSpy).to.have.been.calledWith({
initializingIndex: 123,
currentIndex: 321
});
});
});
});
describe('when the override is undefined', function() {
beforeEach(function() {
dialogField.triggerOverride = undefined;
});
it('does not post a message', function() {
DialogFieldRefresh.triggerAutoRefresh(dialogField);
expect(autoRefreshSpy).not.to.have.been.called;
});
});
});
});
});