summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-app-overlay/src/main/webapp/app/fusion/scripts/view-models/reportdashboard-page/src/components/models/WidgetModel.spec.js
blob: 151e560a29acd9c2605cb58200e6fca1859af451 (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
'use strict';

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

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

  // instantiate service
  var WidgetModel;
  beforeEach(inject(function (_WidgetModel_) {
    WidgetModel = _WidgetModel_;
  }));

  it('should be a function', function() {
    expect(typeof WidgetModel).toEqual('function');
  });

  describe('the constructor', function() {
    var m, Class, Class2, overrides;

    beforeEach(function() {
      Class = {
        name: 'TestWidget', 
        attrs: {},
        dataAttrName: 'attr-name',
        dataModelType: function TestType() {},
        dataModelOptions: {},
        style: { width: '10em' },
        settingsModalOptions: {},
        onSettingsClose: function() {},
        onSettingsDismiss: function() {},
        funkyChicken: {
          cool: false,
          fun: true
        }
      };

      Class2 = {
        name: 'TestWidget2',
        attrs: {},
        dataAttrName: 'attr-name',
        dataModelType: function TestType() {},
        dataModelOptions: {},
        style: { width: '10em' },
        templateUrl: 'my/url.html',
        template: '<div>some template</div>'
      };

      overrides = {
        style: {
          width: '15em'
        }
      };
      spyOn(WidgetModel.prototype, 'setWidth');
      m = new WidgetModel(Class, overrides);
    });

    it('should copy class defaults, so that changes on an instance do not change the Class', function() {
      m.style.width = '20em';
      expect(Class.style.width).toEqual('10em');
    });

    it('should call setWidth', function() {
      expect(WidgetModel.prototype.setWidth).toHaveBeenCalled();
    });

    it('should take overrides as precedent over Class defaults', function() {
      expect(m.style.width).toEqual('15em');
    });

    it('should copy arbitrary data from the widget definition', function() {
      expect(m.funkyChicken.cool).toEqual(false);
      expect(m.funkyChicken.fun).toEqual(true);
      expect(m.funkyChicken===Class.funkyChicken).toEqual(false);
    });

    it('should set templateUrl if and only if it is present on Class', function() {
      var m2 = new WidgetModel(Class2, overrides);
      expect(m2.templateUrl).toEqual('my/url.html');
    });

    it('should set template if and only if it is present on Class', function() {
      delete Class2.templateUrl;
      var m2 = new WidgetModel(Class2, overrides);
      expect(m2.template).toEqual('<div>some template</div>');
    });

    it('should look for directive if neither templateUrl nor template is found on Class', function() {
      delete Class2.templateUrl;
      delete Class2.template;
      Class2.directive = 'ng-bind';
      var m2 = new WidgetModel(Class2, overrides);
      expect(m2.directive).toEqual('ng-bind');
    });

    it('should set the name as directive if templateUrl, template, and directive are not defined', function() {
      delete Class2.templateUrl;
      delete Class2.template;
      var m2 = new WidgetModel(Class2, overrides);
      expect(m2.directive).toEqual('TestWidget2');
    });

    it('should not require overrides', function() {
      var fn = function() {
        var m2 = new WidgetModel(Class);
      }
      expect(fn).not.toThrow();
    });

    it('should copy references to settingsModalOptions, onSettingsClose, onSettingsDismiss', function() {
      var m = new WidgetModel(Class);
      expect(m.settingsModalOptions).toEqual(Class.settingsModalOptions);
      expect(m.onSettingsClose).toEqual(Class.onSettingsClose);
      expect(m.onSettingsDismiss).toEqual(Class.onSettingsDismiss);
    });

  });

  describe('setWidth method', function() {

    var context, setWidth;

    beforeEach(function() {
      context = new WidgetModel({});
      setWidth = WidgetModel.prototype.setWidth;
    });
    
    it('should take one argument as a string with units', function() {
      setWidth.call(context, '100px');
      expect(context.containerStyle.width).toEqual('100px');
    });

    it('should take two args as a number and string as units', function() {
      setWidth.call(context, 100, 'px');
      expect(context.containerStyle.width).toEqual('100px');
    });

    it('should return false and not set anything if width is less than 0', function() {
      var result = setWidth.call(context, -100, 'em');
      expect(result).toEqual(false);
      expect(context.containerStyle.width).not.toEqual('-100em');
    });

    it('should assume % if no unit is given', function() {
      setWidth.call(context, 50);
      expect(context.containerStyle.width).toEqual('50%');
    });

    it('should force greater than 0% and less than or equal 100%', function() {
      setWidth.call(context, '110%');
      expect(context.containerStyle.width).toEqual('100%');
    });

  });

});