chore: Add cache to improve widget performance (#11163)
- Add dynamic importing for routes. - Added caching for `campaign`, `articles` and `inbox_members` API end points. --------- Co-authored-by: Pranav <pranavrajs@gmail.com>
This commit is contained in:
@@ -1,14 +1,60 @@
|
||||
import { API } from 'widget/helpers/axios';
|
||||
import { actions } from '../../agent';
|
||||
import { agents } from './data';
|
||||
import { getFromCache, setCache } from 'shared/helpers/cache';
|
||||
import { getAvailableAgents } from 'widget/api/agent';
|
||||
|
||||
const commit = vi.fn();
|
||||
let commit = vi.fn();
|
||||
vi.mock('widget/helpers/axios');
|
||||
|
||||
vi.mock('widget/api/agent');
|
||||
vi.mock('shared/helpers/cache');
|
||||
|
||||
describe('#actions', () => {
|
||||
describe('#fetchAvailableAgents', () => {
|
||||
const websiteToken = 'test-token';
|
||||
|
||||
beforeEach(() => {
|
||||
commit = vi.fn();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('returns cached data if available', async () => {
|
||||
getFromCache.mockReturnValue(agents);
|
||||
await actions.fetchAvailableAgents({ commit }, websiteToken);
|
||||
|
||||
expect(getFromCache).toHaveBeenCalledWith(
|
||||
`chatwoot_available_agents_${websiteToken}`
|
||||
);
|
||||
expect(getAvailableAgents).not.toHaveBeenCalled();
|
||||
expect(setCache).not.toHaveBeenCalled();
|
||||
expect(commit).toHaveBeenCalledWith('setAgents', agents);
|
||||
expect(commit).toHaveBeenCalledWith('setError', false);
|
||||
expect(commit).toHaveBeenCalledWith('setHasFetched', true);
|
||||
});
|
||||
|
||||
it('fetches and caches data if no cache available', async () => {
|
||||
getFromCache.mockReturnValue(null);
|
||||
getAvailableAgents.mockReturnValue({ data: { payload: agents } });
|
||||
|
||||
await actions.fetchAvailableAgents({ commit }, websiteToken);
|
||||
|
||||
expect(getFromCache).toHaveBeenCalledWith(
|
||||
`chatwoot_available_agents_${websiteToken}`
|
||||
);
|
||||
expect(getAvailableAgents).toHaveBeenCalledWith(websiteToken);
|
||||
expect(setCache).toHaveBeenCalledWith(
|
||||
`chatwoot_available_agents_${websiteToken}`,
|
||||
agents
|
||||
);
|
||||
expect(commit).toHaveBeenCalledWith('setAgents', agents);
|
||||
expect(commit).toHaveBeenCalledWith('setError', false);
|
||||
expect(commit).toHaveBeenCalledWith('setHasFetched', true);
|
||||
});
|
||||
|
||||
it('sends correct actions if API is success', async () => {
|
||||
API.get.mockResolvedValue({ data: { payload: agents } });
|
||||
getFromCache.mockReturnValue(null);
|
||||
|
||||
getAvailableAgents.mockReturnValue({ data: { payload: agents } });
|
||||
await actions.fetchAvailableAgents({ commit }, 'Hi');
|
||||
expect(commit.mock.calls).toEqual([
|
||||
['setAgents', agents],
|
||||
@@ -17,7 +63,11 @@ describe('#actions', () => {
|
||||
]);
|
||||
});
|
||||
it('sends correct actions if API is error', async () => {
|
||||
API.get.mockRejectedValue({ message: 'Authentication required' });
|
||||
getFromCache.mockReturnValue(null);
|
||||
|
||||
getAvailableAgents.mockRejectedValue({
|
||||
message: 'Authentication required',
|
||||
});
|
||||
await actions.fetchAvailableAgents({ commit }, 'Hi');
|
||||
expect(commit.mock.calls).toEqual([
|
||||
['setError', true],
|
||||
|
||||
Reference in New Issue
Block a user