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-05-18 06:45:23 +00:00
|
|
|
{ websiteToken: 'XDsafmADasd', currentURL: 'https://chatwoot.com' }
|
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-05-18 06:45:23 +00:00
|
|
|
expect(campaignTimer.initTimers).toHaveBeenCalledWith({
|
|
|
|
campaigns: [{ id: 11, timeOnPage: '20', url: 'https://chatwoot.com' }],
|
|
|
|
});
|
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 },
|
|
|
|
{ websiteToken: 'XDsafmADasd', currentURL: 'https://www.chatwoot.com' }
|
|
|
|
);
|
2021-05-10 07:31:00 +00:00
|
|
|
expect(commit.mock.calls).toEqual([
|
|
|
|
['setError', true],
|
|
|
|
['setHasFetched', true],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2021-05-18 06:45:23 +00:00
|
|
|
|
|
|
|
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' }],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-05-10 07:31:00 +00:00
|
|
|
});
|