Skip to content

Commit

Permalink
Fixed folder naming, added pagination service with basic tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Aug 5, 2015
1 parent eeb3fe6 commit 6c1b7bf
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 8 deletions.
@@ -1,5 +1,3 @@
///<reference path="../../../build/js/declarations.d.ts" />

(() => {

let seededChance = new Chance(1);
Expand Down
File renamed without changes.
@@ -1,5 +1,3 @@
///<reference path="../../../build/js/declarations.d.ts" />

(() => {

let seededChance = new Chance(1);
Expand Down
93 changes: 93 additions & 0 deletions app/src/common/services/pagination/paginationService.spec.ts
@@ -0,0 +1,93 @@
interface mockEntity {
id:number;
body:string
}

(() => {

let seededChance = new Chance(1);
let fixtures = {

getExampleModel(id:number):mockEntity{

return {
id:id,
body: seededChance.string(),
};
},

getExampleCollection(count:number):mockEntity[] {
return _.range(count).map((id) => fixtures.getExampleModel(id))
},

get exampleCollection(): mockEntity[]{
return fixtures.getExampleCollection(100);
}

};

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

let paginationService:common.services.pagination.PaginationService;
let $httpBackend:ng.IHttpBackendService;
let ngRestAdapter:NgRestAdapter.NgRestAdapterService;

beforeEach(()=> {

module('app');

inject((_$httpBackend_, _paginationService_, _ngRestAdapter_) => {

if (!paginationService) { //dont rebind, so each test gets the singleton
$httpBackend = _$httpBackend_;
paginationService = _paginationService_;
ngRestAdapter = _ngRestAdapter_;
}
});

});

afterEach(() => {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});

describe('Initialisation', () => {

it('should be an injectable service', () => {

return expect(paginationService).to.be.an('object');
});

});

describe('Default paginator', () => {

it ('should default to 10 entities retrieved', () => {


let collection = fixtures.exampleCollection;

$httpBackend.expectGET('/api/collection', (headers) => {
return headers.Range == 'entities=0-9'
})
.respond(206, _.take(collection, 10));

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

let results = paginator.getNext();

$httpBackend.flush();

expect(results).eventually.to.be.instanceof(Array);
expect(results).eventually.to.have.length(10);
expect(results).eventually.to.deep.equal(_.take(collection, 10));

});


});

});

})();
67 changes: 67 additions & 0 deletions app/src/common/services/pagination/paginationService.ts
@@ -0,0 +1,67 @@
module common.services.pagination {

export const namespace = 'common.services.pagination';

export class Paginator {

private limit:number = 10;
private skip:number = 0;

constructor(private url:string, private ngRestAdapter:NgRestAdapter.INgRestAdapterService) {

}

private static getRangeHeader(from:number, to:number):string{
return 'entities=' + from + '-' + to;
}

private getResponse(skip:number = this.skip, limit:number = this.limit):ng.IHttpPromise<any[]>{

return this.ngRestAdapter.get(this.url, {
Range: Paginator.getRangeHeader(skip, skip + limit - 1)
});

}

/**
* Get the next set of paginated results
* @returns {ng.IPromise<any[]>}
*/
public getNext():ng.IPromise<any[]> {

let responsePromise = this.getResponse();

responsePromise.finally(() => {
this.skip += this.limit;
});

return responsePromise.then((response) => {
return response.data;
});

}




}

export class PaginationService {

static $inject:string[] = ['ngRestAdapter'];

constructor(private ngRestAdapter:NgRestAdapter.INgRestAdapterService) {

}

public getPaginatorInstance(url:string){
return new Paginator(url, this.ngRestAdapter);
}

}

angular.module(namespace, [])
.service('paginationService', PaginationService);

}

1 change: 1 addition & 0 deletions app/src/common/services/services.ts
Expand Up @@ -8,6 +8,7 @@ module common.services {
namespace+'.article',
namespace+'.countries',
namespace+'.timezones',
namespace+'.pagination',
]);

}
Expand Down
@@ -1,5 +1,3 @@
///<reference path="../../../build/js/declarations.d.ts" />

(() => {

let seededChance = new Chance(1);
Expand Down
@@ -1,5 +1,3 @@
///<reference path="../../../build/js/declarations.d.ts" />

(() => {

let seededChance = new Chance(1);
Expand Down
File renamed without changes.

0 comments on commit 6c1b7bf

Please sign in to comment.