Skip to content

Instantly share code, notes, and snippets.

@PitBeast
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PitBeast/c3df13c26f5cfd04bafd to your computer and use it in GitHub Desktop.
Save PitBeast/c3df13c26f5cfd04bafd to your computer and use it in GitHub Desktop.
//тесты для текущего(твоего) варианта директивы
describe('statsHeight', function() {
var $compile,
$rootScope,
$timeout;
beforeEach(function () {
angular.mock.module('gby.common');
});
beforeEach(inject(function (_$compile_, _$rootScope_, _$timeout_){
$compile = _$compile_;
$rootScope = _$rootScope_;
$timeout = _$timeout_;
}));
it('string length less than 4', function() {
var element,
fontSize = 93,
content = '999';
element = $compile('<dt stats-height="'+fontSize+'" class="\'list_item_description__number\'" ng-bind="\''+content+'\'"></dt>')($rootScope);
$rootScope.$digest();
$timeout.flush();
expect(element.html().length).toBeLessThan(4);
expect(element.css('font-size')).toEqual(fontSize+'px');
});
it('string length greater than 3', function() {
var element,
fontSize = 93,
content = '999569';
element = $compile('<dt stats-height="'+fontSize+'" class="\'list_item_description__number\'" ng-bind="\''+content+'\'"></dt>')($rootScope);
$rootScope.$digest();
$timeout.flush();
expect(element.html().length).toBeGreaterThan(3);
expect(element.css('font-size')).toEqual((fontSize-10*(content.length-1))+'px');
});
});
//мой вариант директивы(без отслеживания baseMetrics), тесты
commonModule.directive('statsHeight', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var collection={};
scope.$watchCollection(
function() {
collection.length = element.text().length;
collection.fontSize = parseFloat(attrs.statsHeight);
return collection;
},
function (newCollection) {
var newFontSize = newCollection.length < 4
? newCollection.fontSize
: newCollection.fontSize - 10 * (newCollection.length - 1);
element.css({
"font-size" : newFontSize
});
});
}
}
});
//тесты
describe('statsHeight', function() {
var $compile,
$rootScope,
$timeout;
beforeEach(function () {
angular.mock.module('gby.common');
});
beforeEach(inject(function (_$compile_, _$rootScope_, _$timeout_){
$compile = _$compile_;
$rootScope = _$rootScope_;
$timeout = _$timeout_;
}));
it('string length less than 4', function() {
var element,
fontSize = 93,
content = '999';
element = $compile('<dt stats-height="'+fontSize+'" class="\'list_item_description__number\'" ng-bind="\''+content+'\'"></dt>')($rootScope);
$rootScope.$digest();
expect(element.text().length).toBeLessThan(4);
expect(element.css('font-size')).toEqual(fontSize+'px');
});
it('string length greater than 3', function() {
var element,
fontSize = 93,
content = '999569';
element = $compile('<dt stats-height="'+fontSize+'" class="\'list_item_description__number\'" ng-bind="\''+content+'\'"></dt>')($rootScope);
$rootScope.$digest();
expect(element.text().length).toBeGreaterThan(4);
console.log((fontSize-10*(content.length-1)));
expect(element.css('font-size')).toEqual((fontSize-10*(content.length-1))+'px');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment