Skip to content

Commit

Permalink
Fix unit tests and ensure forceRefresh keeps working even when the ur…
Browse files Browse the repository at this point in the history
…l doesn't change
  • Loading branch information
GeorgDangl committed Jun 26, 2021
1 parent f1772ca commit cf9f8d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ describe('PaginationBaseService', () => {
httpMock.expectOne('/users?page=1&pageSize=20');
httpMock.verify();
service.forceRefresh();
await delay(1);
httpMock.expectOne('/users?page=1&pageSize=20');
httpMock.verify();
}));
Expand Down Expand Up @@ -148,13 +149,28 @@ describe('PaginationBaseService', () => {
expect(hasReceivedResponse).toBeFalsy();
}));

it('cancels first request when second request send', async(async () => {
it('cancels first request when second request sent', async(async () => {
let service = getService();
service.baseUrl = '/users';
await delay(1);
let httpMock = getHttpMock();
const firstRequest = httpMock.expectOne('/users?page=1&pageSize=20');
service.forceRefresh();
await delay(1);
const secondRequest = httpMock.expectOne('/users?page=1&pageSize=20');
expect(firstRequest.cancelled).toBeTruthy();
expect(secondRequest.cancelled).toBeFalsy();
}));

it('does not send two requests when calling forceRefresh twice subsequently', async(async () => {
let service = getService();
service.baseUrl = '/users';
await delay(1);
let httpMock = getHttpMock();
const firstRequest = httpMock.expectOne('/users?page=1&pageSize=20');
service.forceRefresh();
service.forceRefresh();
await delay(1);
const secondRequest = httpMock.expectOne('/users?page=1&pageSize=20');
expect(firstRequest.cancelled).toBeTruthy();
expect(secondRequest.cancelled).toBeFalsy();
Expand All @@ -173,6 +189,7 @@ describe('PaginationBaseService', () => {
expect(hasReceivedResponse).toBeFalsy();
httpMock.verify();
service.forceRefresh();
await delay(1);
const secondRequest = httpMock.expectOne('/users?page=1&pageSize=20');
secondRequest.flush({});
expect(hasReceivedResponse).toBeTruthy();
Expand Down Expand Up @@ -237,6 +254,7 @@ describe('PaginationBaseService', () => {
});

requeryAction(service); // This is currently either calling 'forceRefresh()' or 'requery()'
await delay(1);

const secondRequest = httpMock.expectOne('/users?page=1&pageSize=20');
const secondPaginationResponse: PaginationResult<User> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ export abstract class PaginationBaseService<T> {
private forceRefreshUrl = new Subject<string>();

constructor(protected http: HttpClient) {
merge(this.requestUrl, this.forceRefreshUrl)
merge(this.requestUrl.pipe(distinctUntilChanged()), this.forceRefreshUrl)
.pipe(
distinctUntilChanged(),
// We're putting a 0 (zero) debounce time here, mostly to
// ensure that when multiple changes are applied via code, e.g.
// setting a page number and a query parameter, we don't send two requests
Expand Down

0 comments on commit cf9f8d1

Please sign in to comment.