2021-05-10 07:31:00 +00:00
|
|
|
import { API } from 'widget/helpers/axios';
|
|
|
|
import { actions } from '../../campaign';
|
|
|
|
import { campaigns } from './data';
|
|
|
|
|
|
|
|
const commit = jest.fn();
|
2021-05-18 06:45:23 +00:00
|
|
|
const dispatch = jest.fn();
|
2021-05-10 07:31:00 +00:00
|
|
|
jest.mock('widget/helpers/axios');
|
|
|
|
|
2021-05-18 06:45:23 +00:00
|
|
|
import campaignTimer from 'widget/helpers/campaignTimer';
|
|
|
|
jest.mock('widget/helpers/campaignTimer');
|
|
|
|
|
2021-05-10 07:31:00 +00:00
|
|
|
describe('#actions', () => {
|
|
|
|
describe('#fetchCampaigns', () => {
|
|
|
|
it('sends correct actions if API is success', async () => {
|
|
|
|
API.get.mockResolvedValue({ data: campaigns });
|
2021-05-17 16:08:35 +00:00
|
|
|
await actions.fetchCampaigns(
|
|
|
|
{ commit },
|
2021-10-12 12:28:33 +00:00
|
|
|
{
|
|
|
|
websiteToken: 'XDsafmADasd',
|
|
|
|
currentURL: 'https://chatwoot.com',
|
|
|
|
isInBusinessHours: true,
|
|
|
|
}
|
2021-05-17 16:08:35 +00:00
|
|
|
);
|
2021-05-18 06:45:23 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
2021-05-10 07:31:00 +00:00
|
|
|
['setCampaigns', campaigns],
|
|
|
|
['setError', false],
|
|
|
|
['setHasFetched', true],
|
|
|
|
]);
|
2021-10-11 12:48:11 +00:00
|
|
|
expect(campaignTimer.initTimers).toHaveBeenCalledWith(
|
|
|
|
{
|
|
|
|
campaigns: [
|
2021-10-12 12:28:33 +00:00
|
|
|
{
|
|
|
|
id: 11,
|
|
|
|
timeOnPage: '20',
|
|
|
|
url: 'https://chatwoot.com',
|
|
|
|
triggerOnlyDuringBusinessHours: false,
|
|
|
|
},
|
2021-10-11 12:48:11 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
'XDsafmADasd'
|
|
|
|
);
|
2021-05-10 07:31:00 +00:00
|
|
|
});
|
|
|
|
it('sends correct actions if API is error', async () => {
|
|
|
|
API.get.mockRejectedValue({ message: 'Authentication required' });
|
2021-05-17 16:08:35 +00:00
|
|
|
await actions.fetchCampaigns(
|
|
|
|
{ commit },
|
2021-10-12 12:28:33 +00:00
|
|
|
{
|
|
|
|
websiteToken: 'XDsafmADasd',
|
|
|
|
currentURL: 'https://www.chatwoot.com',
|
|
|
|
isInBusinessHours: true,
|
|
|
|
}
|
2021-05-17 16:08:35 +00:00
|
|
|
);
|
2021-05-10 07:31:00 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
['setError', true],
|
|
|
|
['setHasFetched', true],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2021-06-15 14:39:42 +00:00
|
|
|
describe('#initCampaigns', () => {
|
2021-05-18 06:45:23 +00:00
|
|
|
const actionParams = {
|
|
|
|
websiteToken: 'XDsafmADasd',
|
|
|
|
currentURL: 'https://chatwoot.com',
|
|
|
|
};
|
|
|
|
it('sends correct actions if campaigns are empty', async () => {
|
2021-06-15 14:39:42 +00:00
|
|
|
await actions.initCampaigns(
|
2021-05-18 06:45:23 +00:00
|
|
|
{ dispatch, getters: { getCampaigns: [] } },
|
|
|
|
actionParams
|
|
|
|
);
|
|
|
|
expect(dispatch.mock.calls).toEqual([['fetchCampaigns', actionParams]]);
|
|
|
|
expect(campaignTimer.initTimers).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
it('resets time if campaigns are available', async () => {
|
2021-06-15 14:39:42 +00:00
|
|
|
await actions.initCampaigns(
|
2021-05-18 06:45:23 +00:00
|
|
|
{ dispatch, getters: { getCampaigns: campaigns } },
|
|
|
|
actionParams
|
|
|
|
);
|
|
|
|
expect(dispatch.mock.calls).toEqual([]);
|
2021-10-11 12:48:11 +00:00
|
|
|
expect(campaignTimer.initTimers).toHaveBeenCalledWith(
|
|
|
|
{
|
|
|
|
campaigns: [
|
2021-10-12 12:28:33 +00:00
|
|
|
{
|
|
|
|
id: 11,
|
|
|
|
timeOnPage: '20',
|
|
|
|
url: 'https://chatwoot.com',
|
|
|
|
triggerOnlyDuringBusinessHours: false,
|
|
|
|
},
|
2021-10-11 12:48:11 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
'XDsafmADasd'
|
|
|
|
);
|
2021-05-18 06:45:23 +00:00
|
|
|
});
|
|
|
|
});
|
2021-06-15 14:39:42 +00:00
|
|
|
describe('#startCampaign', () => {
|
|
|
|
it('reset campaign if campaign id is not present in the campaign list', async () => {
|
2021-10-11 12:48:11 +00:00
|
|
|
API.get.mockResolvedValue({ data: campaigns });
|
2021-06-15 14:39:42 +00:00
|
|
|
await actions.startCampaign(
|
|
|
|
{ dispatch, getters: { getCampaigns: campaigns }, commit },
|
|
|
|
{ campaignId: 32 }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('start campaign if campaign id passed', async () => {
|
2021-10-11 12:48:11 +00:00
|
|
|
API.get.mockResolvedValue({ data: campaigns });
|
2021-06-15 14:39:42 +00:00
|
|
|
await actions.startCampaign(
|
|
|
|
{ dispatch, getters: { getCampaigns: campaigns }, commit },
|
|
|
|
{ campaignId: 1 }
|
|
|
|
);
|
|
|
|
expect(commit.mock.calls).toEqual([['setActiveCampaign', campaigns[0]]]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('#executeCampaign', () => {
|
|
|
|
it('sends correct actions if execute campaign API is success', async () => {
|
|
|
|
const params = { campaignId: 12, websiteToken: 'XDsafmADasd' };
|
|
|
|
API.post.mockResolvedValue({});
|
|
|
|
await actions.executeCampaign({ commit }, params);
|
|
|
|
expect(commit.mock.calls).toEqual([['setActiveCampaign', {}]]);
|
|
|
|
});
|
|
|
|
it('sends correct actions if execute campaign API is failed', async () => {
|
|
|
|
const params = { campaignId: 12, websiteToken: 'XDsafmADasd' };
|
|
|
|
API.post.mockRejectedValue({ message: 'Authentication required' });
|
|
|
|
await actions.executeCampaign({ commit }, params);
|
|
|
|
expect(commit.mock.calls).toEqual([['setError', true]]);
|
|
|
|
});
|
|
|
|
});
|
2021-05-10 07:31:00 +00:00
|
|
|
});
|