From 75173628babf1e3fcdf64b1ba534c4013eb80349 Mon Sep 17 00:00:00 2001 From: Ilya Lagoshny Date: Fri, 21 Jul 2023 23:12:23 +0300 Subject: [PATCH 1/2] issue-110 customPage remove sort params only when it is passed --- .../paged-resource-collection.spec.ts | 59 ++++++++++++++++++- .../resource/paged-resource-collection.ts | 5 +- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/projects/ngx-hateoas-client/src/lib/model/resource/paged-resource-collection.spec.ts b/projects/ngx-hateoas-client/src/lib/model/resource/paged-resource-collection.spec.ts index 8656180..c4ea750 100644 --- a/projects/ngx-hateoas-client/src/lib/model/resource/paged-resource-collection.spec.ts +++ b/projects/ngx-hateoas-client/src/lib/model/resource/paged-resource-collection.spec.ts @@ -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', () => { @@ -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); + }); + }); + }); diff --git a/projects/ngx-hateoas-client/src/lib/model/resource/paged-resource-collection.ts b/projects/ngx-hateoas-client/src/lib/model/resource/paged-resource-collection.ts index bbcf78e..1e39dd9 100644 --- a/projects/ngx-hateoas-client/src/lib/model/resource/paged-resource-collection.ts +++ b/projects/ngx-hateoas-client/src/lib/model/resource/paged-resource-collection.ts @@ -170,7 +170,10 @@ export class PagedResourceCollection 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(requestUrl.href, options?.useCache, params).pipe( tap(() => { StageLogger.resourceEndLog(this.resources[0], 'CustomPage', {result: 'custom page was performed successful'}); From c976630efdd347a1b056f40787d2219ef269f7fa Mon Sep 17 00:00:00 2001 From: Ilya Lagoshny Date: Fri, 21 Jul 2023 23:21:33 +0300 Subject: [PATCH 2/2] issue-110 updated CHANGELOG.md and version up --- CHANGELOG.md | 4 ++++ projects/ngx-hateoas-client/package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1111155..e556a17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.6.1 (2023-07-21) +#### Changes +Fixed [issue-110](https://github.com/lagoshny/ngx-hateoas-client/issues/110). + ## 3.6.0 (2023-07-07) #### Changes Updated to Angular 16. diff --git a/projects/ngx-hateoas-client/package.json b/projects/ngx-hateoas-client/package.json index d880d69..add45e1 100644 --- a/projects/ngx-hateoas-client/package.json +++ b/projects/ngx-hateoas-client/package.json @@ -1,6 +1,6 @@ { "name": "@lagoshny/ngx-hateoas-client", - "version": "3.6.0", + "version": "3.6.1", "description": "This client used to develop `Angular 12+` applications working with RESTfulll server API with HAL/JSON response type (supports server implementation by Spring HATEOAS)", "readme": "README.md", "license": "MIT",