Skip to content

Commit

Permalink
fix(ActivitiesSdkAdapter): update postAction to use conversation API
Browse files Browse the repository at this point in the history
  • Loading branch information
patricia0817 authored and cipak committed Mar 16, 2022
1 parent be9a9a9 commit 75498ed
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
37 changes: 17 additions & 20 deletions src/ActivitiesSDKAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'rxjs';
import {
catchError,
concatMap,
map,
tap,
} from 'rxjs/operators';
Expand Down Expand Up @@ -137,32 +138,28 @@ export default class ActivitiesSDKAdapter extends ActivitiesAdapter {
* @returns {Observable.<object>} Observable stream that emits data of the newly created action
*/
postAction(activityID, inputs) {
logger.debug('ATTACHMENT-ACTION', undefined, 'postAction()', ['called with', {activityID, inputs}]);

const action$ = from(this.datasource.attachmentActions.create({
type: 'submit',
messageId: activityID,
inputs,
})).pipe(
map((action) => ({
actionID: action.id,
activityID: action.messageId,
inputs: action.inputs,
roomID: action.roomId,
personID: action.personId,
type: action.type,
created: action.created,
})),
logger.debug('ACTION', undefined, 'postAction()', ['called with', {activityID, inputs}]);

return from(this.fetchActivity(activityID)).pipe(
concatMap(async (parentActivity) => {
const encryptedInputs = await this.datasource.internal.encryption
.encryptText(parentActivity.encryptionKeyUrl, JSON.stringify(inputs));

return this.datasource.internal.conversation.cardAction(
parentActivity.target,
{inputs: encryptedInputs},
parentActivity,
);
}),
tap((action) => {
logger.debug('ATTACHMENT-ACTION', action.actionID, 'postAction()', ['emitting posted attachment action', action]);
logger.debug('ACTION', action.id, 'postAction()', ['emitting posted action', action]);
}),
map(fromSDKActivity),
catchError((err) => {
logger.error('ATTACHMENT-ACTION', undefined, 'postAction()', `Unable to create an attachment for activity with id "${activityID}"`, err);
logger.error('ACTION', undefined, 'postAction()', `Unable to create an action for activity with id "${activityID}"`, err);
throw err;
}),
);

return action$;
}

/**
Expand Down
44 changes: 30 additions & 14 deletions src/ActivitiesSDKAdapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,39 +149,55 @@ describe('Activities SDK Adapter', () => {
});

describe('postAction()', () => {
beforeEach(() => {
activitiesSDKAdapter.fetchActivity = jest.fn(
() => Promise.resolve(serverActivity),
);
});
test('emits the posted action object', (done) => {
const inputs = {
firstName: 'My first name',
lastname: 'My last name',
};

activitiesSDKAdapter.postAction(activityID, inputs).pipe(last()).subscribe((action) => {
expect(action).toMatchObject({
actionID: 'actionID',
activityID: 'activityID',
inputs: {
firstName: 'My first name',
lastName: 'My last name',
activitiesSDKAdapter.datasource.internal.conversation.cardAction = jest.fn(
() => Promise.resolve({
ID,
actor: {
id: actorID,
},
object: {
displayName: 'text',
},
roomID: 'roomID',
personID: 'personID',
type: 'submit',
target: {
id: targetID,
},
published: created,
}),
);

activitiesSDKAdapter.postAction(activityID, inputs).subscribe((action) => {
expect(action).toMatchObject({
ID,
text: 'text',
roomID,
personID,
created,
});
done();
});
});

test('emits the sdk error when attachmentActions.create returns a rejected promise', (done) => {
test('emits the sdk error when internal.conversation.cardAction returns a rejected promise', (done) => {
const sdkError = new Error('sdk-error');

activitiesSDKAdapter.datasource.attachmentActions.create = jest.fn(
activitiesSDKAdapter.datasource.internal.conversation.cardAction = jest.fn(
() => Promise.reject(sdkError),
);

activitiesSDKAdapter.postAction({}).subscribe(
activitiesSDKAdapter.postAction(activityID, {x: 1, y: 2}).subscribe(
() => {
done.fail('Created attachment action instead of returning error');
done.fail('Created card action instead of returning error');
},
(error) => {
expect(error).toBe(sdkError);
Expand Down
3 changes: 3 additions & 0 deletions src/mockSdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ export default function createMockSDK(api = {}) {
unsubscribe: jest.fn(() => Promise.resolve()),
},
conversation: mockInternalConversationAPI,
encryption: {
encryptText: jest.fn(() => Promise.resolve('encrypted text')),
},
},
people: {
get: jest.fn(() => Promise.resolve(mockSDKPerson)),
Expand Down

0 comments on commit 75498ed

Please sign in to comment.