Converts the canned-response and agent API clients to CacheEnabledApiClient subclasses and wires revalidate actions into the corresponding Vuex modules, so settings pages and pickers stop fetching the same lists on every visit. The canned-response search box continues to bypass the cache and hit the network directly, so server-side ordering stays authoritative. Also fixes a pre-existing gap in teams/revalidate: SET_TEAMS merges into a record dict by id and never removes missing entries, so a team deleted server-side would survive a reconnect-driven revalidate. Now matches the get action by emitting CLEAR_TEAMS first. Each affected spec clears its IDB store before every case, since the preflight-throws-on-axios-reject pattern they relied on is going away in the next commit.
126 lines
4.5 KiB
JavaScript
126 lines
4.5 KiB
JavaScript
import axios from 'axios';
|
|
import { actions } from '../../agents';
|
|
import * as types from '../../../mutation-types';
|
|
import AgentAPI from '../../../../api/agents';
|
|
import agentList from './fixtures';
|
|
|
|
const commit = vi.fn();
|
|
const dispatch = vi.fn();
|
|
global.axios = axios;
|
|
vi.mock('axios');
|
|
|
|
// Clear the IDB-backed cache between tests so each case starts from a known
|
|
// empty state and isn't affected by data persisted by a previous test.
|
|
beforeEach(async () => {
|
|
await AgentAPI.dataManager.initDb();
|
|
await AgentAPI.dataManager.db.clear(AgentAPI.cacheModelName);
|
|
});
|
|
|
|
describe('#actions', () => {
|
|
describe('#get', () => {
|
|
it('sends correct actions if API is success', async () => {
|
|
axios.get.mockResolvedValue({ data: agentList });
|
|
await actions.get({ commit });
|
|
expect(commit.mock.calls).toEqual([
|
|
[types.default.SET_AGENT_FETCHING_STATUS, true],
|
|
[types.default.SET_AGENT_FETCHING_STATUS, false],
|
|
[types.default.SET_AGENTS, agentList],
|
|
]);
|
|
});
|
|
it('sends correct actions if API is error', async () => {
|
|
axios.get.mockRejectedValue({ message: 'Incorrect header' });
|
|
await actions.get({ commit });
|
|
expect(commit.mock.calls).toEqual([
|
|
[types.default.SET_AGENT_FETCHING_STATUS, true],
|
|
[types.default.SET_AGENT_FETCHING_STATUS, false],
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('#create', () => {
|
|
it('sends correct actions if API is success', async () => {
|
|
axios.post.mockResolvedValue({ data: agentList[0] });
|
|
await actions.create({ commit }, agentList[0]);
|
|
expect(commit.mock.calls).toEqual([
|
|
[types.default.SET_AGENT_CREATING_STATUS, true],
|
|
[types.default.ADD_AGENT, agentList[0]],
|
|
[types.default.SET_AGENT_CREATING_STATUS, false],
|
|
]);
|
|
});
|
|
it('sends correct actions if API is error', async () => {
|
|
axios.post.mockRejectedValue({ message: 'Incorrect header' });
|
|
await expect(actions.create({ commit })).rejects.toEqual({
|
|
message: 'Incorrect header',
|
|
});
|
|
expect(commit.mock.calls).toEqual([
|
|
[types.default.SET_AGENT_CREATING_STATUS, true],
|
|
[types.default.SET_AGENT_CREATING_STATUS, false],
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('#update', () => {
|
|
it('sends correct actions if API is success', async () => {
|
|
axios.patch.mockResolvedValue({ data: agentList[0] });
|
|
await actions.update({ commit }, agentList[0]);
|
|
expect(commit.mock.calls).toEqual([
|
|
[types.default.SET_AGENT_UPDATING_STATUS, true],
|
|
[types.default.EDIT_AGENT, agentList[0]],
|
|
[types.default.SET_AGENT_UPDATING_STATUS, false],
|
|
]);
|
|
});
|
|
it('sends correct actions if API is error', async () => {
|
|
axios.patch.mockRejectedValue({ message: 'Incorrect header' });
|
|
await expect(actions.update({ commit }, agentList[0])).rejects.toThrow(
|
|
Error
|
|
);
|
|
expect(commit.mock.calls).toEqual([
|
|
[types.default.SET_AGENT_UPDATING_STATUS, true],
|
|
[types.default.SET_AGENT_UPDATING_STATUS, false],
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('#delete', () => {
|
|
it('sends correct actions if API is success', async () => {
|
|
axios.delete.mockResolvedValue({ data: agentList[0] });
|
|
await actions.delete({ commit }, agentList[0].id);
|
|
expect(commit.mock.calls).toEqual([
|
|
[types.default.SET_AGENT_DELETING_STATUS, true],
|
|
[types.default.DELETE_AGENT, agentList[0].id],
|
|
[types.default.SET_AGENT_DELETING_STATUS, false],
|
|
]);
|
|
});
|
|
it('sends correct actions if API is error', async () => {
|
|
axios.delete.mockRejectedValue({ message: 'Incorrect header' });
|
|
await expect(actions.delete({ commit }, agentList[0].id)).rejects.toThrow(
|
|
Error
|
|
);
|
|
expect(commit.mock.calls).toEqual([
|
|
[types.default.SET_AGENT_DELETING_STATUS, true],
|
|
[types.default.SET_AGENT_DELETING_STATUS, false],
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('#updatePresence', () => {
|
|
it('sends correct actions', async () => {
|
|
const data = { users: { 1: 'online' }, contacts: { 2: 'online' } };
|
|
actions.updatePresence({ commit, dispatch }, data);
|
|
expect(commit.mock.calls).toEqual([
|
|
[types.default.UPDATE_AGENTS_PRESENCE, data],
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('#updateSingleAgentPresence', () => {
|
|
it('sends correct actions', async () => {
|
|
const data = { id: 1, availabilityStatus: 'online' };
|
|
actions.updateSingleAgentPresence({ commit, dispatch }, data);
|
|
expect(commit.mock.calls).toEqual([
|
|
[types.default.UPDATE_SINGLE_AGENT_PRESENCE, data],
|
|
]);
|
|
});
|
|
});
|
|
});
|