2020-03-08 16:38:25 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
import actions from '../../conversations/actions';
|
2021-11-12 08:11:43 +00:00
|
|
|
import types from '../../../mutation-types';
|
2021-11-18 13:45:02 +00:00
|
|
|
const dataToSend = {
|
|
|
|
payload: [
|
|
|
|
{
|
|
|
|
attribute_key: 'status',
|
|
|
|
filter_operator: 'equal_to',
|
|
|
|
values: ['open'],
|
|
|
|
query_operator: null,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
import { dataReceived } from './testConversationResponse';
|
2020-03-08 16:38:25 +00:00
|
|
|
|
|
|
|
const commit = jest.fn();
|
2020-07-04 14:16:17 +00:00
|
|
|
const dispatch = jest.fn();
|
2020-03-08 16:38:25 +00:00
|
|
|
global.axios = axios;
|
|
|
|
jest.mock('axios');
|
|
|
|
|
|
|
|
describe('#actions', () => {
|
|
|
|
describe('#getConversation', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
2020-06-02 17:29:02 +00:00
|
|
|
axios.get.mockResolvedValue({
|
|
|
|
data: { id: 1, meta: { sender: { id: 1, name: 'Contact 1' } } },
|
|
|
|
});
|
2020-03-08 16:38:25 +00:00
|
|
|
await actions.getConversation({ commit }, 1);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2020-06-02 17:29:02 +00:00
|
|
|
[
|
2021-11-12 08:11:43 +00:00
|
|
|
types.UPDATE_CONVERSATION,
|
2020-06-02 17:29:02 +00:00
|
|
|
{ id: 1, meta: { sender: { id: 1, name: 'Contact 1' } } },
|
|
|
|
],
|
|
|
|
['contacts/SET_CONTACT_ITEM', { id: 1, name: 'Contact 1' }],
|
2020-03-08 16:38:25 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('sends correct actions if API is error', async () => {
|
|
|
|
axios.get.mockRejectedValue({ message: 'Incorrect header' });
|
|
|
|
await actions.getConversation({ commit });
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2020-05-26 12:13:59 +00:00
|
|
|
describe('#muteConversation', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
|
|
|
axios.get.mockResolvedValue(null);
|
|
|
|
await actions.muteConversation({ commit }, 1);
|
2021-11-12 08:11:43 +00:00
|
|
|
expect(commit.mock.calls).toEqual([[types.MUTE_CONVERSATION]]);
|
2020-05-26 12:13:59 +00:00
|
|
|
});
|
|
|
|
it('sends correct actions if API is error', async () => {
|
|
|
|
axios.get.mockRejectedValue({ message: 'Incorrect header' });
|
|
|
|
await actions.getConversation({ commit });
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2020-07-04 14:16:17 +00:00
|
|
|
|
|
|
|
describe('#updateConversation', () => {
|
|
|
|
it('sends setContact action and update_conversation mutation', () => {
|
|
|
|
const conversation = {
|
|
|
|
id: 1,
|
|
|
|
messages: [],
|
|
|
|
meta: { sender: { id: 1, name: 'john-doe' } },
|
2022-10-25 06:49:19 +00:00
|
|
|
labels: ['support'],
|
2020-07-04 14:16:17 +00:00
|
|
|
};
|
2021-12-09 05:50:14 +00:00
|
|
|
actions.updateConversation(
|
|
|
|
{ commit, rootState: { route: { name: 'home' } }, dispatch },
|
|
|
|
conversation
|
|
|
|
);
|
2020-07-04 14:16:17 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
2021-11-12 08:11:43 +00:00
|
|
|
[types.UPDATE_CONVERSATION, conversation],
|
2020-07-04 14:16:17 +00:00
|
|
|
]);
|
|
|
|
expect(dispatch.mock.calls).toEqual([
|
2022-10-25 06:49:19 +00:00
|
|
|
[
|
|
|
|
'conversationLabels/setConversationLabel',
|
|
|
|
{ id: 1, data: ['support'] },
|
|
|
|
],
|
2020-07-04 14:16:17 +00:00
|
|
|
[
|
|
|
|
'contacts/setContact',
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: 'john-doe',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#addConversation', () => {
|
|
|
|
it('doesnot send mutation if conversation is from a different inbox', () => {
|
|
|
|
const conversation = {
|
|
|
|
id: 1,
|
|
|
|
messages: [],
|
|
|
|
meta: { sender: { id: 1, name: 'john-doe' } },
|
|
|
|
inbox_id: 2,
|
|
|
|
};
|
|
|
|
actions.addConversation(
|
2021-11-18 13:45:02 +00:00
|
|
|
{
|
|
|
|
commit,
|
2021-12-09 05:50:14 +00:00
|
|
|
rootState: { route: { name: 'home' } },
|
2021-11-18 13:45:02 +00:00
|
|
|
dispatch,
|
|
|
|
state: { currentInbox: 1, appliedFilters: [] },
|
|
|
|
},
|
|
|
|
conversation
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
expect(dispatch.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('doesnot send mutation if conversation filters are applied', () => {
|
|
|
|
const conversation = {
|
|
|
|
id: 1,
|
|
|
|
messages: [],
|
|
|
|
meta: { sender: { id: 1, name: 'john-doe' } },
|
|
|
|
inbox_id: 1,
|
|
|
|
};
|
|
|
|
actions.addConversation(
|
|
|
|
{
|
|
|
|
commit,
|
2021-12-09 05:50:14 +00:00
|
|
|
rootState: { route: { name: 'home' } },
|
|
|
|
dispatch,
|
|
|
|
state: { currentInbox: 1, appliedFilters: [{ id: 'random-filter' }] },
|
|
|
|
},
|
|
|
|
conversation
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
expect(dispatch.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('doesnot send mutation if the view is conversation mentions', () => {
|
|
|
|
const conversation = {
|
|
|
|
id: 1,
|
|
|
|
messages: [],
|
|
|
|
meta: { sender: { id: 1, name: 'john-doe' } },
|
|
|
|
inbox_id: 1,
|
|
|
|
};
|
|
|
|
actions.addConversation(
|
|
|
|
{
|
|
|
|
commit,
|
|
|
|
rootState: { route: { name: 'conversation_mentions' } },
|
2021-11-18 13:45:02 +00:00
|
|
|
dispatch,
|
|
|
|
state: { currentInbox: 1, appliedFilters: [{ id: 'random-filter' }] },
|
|
|
|
},
|
2020-07-04 14:16:17 +00:00
|
|
|
conversation
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
expect(dispatch.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends correct mutations', () => {
|
|
|
|
const conversation = {
|
|
|
|
id: 1,
|
|
|
|
messages: [],
|
|
|
|
meta: { sender: { id: 1, name: 'john-doe' } },
|
|
|
|
inbox_id: 1,
|
|
|
|
};
|
|
|
|
actions.addConversation(
|
2021-11-18 13:45:02 +00:00
|
|
|
{
|
|
|
|
commit,
|
2021-12-09 05:50:14 +00:00
|
|
|
rootState: { route: { name: 'home' } },
|
2021-11-18 13:45:02 +00:00
|
|
|
dispatch,
|
|
|
|
state: { currentInbox: 1, appliedFilters: [] },
|
|
|
|
},
|
2020-07-04 14:16:17 +00:00
|
|
|
conversation
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2021-11-12 08:11:43 +00:00
|
|
|
[types.ADD_CONVERSATION, conversation],
|
2020-07-04 14:16:17 +00:00
|
|
|
]);
|
|
|
|
expect(dispatch.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
'contacts/setContact',
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: 'john-doe',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends correct mutations if inbox filter is not available', () => {
|
|
|
|
const conversation = {
|
|
|
|
id: 1,
|
|
|
|
messages: [],
|
|
|
|
meta: { sender: { id: 1, name: 'john-doe' } },
|
|
|
|
inbox_id: 1,
|
|
|
|
};
|
2021-11-18 13:45:02 +00:00
|
|
|
actions.addConversation(
|
2021-12-09 05:50:14 +00:00
|
|
|
{
|
|
|
|
commit,
|
|
|
|
rootState: { route: { name: 'home' } },
|
|
|
|
dispatch,
|
|
|
|
state: { appliedFilters: [] },
|
|
|
|
},
|
2021-11-18 13:45:02 +00:00
|
|
|
conversation
|
|
|
|
);
|
2020-07-04 14:16:17 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
2021-11-12 08:11:43 +00:00
|
|
|
[types.ADD_CONVERSATION, conversation],
|
2020-07-04 14:16:17 +00:00
|
|
|
]);
|
|
|
|
expect(dispatch.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
'contacts/setContact',
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: 'john-doe',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2020-07-25 17:24:45 +00:00
|
|
|
describe('#addMessage', () => {
|
|
|
|
it('sends correct mutations if message is incoming', () => {
|
|
|
|
const message = {
|
|
|
|
id: 1,
|
|
|
|
message_type: 0,
|
|
|
|
conversation_id: 1,
|
|
|
|
};
|
|
|
|
actions.addMessage({ commit }, message);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2021-11-12 08:11:43 +00:00
|
|
|
[types.ADD_MESSAGE, message],
|
2020-07-25 17:24:45 +00:00
|
|
|
[
|
2021-11-12 08:11:43 +00:00
|
|
|
types.SET_CONVERSATION_CAN_REPLY,
|
2020-07-25 17:24:45 +00:00
|
|
|
{ conversationId: 1, canReply: true },
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('sends correct mutations if message is not an incoming message', () => {
|
|
|
|
const message = {
|
|
|
|
id: 1,
|
|
|
|
message_type: 1,
|
|
|
|
conversation_id: 1,
|
|
|
|
};
|
|
|
|
actions.addMessage({ commit }, message);
|
2021-11-12 08:11:43 +00:00
|
|
|
expect(commit.mock.calls).toEqual([[types.ADD_MESSAGE, message]]);
|
2020-07-25 17:24:45 +00:00
|
|
|
});
|
|
|
|
});
|
2020-08-03 08:10:20 +00:00
|
|
|
|
|
|
|
describe('#markMessagesRead', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends correct mutations if api is successful', async () => {
|
|
|
|
const lastSeen = new Date().getTime() / 1000;
|
|
|
|
axios.post.mockResolvedValue({
|
|
|
|
data: { id: 1, agent_last_seen_at: lastSeen },
|
|
|
|
});
|
|
|
|
await actions.markMessagesRead({ commit }, { id: 1 });
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(commit).toHaveBeenCalledTimes(1);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2021-11-12 08:11:43 +00:00
|
|
|
[types.MARK_MESSAGE_READ, { id: 1, lastSeen }],
|
2020-08-03 08:10:20 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('sends correct mutations if api is unsuccessful', async () => {
|
|
|
|
axios.post.mockRejectedValue({ message: 'Incorrect header' });
|
|
|
|
await actions.markMessagesRead({ commit }, { id: 1 });
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2020-08-17 05:55:13 +00:00
|
|
|
|
|
|
|
describe('#sendEmailTranscript', () => {
|
|
|
|
it('sends correct mutations if api is successful', async () => {
|
|
|
|
axios.post.mockResolvedValue({});
|
|
|
|
await actions.sendEmailTranscript(
|
|
|
|
{ commit },
|
|
|
|
{ conversationId: 1, email: 'testemail@example.com' }
|
|
|
|
);
|
|
|
|
expect(commit).toHaveBeenCalledTimes(0);
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2021-03-15 13:07:03 +00:00
|
|
|
|
|
|
|
describe('#assignAgent', () => {
|
|
|
|
it('sends correct mutations if assignment is successful', async () => {
|
|
|
|
axios.post.mockResolvedValue({
|
|
|
|
data: { id: 1, name: 'User' },
|
|
|
|
});
|
|
|
|
await actions.assignAgent({ commit }, { conversationId: 1, agentId: 1 });
|
|
|
|
expect(commit).toHaveBeenCalledTimes(0);
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#setCurrentChatAssignee', () => {
|
|
|
|
it('sends correct mutations if assignment is successful', async () => {
|
|
|
|
axios.post.mockResolvedValue({
|
|
|
|
data: { id: 1, name: 'User' },
|
|
|
|
});
|
|
|
|
await actions.setCurrentChatAssignee({ commit }, { id: 1, name: 'User' });
|
|
|
|
expect(commit).toHaveBeenCalledTimes(1);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
['ASSIGN_AGENT', { id: 1, name: 'User' }],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-02 07:41:07 +00:00
|
|
|
describe('#toggleStatus', () => {
|
|
|
|
it('sends correct mutations if toggle status is successful', async () => {
|
|
|
|
axios.post.mockResolvedValue({
|
2021-09-29 14:03:51 +00:00
|
|
|
data: {
|
|
|
|
payload: {
|
|
|
|
conversation_id: 1,
|
|
|
|
current_status: 'snoozed',
|
|
|
|
snoozed_until: null,
|
|
|
|
},
|
|
|
|
},
|
2021-08-02 07:41:07 +00:00
|
|
|
});
|
|
|
|
await actions.toggleStatus(
|
|
|
|
{ commit },
|
2021-09-29 14:03:51 +00:00
|
|
|
{ conversationId: 1, status: 'snoozed' }
|
2021-08-02 07:41:07 +00:00
|
|
|
);
|
|
|
|
expect(commit).toHaveBeenCalledTimes(1);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2021-09-29 14:03:51 +00:00
|
|
|
[
|
|
|
|
'CHANGE_CONVERSATION_STATUS',
|
|
|
|
{ conversationId: 1, status: 'snoozed', snoozedUntil: null },
|
|
|
|
],
|
2021-08-02 07:41:07 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-15 13:07:03 +00:00
|
|
|
describe('#assignTeam', () => {
|
|
|
|
it('sends correct mutations if assignment is successful', async () => {
|
|
|
|
axios.post.mockResolvedValue({
|
|
|
|
data: { id: 1, name: 'Team' },
|
|
|
|
});
|
|
|
|
await actions.assignTeam({ commit }, { conversationId: 1, teamId: 1 });
|
|
|
|
expect(commit).toHaveBeenCalledTimes(0);
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#setCurrentChatTeam', () => {
|
|
|
|
it('sends correct mutations if assignment is successful', async () => {
|
|
|
|
axios.post.mockResolvedValue({
|
|
|
|
data: { id: 1, name: 'Team' },
|
|
|
|
});
|
2022-08-05 05:27:58 +00:00
|
|
|
await actions.setCurrentChatTeam(
|
|
|
|
{ commit },
|
|
|
|
{ team: { id: 1, name: 'Team' }, conversationId: 1 }
|
|
|
|
);
|
2021-03-15 13:07:03 +00:00
|
|
|
expect(commit).toHaveBeenCalledTimes(1);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2022-08-05 05:27:58 +00:00
|
|
|
['ASSIGN_TEAM', { team: { id: 1, name: 'Team' }, conversationId: 1 }],
|
2021-03-15 13:07:03 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2021-11-18 13:45:02 +00:00
|
|
|
|
|
|
|
describe('#fetchFilteredConversations', () => {
|
|
|
|
it('fetches filtered conversations with a mock commit', async () => {
|
|
|
|
axios.post.mockResolvedValue({
|
|
|
|
data: dataReceived,
|
|
|
|
});
|
|
|
|
await actions.fetchFilteredConversations({ commit }, dataToSend);
|
|
|
|
expect(commit).toHaveBeenCalledTimes(2);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
['SET_LIST_LOADING_STATUS'],
|
|
|
|
['SET_ALL_CONVERSATION', dataReceived.payload],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#setConversationFilter', () => {
|
|
|
|
it('commits the correct mutation and sets filter state', () => {
|
|
|
|
const filters = [
|
|
|
|
{
|
|
|
|
attribute_key: 'status',
|
|
|
|
filter_operator: 'equal_to',
|
|
|
|
values: [{ id: 'snoozed', name: 'Snoozed' }],
|
|
|
|
query_operator: 'and',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
actions.setConversationFilters({ commit }, filters);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[types.SET_CONVERSATION_FILTERS, filters],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#clearConversationFilter', () => {
|
|
|
|
it('commits the correct mutation and clears filter state', () => {
|
|
|
|
actions.clearConversationFilters({ commit });
|
|
|
|
expect(commit.mock.calls).toEqual([[types.CLEAR_CONVERSATION_FILTERS]]);
|
|
|
|
});
|
|
|
|
});
|
2020-03-08 16:38:25 +00:00
|
|
|
});
|
2021-06-29 05:49:08 +00:00
|
|
|
|
|
|
|
describe('#deleteMessage', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
|
|
|
const [conversationId, messageId] = [1, 1];
|
|
|
|
axios.delete.mockResolvedValue({ data: { id: 1, content: 'deleted' } });
|
|
|
|
await actions.deleteMessage({ commit }, { conversationId, messageId });
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2021-11-12 08:11:43 +00:00
|
|
|
[types.ADD_MESSAGE, { id: 1, content: 'deleted' }],
|
2021-06-29 05:49:08 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('sends no actions if API is error', async () => {
|
|
|
|
const [conversationId, messageId] = [1, 1];
|
|
|
|
axios.delete.mockRejectedValue({ message: 'Incorrect header' });
|
|
|
|
await expect(
|
|
|
|
actions.deleteMessage({ commit }, { conversationId, messageId })
|
|
|
|
).rejects.toThrow(Error);
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
});
|
2021-10-30 01:39:46 +00:00
|
|
|
|
|
|
|
describe('#updateCustomAttributes', () => {
|
|
|
|
it('update conversation custom attributes', async () => {
|
|
|
|
axios.post.mockResolvedValue({
|
|
|
|
data: { custom_attributes: { order_d: '1001' } },
|
|
|
|
});
|
|
|
|
await actions.updateCustomAttributes(
|
|
|
|
{ commit },
|
|
|
|
{
|
|
|
|
conversationId: 1,
|
|
|
|
customAttributes: { order_d: '1001' },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2021-11-12 08:11:43 +00:00
|
|
|
[types.UPDATE_CONVERSATION_CUSTOM_ATTRIBUTES, { order_d: '1001' }],
|
2021-10-30 01:39:46 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2021-06-29 05:49:08 +00:00
|
|
|
});
|
2021-12-09 05:50:14 +00:00
|
|
|
|
|
|
|
describe('#addMentions', () => {
|
|
|
|
it('does not send mutations if the view is not mentions', () => {
|
|
|
|
actions.addMentions(
|
|
|
|
{ commit, dispatch, rootState: { route: { name: 'home' } } },
|
|
|
|
{ id: 1 }
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
expect(dispatch.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('send mutations if the view is mentions', () => {
|
|
|
|
actions.addMentions(
|
|
|
|
{
|
|
|
|
dispatch,
|
|
|
|
rootState: { route: { name: 'conversation_mentions' } },
|
|
|
|
},
|
|
|
|
{ id: 1, meta: { sender: { id: 1 } } }
|
|
|
|
);
|
|
|
|
expect(dispatch.mock.calls).toEqual([
|
|
|
|
['updateConversation', { id: 1, meta: { sender: { id: 1 } } }],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|