3 describe('Factory: WidgetModel', function () {
5 // load the service's module
6 beforeEach(module('ui.dashboard'));
10 beforeEach(inject(function (_WidgetModel_) {
11 WidgetModel = _WidgetModel_;
14 it('should be a function', function() {
15 expect(typeof WidgetModel).toEqual('function');
18 describe('the constructor', function() {
19 var m, Class, Class2, overrides;
21 beforeEach(function() {
25 dataAttrName: 'attr-name',
26 dataModelType: function TestType() {},
28 style: { width: '10em' },
29 settingsModalOptions: {},
30 onSettingsClose: function() {},
31 onSettingsDismiss: function() {},
41 dataAttrName: 'attr-name',
42 dataModelType: function TestType() {},
44 style: { width: '10em' },
45 templateUrl: 'my/url.html',
46 template: '<div>some template</div>'
54 spyOn(WidgetModel.prototype, 'setWidth');
55 m = new WidgetModel(Class, overrides);
58 it('should copy class defaults, so that changes on an instance do not change the Class', function() {
59 m.style.width = '20em';
60 expect(Class.style.width).toEqual('10em');
63 it('should call setWidth', function() {
64 expect(WidgetModel.prototype.setWidth).toHaveBeenCalled();
67 it('should take overrides as precedent over Class defaults', function() {
68 expect(m.style.width).toEqual('15em');
71 it('should copy arbitrary data from the widget definition', function() {
72 expect(m.funkyChicken.cool).toEqual(false);
73 expect(m.funkyChicken.fun).toEqual(true);
74 expect(m.funkyChicken===Class.funkyChicken).toEqual(false);
77 it('should set templateUrl if and only if it is present on Class', function() {
78 var m2 = new WidgetModel(Class2, overrides);
79 expect(m2.templateUrl).toEqual('my/url.html');
82 it('should set template if and only if it is present on Class', function() {
83 delete Class2.templateUrl;
84 var m2 = new WidgetModel(Class2, overrides);
85 expect(m2.template).toEqual('<div>some template</div>');
88 it('should look for directive if neither templateUrl nor template is found on Class', function() {
89 delete Class2.templateUrl;
90 delete Class2.template;
91 Class2.directive = 'ng-bind';
92 var m2 = new WidgetModel(Class2, overrides);
93 expect(m2.directive).toEqual('ng-bind');
96 it('should set the name as directive if templateUrl, template, and directive are not defined', function() {
97 delete Class2.templateUrl;
98 delete Class2.template;
99 var m2 = new WidgetModel(Class2, overrides);
100 expect(m2.directive).toEqual('TestWidget2');
103 it('should not require overrides', function() {
104 var fn = function() {
105 var m2 = new WidgetModel(Class);
107 expect(fn).not.toThrow();
110 it('should copy references to settingsModalOptions, onSettingsClose, onSettingsDismiss', function() {
111 var m = new WidgetModel(Class);
112 expect(m.settingsModalOptions).toEqual(Class.settingsModalOptions);
113 expect(m.onSettingsClose).toEqual(Class.onSettingsClose);
114 expect(m.onSettingsDismiss).toEqual(Class.onSettingsDismiss);
119 describe('setWidth method', function() {
121 var context, setWidth;
123 beforeEach(function() {
124 context = new WidgetModel({});
125 setWidth = WidgetModel.prototype.setWidth;
128 it('should take one argument as a string with units', function() {
129 setWidth.call(context, '100px');
130 expect(context.containerStyle.width).toEqual('100px');
133 it('should take two args as a number and string as units', function() {
134 setWidth.call(context, 100, 'px');
135 expect(context.containerStyle.width).toEqual('100px');
138 it('should return false and not set anything if width is less than 0', function() {
139 var result = setWidth.call(context, -100, 'em');
140 expect(result).toEqual(false);
141 expect(context.containerStyle.width).not.toEqual('-100em');
144 it('should assume % if no unit is given', function() {
145 setWidth.call(context, 50);
146 expect(context.containerStyle.width).toEqual('50%');
149 it('should force greater than 0% and less than or equal 100%', function() {
150 setWidth.call(context, '110%');
151 expect(context.containerStyle.width).toEqual('100%');