feat: Rewrite conversations mixin to a helper (#9931)
This commit is contained in:
101
app/javascript/dashboard/helper/specs/conversationHelper.spec.js
Normal file
101
app/javascript/dashboard/helper/specs/conversationHelper.spec.js
Normal file
@@ -0,0 +1,101 @@
|
||||
import {
|
||||
filterDuplicateSourceMessages,
|
||||
getLastMessage,
|
||||
getReadMessages,
|
||||
getUnreadMessages,
|
||||
} from '../conversationHelper';
|
||||
import {
|
||||
conversationData,
|
||||
lastMessageData,
|
||||
readMessagesData,
|
||||
unReadMessagesData,
|
||||
} from './fixtures/conversationFixtures';
|
||||
|
||||
describe('conversationHelper', () => {
|
||||
describe('#filterDuplicateSourceMessages', () => {
|
||||
it('returns messages without duplicate source_id and all messages without source_id', () => {
|
||||
const input = [
|
||||
{ source_id: null, id: 1 },
|
||||
{ source_id: '', id: 2 },
|
||||
{ id: 3 },
|
||||
{ source_id: 'wa_1', id: 4 },
|
||||
{ source_id: 'wa_1', id: 5 },
|
||||
{ source_id: 'wa_1', id: 6 },
|
||||
{ source_id: 'wa_2', id: 7 },
|
||||
{ source_id: 'wa_2', id: 8 },
|
||||
{ source_id: 'wa_3', id: 9 },
|
||||
];
|
||||
const expected = [
|
||||
{ source_id: null, id: 1 },
|
||||
{ source_id: '', id: 2 },
|
||||
{ id: 3 },
|
||||
{ source_id: 'wa_1', id: 4 },
|
||||
{ source_id: 'wa_2', id: 7 },
|
||||
{ source_id: 'wa_3', id: 9 },
|
||||
];
|
||||
expect(filterDuplicateSourceMessages(input)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#readMessages', () => {
|
||||
it('should return read messages if conversation is passed', () => {
|
||||
expect(
|
||||
getReadMessages(
|
||||
conversationData.messages,
|
||||
conversationData.agent_last_seen_at
|
||||
)
|
||||
).toEqual(readMessagesData);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#unReadMessages', () => {
|
||||
it('should return unread messages if conversation is passed', () => {
|
||||
expect(
|
||||
getUnreadMessages(
|
||||
conversationData.messages,
|
||||
conversationData.agent_last_seen_at
|
||||
)
|
||||
).toEqual(unReadMessagesData);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#lastMessage', () => {
|
||||
it("should return last activity message if both api and store doesn't have other messages", () => {
|
||||
const testConversation = {
|
||||
messages: [conversationData.messages[0]],
|
||||
last_non_activity_message: null,
|
||||
};
|
||||
expect(getLastMessage(testConversation)).toEqual(
|
||||
testConversation.messages[0]
|
||||
);
|
||||
});
|
||||
|
||||
it('should return message from store if store has latest message', () => {
|
||||
const testConversation = {
|
||||
messages: [],
|
||||
last_non_activity_message: lastMessageData,
|
||||
};
|
||||
expect(getLastMessage(testConversation)).toEqual(lastMessageData);
|
||||
});
|
||||
|
||||
it('should return last non activity message from store if api value is empty', () => {
|
||||
const testConversation = {
|
||||
messages: [conversationData.messages[0], conversationData.messages[1]],
|
||||
last_non_activity_message: null,
|
||||
};
|
||||
expect(getLastMessage(testConversation)).toEqual(
|
||||
testConversation.messages[1]
|
||||
);
|
||||
});
|
||||
|
||||
it("should return last non activity message from store if store doesn't have any messages", () => {
|
||||
const testConversation = {
|
||||
messages: [conversationData.messages[1], conversationData.messages[2]],
|
||||
last_non_activity_message: conversationData.messages[0],
|
||||
};
|
||||
expect(getLastMessage(testConversation)).toEqual(
|
||||
testConversation.messages[1]
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
185
app/javascript/dashboard/helper/specs/fixtures/conversationFixtures.js
vendored
Normal file
185
app/javascript/dashboard/helper/specs/fixtures/conversationFixtures.js
vendored
Normal file
@@ -0,0 +1,185 @@
|
||||
export const conversationData = {
|
||||
meta: {
|
||||
sender: {
|
||||
additional_attributes: {
|
||||
created_at_ip: '127.0.0.1',
|
||||
},
|
||||
availability_status: 'offline',
|
||||
email: null,
|
||||
id: 5017687,
|
||||
name: 'long-flower-143',
|
||||
phone_number: null,
|
||||
thumbnail: '',
|
||||
custom_attributes: {},
|
||||
},
|
||||
channel: 'Channel::WebWidget',
|
||||
assignee: {
|
||||
account_id: 1,
|
||||
availability_status: 'offline',
|
||||
confirmed: true,
|
||||
email: 'muhsin@chatwoot.com',
|
||||
available_name: 'Muhsin Keloth',
|
||||
id: 21,
|
||||
name: 'Muhsin Keloth',
|
||||
role: 'administrator',
|
||||
thumbnail: 'http://example.com/image.png',
|
||||
},
|
||||
},
|
||||
id: 5815,
|
||||
messages: [
|
||||
{
|
||||
id: 438072,
|
||||
content: 'Campaign after 5 seconds',
|
||||
account_id: 1,
|
||||
inbox_id: 37,
|
||||
conversation_id: 5811,
|
||||
message_type: 1,
|
||||
created_at: 1620980262,
|
||||
updated_at: '2021-05-14T08:17:42.041Z',
|
||||
private: false,
|
||||
status: 'sent',
|
||||
source_id: null,
|
||||
content_type: null,
|
||||
content_attributes: {},
|
||||
sender_type: 'User',
|
||||
sender_id: 1,
|
||||
external_source_ids: {},
|
||||
},
|
||||
{
|
||||
id: 4382131101,
|
||||
content: 'Hello',
|
||||
account_id: 1,
|
||||
inbox_id: 37,
|
||||
conversation_id: 5815,
|
||||
message_type: 0,
|
||||
created_at: 1621145476,
|
||||
updated_at: '2021-05-16T05:48:43.910Z',
|
||||
private: false,
|
||||
status: 'sent',
|
||||
source_id: null,
|
||||
content_type: 'text',
|
||||
content_attributes: {},
|
||||
sender_type: null,
|
||||
sender_id: null,
|
||||
external_source_ids: {},
|
||||
},
|
||||
{
|
||||
id: 438100,
|
||||
content: 'Hey',
|
||||
account_id: 1,
|
||||
inbox_id: 37,
|
||||
conversation_id: 5815,
|
||||
message_type: 0,
|
||||
created_at: 1621145476,
|
||||
updated_at: '2021-05-16T05:48:43.910Z',
|
||||
private: false,
|
||||
status: 'sent',
|
||||
source_id: null,
|
||||
content_type: 'text',
|
||||
content_attributes: {},
|
||||
sender_type: null,
|
||||
sender_id: null,
|
||||
external_source_ids: {},
|
||||
},
|
||||
],
|
||||
inbox_id: 37,
|
||||
status: 'open',
|
||||
muted: false,
|
||||
can_reply: true,
|
||||
timestamp: 1621144123,
|
||||
contact_last_seen_at: 0,
|
||||
agent_last_seen_at: 1621144123,
|
||||
unread_count: 0,
|
||||
additional_attributes: {
|
||||
browser: {
|
||||
device_name: 'Unknown',
|
||||
browser_name: 'Chrome',
|
||||
platform_name: 'macOS',
|
||||
browser_version: '90.0.4430.212',
|
||||
platform_version: '10.15.7',
|
||||
},
|
||||
widget_language: null,
|
||||
browser_language: 'en',
|
||||
},
|
||||
account_id: 1,
|
||||
labels: [],
|
||||
};
|
||||
|
||||
export const lastMessageData = {
|
||||
id: 438100,
|
||||
content: 'Hey',
|
||||
account_id: 1,
|
||||
inbox_id: 37,
|
||||
conversation_id: 5815,
|
||||
message_type: 0,
|
||||
created_at: 1621145476,
|
||||
updated_at: '2021-05-16T05:48:43.910Z',
|
||||
private: false,
|
||||
status: 'sent',
|
||||
source_id: null,
|
||||
content_type: 'text',
|
||||
content_attributes: {},
|
||||
sender_type: null,
|
||||
sender_id: null,
|
||||
external_source_ids: {},
|
||||
};
|
||||
|
||||
export const readMessagesData = [
|
||||
{
|
||||
id: 438072,
|
||||
content: 'Campaign after 5 seconds',
|
||||
account_id: 1,
|
||||
inbox_id: 37,
|
||||
conversation_id: 5811,
|
||||
message_type: 1,
|
||||
created_at: 1620980262,
|
||||
updated_at: '2021-05-14T08:17:42.041Z',
|
||||
private: false,
|
||||
status: 'sent',
|
||||
source_id: null,
|
||||
content_type: null,
|
||||
content_attributes: {},
|
||||
sender_type: 'User',
|
||||
sender_id: 1,
|
||||
external_source_ids: {},
|
||||
},
|
||||
];
|
||||
|
||||
export const unReadMessagesData = [
|
||||
{
|
||||
id: 4382131101,
|
||||
content: 'Hello',
|
||||
account_id: 1,
|
||||
inbox_id: 37,
|
||||
conversation_id: 5815,
|
||||
message_type: 0,
|
||||
created_at: 1621145476,
|
||||
updated_at: '2021-05-16T05:48:43.910Z',
|
||||
private: false,
|
||||
status: 'sent',
|
||||
source_id: null,
|
||||
content_type: 'text',
|
||||
content_attributes: {},
|
||||
sender_type: null,
|
||||
sender_id: null,
|
||||
external_source_ids: {},
|
||||
},
|
||||
{
|
||||
id: 438100,
|
||||
content: 'Hey',
|
||||
account_id: 1,
|
||||
inbox_id: 37,
|
||||
conversation_id: 5815,
|
||||
message_type: 0,
|
||||
created_at: 1621145476,
|
||||
updated_at: '2021-05-16T05:48:43.910Z',
|
||||
private: false,
|
||||
status: 'sent',
|
||||
source_id: null,
|
||||
content_type: 'text',
|
||||
content_attributes: {},
|
||||
sender_type: null,
|
||||
sender_id: null,
|
||||
external_source_ids: {},
|
||||
},
|
||||
];
|
||||
Reference in New Issue
Block a user