diff options
Diffstat (limited to 'spec/frontend/deploy_freeze')
4 files changed, 171 insertions, 21 deletions
diff --git a/spec/frontend/deploy_freeze/components/deploy_freeze_modal_spec.js b/spec/frontend/deploy_freeze/components/deploy_freeze_modal_spec.js index d8ce184940a..7c46c280d46 100644 --- a/spec/frontend/deploy_freeze/components/deploy_freeze_modal_spec.js +++ b/spec/frontend/deploy_freeze/components/deploy_freeze_modal_spec.js @@ -1,13 +1,16 @@ import { GlButton, GlModal } from '@gitlab/ui'; -import { createLocalVue, shallowMount } from '@vue/test-utils'; +import { shallowMount } from '@vue/test-utils'; +import Vue from 'vue'; import Vuex from 'vuex'; +import Api from '~/api'; import DeployFreezeModal from '~/deploy_freeze/components/deploy_freeze_modal.vue'; import createStore from '~/deploy_freeze/store'; import TimezoneDropdown from '~/vue_shared/components/timezone_dropdown.vue'; import { freezePeriodsFixture, timezoneDataFixture } from '../helpers'; -const localVue = createLocalVue(); -localVue.use(Vuex); +jest.mock('~/api'); + +Vue.use(Vuex); describe('Deploy freeze modal', () => { let wrapper; @@ -23,18 +26,19 @@ describe('Deploy freeze modal', () => { stubs: { GlModal, }, - localVue, store, }); }); - const findModal = () => wrapper.find(GlModal); - const addDeployFreezeButton = () => findModal().findAll(GlButton).at(1); + const findModal = () => wrapper.findComponent(GlModal); + const submitDeployFreezeButton = () => findModal().findAllComponents(GlButton).at(1); - const setInput = (freezeStartCron, freezeEndCron, selectedTimezone) => { + const setInput = (freezeStartCron, freezeEndCron, selectedTimezone, id = '') => { store.state.freezeStartCron = freezeStartCron; store.state.freezeEndCron = freezeEndCron; store.state.selectedTimezone = selectedTimezone; + store.state.selectedTimezoneIdentifier = selectedTimezone; + store.state.selectedId = id; wrapper.find('#deploy-freeze-start').trigger('input'); wrapper.find('#deploy-freeze-end').trigger('input'); @@ -48,18 +52,36 @@ describe('Deploy freeze modal', () => { describe('Basic interactions', () => { it('button is disabled when freeze period is invalid', () => { - expect(addDeployFreezeButton().attributes('disabled')).toBeTruthy(); + expect(submitDeployFreezeButton().attributes('disabled')).toBeTruthy(); }); }); describe('Adding a new deploy freeze', () => { + const { freeze_start, freeze_end, cron_timezone } = freezePeriodsFixture[0]; + beforeEach(() => { - const { freeze_start, freeze_end, cron_timezone } = freezePeriodsFixture[0]; setInput(freeze_start, freeze_end, cron_timezone); }); it('button is enabled when valid freeze period settings are present', () => { - expect(addDeployFreezeButton().attributes('disabled')).toBeUndefined(); + expect(submitDeployFreezeButton().attributes('disabled')).toBeUndefined(); + }); + + it('should display Add deploy freeze', () => { + expect(findModal().props('title')).toBe('Add deploy freeze'); + expect(submitDeployFreezeButton().text()).toBe('Add deploy freeze'); + }); + + it('should call the add deploy freze API', () => { + Api.createFreezePeriod.mockResolvedValue(); + findModal().vm.$emit('primary'); + + expect(Api.createFreezePeriod).toHaveBeenCalledTimes(1); + expect(Api.createFreezePeriod).toHaveBeenCalledWith(store.state.projectId, { + freeze_start, + freeze_end, + cron_timezone, + }); }); }); @@ -70,7 +92,7 @@ describe('Deploy freeze modal', () => { }); it('disables the add deploy freeze button', () => { - expect(addDeployFreezeButton().attributes('disabled')).toBeTruthy(); + expect(submitDeployFreezeButton().attributes('disabled')).toBeTruthy(); }); }); @@ -81,7 +103,32 @@ describe('Deploy freeze modal', () => { }); it('does not disable the submit button', () => { - expect(addDeployFreezeButton().attributes('disabled')).toBeFalsy(); + expect(submitDeployFreezeButton().attributes('disabled')).toBeFalsy(); + }); + }); + }); + + describe('Editing an existing deploy freeze', () => { + const { freeze_start, freeze_end, cron_timezone, id } = freezePeriodsFixture[0]; + beforeEach(() => { + setInput(freeze_start, freeze_end, cron_timezone, id); + }); + + it('should display Edit deploy freeze', () => { + expect(findModal().props('title')).toBe('Edit deploy freeze'); + expect(submitDeployFreezeButton().text()).toBe('Save deploy freeze'); + }); + + it('should call the update deploy freze API', () => { + Api.updateFreezePeriod.mockResolvedValue(); + findModal().vm.$emit('primary'); + + expect(Api.updateFreezePeriod).toHaveBeenCalledTimes(1); + expect(Api.updateFreezePeriod).toHaveBeenCalledWith(store.state.projectId, { + id, + freeze_start, + freeze_end, + cron_timezone, }); }); }); diff --git a/spec/frontend/deploy_freeze/components/deploy_freeze_table_spec.js b/spec/frontend/deploy_freeze/components/deploy_freeze_table_spec.js index e4ee1b9ad26..168ddcfeacc 100644 --- a/spec/frontend/deploy_freeze/components/deploy_freeze_table_spec.js +++ b/spec/frontend/deploy_freeze/components/deploy_freeze_table_spec.js @@ -2,6 +2,7 @@ import { createLocalVue, mount } from '@vue/test-utils'; import Vuex from 'vuex'; import DeployFreezeTable from '~/deploy_freeze/components/deploy_freeze_table.vue'; import createStore from '~/deploy_freeze/store'; +import { RECEIVE_FREEZE_PERIODS_SUCCESS } from '~/deploy_freeze/store/mutation_types'; import { freezePeriodsFixture, timezoneDataFixture } from '../helpers'; const localVue = createLocalVue(); @@ -26,6 +27,7 @@ describe('Deploy freeze table', () => { const findEmptyFreezePeriods = () => wrapper.find('[data-testid="empty-freeze-periods"]'); const findAddDeployFreezeButton = () => wrapper.find('[data-testid="add-deploy-freeze"]'); + const findEditDeployFreezeButton = () => wrapper.find('[data-testid="edit-deploy-freeze"]'); const findDeployFreezeTable = () => wrapper.find('[data-testid="deploy-freeze-table"]'); beforeEach(() => { @@ -45,17 +47,31 @@ describe('Deploy freeze table', () => { it('displays empty', () => { expect(findEmptyFreezePeriods().exists()).toBe(true); expect(findEmptyFreezePeriods().text()).toBe( - 'No deploy freezes exist for this project. To add one, click Add deploy freeze', + 'No deploy freezes exist for this project. To add one, select Add deploy freeze', ); }); - it('displays data', () => { - store.state.freezePeriods = freezePeriodsFixture; + describe('with data', () => { + beforeEach(async () => { + store.commit(RECEIVE_FREEZE_PERIODS_SUCCESS, freezePeriodsFixture); + await wrapper.vm.$nextTick(); + }); - return wrapper.vm.$nextTick(() => { + it('displays data', () => { const tableRows = findDeployFreezeTable().findAll('tbody tr'); expect(tableRows.length).toBe(freezePeriodsFixture.length); expect(findEmptyFreezePeriods().exists()).toBe(false); + expect(findEditDeployFreezeButton().exists()).toBe(true); + }); + + it('allows user to edit deploy freeze', async () => { + findEditDeployFreezeButton().trigger('click'); + await wrapper.vm.$nextTick(); + + expect(store.dispatch).toHaveBeenCalledWith( + 'setFreezePeriod', + store.state.freezePeriods[0], + ); }); }); }); diff --git a/spec/frontend/deploy_freeze/store/actions_spec.js b/spec/frontend/deploy_freeze/store/actions_spec.js index f4d9802e39a..9c784f3c5a2 100644 --- a/spec/frontend/deploy_freeze/store/actions_spec.js +++ b/spec/frontend/deploy_freeze/store/actions_spec.js @@ -23,12 +23,46 @@ describe('deploy freeze store actions', () => { }); Api.freezePeriods.mockResolvedValue({ data: freezePeriodsFixture }); Api.createFreezePeriod.mockResolvedValue(); + Api.updateFreezePeriod.mockResolvedValue(); }); afterEach(() => { mock.restore(); }); + 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, + }, + ], + ); + }); + }); + describe('setSelectedTimezone', () => { it('commits SET_SELECTED_TIMEZONE mutation', () => { testAction(actions.setSelectedTimezone, {}, {}, [ @@ -68,10 +102,16 @@ describe('deploy freeze store actions', () => { state, [{ type: 'RESET_MODAL' }], [ - { type: 'requestAddFreezePeriod' }, - { type: 'receiveAddFreezePeriodSuccess' }, + { type: 'requestFreezePeriod' }, + { type: 'receiveFreezePeriodSuccess' }, { type: 'fetchFreezePeriods' }, ], + () => + expect(Api.createFreezePeriod).toHaveBeenCalledWith(state.projectId, { + freeze_start: state.freezeStartCron, + freeze_end: state.freezeEndCron, + cron_timezone: state.selectedTimezoneIdentifier, + }), ); }); @@ -83,7 +123,43 @@ describe('deploy freeze store actions', () => { {}, state, [], - [{ type: 'requestAddFreezePeriod' }, { type: 'receiveAddFreezePeriodError' }], + [{ type: 'requestFreezePeriod' }, { type: 'receiveFreezePeriodError' }], + () => expect(createFlash).toHaveBeenCalled(), + ); + }); + }); + + describe('updateFreezePeriod', () => { + it('dispatch correct actions on updating a freeze period', () => { + testAction( + actions.updateFreezePeriod, + {}, + state, + [{ type: 'RESET_MODAL' }], + [ + { type: 'requestFreezePeriod' }, + { type: 'receiveFreezePeriodSuccess' }, + { type: 'fetchFreezePeriods' }, + ], + () => + expect(Api.updateFreezePeriod).toHaveBeenCalledWith(state.projectId, { + id: state.selectedId, + freeze_start: state.freezeStartCron, + freeze_end: state.freezeEndCron, + cron_timezone: state.selectedTimezoneIdentifier, + }), + ); + }); + + it('should show flash error and set error in state on add failure', () => { + Api.updateFreezePeriod.mockRejectedValue(); + + testAction( + actions.updateFreezePeriod, + {}, + state, + [], + [{ type: 'requestFreezePeriod' }, { type: 'receiveFreezePeriodError' }], () => expect(createFlash).toHaveBeenCalled(), ); }); diff --git a/spec/frontend/deploy_freeze/store/mutations_spec.js b/spec/frontend/deploy_freeze/store/mutations_spec.js index 54cbdfcb64c..ce75e3b89c3 100644 --- a/spec/frontend/deploy_freeze/store/mutations_spec.js +++ b/spec/frontend/deploy_freeze/store/mutations_spec.js @@ -33,7 +33,10 @@ describe('Deploy freeze mutations', () => { const expectedFreezePeriods = freezePeriodsFixture.map((freezePeriod, index) => ({ ...convertObjectPropsToCamelCase(freezePeriod), - cronTimezone: timezoneNames[index], + cronTimezone: { + formattedTimezone: timezoneNames[index], + identifier: freezePeriod.cronTimezone, + }, })); expect(stateCopy.freezePeriods).toMatchObject(expectedFreezePeriods); @@ -62,11 +65,19 @@ describe('Deploy freeze mutations', () => { }); }); - describe('SET_FREEZE_ENDT_CRON', () => { + describe('SET_FREEZE_END_CRON', () => { it('should set freezeEndCron', () => { mutations[types.SET_FREEZE_END_CRON](stateCopy, '5 0 * 8 *'); expect(stateCopy.freezeEndCron).toBe('5 0 * 8 *'); }); }); + + describe('SET_SELECTED_ID', () => { + it('should set selectedId', () => { + mutations[types.SET_SELECTED_ID](stateCopy, 5); + + expect(stateCopy.selectedId).toBe(5); + }); + }); }); |