Skip to content

Commit

Permalink
feat(PeopleSdkAdapter): add search people method
Browse files Browse the repository at this point in the history
  • Loading branch information
harshrai-11 authored and kishorepmr committed May 24, 2022
1 parent acb75b6 commit fec1465
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/PeopleSDKAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ function getStatus(status) {
return personStatus === undefined ? null : personStatus;
}

/**
* Maps SDK People to adapter people
*
* @private
* @param {object} sdkPeople SDK people object
* @returns {Person} Adapter person object
*/
function fromSDKPeople(sdkPeople) {
const {items} = sdkPeople;

return items.map((item) => ({
ID: item.id,
emails: item.emails,
displayName: item.displayName,
firstName: item.firstName,
lastName: item.lastName,
nickName: item.nickName,
avatar: item.avatar,
orgID: item.orgId,
status: item.status,
}));
}

/**
* The `PeopleSDKAdapter` is an implementation of the `PeopleAdapter` interface.
* This adapter utilizes the Webex JS SDK to fetch data about a person.
Expand Down Expand Up @@ -184,4 +207,25 @@ export default class PeopleSDKAdapter extends PeopleAdapter {

return this.getPersonObservables[ID];
}

/**
* Returns an observable that emits a list of people that match the given query.
* An empty array is returned when there are no matches.
*
* @param {string} query Search query
* @returns {external:Observable.<Person[]>} Observable that emits person list based on search query
*/
searchPeople(query) {
logger.debug('PEOPLE', undefined, 'searchPeople()', ['called with', {query}]);

return defer(() => this.datasource.people.list({displayName: query}))
.pipe(
map(fromSDKPeople),
tap((peopleList) => logger.debug('PEOPLE', undefined, 'searchPeople()', ['emit persons list', peopleList])),
catchError((error) => {
logger.error('PEOPLE', undefined, 'searchPeople()', 'error fetching person list', error);
throw error;
}),
);
}
}
54 changes: 54 additions & 0 deletions src/PeopleSDKAdapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ describe('People SDK Adapter', () => {
let mockSDK;
let peopleSDKAdapter;
let personID;
let query;

beforeEach(() => {
mockSDK = createMockSDK();
peopleSDKAdapter = new PeopleSDKAdapter(mockSDK);
personID = 'personID';
query = 'query';
});

describe('getMe()', () => {
Expand Down Expand Up @@ -119,6 +121,58 @@ describe('People SDK Adapter', () => {
});
});

describe('searchPeople', () => {
test('returns an observable', () => {
expect(isObservable(peopleSDKAdapter.searchPeople(query))).toBeTruthy();
});

test('emits persons list on subscription', (done) => {
mockSDK.people.list = jest.fn(() => Promise.resolve({
items: [
{
id: 'personIDCurrentUser',
emails: ['[email protected]'],
displayName: 'Webex Components',
firstName: 'Webex',
lastName: 'Components',
nickName: 'Webex',
avatar: 'avatar',
orgId: 'orgID',
status: 'active',
},
],
}));

peopleSDKAdapter.searchPeople(query).subscribe((peopleList) => {
expect(peopleList).toEqual([{
ID: 'personIDCurrentUser',
emails: ['[email protected]'],
displayName: 'Webex Components',
firstName: 'Webex',
lastName: 'Components',
nickName: 'Webex',
avatar: 'avatar',
orgID: 'orgID',
status: 'active',
}]);
});
done();
});

test('emits an error if the SDK fails to fetch the person list', (done) => {
const errorMsg = 'error fetching person list';

mockSDK.people.list = jest.fn(() => Promise.reject(new Error(errorMsg)));
peopleSDKAdapter.searchPeople(query).subscribe(
() => {},
(error) => {
expect(error.message).toBe(errorMsg);
done();
},
);
});
});

afterEach(() => {
mockSDK = null;
peopleSDKAdapter = null;
Expand Down

0 comments on commit fec1465

Please sign in to comment.