Skip to content

Commit

Permalink
Merge pull request #18 from DataTorrent/improve_coverage
Browse files Browse the repository at this point in the history
Improve coverage for models
  • Loading branch information
andyperlitch committed May 12, 2014
2 parents 038a1a4 + c502f67 commit b2377f9
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/models/widgetModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ angular.module('ui.dashboard')
name: Class.name,
attrs: Class.attrs,
dataAttrName: Class.dataAttrName,
dataTypes: Class.dataTypes,
dataModelType: Class.dataModelType,
//AW Need deep copy of options to support widget options editing
dataModelOptions: Class.dataModelOptions,
Expand Down
63 changes: 63 additions & 0 deletions test/spec/widgetDataModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

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

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

// instantiate service
var WidgetDataModel, m, widget, scope;

beforeEach(inject(function (_WidgetDataModel_) {
WidgetDataModel = _WidgetDataModel_;
m = new WidgetDataModel();
widget = {
dataAttrName: 'testing',
dataModelOptions: { opt: true }
};
scope = {
fake: 'scope'
};
}));

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

describe('setup method', function() {

it('should set dataAttrName, dataModelOptions, and widgetScope from args', function() {
m.setup(widget, scope);
expect(m.dataAttrName).toEqual(widget.dataAttrName);
expect(m.dataModelOptions).toEqual(widget.dataModelOptions);
expect(m.widgetScope).toEqual(scope);
});

});

describe('updateScope method', function() {

it('should set scope.widgetData to passed data', function() {
m.setup(widget, scope);
var newData = [];
m.updateScope(newData);
expect(scope.widgetData).toEqual(newData);
});

});

describe('init method', function() {
it('should be an empty (noop) implementation', function() {
expect(typeof m.init).toEqual('function');
expect(m.init).not.toThrow();
});
});

describe('destroy method', function() {
it('should be an empty (noop) implementation', function() {
expect(typeof m.destroy).toEqual('function');
expect(m.destroy).not.toThrow();
});
});

});
141 changes: 141 additions & 0 deletions test/spec/widgetModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
'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' }
};

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 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 NOT set template if templateUrl was specified', function() {
var m2 = new WidgetModel(Class2, overrides);
expect(m2.template).toBeUndefined();
});

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();
});

});

describe('setWidth method', function() {

var context, setWidth;

beforeEach(function() {
context = { style: {} };
setWidth = WidgetModel.prototype.setWidth;
});

it('should take one argument as a string with units', function() {
setWidth.call(context, '100px');
expect(context.style.width).toEqual('100px');
});

it('should take two args as a number and string as units', function() {
setWidth.call(context, 100, 'px');
expect(context.style.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.style.width).not.toEqual('-100em');
});

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

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

});

});

0 comments on commit b2377f9

Please sign in to comment.