2020-10-24 23:57:45 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import testAction from 'helpers/vuex_action_helper';
|
|
|
|
import Api from '~/api';
|
|
|
|
import * as actions from '~/deploy_freeze/store/actions';
|
|
|
|
import * as types from '~/deploy_freeze/store/mutation_types';
|
2021-03-11 19:13:27 +05:30
|
|
|
import getInitialState from '~/deploy_freeze/store/state';
|
2023-05-27 22:25:52 +05:30
|
|
|
import { createAlert } from '~/alert';
|
2021-11-11 11:23:49 +05:30
|
|
|
import * as logger from '~/lib/logger';
|
2021-03-11 19:13:27 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2022-11-25 23:54:43 +05:30
|
|
|
import { freezePeriodsFixture } from '../helpers';
|
|
|
|
import { timezoneDataFixture } from '../../vue_shared/components/timezone_dropdown/helpers';
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
jest.mock('~/api.js');
|
2023-05-27 22:25:52 +05:30
|
|
|
jest.mock('~/alert');
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
describe('deploy freeze store actions', () => {
|
2021-11-11 11:23:49 +05:30
|
|
|
const freezePeriodFixture = freezePeriodsFixture[0];
|
2020-10-24 23:57:45 +05:30
|
|
|
let mock;
|
|
|
|
let state;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
state = getInitialState({
|
|
|
|
projectId: '8',
|
|
|
|
timezoneData: timezoneDataFixture,
|
|
|
|
});
|
|
|
|
Api.freezePeriods.mockResolvedValue({ data: freezePeriodsFixture });
|
|
|
|
Api.createFreezePeriod.mockResolvedValue();
|
2021-04-29 21:17:54 +05:30
|
|
|
Api.updateFreezePeriod.mockResolvedValue();
|
2021-11-11 11:23:49 +05:30
|
|
|
Api.deleteFreezePeriod.mockResolvedValue();
|
2020-10-24 23:57:45 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
describe('setSelectedFreezePeriod', () => {
|
|
|
|
it('commits SET_SELECTED_TIMEZONE mutation', () => {
|
|
|
|
testAction(
|
|
|
|
actions.setFreezePeriod,
|
|
|
|
{
|
|
|
|
id: 3,
|
|
|
|
cronTimezone: 'UTC',
|
|
|
|
freezeStart: 'start',
|
|
|
|
freezeEnd: 'end',
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
[
|
|
|
|
{
|
|
|
|
payload: 3,
|
|
|
|
type: types.SET_SELECTED_ID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
payload: 'UTC',
|
|
|
|
type: types.SET_SELECTED_TIMEZONE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
payload: 'start',
|
|
|
|
type: types.SET_FREEZE_START_CRON,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
payload: 'end',
|
|
|
|
type: types.SET_FREEZE_END_CRON,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
describe('setSelectedTimezone', () => {
|
|
|
|
it('commits SET_SELECTED_TIMEZONE mutation', () => {
|
|
|
|
testAction(actions.setSelectedTimezone, {}, {}, [
|
|
|
|
{
|
|
|
|
payload: {},
|
|
|
|
type: types.SET_SELECTED_TIMEZONE,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setFreezeStartCron', () => {
|
|
|
|
it('commits SET_FREEZE_START_CRON mutation', () => {
|
|
|
|
testAction(actions.setFreezeStartCron, {}, {}, [
|
|
|
|
{
|
|
|
|
type: types.SET_FREEZE_START_CRON,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setFreezeEndCron', () => {
|
|
|
|
it('commits SET_FREEZE_END_CRON mutation', () => {
|
|
|
|
testAction(actions.setFreezeEndCron, {}, {}, [
|
|
|
|
{
|
|
|
|
type: types.SET_FREEZE_END_CRON,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('addFreezePeriod', () => {
|
2023-03-04 22:38:38 +05:30
|
|
|
it('dispatch correct actions on adding a freeze period', async () => {
|
|
|
|
await testAction(
|
2020-10-24 23:57:45 +05:30
|
|
|
actions.addFreezePeriod,
|
|
|
|
{},
|
|
|
|
state,
|
|
|
|
[{ type: 'RESET_MODAL' }],
|
|
|
|
[
|
2021-04-29 21:17:54 +05:30
|
|
|
{ type: 'requestFreezePeriod' },
|
|
|
|
{ type: 'receiveFreezePeriodSuccess' },
|
2020-10-24 23:57:45 +05:30
|
|
|
{ type: 'fetchFreezePeriods' },
|
|
|
|
],
|
|
|
|
);
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
expect(Api.createFreezePeriod).toHaveBeenCalledWith(state.projectId, {
|
|
|
|
freeze_start: state.freezeStartCron,
|
|
|
|
freeze_end: state.freezeEndCron,
|
|
|
|
cron_timezone: state.selectedTimezoneIdentifier,
|
|
|
|
});
|
2020-10-24 23:57:45 +05:30
|
|
|
});
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it('should show alert and set error in state on add failure', async () => {
|
2020-10-24 23:57:45 +05:30
|
|
|
Api.createFreezePeriod.mockRejectedValue();
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
await testAction(
|
2020-10-24 23:57:45 +05:30
|
|
|
actions.addFreezePeriod,
|
|
|
|
{},
|
|
|
|
state,
|
|
|
|
[],
|
2021-04-29 21:17:54 +05:30
|
|
|
[{ type: 'requestFreezePeriod' }, { type: 'receiveFreezePeriodError' }],
|
|
|
|
);
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
expect(createAlert).toHaveBeenCalled();
|
2021-04-29 21:17:54 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('updateFreezePeriod', () => {
|
2023-03-04 22:38:38 +05:30
|
|
|
it('dispatch correct actions on updating a freeze period', async () => {
|
|
|
|
await testAction(
|
2021-04-29 21:17:54 +05:30
|
|
|
actions.updateFreezePeriod,
|
|
|
|
{},
|
|
|
|
state,
|
|
|
|
[{ type: 'RESET_MODAL' }],
|
|
|
|
[
|
|
|
|
{ type: 'requestFreezePeriod' },
|
|
|
|
{ type: 'receiveFreezePeriodSuccess' },
|
|
|
|
{ type: 'fetchFreezePeriods' },
|
|
|
|
],
|
|
|
|
);
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
expect(Api.updateFreezePeriod).toHaveBeenCalledWith(state.projectId, {
|
|
|
|
id: state.selectedId,
|
|
|
|
freeze_start: state.freezeStartCron,
|
|
|
|
freeze_end: state.freezeEndCron,
|
|
|
|
cron_timezone: state.selectedTimezoneIdentifier,
|
|
|
|
});
|
2021-04-29 21:17:54 +05:30
|
|
|
});
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it('should show alert and set error in state on add failure', async () => {
|
2021-04-29 21:17:54 +05:30
|
|
|
Api.updateFreezePeriod.mockRejectedValue();
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
await testAction(
|
2021-04-29 21:17:54 +05:30
|
|
|
actions.updateFreezePeriod,
|
|
|
|
{},
|
|
|
|
state,
|
|
|
|
[],
|
|
|
|
[{ type: 'requestFreezePeriod' }, { type: 'receiveFreezePeriodError' }],
|
2020-10-24 23:57:45 +05:30
|
|
|
);
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
expect(createAlert).toHaveBeenCalled();
|
2020-10-24 23:57:45 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchFreezePeriods', () => {
|
|
|
|
it('dispatch correct actions on fetchFreezePeriods', () => {
|
2023-03-04 22:38:38 +05:30
|
|
|
return testAction(
|
2020-10-24 23:57:45 +05:30
|
|
|
actions.fetchFreezePeriods,
|
|
|
|
{},
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{ type: types.REQUEST_FREEZE_PERIODS },
|
|
|
|
{ type: types.RECEIVE_FREEZE_PERIODS_SUCCESS, payload: freezePeriodsFixture },
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it('should show alert and set error in state on fetch variables failure', async () => {
|
2020-10-24 23:57:45 +05:30
|
|
|
Api.freezePeriods.mockRejectedValue();
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
await testAction(
|
2020-10-24 23:57:45 +05:30
|
|
|
actions.fetchFreezePeriods,
|
|
|
|
{},
|
|
|
|
state,
|
|
|
|
[{ type: types.REQUEST_FREEZE_PERIODS }],
|
|
|
|
[],
|
|
|
|
);
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
expect(createAlert).toHaveBeenCalledWith({
|
|
|
|
message: 'There was an error fetching the deploy freezes.',
|
|
|
|
});
|
2020-10-24 23:57:45 +05:30
|
|
|
});
|
|
|
|
});
|
2021-11-11 11:23:49 +05:30
|
|
|
|
|
|
|
describe('deleteFreezePeriod', () => {
|
2023-03-04 22:38:38 +05:30
|
|
|
it('dispatch correct actions on deleting a freeze period', async () => {
|
|
|
|
await testAction(
|
2021-11-11 11:23:49 +05:30
|
|
|
actions.deleteFreezePeriod,
|
|
|
|
freezePeriodFixture,
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{ type: 'REQUEST_DELETE_FREEZE_PERIOD', payload: freezePeriodFixture.id },
|
|
|
|
{ type: 'RECEIVE_DELETE_FREEZE_PERIOD_SUCCESS', payload: freezePeriodFixture.id },
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
);
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
expect(Api.deleteFreezePeriod).toHaveBeenCalledWith(state.projectId, freezePeriodFixture.id);
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it('should show alert and set error in state on delete failure', async () => {
|
2021-11-11 11:23:49 +05:30
|
|
|
jest.spyOn(logger, 'logError').mockImplementation();
|
|
|
|
const error = new Error();
|
|
|
|
Api.deleteFreezePeriod.mockRejectedValue(error);
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
await testAction(
|
2021-11-11 11:23:49 +05:30
|
|
|
actions.deleteFreezePeriod,
|
|
|
|
freezePeriodFixture,
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{ type: 'REQUEST_DELETE_FREEZE_PERIOD', payload: freezePeriodFixture.id },
|
|
|
|
{ type: 'RECEIVE_DELETE_FREEZE_PERIOD_ERROR', payload: freezePeriodFixture.id },
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
);
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
expect(createAlert).toHaveBeenCalled();
|
|
|
|
|
|
|
|
expect(logger.logError).toHaveBeenCalledWith('Unable to delete deploy freeze', error);
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
});
|
2020-10-24 23:57:45 +05:30
|
|
|
});
|