Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed folder naming, added pagination service with basic tests.
- Loading branch information
Showing
12 changed files
with
161 additions
and
8 deletions.
There are no files selected for viewing
2 changes: 0 additions & 2 deletions
2
...ces/articleService/articleService.spec.ts → ...n/services/article/articleService.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
///<reference path="../../../build/js/declarations.d.ts" /> | ||
|
||
(() => { | ||
|
||
let seededChance = new Chance(1); | ||
|
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions
2
...countriesService/countriesService.spec.ts → ...rvices/countries/countriesService.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
///<reference path="../../../build/js/declarations.d.ts" /> | ||
|
||
(() => { | ||
|
||
let seededChance = new Chance(1); | ||
|
File renamed without changes.
93 changes: 93 additions & 0 deletions
93
app/src/common/services/pagination/paginationService.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
|
||
}); | ||
|
||
|
||
}); | ||
|
||
}); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
...timezonesService/timezonesService.spec.ts → ...rvices/timezones/timezonesService.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
///<reference path="../../../build/js/declarations.d.ts" /> | ||
|
||
(() => { | ||
|
||
let seededChance = new Chance(1); | ||
|
File renamed without changes.
2 changes: 0 additions & 2 deletions
2
.../services/userService/userService.spec.ts → .../common/services/user/userService.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
///<reference path="../../../build/js/declarations.d.ts" /> | ||
|
||
(() => { | ||
|
||
let seededChance = new Chance(1); | ||
|
File renamed without changes.