Skip to content

Commit

Permalink
issue-110 customPage remove sort params only when it is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
lagoshny committed Jul 21, 2023
1 parent 28f6611 commit 7517362
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PagedResourceCollectionHttpService } from '../../service/internal/paged
import { DependencyInjector } from '../../util/dependency-injector';
import { of } from 'rxjs';
import { ResourceCollection } from './resource-collection';
import { PagedGetOption } from '../declarations';
import { PagedGetOption, Sort } from '../declarations';

describe('PagedResourceCollection', () => {

Expand Down Expand Up @@ -147,5 +147,62 @@ describe('PagedResourceCollection', () => {
});
});

it('CUSTOM_PAGE should use sort params passed as params', () => {
pagedResourceCollectionHttpServiceSpy.get.and.returnValue(of(new PagedResourceCollection(new ResourceCollection())));

const pagedResourceCollection = new PagedResourceCollection(new SimpleResourceCollection(), {
...pageDataWithLinks,
_links: {
...pageDataWithLinks._links,
self: {
href: 'http://localhost:8080/api/v1/tasks?page=0&size=1&sort=first,ASC'
}
}
});
const sortParams: Sort = {first: 'ASC', second: 'DESC'};
pagedResourceCollection.customPage({pageParams: {page: 2, size: 8}, sort: sortParams})
.subscribe((customPageCollection) => {
const urlString: string = pagedResourceCollectionHttpServiceSpy.get.calls.argsFor(0)[0];
const url = new URL(urlString);
expect(url.searchParams.has('page')).toBeFalse();
expect(url.searchParams.has('size')).toBeFalse();
expect(url.searchParams.has('sort')).toBeFalse();

const actualSortParams = pagedResourceCollectionHttpServiceSpy.get.calls.argsFor(0)[1].sort;
expect(sortParams).toBe(actualSortParams);
});
});

it('CUSTOM_PAGE should use previous sort params when sort params is not passed', () => {
pagedResourceCollectionHttpServiceSpy.get.and.returnValue(of(new PagedResourceCollection(new ResourceCollection())));

const pagedResourceCollection = new PagedResourceCollection(new SimpleResourceCollection(), {
...pageDataWithLinks,
_links: {
...pageDataWithLinks._links,
self: {
href: 'http://localhost:8080/api/v1/tasks?page=0&size=1&sort=first,ASC&sort=second,DESC'
}
}
});
pagedResourceCollection.customPage({pageParams: {page: 2, size: 8}})
.subscribe((customPageCollection) => {
const urlString: string = pagedResourceCollectionHttpServiceSpy.get.calls.argsFor(0)[0];
const url = new URL(urlString);
expect(url.searchParams.has('page')).toBeFalse();
expect(url.searchParams.has('size')).toBeFalse();
expect(url.searchParams.has('sort')).toBeTrue();
});
});

it('PAGE should not change pageSize when request new page', () => {
pagedResourceCollectionHttpServiceSpy.get.and.returnValue(of(new PagedResourceCollection(new ResourceCollection(), pageDataWithLinks)));

const pagedResourceCollection = new PagedResourceCollection(new SimpleResourceCollection(), pageDataWithLinks);
pagedResourceCollection.page(2).subscribe(pagedCollection => {
expect(pagedCollection.pageSize).toBe(10);
});
});

});

Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ export class PagedResourceCollection<T extends BaseResource> extends ResourceCol
const requestUrl = new URL(this.selfLink.href);
requestUrl.searchParams.delete('page');
requestUrl.searchParams.delete('size');
requestUrl.searchParams.delete('sort');
if (!isEmpty(params.sort)) {
requestUrl.searchParams.delete('sort');
}

return doRequest<T>(requestUrl.href, options?.useCache, params).pipe(
tap(() => {
StageLogger.resourceEndLog(this.resources[0], 'CustomPage', {result: 'custom page was performed successful'});
Expand Down

0 comments on commit 7517362

Please sign in to comment.