55604646985ad8d6f6c07d574c902a1ace69d930
[portal/sdk.git] /
1 'use strict';
2
3 describe('Controller: DashboardWidgetCtrl', function() {
4
5   var $scope, $element, $timeout, injections;
6
7   beforeEach(module('ui.dashboard'));
8
9   beforeEach(inject(function($rootScope, $controller){
10     $scope = $rootScope.$new();
11     $element = angular.element('<div><div class="widget"></div></div>');
12     $timeout = function timeout(fn) {
13       fn();
14     };
15     injections = {
16       $scope: $scope,
17       $element: $element,
18       $timeout: $timeout
19     };
20     spyOn(injections, '$timeout');
21     $controller('DashboardWidgetCtrl', injections);
22   }));
23
24   describe('the makeTemplateString method', function() {
25
26     it('should return a string', function() {
27       $scope.widget = {
28         templateUrl: 'some/template.html'
29       };
30       expect(typeof $scope.makeTemplateString()).toEqual('string');
31     });
32
33     it('should use ng-include if templateUrl is specified on widget, despite any other options', function() {
34       $scope.widget = {
35         templateUrl: 'some/template.html',
36         template: 'not this one',
37         directive: 'or-this',
38         attrs: {
39           something: 'awesome',
40           other: 'thing'
41         }
42       };
43       expect($scope.makeTemplateString()).toMatch(/ng-include="'some\/template\.html'"/);
44     });
45
46     it('should return widget.template if specified, regardless of presence of directive or attrs', function() {
47       $scope.widget = {
48         template: '<div class="testing"></div>',
49         directive: 'no-good'
50       };
51       expect($scope.makeTemplateString()).toEqual($scope.widget.template);
52     });
53
54     it('should use widget.directive as attribute directive', function() {
55       $scope.widget = {
56         directive: 'ng-awesome'
57       };
58       expect($scope.makeTemplateString()).toEqual('<div ng-awesome></div>');
59     });
60
61     it('should attach attributes if provided', function() {
62       $scope.widget = {
63         directive: 'ng-awesome',
64         attrs: {
65           'ng-awesome': 'test1',
66           other: 'attr',
67           more: 'stuff'
68         }
69       };
70       expect($scope.makeTemplateString()).toEqual('<div ng-awesome="test1" other="attr" more="stuff"></div>');
71     });
72
73     it('should place widgetData into dataAttrName attribute if specified', function() {
74       $scope.widget = {
75         directive: 'ng-awesome',
76         attrs: {
77           'ng-awesome': 'test1',
78           other: 'attr',
79           more: 'stuff'
80         },
81         dataAttrName: 'data'
82       };
83       expect($scope.makeTemplateString()).toEqual('<div ng-awesome="test1" other="attr" more="stuff" data="widgetData"></div>');
84     });
85
86     it('should add attrs to the widget object if it does not exist and dataAttrName is specified', function() {
87       $scope.widget = {
88         directive: 'ng-awesome',
89         dataAttrName: 'data'
90       };
91       expect($scope.makeTemplateString()).toEqual('<div ng-awesome data="widgetData"></div>');
92     });
93
94   });
95
96   describe('the grabResizer method', function() {
97
98     var evt, widget, WidgetModel;
99
100     beforeEach(inject(function (_WidgetModel_) {
101       WidgetModel = _WidgetModel_;
102     }));
103
104     beforeEach(function() {
105       evt = {
106         stopPropagation: jasmine.createSpy('stopPropagation'),
107         originalEvent: {
108           preventDefault: jasmine.createSpy('preventDefault')
109         },
110         clientX: 100,
111         which: 1
112       };
113       $scope.widget = widget = new WidgetModel({
114         style: {
115           width: '30%'
116         }
117       });
118     });
119
120     it('should do nothing if event.which is not 1 (left click)', function() {
121       evt.which = 2;
122       $scope.grabResizer(evt);
123       expect(evt.stopPropagation).not.toHaveBeenCalled();
124     });
125
126     it('should call stopPropagation and preventDefault', function() {
127       $scope.grabResizer(evt);
128       expect(evt.stopPropagation).toHaveBeenCalled();
129       expect(evt.originalEvent.preventDefault).toHaveBeenCalled();
130     });
131
132     it('should add a .widget-resizer-marquee element to the .widget element', function() {
133       $scope.grabResizer(evt);
134       expect($element.find('.widget-resizer-marquee').length).toBeGreaterThan(0);
135     });
136
137   });
138
139   describe('the editTitle method', function() {
140     
141     it('should set editingTitle=true on the widget object', function() {
142       var widget = {};
143       $scope.editTitle(widget);
144       expect(widget.editingTitle).toEqual(true);      
145     });
146
147     it('should call $timeout', function() {
148       var widget = {};
149       $scope.editTitle(widget);
150       expect(injections.$timeout).toHaveBeenCalled();
151     });
152
153   });
154
155   describe('the saveTitleEdit method', function() {
156     
157     it('should set editingTitle=false', function() {
158       var widget = { editingTitle: true };
159       $scope.saveTitleEdit(widget);
160       expect(widget.editingTitle).toEqual(false);
161     });
162   });
163
164 });