Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ The response `data` is the same as the [API response body](https://docs.aws.amaz
##### `chatSession.sendEvent()`

```js
/**
* @param {Object} args - Event arguments with contentType and optional metadata and content
* @param {string} args.contentType - Content type of the event
* @param {string|Object} [args.content] - Optional payload (stringified if object)
*/
const awsSdkResponse = await chatSession.sendEvent({
contentType: "application/vnd.amazonaws.connect.event.typing"
});
Expand Down
10 changes: 8 additions & 2 deletions src/core/chatController.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ class ChatController {
return this.chatClient.sendEvent(...args);
}

/**
* @param {Object} args - Event arguments with contentType and optional metadata and content
* @param {string} args.contentType - Content type of the event
* @param {string|Object} [args.content] - Optional payload (stringified if object)
*/
sendEvent(args) {
if (!this._validateConnectionStatus('sendEvent')) {
return Promise.reject(`Failed to call sendEvent, No active connection`);
Expand All @@ -154,6 +159,7 @@ class ChatController {
const content = args.content || null;
var eventType = getEventTypeFromContentType(args.contentType);
var parsedContent = typeof content === "string" ? JSON.parse(content) : content;
var stringifiedContent = content && (typeof content === "string" ? content : (typeof content === "object" ? JSON.stringify(content) : null));
if (this.messageReceiptUtil.isMessageReceipt(eventType, args)) {
// Ignore all MessageReceipt events
if(!GlobalConfig.isFeatureEnabled(FEATURES.MESSAGE_RECEIPTS_ENABLED) || !parsedContent.messageId) {
Expand All @@ -167,7 +173,7 @@ class ChatController {
return this.messageReceiptUtil.prioritizeAndSendMessageReceipt(this.chatClient, this.sendEventIfChatHasNotEnded.bind(this),
connectionToken,
args.contentType,
content,
stringifiedContent,
eventType,
GlobalConfig.getMessageReceiptsThrottleTime())
.then(this.handleRequestSuccess(metadata, ACPS_METHODS.SEND_EVENT, startTime, args.contentType))
Expand All @@ -177,7 +183,7 @@ class ChatController {
.sendEvent(
connectionToken,
args.contentType,
content
stringifiedContent
)
.then(this.handleRequestSuccess(metadata, ACPS_METHODS.SEND_EVENT, startTime, args.contentType))
.catch(this.handleRequestFailure(metadata, ACPS_METHODS.SEND_EVENT, startTime, args.contentType));
Expand Down
71 changes: 71 additions & 0 deletions src/core/chatController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,77 @@ describe("ChatController", () => {
});

describe("Test ChatController induvidual methods with mock data", () => {
describe("sendEvent content handling", () => {
let chatController;

beforeEach(async () => {
chatController = getChatController();
await chatController.connect();
chatClient.sendEvent.mockClear();
});

test("handles string content without modification", async () => {
const stringContent = JSON.stringify({ key: "value" });
const args = {
contentType: CONTENT_TYPE.participantJoined,
content: stringContent
};

await chatController.sendEvent(args);

expect(chatClient.sendEvent).toHaveBeenCalledWith(
"token",
CONTENT_TYPE.participantJoined,
stringContent
);
});

test("stringifies object content", async () => {
const objectContent = { key: "value" };
const args = {
contentType: CONTENT_TYPE.participantJoined,
content: objectContent
};

await chatController.sendEvent(args);

expect(chatClient.sendEvent).toHaveBeenCalledWith(
"token",
CONTENT_TYPE.participantJoined,
JSON.stringify(objectContent)
);
});

test("handles null content as null", async () => {
const args = {
contentType: CONTENT_TYPE.participantJoined,
content: null
};

await chatController.sendEvent(args);

expect(chatClient.sendEvent).toHaveBeenCalledWith(
"token",
CONTENT_TYPE.participantJoined,
null
);
});

test("handles undefined content as null", async () => {
const args = {
contentType: CONTENT_TYPE.participantJoined
};

await chatController.sendEvent(args);

expect(chatClient.sendEvent).toHaveBeenCalledWith(
"token",
CONTENT_TYPE.participantJoined,
null
);
});
});

test("getEventTypeFromContentType should return default INCOMING_MESSAGE type", () => {
const chatController = getChatController();
chatController._forwardChatEvent = jest.fn();
Expand Down