First pass at unit tests for the filters in shared/filters

This commit is contained in:
Michael Abashian 2017-03-03 11:50:20 -05:00
parent 4dfe85af7f
commit 506fae0b9c
13 changed files with 162 additions and 46 deletions

View File

@ -1,10 +1,15 @@
export default function() {
return function(string, append) {
if (string) {
return string + append;
if (append) {
return string + append;
}
else {
return string;
}
}
else {
return "";
}
return "";
};
}

View File

@ -10,11 +10,3 @@
return input;
};
}];
// export default
// angular.module('capitalizeFilter', []).filter('capitalize', function() {
// return function(input) {
// input = input.charAt(0).toUpperCase() + input.substr(1).toLowerCase();
// return input;
// };
// });

View File

@ -12,5 +12,3 @@ export default
};
}
];

View File

@ -7,7 +7,7 @@
export default ['moment', function(moment) {
return function(input) {
var date;
if(input === null){
if(input === null || input === undefined){
return "";
}else {
date = moment(input);
@ -15,23 +15,3 @@
}
};
}];
// function longDateFilter(moment, input) {
// var date;
// if(input === null){
// return "";
// }else {
// date = moment(input);
// return date.format('l LTS');
// }
// }
//
// export default
// angular.module('longDateFilter', [])
// .filter('longDate',
// [ 'moment',
// function(moment) {
// return _.partial(longDateFilter, moment);
// }
// ]);

View File

@ -1,9 +1,15 @@
export default function() {
return function(string, prepend) {
if (string) {
return prepend + string;
if(prepend) {
return prepend + string;
}
else {
return string;
}
}
else {
return "";
}
return "";
};
}

View File

@ -10,11 +10,3 @@
return input;
};
}];
// export default
// angular.module('sanitizeFilter', []).filter('sanitize', function() {
// return function(input) {
// input = $("<span>").text(input)[0].innerHTML;
// return input;
// };
// });

View File

@ -0,0 +1,25 @@
'use strict';
describe('Filter: append', () => {
var filter;
beforeEach(angular.mock.module('Tower'));
beforeEach(function(){
inject(function($injector){
filter = $injector.get('$filter')('append');
});
});
it('should append the two parameters passed', function(){
expect(filter("foo", "bar")).toBe("foobar");
});
it('should return string if no append param passed', function(){
expect(filter("foo")).toBe("foo");
});
it('should return empty string if no params passed', function(){
expect(filter()).toBe("");
});
});

View File

@ -0,0 +1,20 @@
'use strict';
describe('Filter: capitalize', () => {
var filter;
beforeEach(angular.mock.module('Tower'));
beforeEach(function(){
inject(function($injector){
filter = $injector.get('$filter')('capitalize');
});
});
it('should capitalize the first letter', function(){
expect(filter("foo")).toBe("Foo");
expect(filter("Foo")).toBe("Foo");
expect(filter("FOO")).toBe("Foo");
expect(filter("FoO")).toBe("Foo");
});
});

View File

@ -0,0 +1,18 @@
'use strict';
describe('Filter: formatEpoch', () => {
var filter;
beforeEach(angular.mock.module('Tower'));
beforeEach(function(){
inject(function($injector){
filter = $injector.get('$filter')('formatEpoch');
});
});
it('should convert epoch to datetime string', function(){
expect(filter(11111)).toBe("Dec 31, 1969 10:05 PM");
expect(filter(610430400)).toBe("May 6, 1989 12:00 AM");
});
});

View File

@ -0,0 +1,18 @@
'use strict';
describe('Filter: isEmpty', () => {
var filter;
beforeEach(angular.mock.module('Tower'));
beforeEach(function(){
inject(function($injector){
filter = $injector.get('$filter')('isEmpty');
});
});
it('check if an object is empty', function(){
expect(filter({})).toBe(true);
expect(filter({foo: 'bar'})).toBe(false);
});
});

View File

@ -0,0 +1,20 @@
'use strict';
describe('Filter: longDate', () => {
var filter;
beforeEach(angular.mock.module('Tower'));
beforeEach(function(){
inject(function($injector){
filter = $injector.get('$filter')('longDate');
});
});
it('should convert the timestamp to a UI friendly date and time', function(){
expect(filter("2017-02-13T22:00:14.106Z")).toBe("2/13/2017 5:00:14 PM");
});
it('should return an empty string if no timestamp is passed', function(){
expect(filter()).toBe("");
});
});

View File

@ -0,0 +1,25 @@
'use strict';
describe('Filter: prepend', () => {
var filter;
beforeEach(angular.mock.module('Tower'));
beforeEach(function(){
inject(function($injector){
filter = $injector.get('$filter')('prepend');
});
});
it('should prepend the second param to the first', function(){
expect(filter("foo", "bar")).toBe("barfoo");
});
it('should return string if no prepend param passed', function(){
expect(filter("foo")).toBe("foo");
});
it('should return empty string if no params passed', function(){
expect(filter()).toBe("");
});
});

View File

@ -0,0 +1,17 @@
'use strict';
describe('Filter: sanitize', () => {
var filter;
beforeEach(angular.mock.module('Tower'));
beforeEach(function(){
inject(function($injector){
filter = $injector.get('$filter')('sanitize');
});
});
it('should sanitize xss-vulnerable strings', function(){
expect(filter("<div>foobar</div>")).toBe("&lt;div&gt;foobar&lt;/div&gt;");
});
});