2020-01-01 17:00:43 +00:00
|
|
|
import axios from 'axios';
|
2020-11-10 09:55:26 +00:00
|
|
|
import Contacts from '../../contacts';
|
|
|
|
import types from '../../../mutation-types';
|
2020-01-01 17:00:43 +00:00
|
|
|
import contactList from './fixtures';
|
2020-08-22 18:35:07 +00:00
|
|
|
import { DuplicateContactException } from '../../../../../shared/helpers/CustomErrors';
|
2020-01-01 17:00:43 +00:00
|
|
|
|
2020-11-10 09:55:26 +00:00
|
|
|
const { actions } = Contacts;
|
|
|
|
|
2020-01-01 17:00:43 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
global.axios = axios;
|
|
|
|
jest.mock('axios');
|
|
|
|
|
|
|
|
describe('#actions', () => {
|
|
|
|
describe('#get', () => {
|
2020-07-04 14:16:17 +00:00
|
|
|
it('sends correct mutations if API is success', async () => {
|
2020-11-10 09:55:26 +00:00
|
|
|
axios.get.mockResolvedValue({
|
|
|
|
data: { payload: contactList, meta: { count: 100, current_page: 1 } },
|
|
|
|
});
|
2020-01-01 17:00:43 +00:00
|
|
|
await actions.get({ commit });
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2020-11-10 09:55:26 +00:00
|
|
|
[types.SET_CONTACT_UI_FLAG, { isFetching: true }],
|
|
|
|
[types.CLEAR_CONTACTS],
|
|
|
|
[types.SET_CONTACTS, contactList],
|
|
|
|
[types.SET_CONTACT_META, { count: 100, current_page: 1 }],
|
|
|
|
[types.SET_CONTACT_UI_FLAG, { isFetching: false }],
|
2020-01-01 17:00:43 +00:00
|
|
|
]);
|
|
|
|
});
|
2020-07-04 14:16:17 +00:00
|
|
|
it('sends correct mutations if API is error', async () => {
|
2020-01-01 17:00:43 +00:00
|
|
|
axios.get.mockRejectedValue({ message: 'Incorrect header' });
|
|
|
|
await actions.get({ commit });
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2020-11-10 09:55:26 +00:00
|
|
|
[types.SET_CONTACT_UI_FLAG, { isFetching: true }],
|
|
|
|
[types.SET_CONTACT_UI_FLAG, { isFetching: false }],
|
2020-01-01 17:00:43 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#show', () => {
|
2020-07-04 14:16:17 +00:00
|
|
|
it('sends correct mutations if API is success', async () => {
|
2020-01-01 17:00:43 +00:00
|
|
|
axios.get.mockResolvedValue({ data: { payload: contactList[0] } });
|
|
|
|
await actions.show({ commit }, { id: contactList[0].id });
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2020-11-10 09:55:26 +00:00
|
|
|
[types.SET_CONTACT_UI_FLAG, { isFetchingItem: true }],
|
|
|
|
[types.SET_CONTACT_ITEM, contactList[0]],
|
|
|
|
[types.SET_CONTACT_UI_FLAG, { isFetchingItem: false }],
|
2020-01-01 17:00:43 +00:00
|
|
|
]);
|
|
|
|
});
|
2020-07-04 14:16:17 +00:00
|
|
|
it('sends correct mutations if API is error', async () => {
|
2020-01-01 17:00:43 +00:00
|
|
|
axios.get.mockRejectedValue({ message: 'Incorrect header' });
|
|
|
|
await actions.show({ commit }, { id: contactList[0].id });
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2020-11-10 09:55:26 +00:00
|
|
|
[types.SET_CONTACT_UI_FLAG, { isFetchingItem: true }],
|
|
|
|
[types.SET_CONTACT_UI_FLAG, { isFetchingItem: false }],
|
2020-01-01 17:00:43 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#update', () => {
|
2020-07-04 14:16:17 +00:00
|
|
|
it('sends correct mutations if API is success', async () => {
|
2020-01-01 17:00:43 +00:00
|
|
|
axios.patch.mockResolvedValue({ data: { payload: contactList[0] } });
|
|
|
|
await actions.update({ commit }, contactList[0]);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2020-11-10 09:55:26 +00:00
|
|
|
[types.SET_CONTACT_UI_FLAG, { isUpdating: true }],
|
|
|
|
[types.EDIT_CONTACT, contactList[0]],
|
|
|
|
[types.SET_CONTACT_UI_FLAG, { isUpdating: false }],
|
2020-01-01 17:00:43 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('sends correct actions if API is error', async () => {
|
|
|
|
axios.patch.mockRejectedValue({ message: 'Incorrect header' });
|
|
|
|
await expect(actions.update({ commit }, contactList[0])).rejects.toThrow(
|
|
|
|
Error
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2020-11-10 09:55:26 +00:00
|
|
|
[types.SET_CONTACT_UI_FLAG, { isUpdating: true }],
|
|
|
|
[types.SET_CONTACT_UI_FLAG, { isUpdating: false }],
|
2020-01-01 17:00:43 +00:00
|
|
|
]);
|
|
|
|
});
|
2020-08-22 18:35:07 +00:00
|
|
|
|
|
|
|
it('sends correct actions if duplicate contact is found', async () => {
|
|
|
|
axios.patch.mockRejectedValue({
|
|
|
|
response: {
|
|
|
|
data: {
|
|
|
|
message: 'Incorrect header',
|
|
|
|
contact: { id: 1, name: 'contact-name' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await expect(actions.update({ commit }, contactList[0])).rejects.toThrow(
|
|
|
|
DuplicateContactException
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
2020-11-10 09:55:26 +00:00
|
|
|
[types.SET_CONTACT_UI_FLAG, { isUpdating: true }],
|
|
|
|
[types.SET_CONTACT_UI_FLAG, { isUpdating: false }],
|
2020-08-22 18:35:07 +00:00
|
|
|
]);
|
|
|
|
});
|
2020-01-01 17:00:43 +00:00
|
|
|
});
|
2020-07-04 14:16:17 +00:00
|
|
|
|
|
|
|
describe('#setContact', () => {
|
|
|
|
it('returns correct mutations', () => {
|
|
|
|
const data = { id: 1, name: 'john doe', availability_status: 'online' };
|
|
|
|
actions.setContact({ commit }, data);
|
2020-11-10 09:55:26 +00:00
|
|
|
expect(commit.mock.calls).toEqual([[types.SET_CONTACT_ITEM, data]]);
|
2020-07-04 14:16:17 +00:00
|
|
|
});
|
|
|
|
});
|
2020-01-01 17:00:43 +00:00
|
|
|
});
|