Skip to content

Commit

Permalink
Added tests for content-header validation and model factory creation
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Aug 7, 2015
1 parent 57d8518 commit a4e58a2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
55 changes: 54 additions & 1 deletion app/src/common/services/pagination/paginationService.spec.ts
Expand Up @@ -62,7 +62,7 @@ interface mockEntity {

});

describe.only('Paginator', () => {
describe('Paginator', () => {

let collection = fixtures.exampleCollection;

Expand Down Expand Up @@ -188,6 +188,31 @@ interface mockEntity {
expect(results).eventually.to.deep.equal(collection.slice(5, 25));


});

it('should be able to define a custom model factory', () => {

function Foo(data){
_.assign(this, data);
}

let modelFactory = (data:any) => new Foo(data);

let paginator = paginationService.getPaginatorInstance('/collection').setModelFactory(modelFactory);

$httpBackend.expectGET('/api/collection').respond(206, _.take(collection, 10));

let results = paginator.getNext();

results.then((items) => {
expect(_.first(items)).to.be.instanceOf(Foo);
});

$httpBackend.flush();

expect(results).eventually.to.be.instanceOf(Array);


});

it('should redirect to the error handler if a fatal response comes from the api', () => {
Expand Down Expand Up @@ -272,6 +297,34 @@ interface mockEntity {

});

it('should throw exception when an invalid `Content-Range` header is passed', () => {

let valid = [
'entities 10-19/50',
'entities 10-19/*',
];

let invalid = [
'erghewar',
'entities=10/*',
'entities 10-19',
];

let validFns = _.map(valid, (headerString:string) => () => common.services.pagination.Paginator.parseContentRangeHeader(headerString));
let invalidFns = _.map(invalid, (headerString:string) => () => common.services.pagination.Paginator.parseContentRangeHeader(headerString));

_.each(validFns, (validFn) => {

expect(validFn).not.to.throw(common.services.pagination.PaginatorException);
});

_.each(invalidFns, (invalidFn) => {

expect(invalidFn).to.throw(common.services.pagination.PaginatorException);
});

});


});

Expand Down
6 changes: 3 additions & 3 deletions app/src/common/services/pagination/paginationService.ts
Expand Up @@ -144,17 +144,17 @@ module common.services.pagination {
}
}

private static parseContentRangeHeader(headerString:String):IRangeHeaderData {
public static parseContentRangeHeader(headerString:String):IRangeHeaderData {
let parts = headerString.split(/[\s\/]/);

if (parts.length !== 3){
throw new PaginatorException("Invalid range header; expected pattern: `entities 1-10/50`");
throw new PaginatorException("Invalid range header; expected pattern: `entities 1-10/50`, got `"+headerString+"`");
}

let rangeParts = parts[1].split('-');

if (rangeParts.length !== 2){
throw new PaginatorException("Invalid range header; expected pattern: `entities 1-10/50`");
throw new PaginatorException("Invalid range header; expected pattern: `entities 1-10/50`, got `"+headerString+"`");
}

let count:any = parts[2];
Expand Down

0 comments on commit a4e58a2

Please sign in to comment.