summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-app-overlay/src/main/webapp/app/fusion/scripts/view-models/reportdashboard-page/src/components/models/LayoutStorage.spec.js
blob: 3310cad94bc2e6b8587baf16232a212e130c3e72 (plain)
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
'use strict';

describe('Factory: LayoutStorage', function () {

  // mock UI Sortable
  beforeEach(function () {
    angular.module('ui.sortable', []);
  });

  // load the service's module
  beforeEach(module('ui.dashboard'));

  // instantiate service
  var LayoutStorage;
  beforeEach(inject(function (_LayoutStorage_) {
    LayoutStorage = _LayoutStorage_;
  }));

  describe('the constructor', function() {
    
    var storage, options;

    beforeEach(function() {
      options = {
        storageId: 'testingStorage',
        storage: {
          setItem: function(key, value) {

          },
          getItem: function(key) {

          },
          removeItem: function(key) {

          }
        },
        storageHash: 'ds5f9d1f',
        stringifyStorage: true,
        widgetDefinitions: [

        ],
        defaultLayouts: [
          {title: 'something'},
          {title: 'something'},
          {title: 'something'}
        ],
        widgetButtons: false,
        explicitSave: false,
        settingsModalOptions: {},
        onSettingsClose: function() {

        },
        onSettingsDismiss: function() {

        }
      };
      storage = new LayoutStorage(options);
    });

    it('should provide an empty implementation of storage if it is not provided', function() {
      delete options.storage;
      var stateless = new LayoutStorage(options);
      var noop = stateless.storage;
      angular.forEach(['setItem', 'getItem', 'removeItem'], function(method) {
        expect(typeof noop[method]).toEqual('function');
        expect(noop[method]).not.toThrow();
        noop[method]();
      });
    });

    it('should set a subset of the options directly on the LayoutStorage instance itself', function() {
      var properties = {
        id: 'storageId',
        storage: 'storage',
        storageHash: 'storageHash',
        stringifyStorage: 'stringifyStorage',
        widgetDefinitions: 'widgetDefinitions',
        defaultLayouts: 'defaultLayouts',
        widgetButtons: 'widgetButtons',
        explicitSave: 'explicitSave',
        settingsModalOptions: 'settingsModalOptions',
        onSettingsClose: 'onSettingsClose',
        onSettingsDismiss: 'onSettingsDismiss'
      };

      angular.forEach(properties, function(val, key) {
        expect( storage[key] ).toEqual( options[val] );
      });

    });

    it('should set stringify as true by default', function() {
      delete options.stringifyStorage;
      storage = new LayoutStorage(options);
      expect(storage.stringifyStorage).toEqual(true);
    });

    it('should allow stringify to be overridden by option', function() {
      options.stringifyStorage = false;
      storage = new LayoutStorage(options);
      expect(storage.stringifyStorage).toEqual(false);
    });

    it('should create a layouts array and states object', function() {
      expect(storage.layouts instanceof Array).toEqual(true);
      expect(typeof storage.states).toEqual('object');
    });

    it('should call load', function() {
      spyOn(LayoutStorage.prototype, 'load');
      storage = new LayoutStorage(options);
      expect(LayoutStorage.prototype.load).toHaveBeenCalled();
    });

  });

  describe('the load method', function() {

    var options, storage;

    beforeEach(function() {
      options = {
        storageId: 'testingStorage',
        storage: {
          setItem: function(key, value) {

          },
          getItem: function(key) {

          },
          removeItem: function(key) {

          }
        },
        storageHash: 'ds5f9d1f',
        stringifyStorage: true,
        widgetDefinitions: [

        ],
        defaultLayouts: [
          {title: 'something'},
          {title: 'something'},
          {title: 'something'}
        ],
        widgetButtons: false,
        explicitSave: false
      }
      storage = new LayoutStorage(options);
    });

    it('should use the default layouts if no stored info was found', function() {
      expect(storage.layouts.length).toEqual(options.defaultLayouts.length);
    });

    it('should clone default layouts rather than use them directly', function() {
      expect(storage.layouts.indexOf(options.defaultLayouts[0])).toEqual(-1);
    });

    it('should use the result from getItem for layouts.', function() {
      spyOn(options.storage, 'getItem').and.returnValue(JSON.stringify({
        storageHash: 'ds5f9d1f',
        layouts: [
          { id: 0, title: 'title', defaultWidgets: [], active: true },
          { id: 1, title: 'title2', defaultWidgets: [], active: false },
          { id: 2, title: 'title3', defaultWidgets: [], active: false },
          { id: 3, title: 'custom', defaultWidgets: [], active: false }
        ],
        states: {
          0: {},
          1: {},
          2: {}
        }
      }));
      storage.load();
      expect(storage.layouts.map(function(l) {return l.title})).toEqual(['title', 'title2', 'title3', 'custom']);
    });

    it('should NOT use result from getItem for layouts if the storageHash doesnt match', function() {
      spyOn(options.storage, 'getItem').and.returnValue(JSON.stringify({
        storageHash: 'alskdjf02iej',
        layouts: [
          { id: 0, title: 'title', defaultWidgets: [], active: true },
          { id: 1, title: 'title2', defaultWidgets: [], active: false },
          { id: 2, title: 'title3', defaultWidgets: [], active: false },
          { id: 3, title: 'custom', defaultWidgets: [], active: false }
        ],
        states: {
          0: {},
          1: {},
          2: {}
        }
      }));
      storage.load();
      expect(storage.layouts.map(function(l) {return l.title})).toEqual(['something', 'something', 'something']);
    });

    it('should be able to handle async loading via promise', inject(function($rootScope,$q) {
      var deferred = $q.defer();
      spyOn(options.storage, 'getItem').and.returnValue(deferred.promise);
      storage.load();
      expect(storage.layouts).toEqual([]);
      deferred.resolve(JSON.stringify({
        storageHash: 'ds5f9d1f',
        layouts: [
          { id: 0, title: 'title', defaultWidgets: [], active: true },
          { id: 1, title: 'title2', defaultWidgets: [], active: false },
          { id: 2, title: 'title3', defaultWidgets: [], active: false },
          { id: 3, title: 'custom', defaultWidgets: [], active: false }
        ],
        states: {
          0: {},
          1: {},
          2: {}
        }
      }));
      $rootScope.$apply();
      expect(storage.layouts.map(function(l) {return l.title})).toEqual(['title', 'title2', 'title3', 'custom']);
    }));

    it('should load defaults if the deferred is rejected', inject(function($rootScope,$q) {
      var deferred = $q.defer();
      spyOn(options.storage, 'getItem').and.returnValue(deferred.promise);
      storage.load();
      deferred.reject();
      $rootScope.$apply();
      expect(storage.layouts.map(function(l) {return l.title})).toEqual(['something', 'something', 'something']);
    }));

    it('should load defaults if the json is malformed', inject(function($rootScope,$q) {
      var deferred = $q.defer();
      spyOn(options.storage, 'getItem').and.returnValue(deferred.promise);
      storage.load();
      expect(storage.layouts).toEqual([]);
      deferred.resolve(JSON.stringify({
        storageHash: 'ds5f9d1f',
        layouts: [
          { id: 0, title: 'title', defaultWidgets: [], active: true },
          { id: 1, title: 'title2', defaultWidgets: [], active: false },
          { id: 2, title: 'title3', defaultWidgets: [], active: false },
          { id: 3, title: 'custom', defaultWidgets: [], active: false }
        ],
        states: {
          0: {},
          1: {},
          2: {}
        }
      }).replace('{','{{'));
      $rootScope.$apply();
      expect(storage.layouts.map(function(l) {return l.title})).toEqual(['something', 'something', 'something']);
    }));

    it('should not try to JSON.parse the result if stringifyStorage is false.', function() {
      options.stringifyStorage = false;
      storage = new LayoutStorage(options);
      spyOn(options.storage, 'getItem').and.returnValue({
        storageHash: 'ds5f9d1f',
        layouts: [
          { id: 0, title: 'title', defaultWidgets: [], active: true },
          { id: 1, title: 'title2', defaultWidgets: [], active: false },
          { id: 2, title: 'title3', defaultWidgets: [], active: false },
          { id: 3, title: 'custom', defaultWidgets: [], active: false }
        ],
        states: {
          0: {},
          1: {},
          2: {}
        }
      });
      storage.load();
      expect(storage.layouts.map(function(l) {return l.title})).toEqual(['title', 'title2', 'title3', 'custom']);
    });

  });

  describe('the add method', function() {
    
    var storage, options;

    beforeEach(function() {
      options = {
        storageId: 'testingStorage',
        storage: {
          setItem: function(key, value) {

          },
          getItem: function(key) {

          },
          removeItem: function(key) {

          }
        },
        storageHash: 'ds5f9d1f',
        stringifyStorage: true,
        widgetDefinitions: [

        ],
        defaultLayouts: [],
        widgetButtons: false,
        explicitSave: false
      }

      spyOn(LayoutStorage.prototype, 'load' );

      storage = new LayoutStorage(options);

    });

    it('should add to storage.layouts', function() {
      var newLayout = { title: 'my-layout' };
      storage.add(newLayout);
      expect(storage.layouts[0]).toEqual(newLayout);
    });

    it('should be able to take an array of new layouts', function() {
      var newLayouts = [ { title: 'my-layout' }, { title: 'my-layout-2' } ];
      storage.add(newLayouts);
      expect(storage.layouts.length).toEqual(2);
      expect(storage.layouts.indexOf(newLayouts[0])).not.toEqual(-1);
      expect(storage.layouts.indexOf(newLayouts[1])).not.toEqual(-1);
    });

    it('should look for defaultWidgets on storage options if not supplied on layout definition', function() {
      options.defaultWidgets = [{name: 'a'}, {name: 'b'}, {name: 'c'}];
      storage = new LayoutStorage(options);

      var newLayouts = [ { title: 'my-layout', defaultWidgets: [] }, { title: 'my-layout-2' } ];
      storage.add(newLayouts);
      expect(newLayouts[0].dashboard.defaultWidgets === newLayouts[0].defaultWidgets).toEqual(true);
      expect(newLayouts[1].dashboard.defaultWidgets === options.defaultWidgets).toEqual(true);
    });

    it('should use defaultWidgets if supplied in the layout definition', function() {
      options.defaultWidgets = [{name: 'a'}, {name: 'b'}, {name: 'c'}];
      storage = new LayoutStorage(options);

      var newLayouts = [ { title: 'my-layout', defaultWidgets: [] }, { title: 'my-layout-2' } ];
      storage.add(newLayouts);
      expect(newLayouts[0].dashboard.defaultWidgets).toEqual([]);
      expect(newLayouts[1].dashboard.defaultWidgets).toEqual(options.defaultWidgets);
    });

    it('should look for widgetDefinitions on storage options if not supplied on layout definition', function() {
      options.widgetDefinitions = [{name: 'a'}, {name: 'b'}, {name: 'c'}];
      storage = new LayoutStorage(options);

      var newLayouts = [ { title: 'my-layout', widgetDefinitions: [] }, { title: 'my-layout-2' } ];
      storage.add(newLayouts);
      expect(newLayouts[0].dashboard.widgetDefinitions === newLayouts[0].widgetDefinitions).toEqual(true);
      expect(newLayouts[1].dashboard.widgetDefinitions === options.widgetDefinitions).toEqual(true);
    });

    it('should use widgetDefinitions if supplied in the layout definition', function() {
      options.widgetDefinitions = [{name: 'a'}, {name: 'b'}, {name: 'c'}];
      storage = new LayoutStorage(options);

      var newLayouts = [ { title: 'my-layout', widgetDefinitions: [] }, { title: 'my-layout-2' } ];
      storage.add(newLayouts);
      expect(newLayouts[0].dashboard.widgetDefinitions).toEqual([]);
      expect(newLayouts[1].dashboard.widgetDefinitions).toEqual(options.widgetDefinitions);
    });

  });

  describe('the remove method', function() {

    var storage, options;

    beforeEach(function() {
      options = {
        storageId: 'testingStorage',
        storageHash: 'ds5f9d1f',
        stringifyStorage: true,
        widgetDefinitions: [
          { name: 'A' },
          { name: 'B' },
          { name: 'C' }
        ],
        defaultLayouts: [
          { title: '1' },
          { title: '2', active: true },
          { title: '3' }
        ],
        widgetButtons: false,
        explicitSave: false
      }

      storage = new LayoutStorage(options);
    });

    it('should remove the supplied layout', function() {
      var layout = storage.layouts[1];
      storage.remove(layout);
      expect(storage.layouts.indexOf(layout)).toEqual(-1);
    });

    it('should delete the state', function() {
      var layout = storage.layouts[1];
      storage.setItem(layout.id, {});
      storage.remove(layout);
      expect(storage.states[layout.id]).toBeUndefined();
    });

    it('should do nothing if layout is not in layouts', function() {
      var layout = {};
      var before = storage.layouts.length;
      storage.remove(layout);
      var after = storage.layouts.length;
      expect(before).toEqual(after);
    });

    it('should set another dashboard to active if the layout removed was active', function() {
      var layout = storage.layouts[1];
      storage.remove(layout);
      expect(storage.layouts[0].active || storage.layouts[1].active).toEqual(true);
    });

    it('should set the layout at index 0 to active if the removed layout was 0', function() {
      storage.layouts[1].active = false;
      storage.layouts[0].active = true;
      storage.remove(storage.layouts[0]);
      expect(storage.layouts[0].active).toEqual(true);
    });

    it('should not change the active layout if it was not the one that got removed', function() {
      var active = storage.layouts[1];
      var layout = storage.layouts[0];
      storage.remove(layout);
      expect(active.active).toEqual(true);
    });

  });

  describe('the save method', function() {

    var options, storage;

    beforeEach(function() {
      options = {
        storageId: 'testingStorage',
        storage: {
          setItem: function(key, value) {

          },
          getItem: function(key) {

          },
          removeItem: function(key) {

          }
        },
        storageHash: 'ds5f9d1f',
        stringifyStorage: true,
        widgetDefinitions: [

        ],
        defaultLayouts: [
          {title: 'something'},
          {title: 'something'},
          {title: 'something'}
        ],
        widgetButtons: false,
        explicitSave: false
      }
      storage = new LayoutStorage(options);
    });
    
    it('should call options.storage.setItem with a stringified object', function() {
      spyOn(options.storage, 'setItem' );
      storage.save();
      expect(options.storage.setItem).toHaveBeenCalled();
      expect(options.storage.setItem.calls.argsFor(0)[0]).toEqual(storage.id);
      expect(typeof options.storage.setItem.calls.argsFor(0)[1]).toEqual('string');
      expect(function(){
        JSON.parse(options.storage.setItem.calls.argsFor(0)[1]);
      }).not.toThrow();
    });

    it('should save an object that has layouts, states, and storageHash', function() {
      spyOn(options.storage, 'setItem' );
      storage.save();
      var obj = JSON.parse(options.storage.setItem.calls.argsFor(0)[1]);
      expect(obj.hasOwnProperty('layouts')).toEqual(true);
      expect(obj.layouts instanceof Array).toEqual(true);
      expect(obj.hasOwnProperty('states')).toEqual(true);
      expect(typeof obj.states).toEqual('object');
      expect(obj.hasOwnProperty('storageHash')).toEqual(true);
      expect(typeof obj.storageHash).toEqual('string');
    });

    it('should call options.storage.setItem with an object when stringifyStorage is false', function() {
      options.stringifyStorage = false;
      storage = new LayoutStorage(options);
      spyOn(options.storage, 'setItem' );
      storage.save();
      expect(options.storage.setItem).toHaveBeenCalled();
      expect(options.storage.setItem.calls.argsFor(0)[0]).toEqual(storage.id);
      expect(typeof options.storage.setItem.calls.argsFor(0)[1]).toEqual('object');
    });

  });

  describe('the setItem method', function() {
    
    var options, storage;

    beforeEach(function() {
      options = {
        storageId: 'testingStorage',
        storage: {
          setItem: function(key, value) {

          },
          getItem: function(key) {

          },
          removeItem: function(key) {

          }
        },
        storageHash: 'ds5f9d1f',
        stringifyStorage: true,
        widgetDefinitions: [

        ],
        defaultLayouts: [
          {title: 'something'},
          {title: 'something'},
          {title: 'something'}
        ],
        widgetButtons: false,
        explicitSave: false
      }
      storage = new LayoutStorage(options);
    });

    it('should set storage.states[id] to the second argument', function() {
      var state = { some: 'thing'};
      storage.setItem('id', state);
      expect(storage.states.id).toEqual(state);
    });

    it('should call save', function() {
      spyOn(storage, 'save');
      var state = { some: 'thing'};
      storage.setItem('id', state);
      expect(storage.save).toHaveBeenCalled();
    });

  });

  describe('the getItem method', function() {

    var options, storage;

    beforeEach(function() {
      options = {
        storageId: 'testingStorage',
        storage: {
          setItem: function(key, value) {

          },
          getItem: function(key) {

          },
          removeItem: function(key) {

          }
        },
        storageHash: 'ds5f9d1f',
        stringifyStorage: true,
        widgetDefinitions: [

        ],
        defaultLayouts: [
          {title: 'something'},
          {title: 'something'},
          {title: 'something'}
        ],
        widgetButtons: false,
        explicitSave: false
      }
      storage = new LayoutStorage(options);
    });
    
    it('should return states[id]', function() {
      storage.states['myId'] = {};
      var result = storage.getItem('myId');
      expect(result === storage.states['myId']).toEqual(true);
    });

  });

  describe('the getActiveLayout method', function() {
    var options, storage;

    beforeEach(function() {
      options = {
        storageId: 'testingStorage',
        storage: {
          setItem: function(key, value) {

          },
          getItem: function(key) {

          },
          removeItem: function(key) {

          }
        },
        storageHash: 'ds5f9d1f',
        stringifyStorage: true,
        widgetDefinitions: [

        ],
        defaultLayouts: [
          {title: 'i am active', active: true},
          {title: 'i am not'},
          {title: 'me neither'}
        ],
        widgetButtons: false,
        explicitSave: false
      }
      storage = new LayoutStorage(options);
    });

    it('should return the layout with active:true', function() {
      var layout = storage.getActiveLayout();
      expect(layout.title).toEqual('i am active');
    });

    it('should return false if no layout is active', function() {
      var layout = storage.getActiveLayout();
      layout.active = false;
      var result = storage.getActiveLayout();
      expect(result).toEqual(false);
    });

  });

  describe('the removeItem', function() {
    
    var options, storage;

    beforeEach(function() {
      options = {
        storageId: 'testingStorage',
        storage: {
          setItem: function(key, value) {

          },
          getItem: function(key) {

          },
          removeItem: function(key) {

          }
        },
        storageHash: 'ds5f9d1f',
        stringifyStorage: true,
        widgetDefinitions: [

        ],
        defaultLayouts: [
          {title: 'i am active', active: true},
          {title: 'i am not'},
          {title: 'me neither'}
        ],
        widgetButtons: false,
        explicitSave: false
      }
      storage = new LayoutStorage(options);
    });

    it('should remove states[id]', function() {
      var state = {};
      storage.setItem('1', state);
      storage.removeItem('1');
      expect(storage.states['1']).toBeUndefined();
    });

    it('should call save', function() {
      spyOn(storage, 'save');
      var state = {};
      storage.setItem('1', state);
      storage.removeItem('1');
      expect(storage.save).toHaveBeenCalled();
    });

  });

});