Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
feat(collaborators): add totalCount function (#190)
Browse files Browse the repository at this point in the history
Add getTotalCount function returning the meta.totalCount property of the last performed query,
allowing clients to display the total number of collaborators without cascading through the
links.next results.
  • Loading branch information
andrewazores authored and joshuawilson committed Oct 18, 2018
1 parent a3564eb commit e2f9693
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
37 changes: 36 additions & 1 deletion src/app/collaborators/collaborator.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Broadcaster, Logger } from 'ngx-base';

import { WIT_API_URL } from '../api/wit-api';
import { CollaboratorService } from './collaborator.service';
import { concatMap } from 'rxjs/operators';

describe('Service: CollaboratorService', () => {

Expand Down Expand Up @@ -86,7 +87,19 @@ describe('Service: CollaboratorService', () => {
'type': 'identities'
}
];
let response = { data: responseData, links: {} };
let response = { data: responseData, links: {}, meta: { totalCount: 3 } };

describe('#getTotalCount', () => {
it('should default to -1', () => {
collaboratorService.getTotalCount()
.subscribe(
(count: number): void => {
expect(count).toEqual(-1);
},
fail
);
});
});

describe('#getInitialBySpaceId', () => {
it('should get collaborators by Space ID', (done: DoneFn) => {
Expand All @@ -109,6 +122,28 @@ describe('Service: CollaboratorService', () => {
done();
});

it('should update totalCount', (done: DoneFn) => {
// when
collaboratorService.getInitialBySpaceId('1')
.pipe(
concatMap(() => collaboratorService.getTotalCount())
)
.subscribe((count: number): void => {
// then
expect(count).toEqual(3);
},
fail
);
// CollaboratorService should have made one request to GET collaborators from expected URL
const req = httpTestingController.expectOne(collaboratorService.spacesUrl + '/1/collaborators?page[limit]=20');
expect(req.request.method).toEqual('GET');

// Respond with the mock collaborator
req.flush(response);
httpTestingController.verify();
done();
});

it('can test for network error', () => {
const emsg = 'simulated network error';

Expand Down
13 changes: 12 additions & 1 deletion src/app/collaborators/collaborator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {

import {
Observable,
throwError as observableThrowError
throwError as observableThrowError,
of
} from 'rxjs';
import {
catchError,
Expand All @@ -25,6 +26,7 @@ export class CollaboratorService {

private headers = new HttpHeaders({ 'Content-Type': 'application/json' });
private nextLink: string;
private totalCount: number = -1;

constructor(
private http: HttpClient,
Expand All @@ -49,6 +51,11 @@ export class CollaboratorService {
} else {
this.nextLink = null;
}
if (response.meta && response.meta.hasOwnProperty('totalCount')) {
this.totalCount = response.meta.totalCount;
} else {
this.totalCount = -1;
}

let collaborators: User[] = response.data as User[];
return collaborators;
Expand Down Expand Up @@ -104,6 +111,10 @@ export class CollaboratorService {
);
}

getTotalCount(): Observable<number> {
return of(this.totalCount);
}

private handleError(error: any) {
this.logger.error(error);
return observableThrowError(error.message || error);
Expand Down

0 comments on commit e2f9693

Please sign in to comment.