2020-02-16 11:50:38 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import { actions } from '../../auth';
|
|
|
|
import * as types from '../../../mutation-types';
|
|
|
|
import { setUser, clearCookiesOnLogout } from '../../../utils/api';
|
|
|
|
import '../../../../routes';
|
|
|
|
|
|
|
|
jest.mock('../../../../routes', () => {});
|
|
|
|
jest.mock('../../../utils/api', () => ({
|
|
|
|
setUser: jest.fn(),
|
|
|
|
clearCookiesOnLogout: jest.fn(),
|
|
|
|
getHeaderExpiry: jest.fn(),
|
|
|
|
}));
|
|
|
|
jest.mock('js-cookie', () => ({
|
|
|
|
getJSON: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const commit = jest.fn();
|
|
|
|
const dispatch = jest.fn();
|
|
|
|
global.axios = axios;
|
|
|
|
jest.mock('axios');
|
|
|
|
|
|
|
|
describe('#actions', () => {
|
|
|
|
describe('#validityCheck', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
|
|
|
axios.get.mockResolvedValue({
|
|
|
|
data: { payload: { data: { id: 1, name: 'John' } } },
|
|
|
|
headers: { expiry: 581842904 },
|
|
|
|
});
|
|
|
|
await actions.validityCheck({ commit });
|
|
|
|
expect(setUser).toHaveBeenCalledTimes(1);
|
2022-04-14 15:24:26 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[types.default.SET_CURRENT_USER, { id: 1, name: 'John' }],
|
|
|
|
]);
|
2020-02-16 11:50:38 +00:00
|
|
|
});
|
|
|
|
it('sends correct actions if API is error', async () => {
|
|
|
|
axios.get.mockRejectedValue({
|
|
|
|
response: { status: 401 },
|
|
|
|
});
|
|
|
|
await actions.validityCheck({ commit });
|
|
|
|
expect(clearCookiesOnLogout);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#updateProfile', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
|
|
|
axios.put.mockResolvedValue({
|
|
|
|
data: { id: 1, name: 'John' },
|
|
|
|
headers: { expiry: 581842904 },
|
|
|
|
});
|
|
|
|
await actions.updateProfile({ commit }, { name: 'Pranav' });
|
2022-04-14 15:24:26 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[types.default.SET_CURRENT_USER, { id: 1, name: 'John' }],
|
|
|
|
]);
|
2020-02-16 11:50:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-21 17:13:27 +00:00
|
|
|
describe('#updateAvailability', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
2021-10-07 07:51:46 +00:00
|
|
|
axios.post.mockResolvedValue({
|
|
|
|
data: {
|
|
|
|
id: 1,
|
2022-04-14 15:24:26 +00:00
|
|
|
name: 'John',
|
|
|
|
accounts: [{ account_id: 1, availability_status: 'offline' }],
|
2021-10-07 07:51:46 +00:00
|
|
|
},
|
2020-11-21 17:13:27 +00:00
|
|
|
headers: { expiry: 581842904 },
|
|
|
|
});
|
|
|
|
await actions.updateAvailability(
|
|
|
|
{ commit, dispatch },
|
2021-11-26 19:26:07 +00:00
|
|
|
{ availability: 'offline', account_id: 1 }
|
2020-11-21 17:13:27 +00:00
|
|
|
);
|
2022-04-14 15:24:26 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
types.default.SET_CURRENT_USER,
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: 'John',
|
|
|
|
accounts: [{ account_id: 1, availability_status: 'offline' }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]);
|
2020-11-21 17:13:27 +00:00
|
|
|
expect(dispatch.mock.calls).toEqual([
|
2022-08-09 07:02:09 +00:00
|
|
|
[
|
|
|
|
'agents/updateSingleAgentPresence',
|
|
|
|
{ availabilityStatus: 'offline', id: 1 },
|
|
|
|
],
|
2020-11-21 17:13:27 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-01-10 13:55:33 +00:00
|
|
|
describe('#updateUISettings', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
|
|
|
axios.put.mockResolvedValue({
|
|
|
|
data: {
|
|
|
|
id: 1,
|
|
|
|
name: 'John',
|
|
|
|
availability_status: 'offline',
|
|
|
|
ui_settings: { is_contact_sidebar_open: true },
|
|
|
|
},
|
|
|
|
headers: { expiry: 581842904 },
|
|
|
|
});
|
|
|
|
await actions.updateUISettings(
|
|
|
|
{ commit, dispatch },
|
|
|
|
{ uiSettings: { is_contact_sidebar_open: false } }
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
types.default.SET_CURRENT_USER_UI_SETTINGS,
|
|
|
|
{ uiSettings: { is_contact_sidebar_open: false } },
|
|
|
|
],
|
2022-04-14 15:24:26 +00:00
|
|
|
[
|
|
|
|
types.default.SET_CURRENT_USER,
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: 'John',
|
|
|
|
availability_status: 'offline',
|
|
|
|
ui_settings: { is_contact_sidebar_open: true },
|
|
|
|
},
|
|
|
|
],
|
2021-01-10 13:55:33 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-16 11:50:38 +00:00
|
|
|
describe('#setUser', () => {
|
|
|
|
it('sends correct actions if user is logged in', async () => {
|
|
|
|
Cookies.getJSON.mockImplementation(() => true);
|
|
|
|
actions.setUser({ commit, dispatch });
|
2022-04-14 15:24:26 +00:00
|
|
|
expect(commit.mock.calls).toEqual([]);
|
2020-02-16 11:50:38 +00:00
|
|
|
expect(dispatch.mock.calls).toEqual([['validityCheck']]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends correct actions if user is not logged in', async () => {
|
|
|
|
Cookies.getJSON.mockImplementation(() => false);
|
|
|
|
actions.setUser({ commit, dispatch });
|
2022-04-14 15:24:26 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[types.default.CLEAR_USER],
|
|
|
|
[types.default.SET_CURRENT_USER_UI_FLAGS, { isFetching: false }],
|
|
|
|
]);
|
2020-02-16 11:50:38 +00:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
});
|
2020-07-04 14:33:16 +00:00
|
|
|
|
2021-10-14 19:00:48 +00:00
|
|
|
describe('#setCurrentUserAvailability', () => {
|
2020-07-04 14:33:16 +00:00
|
|
|
it('sends correct mutations if user id is available', async () => {
|
2021-10-14 19:00:48 +00:00
|
|
|
actions.setCurrentUserAvailability(
|
2020-07-04 14:33:16 +00:00
|
|
|
{
|
|
|
|
commit,
|
|
|
|
state: { currentUser: { id: 1 } },
|
|
|
|
},
|
|
|
|
{ 1: 'online' }
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
[types.default.SET_CURRENT_USER_AVAILABILITY, 'online'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not send correct mutations if user id is not available', async () => {
|
2021-10-14 19:00:48 +00:00
|
|
|
actions.setCurrentUserAvailability(
|
2020-07-04 14:33:16 +00:00
|
|
|
{
|
|
|
|
commit,
|
|
|
|
state: { currentUser: { id: 1 } },
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2020-02-16 11:50:38 +00:00
|
|
|
});
|