Chore: Fix issues with conversation data models (#1000)

Co-authored-by: Sojan <sojan@pepalo.com>
This commit is contained in:
Pranav Raj S
2020-07-04 19:46:17 +05:30
committed by GitHub
parent 36661ea45d
commit 4612494923
15 changed files with 147 additions and 51 deletions

View File

@@ -63,6 +63,10 @@ export const actions = {
updatePresence: ({ commit }, data) => {
commit(types.default.UPDATE_CONTACTS_PRESENCE, data);
},
setContact({ commit }, data) {
commit(types.default.SET_CONTACT_ITEM, data);
},
};
export const mutations = {

View File

@@ -156,19 +156,24 @@ const actions = {
commit(types.default.ADD_MESSAGE, message);
},
addConversation({ commit, state }, conversation) {
addConversation({ commit, state, dispatch }, conversation) {
const { currentInbox } = state;
if (!currentInbox || Number(currentInbox) === conversation.inbox_id) {
const {
inbox_id: inboxId,
meta: { sender },
} = conversation;
if (!currentInbox || Number(currentInbox) === inboxId) {
commit(types.default.ADD_CONVERSATION, conversation);
commit(
`contacts/${types.default.SET_CONTACT_ITEM}`,
conversation.meta.sender
);
dispatch('contacts/setContact', sender);
}
},
updateConversation({ commit }, conversation) {
updateConversation({ commit, dispatch }, conversation) {
const {
meta: { sender },
} = conversation;
commit(types.default.UPDATE_CONVERSATION, conversation);
dispatch('contacts/setContact', sender);
},
markMessagesRead: async ({ commit }, data) => {

View File

@@ -140,9 +140,10 @@ const mutations = {
c => c.id === conversation.id
);
if (currentConversationIndex > -1) {
const { messages, ...conversationAttributes } = conversation;
const currentConversation = {
...allConversations[currentConversationIndex],
...conversation,
...conversationAttributes,
};
Vue.set(allConversations, currentConversationIndex, currentConversation);
if (_state.selectedChat.id === conversation.id) {

View File

@@ -9,7 +9,7 @@ jest.mock('axios');
describe('#actions', () => {
describe('#get', () => {
it('sends correct actions if API is success', async () => {
it('sends correct mutations if API is success', async () => {
axios.get.mockResolvedValue({ data: { payload: contactList } });
await actions.get({ commit });
expect(commit.mock.calls).toEqual([
@@ -18,7 +18,7 @@ describe('#actions', () => {
[types.default.SET_CONTACT_UI_FLAG, { isFetching: false }],
]);
});
it('sends correct actions if API is error', async () => {
it('sends correct mutations if API is error', async () => {
axios.get.mockRejectedValue({ message: 'Incorrect header' });
await actions.get({ commit });
expect(commit.mock.calls).toEqual([
@@ -29,7 +29,7 @@ describe('#actions', () => {
});
describe('#show', () => {
it('sends correct actions if API is success', async () => {
it('sends correct mutations if API is success', async () => {
axios.get.mockResolvedValue({ data: { payload: contactList[0] } });
await actions.show({ commit }, { id: contactList[0].id });
expect(commit.mock.calls).toEqual([
@@ -38,7 +38,7 @@ describe('#actions', () => {
[types.default.SET_CONTACT_UI_FLAG, { isFetchingItem: false }],
]);
});
it('sends correct actions if API is error', async () => {
it('sends correct mutations if API is error', async () => {
axios.get.mockRejectedValue({ message: 'Incorrect header' });
await actions.show({ commit }, { id: contactList[0].id });
expect(commit.mock.calls).toEqual([
@@ -49,7 +49,7 @@ describe('#actions', () => {
});
describe('#update', () => {
it('sends correct actions if API is success', async () => {
it('sends correct mutations if API is success', async () => {
axios.patch.mockResolvedValue({ data: { payload: contactList[0] } });
await actions.update({ commit }, contactList[0]);
expect(commit.mock.calls).toEqual([
@@ -69,4 +69,14 @@ describe('#actions', () => {
]);
});
});
describe('#setContact', () => {
it('returns correct mutations', () => {
const data = { id: 1, name: 'john doe', availability_status: 'online' };
actions.setContact({ commit }, data);
expect(commit.mock.calls).toEqual([
[types.default.SET_CONTACT_ITEM, data],
]);
});
});
});

View File

@@ -3,6 +3,7 @@ import actions from '../../conversations/actions';
import * as types from '../../../mutation-types';
const commit = jest.fn();
const dispatch = jest.fn();
global.axios = axios;
jest.mock('axios');
@@ -39,4 +40,91 @@ describe('#actions', () => {
expect(commit.mock.calls).toEqual([]);
});
});
describe('#updateConversation', () => {
it('sends setContact action and update_conversation mutation', () => {
const conversation = {
id: 1,
messages: [],
meta: { sender: { id: 1, name: 'john-doe' } },
};
actions.updateConversation({ commit, dispatch }, conversation);
expect(commit.mock.calls).toEqual([
[types.default.UPDATE_CONVERSATION, conversation],
]);
expect(dispatch.mock.calls).toEqual([
[
'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(
{ commit, dispatch, state: { currentInbox: 1 } },
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(
{ commit, dispatch, state: { currentInbox: 1 } },
conversation
);
expect(commit.mock.calls).toEqual([
[types.default.ADD_CONVERSATION, conversation],
]);
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,
};
actions.addConversation({ commit, dispatch, state: {} }, conversation);
expect(commit.mock.calls).toEqual([
[types.default.ADD_CONVERSATION, conversation],
]);
expect(dispatch.mock.calls).toEqual([
[
'contacts/setContact',
{
id: 1,
name: 'john-doe',
},
],
]);
});
});
});