summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-app-overlay/src/main/webapp/app/fusion/scripts/view-models/reportdashboard-page/src/components/directives/dashboardLayouts/dashboardLayouts.spec.js
blob: 8533a2110342d0e5dfa169c953fb85d866383787 (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
/*
 * Copyright (c) 2014 DataTorrent, Inc. ALL Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

describe('Directive: dashboard-layouts', function () {

  var $rootScope, element, options, childScope, DashboardState, LayoutStorage, $mockModal, $mockTimeout, toFn;

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

  // load the directive's module
  beforeEach(module('ui.dashboard', function($provide) {
    $mockModal = {
      open: function() {}
    };
    $mockTimeout = function(fn, delay) {
      toFn = fn;
    };
    $provide.value('$uibModal', $mockModal);
    $provide.value('$timeout', $mockTimeout);
  }));

  beforeEach(inject(function ($compile, _$rootScope_, _DashboardState_, _LayoutStorage_) {
    // services
    $rootScope = _$rootScope_;
    DashboardState = _DashboardState_;
    LayoutStorage = _LayoutStorage_;

    // options
    var widgetDefinitions = [
      {
        name: 'wt-one',
        template: '<div class="wt-one-value">{{2 + 2}}</div>'
      },
      {
        name: 'wt-two',
        template: '<span class="wt-two-value">{{value}}</span>'
      }
    ];
    var defaultWidgets = _.clone(widgetDefinitions);
    $rootScope.dashboardOptions = options = {
      widgetButtons: true,
      widgetDefinitions: widgetDefinitions,
      defaultLayouts: [
        {
          title: 'first',
          active: true,
          defaultWidgets: defaultWidgets
        },
        {
          title: 'second',
          active: false,
          defaultWidgets: defaultWidgets
        }
      ],
      defaultWidgets: defaultWidgets,
      storage: {
        setItem: function(key, val) {

        },
        getItem: function(key) {

        },
        removeItem: function(key) {

        }
      }
    };
    $rootScope.value = 10;

    // element setup 
    element = $compile('<div dashboard-layouts="dashboardOptions"></div>')($rootScope);
    $rootScope.$digest();
    childScope = element.scope();
  }));

  it('should not require storage', inject(function($compile) {
    delete $rootScope.dashboardOptions.storage;
    expect(function() {
      var noStorageEl = $compile('<div dashboard-layouts="dashboardOptions"></div>')($rootScope);
      $rootScope.$digest();
    }).not.toThrow();

  }));

  it('should be able to use a different dashboard-layouts template', inject(function($compile, $templateCache) {
    $templateCache.put(
      'myCustomTemplate.html',
      '<ul class="my-custom-tabs layout-tabs">' +
      '<li ng-repeat="layout in layouts" ng-class="{ active: layout.active }">' +
      '<a ng-click="makeLayoutActive(layout)">' +
      '<span ng-dblclick="editTitle(layout)" ng-show="!layout.editingTitle">{{layout.title}}</span>' +
      '<form action="" class="layout-title" ng-show="layout.editingTitle" ng-submit="saveTitleEdit(layout)">' +
      '<input type="text" ng-model="layout.title" class="form-control" data-layout="{{layout.id}}">' +
      '</form>' +
      '<span ng-click="removeLayout(layout)" class="glyphicon glyphicon-remove icon-erase"></span>' +
      '<!-- <span class="glyphicon glyphicon-pencil"></span> -->' +
      '<!-- <span class="glyphicon glyphicon-remove"></span> -->' +
      '</a>' +
      '</li>' +
      '<li>' +
      '<a ng-click="createNewLayout()">' +
      '<span class="glyphicon glyphicon-plus"></span>' +
      '</a>' +
      '</li>' +
      '</ul>' +
      '<div ng-repeat="layout in layouts | filter:isActive" dashboard="layout.dashboard" templateUrl="template/dashboard.html"></div>'
    );
    var customElement = $compile('<div dashboard-layouts="dashboardOptions" template-url="myCustomTemplate.html"></div>')($rootScope);
    $rootScope.$digest();
    expect(customElement.find('ul.my-custom-tabs').length).toEqual(1);

  }));

  it('should set the first dashboard to active if there is not one already active', inject(function($compile) {
    options.defaultLayouts[0].active = options.defaultLayouts[1].active = false;
    element = $compile('<div dashboard-layouts="dashboardOptions"></div>')($rootScope);
    $rootScope.$digest();
    childScope = element.scope();
    
    var layouts = childScope.layouts;
    var active;
    for (var i = 0; i < layouts.length; i++) {
      if (layouts[i].active) {
        active = layouts[i];
        break;
      }
    };
    expect(active).not.toBeUndefined();
  }));

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

    it('should call the add and save methods of LayoutStorage', function() {
      spyOn(LayoutStorage.prototype, 'add');
      spyOn(LayoutStorage.prototype, 'save');

      childScope.createNewLayout();
      expect(LayoutStorage.prototype.add).toHaveBeenCalled();
      expect(LayoutStorage.prototype.save).toHaveBeenCalled();
    });

    it('should return the newly created layout object', function() {
      var result = childScope.createNewLayout();
      expect(typeof result).toEqual('object');
    });

    it('should set active=true on the newly created layout', function() {
      var result = childScope.createNewLayout();
      expect(result.active).toEqual(true);
    });

    it('should set defaultWidgets to dashboardOptions.defaultWidgets if it is present', function() {
      var result = childScope.createNewLayout();
      expect(result.defaultWidgets === options.defaultWidgets).toEqual(true);
    });

    it('should set defaultWidgets to an empty array if dashboardOptions.defaultWidgets is not present', inject(function($compile) {
      delete options.defaultWidgets;
      element = $compile('<div dashboard-layouts="dashboardOptions"></div>')($rootScope);
      $rootScope.$digest();
      childScope = element.scope();
      var result = childScope.createNewLayout();
      expect(result.defaultWidgets).toEqual([]);
    }));

  });

  describe('the removeLayout method', function() {
    
    it('should call the remove and save methods of LayoutStorage', function() {
      spyOn(LayoutStorage.prototype, 'remove');
      spyOn(LayoutStorage.prototype, 'save');

      childScope.removeLayout(childScope.layouts[0]);
      expect(LayoutStorage.prototype.remove).toHaveBeenCalled();
      expect(LayoutStorage.prototype.save).toHaveBeenCalled();
    });

    it('should call remove with the layout it was passed', function() {
      spyOn(LayoutStorage.prototype, 'remove');
      var layout = childScope.layouts[0];
      childScope.removeLayout(layout);
      expect(LayoutStorage.prototype.remove.calls.argsFor(0)[0]).toEqual(layout);
    });

  });

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

    it('should call _makeLayoutActive if there is not a currently active dashboard with unsaved changes', function() {
      spyOn(childScope, '_makeLayoutActive');
      var layout = childScope.layouts[1];
      childScope.makeLayoutActive(layout);
      expect(childScope._makeLayoutActive).toHaveBeenCalled();
    });

    describe('when there are unsaved changes on the current dashboard', function() {
      
      var current, options, successCb, errorCb, layout;

      beforeEach(function() {
        current = childScope.layouts[0];
        current.dashboard.unsavedChangeCount = 1;

        spyOn($mockModal, 'open').and.callFake(function(arg) {
          options = arg;
          return {
            result: {
              then: function(success, error) {
                successCb = success;
                errorCb = error;
              }
            }
          }
        });

        layout = childScope.layouts[1];
        childScope.makeLayoutActive(layout);
      });

      it('should create a modal', function() {
        expect($mockModal.open).toHaveBeenCalled();
      });

      it('should resolve layout to the layout to be made active', function() {
        expect(options.resolve.layout()).toEqual(layout);
      });

      it('should provide a success callback that saves the current dashboard and then calls _makeLayoutActive', function() {
        spyOn(current.dashboard, 'saveDashboard');
        spyOn(childScope, '_makeLayoutActive');
        successCb();
        expect(current.dashboard.saveDashboard).toHaveBeenCalled();
        expect(childScope._makeLayoutActive).toHaveBeenCalled();
        expect(childScope._makeLayoutActive.calls.argsFor(0)[0]).toEqual(layout);
      });

      it('should provide an error callback that only calls _makeLayoutActive', function() {
        spyOn(current.dashboard, 'saveDashboard');
        spyOn(childScope, '_makeLayoutActive');
        errorCb();
        expect(current.dashboard.saveDashboard).not.toHaveBeenCalled();
        expect(childScope._makeLayoutActive).toHaveBeenCalled();
        expect(childScope._makeLayoutActive.calls.argsFor(0)[0]).toEqual(layout);
      });

    });

  });

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

    it('should set the editingTitle attribute to true on the layout it is passed', function() {
      var layout = { id: '1' };
      childScope.editTitle(layout);
      $rootScope.$digest();
      expect(layout.editingTitle).toEqual(true);
      toFn();
    });

  });

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

    it('should set editingTitle to false', function() {
      var layout = { id: '1' };
      childScope.saveTitleEdit(layout);
      expect(layout.editingTitle).toEqual(false);
    });

    it('should call layoutStorage.save', function() {
      var layout = { id: '1' };
      spyOn(LayoutStorage.prototype, 'save').and.callThrough();
      childScope.saveTitleEdit(layout);
      expect(LayoutStorage.prototype.save).toHaveBeenCalled();
    });

  });

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

    it('should call LayoutStorage.save', function() {
      spyOn(LayoutStorage.prototype, 'save').and.callThrough();
      $rootScope.dashboardOptions.saveLayouts();
      expect(LayoutStorage.prototype.save).toHaveBeenCalled();
    });

    it('should call LayoutStorage.save with true as the first arg', function() {
      spyOn(LayoutStorage.prototype, 'save').and.callThrough();
      $rootScope.dashboardOptions.saveLayouts();
      expect(LayoutStorage.prototype.save.calls.argsFor(0)[0]).toEqual(true);
    });

  });
  describe('the proxy methods to active layout', function() {

    var mockDash, galSpy;

    beforeEach(function() {
      mockDash = {
        active: true,
        dashboard: {
          addWidget: function() {},
          loadWidgets: function() {},
          saveDashboard: function() {}
        }
      };
      spyOn(mockDash.dashboard, 'addWidget');
      spyOn(mockDash.dashboard, 'loadWidgets');
      spyOn(mockDash.dashboard, 'saveDashboard');
      galSpy = spyOn(LayoutStorage.prototype, 'getActiveLayout').and;
      galSpy.returnValue(mockDash);
    });
    
    describe('the addWidget method', function() {

      it('should call dashboard.addWidget method of the active layout', function() {
        options.addWidget(1,2,3);
        expect(mockDash.dashboard.addWidget).toHaveBeenCalled();
        var firstCall = mockDash.dashboard.addWidget.calls.first();
        expect(firstCall.object).toEqual(mockDash.dashboard);
        expect(firstCall.args).toEqual([1,2,3]);
      });

      it('should do nothing if there is no active layout', function() {
        galSpy.returnValue(null);
        expect(function() {
          options.addWidget();
        }).not.toThrow();
      });

    });

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

      it('should call dashboard.loadWidgets of the current layout', function() {
        options.loadWidgets(1,2,3);
        expect(mockDash.dashboard.loadWidgets).toHaveBeenCalled();
        var firstCall = mockDash.dashboard.loadWidgets.calls.first();
        expect(firstCall.object).toEqual(mockDash.dashboard);
        expect(firstCall.args).toEqual([1,2,3]);
      });

      it('should do nothing if there is no active layout', function() {
        galSpy.returnValue(null);
        expect(function() {
          options.loadWidgets();
        }).not.toThrow();
      });

    });

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

      it('should call dashboard.saveDashboard of the current layout', function() {
        options.saveDashboard(1,2,3);
        expect(mockDash.dashboard.saveDashboard).toHaveBeenCalled();
        var firstCall = mockDash.dashboard.saveDashboard.calls.first();
        expect(firstCall.object).toEqual(mockDash.dashboard);
        expect(firstCall.args).toEqual([1,2,3]);
      });

      it('should do nothing if there is no active layout', function() {
        galSpy.returnValue(null);
        expect(function() {
          options.saveDashboard();
        }).not.toThrow();
      });

    });

  });

});