Skip to content

Commit

Permalink
fix(pagination): when item is added it should trigger pagination changed
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding-SE committed Oct 5, 2019
1 parent df9af21 commit c953a23
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,16 @@ describe('PaginationService', () => {

it('should call "processOnItemAddedOrRemoved" and expect the (To) to be incremented by 1 when "onItemAdded" is triggered with a single item', (done) => {
const mockItems = { name: 'John' };
const spy = jest.spyOn(service, 'recalculateFromToIndexes');
const paginationSpy = jest.spyOn(service.onPaginationChanged, 'next');
const recalculateSpy = jest.spyOn(service, 'recalculateFromToIndexes');

service.init(gridStub, dataviewStub, mockGridOption);
gridServiceStub.onItemAdded.next(mockItems);

setTimeout(() => {
expect(spy).toHaveBeenCalled();
// called 2x times by init() then by processOnItemAddedOrRemoved()
expect(paginationSpy).toHaveBeenCalledTimes(2);
expect(recalculateSpy).toHaveBeenCalledTimes(2);
expect(service.pager.from).toBe(26);
expect(service.pager.to).toBe(50 + 1);
done();
Expand All @@ -494,24 +497,33 @@ describe('PaginationService', () => {

it('should call "processOnItemAddedOrRemoved" and expect the (To) to be incremented by 2 when "onItemAdded" is triggered with an array of 2 new items', (done) => {
const mockItems = [{ name: 'John' }, { name: 'Jane' }];
const spy = jest.spyOn(service, 'recalculateFromToIndexes');
const paginationSpy = jest.spyOn(service.onPaginationChanged, 'next');
const recalculateSpy = jest.spyOn(service, 'recalculateFromToIndexes');

service.init(gridStub, dataviewStub, mockGridOption);
gridServiceStub.onItemAdded.next(mockItems);

setTimeout(() => {
expect(spy).toHaveBeenCalled();
// called 2x times by init() then by processOnItemAddedOrRemoved()
expect(paginationSpy).toHaveBeenCalledTimes(2);
expect(recalculateSpy).toHaveBeenCalledTimes(2);
expect(service.pager.from).toBe(26);
expect(service.pager.to).toBe(50 + mockItems.length);
done();
});
});

it('should call "processOnItemAddedOrRemoved" and expect the (To) to remain the same when "onItemAdded" is triggered without any items', (done) => {
const paginationSpy = jest.spyOn(service.onPaginationChanged, 'next');
const recalculateSpy = jest.spyOn(service, 'recalculateFromToIndexes');

service.init(gridStub, dataviewStub, mockGridOption);
gridServiceStub.onItemAdded.next(null);

setTimeout(() => {
// called 1x time by init() only
expect(paginationSpy).toHaveBeenCalledTimes(1);
expect(recalculateSpy).toHaveBeenCalledTimes(1);
expect(service.pager.from).toBe(26);
expect(service.pager.to).toBe(50);
done();
Expand All @@ -520,13 +532,16 @@ describe('PaginationService', () => {

it('should call "processOnItemAddedOrRemoved" and expect the (To) to be decremented by 2 when "onItemDeleted" is triggered with a single item', (done) => {
const mockItems = { name: 'John' };
const spy = jest.spyOn(service, 'recalculateFromToIndexes');
const paginationSpy = jest.spyOn(service.onPaginationChanged, 'next');
const recalculateSpy = jest.spyOn(service, 'recalculateFromToIndexes');

service.init(gridStub, dataviewStub, mockGridOption);
gridServiceStub.onItemDeleted.next(mockItems);

setTimeout(() => {
expect(spy).toHaveBeenCalled();
// called 2x times by init() then by processOnItemAddedOrRemoved()
expect(paginationSpy).toHaveBeenCalledTimes(2);
expect(recalculateSpy).toHaveBeenCalledTimes(2);
expect(service.pager.from).toBe(26);
expect(service.pager.to).toBe(50 - 1);
done();
Expand All @@ -535,24 +550,33 @@ describe('PaginationService', () => {

it('should call "processOnItemAddedOrRemoved" and expect the (To) to be decremented by 2 when "onItemDeleted" is triggered with an array of 2 new items', (done) => {
const mockItems = [{ name: 'John' }, { name: 'Jane' }];
const spy = jest.spyOn(service, 'recalculateFromToIndexes');
const paginationSpy = jest.spyOn(service.onPaginationChanged, 'next');
const recalculateSpy = jest.spyOn(service, 'recalculateFromToIndexes');

service.init(gridStub, dataviewStub, mockGridOption);
gridServiceStub.onItemDeleted.next(mockItems);

setTimeout(() => {
expect(spy).toHaveBeenCalled();
// called 2x times by init() then by processOnItemAddedOrRemoved()
expect(paginationSpy).toHaveBeenCalledTimes(2);
expect(recalculateSpy).toHaveBeenCalledTimes(2);
expect(service.pager.from).toBe(26);
expect(service.pager.to).toBe(50 - mockItems.length);
done();
});
});

it('should call "processOnItemAddedOrRemoved" and expect the (To) to remain the same when "onItemDeleted" is triggered without any items', (done) => {
const paginationSpy = jest.spyOn(service.onPaginationChanged, 'next');
const recalculateSpy = jest.spyOn(service, 'recalculateFromToIndexes');

service.init(gridStub, dataviewStub, mockGridOption);
gridServiceStub.onItemDeleted.next(null);

setTimeout(() => {
// called 1x time by init() only
expect(paginationSpy).toHaveBeenCalledTimes(1);
expect(recalculateSpy).toHaveBeenCalledTimes(1);
expect(service.pager.from).toBe(26);
expect(service.pager.to).toBe(50);
done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export class PaginationService {
// finally refresh the "To" count and we know it might be different than the "items per page" count
// but this is necessary since we don't want an actual backend refresh
this._dataTo = previousDataTo + itemCountWithDirection;
this.onPaginationChanged.next(this.pager);
}
}
}

0 comments on commit c953a23

Please sign in to comment.