Skip to content

Commit

Permalink
Allow use of totalItems for in-memory paging
Browse files Browse the repository at this point in the history
fixes #115
  • Loading branch information
michaelbromley committed Feb 23, 2017
1 parent 44f42f2 commit 43ca1fe
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Changelog

## 2.0.1 (in progress)
* Fix exception when config object uses accessors for itemsPerPage and currentPage ([#128])[https://github.com/michaelbromley/ng2-pagination/issues/128])
* In-memory paging works even when `totalItems` is specified, fixes [#115](https://github.com/michaelbromley/ng2-pagination/issues/115)

# 2.0.0 (2017-01-12)

#### Breaking Changes
Expand Down
83 changes: 47 additions & 36 deletions src/paginate.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ describe('PaginatePipe:', () => {
expect(instance.itemsPerPage).toBe(50);
});


it('should use default id if none specified', () => {
let config = {
itemsPerPage: 10,
Expand All @@ -61,6 +60,19 @@ describe('PaginatePipe:', () => {
expect(paginationService.getInstance()).toBeDefined();
});

it('should not break when totalItems is specified for in-memory paging', () => {
let config = {
itemsPerPage: 10,
currentPage: 1,
totalItems: 100
};

let result = pipe.transform(collection, [config]);
expect(result.length).toBe(10);
expect(result[0]).toBe('item 1');
expect(result[9]).toBe('item 10');
});

describe('collection modification', () => {
it('should detect simple push or splice without insert', () => {
let config = {
Expand Down Expand Up @@ -126,51 +138,50 @@ describe('PaginatePipe:', () => {
expect(result2.length).toBe(50);
expect(result2[0]).toBe('item 51');
expect(result2[49]).toBe('item 100');
});


describe('server-side pagination', () => {
let config: PaginationInstance;

beforeEach(() => {
config = {
itemsPerPage: 10,
currentPage: 1,
totalItems: 500
};
});
describe('server-side pagination', () => {
let config: PaginationInstance;

it('should truncate collection', () => {
collection = collection.slice(0, 10);
let result = pipe.transform(collection, [config]);

expect(result.length).toBe(10);
expect(result[0]).toBe('item 1');
expect(result[9]).toBe('item 10');
});
beforeEach(() => {
config = {
itemsPerPage: 10,
currentPage: 1,
totalItems: 500
};
});

it('should display page 2', () => {
collection = collection.slice(10, 10);
config.currentPage = 2;
let result = pipe.transform(collection, [config]);
it('should truncate collection', () => {
collection = collection.slice(0, 10);
let result = pipe.transform(collection, [config]);

expect(result.length).toBe(10);
expect(result[0]).toBe('item 11');
expect(result[9]).toBe('item 20');
});
expect(result.length).toBe(10);
expect(result[0]).toBe('item 1');
expect(result[9]).toBe('item 10');
});

it('should return identical array for the same input values', () => {
let config = {
id: 'first_one',
itemsPerPage: 10,
currentPage: 1
};
let result1 = pipe.transform(collection, [config]);
let result2 = pipe.transform(collection, [config]);
it('should display page 2', () => {
collection = collection.slice(10, 20);
config.currentPage = 2;
let result = pipe.transform(collection, [config]);

expect(result1 === result2).toBe(true);
expect(result.length).toBe(10);
expect(result[0]).toBe('item 11');
expect(result[9]).toBe('item 20');
});
});

it('should return identical array for the same input values', () => {
let config = {
id: 'first_one',
itemsPerPage: 10,
currentPage: 1
};
let result1 = pipe.transform(collection, [config]);
let result2 = pipe.transform(collection, [config]);

expect(result1 === result2).toBe(true);
});

describe('unexpected input:', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/paginate.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class PaginatePipe {
}
}

let serverSideMode = args.totalItems !== undefined;
let serverSideMode = args.totalItems && args.totalItems !== collection.length;
let instance = this.createInstance(collection, args);
let id = instance.id;
let start, end;
Expand Down

0 comments on commit 43ca1fe

Please sign in to comment.