fix: Resolve infinite loop with campaign API call (#2290)
Co-authored-by: Muhsin <muhsinkeramam@gmail.com>
This commit is contained in:
@@ -3,21 +3,28 @@ import { actions } from '../../campaign';
|
||||
import { campaigns } from './data';
|
||||
|
||||
const commit = jest.fn();
|
||||
const dispatch = jest.fn();
|
||||
jest.mock('widget/helpers/axios');
|
||||
|
||||
import campaignTimer from 'widget/helpers/campaignTimer';
|
||||
jest.mock('widget/helpers/campaignTimer');
|
||||
|
||||
describe('#actions', () => {
|
||||
describe('#fetchCampaigns', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
API.get.mockResolvedValue({ data: campaigns });
|
||||
await actions.fetchCampaigns(
|
||||
{ commit },
|
||||
{ websiteToken: 'XDsafmADasd', currentURL: 'https://www.chatwoot.com' }
|
||||
{ websiteToken: 'XDsafmADasd', currentURL: 'https://chatwoot.com' }
|
||||
);
|
||||
expect(commit.mock.calls).not.toEqual([
|
||||
expect(commit.mock.calls).toEqual([
|
||||
['setCampaigns', campaigns],
|
||||
['setError', false],
|
||||
['setHasFetched', true],
|
||||
]);
|
||||
expect(campaignTimer.initTimers).toHaveBeenCalledWith({
|
||||
campaigns: [{ id: 11, timeOnPage: '20', url: 'https://chatwoot.com' }],
|
||||
});
|
||||
});
|
||||
it('sends correct actions if API is error', async () => {
|
||||
API.get.mockRejectedValue({ message: 'Authentication required' });
|
||||
@@ -31,4 +38,30 @@ describe('#actions', () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#startCampaigns', () => {
|
||||
const actionParams = {
|
||||
websiteToken: 'XDsafmADasd',
|
||||
currentURL: 'https://chatwoot.com',
|
||||
};
|
||||
|
||||
it('sends correct actions if campaigns are empty', async () => {
|
||||
await actions.startCampaigns(
|
||||
{ dispatch, getters: { getCampaigns: [] } },
|
||||
actionParams
|
||||
);
|
||||
expect(dispatch.mock.calls).toEqual([['fetchCampaigns', actionParams]]);
|
||||
expect(campaignTimer.initTimers).not.toHaveBeenCalled();
|
||||
});
|
||||
it('resets time if campaigns are available', async () => {
|
||||
await actions.startCampaigns(
|
||||
{ dispatch, getters: { getCampaigns: campaigns } },
|
||||
actionParams
|
||||
);
|
||||
expect(dispatch.mock.calls).toEqual([]);
|
||||
expect(campaignTimer.initTimers).toHaveBeenCalledWith({
|
||||
campaigns: [{ id: 11, timeOnPage: '20', url: 'https://chatwoot.com' }],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user