Commit e58e0dcb authored by Jake Burden's avatar Jake Burden Committed by Jose Ivan Vargas

Resolve "Decouple timezone dropdown component from deploy freeze"

parent be4faf17
...@@ -4,7 +4,7 @@ import { mapActions, mapState } from 'vuex'; ...@@ -4,7 +4,7 @@ import { mapActions, mapState } from 'vuex';
import { mapComputed } from '~/vuex_shared/bindings'; import { mapComputed } from '~/vuex_shared/bindings';
import { __ } from '~/locale'; import { __ } from '~/locale';
import { MODAL_ID } from '../constants'; import { MODAL_ID } from '../constants';
import DeployFreezeTimezoneDropdown from './deploy_freeze_timezone_dropdown.vue'; import TimezoneDropdown from '~/vue_shared/components/timezone_dropdown.vue';
import { isValidCron } from 'cron-validator'; import { isValidCron } from 'cron-validator';
export default { export default {
...@@ -14,7 +14,7 @@ export default { ...@@ -14,7 +14,7 @@ export default {
GlModal, GlModal,
GlSprintf, GlSprintf,
GlLink, GlLink,
DeployFreezeTimezoneDropdown, TimezoneDropdown,
}, },
modalOptions: { modalOptions: {
ref: 'modal', ref: 'modal',
...@@ -39,7 +39,6 @@ export default { ...@@ -39,7 +39,6 @@ export default {
'timezoneData', 'timezoneData',
'freezeStartCron', 'freezeStartCron',
'freezeEndCron', 'freezeEndCron',
'selectedTimezone',
]), ]),
...mapComputed([ ...mapComputed([
{ key: 'freezeStartCron', updateFn: 'setFreezeStartCron' }, { key: 'freezeStartCron', updateFn: 'setFreezeStartCron' },
...@@ -71,6 +70,14 @@ export default { ...@@ -71,6 +70,14 @@ export default {
freezeEndCronState() { freezeEndCronState() {
return Boolean(!this.freezeEndCron || isValidCron(this.freezeEndCron)); return Boolean(!this.freezeEndCron || isValidCron(this.freezeEndCron));
}, },
timezone: {
get() {
return this.selectedTimezone;
},
set(selectedTimezone) {
this.setSelectedTimezone(selectedTimezone);
},
},
}, },
methods: { methods: {
...mapActions(['addFreezePeriod', 'setSelectedTimezone', 'resetModal']), ...mapActions(['addFreezePeriod', 'setSelectedTimezone', 'resetModal']),
...@@ -137,11 +144,7 @@ export default { ...@@ -137,11 +144,7 @@ export default {
</gl-form-group> </gl-form-group>
<gl-form-group :label="__('Cron time zone')" label-for="cron-time-zone-dropdown"> <gl-form-group :label="__('Cron time zone')" label-for="cron-time-zone-dropdown">
<deploy-freeze-timezone-dropdown <timezone-dropdown v-model="timezone" :timezone-data="timezoneData" />
:timezone-data="timezoneData"
:value="selectedTimezone"
@selectTimezone="setSelectedTimezone"
/>
</gl-form-group> </gl-form-group>
</gl-modal> </gl-modal>
</template> </template>
...@@ -64,6 +64,7 @@ export const fetchFreezePeriods = ({ dispatch, state }) => { ...@@ -64,6 +64,7 @@ export const fetchFreezePeriods = ({ dispatch, state }) => {
export const setSelectedTimezone = ({ commit }, timezone) => { export const setSelectedTimezone = ({ commit }, timezone) => {
commit(types.SET_SELECTED_TIMEZONE, timezone); commit(types.SET_SELECTED_TIMEZONE, timezone);
}; };
export const setFreezeStartCron = ({ commit }, { freezeStartCron }) => { export const setFreezeStartCron = ({ commit }, { freezeStartCron }) => {
commit(types.SET_FREEZE_START_CRON, freezeStartCron); commit(types.SET_FREEZE_START_CRON, freezeStartCron);
}; };
......
...@@ -4,7 +4,7 @@ import { __ } from '~/locale'; ...@@ -4,7 +4,7 @@ import { __ } from '~/locale';
import autofocusonshow from '~/vue_shared/directives/autofocusonshow'; import autofocusonshow from '~/vue_shared/directives/autofocusonshow';
export default { export default {
name: 'DeployFreezeTimezoneDropdown', name: 'TimezoneDropdown',
components: { components: {
GlNewDropdown, GlNewDropdown,
GlDropdownItem, GlDropdownItem,
...@@ -17,7 +17,7 @@ export default { ...@@ -17,7 +17,7 @@ export default {
props: { props: {
value: { value: {
type: String, type: String,
required: false, required: true,
default: '', default: '',
}, },
timezoneData: { timezoneData: {
...@@ -28,7 +28,7 @@ export default { ...@@ -28,7 +28,7 @@ export default {
}, },
data() { data() {
return { return {
searchTerm: this.value || '', searchTerm: '',
}; };
}, },
tranlations: { tranlations: {
...@@ -47,18 +47,13 @@ export default { ...@@ -47,18 +47,13 @@ export default {
timezone.formattedTimezone.toLowerCase().includes(lowerCasedSearchTerm), timezone.formattedTimezone.toLowerCase().includes(lowerCasedSearchTerm),
); );
}, },
selectTimezoneLabel() { selectedTimezoneLabel() {
return this.value || __('Select timezone'); return this.value || __('Select timezone');
}, },
}, },
watch: {
value(newVal) {
this.searchTerm = newVal;
},
},
methods: { methods: {
selectTimezone(selected) { selectTimezone(selectedTimezone) {
this.$emit('selectTimezone', selected); this.$emit('input', selectedTimezone);
this.searchTerm = ''; this.searchTerm = '';
}, },
isSelected(timezone) { isSelected(timezone) {
...@@ -81,9 +76,9 @@ export default { ...@@ -81,9 +76,9 @@ export default {
<template> <template>
<gl-new-dropdown :text="value" block lazy menu-class="gl-w-full!"> <gl-new-dropdown :text="value" block lazy menu-class="gl-w-full!">
<template #button-content> <template #button-content>
<span ref="buttonText" class="gl-flex-grow-1" :class="{ 'gl-text-gray-500': !value }">{{ <span class="gl-flex-grow-1" :class="{ 'gl-text-gray-500': !value }">
selectTimezoneLabel {{ selectedTimezoneLabel }}
}}</span> </span>
<gl-icon name="chevron-down" /> <gl-icon name="chevron-down" />
</template> </template>
...@@ -100,7 +95,7 @@ export default { ...@@ -100,7 +95,7 @@ export default {
/> />
{{ timezone.formattedTimezone }} {{ timezone.formattedTimezone }}
</gl-dropdown-item> </gl-dropdown-item>
<gl-dropdown-item v-if="!filteredResults.length" ref="noMatchingResults"> <gl-dropdown-item v-if="!filteredResults.length" data-testid="noMatchingResults">
{{ $options.tranlations.noResultsText }} {{ $options.tranlations.noResultsText }}
</gl-dropdown-item> </gl-dropdown-item>
</gl-new-dropdown> </gl-new-dropdown>
......
...@@ -2,7 +2,7 @@ import Vuex from 'vuex'; ...@@ -2,7 +2,7 @@ import Vuex from 'vuex';
import { createLocalVue, shallowMount } from '@vue/test-utils'; import { createLocalVue, shallowMount } from '@vue/test-utils';
import { GlButton, GlModal } from '@gitlab/ui'; import { GlButton, GlModal } from '@gitlab/ui';
import DeployFreezeModal from '~/deploy_freeze/components/deploy_freeze_modal.vue'; import DeployFreezeModal from '~/deploy_freeze/components/deploy_freeze_modal.vue';
import DeployFreezeTimezoneDropdown from '~/deploy_freeze/components/deploy_freeze_timezone_dropdown.vue'; import TimezoneDropdown from '~/vue_shared/components/timezone_dropdown.vue';
import createStore from '~/deploy_freeze/store'; import createStore from '~/deploy_freeze/store';
import { mockDeployFreezePayload, mockTimezoneData } from '../mock_data'; import { mockDeployFreezePayload, mockTimezoneData } from '../mock_data';
...@@ -41,7 +41,7 @@ describe('Deploy freeze modal', () => { ...@@ -41,7 +41,7 @@ describe('Deploy freeze modal', () => {
wrapper.find('#deploy-freeze-start').trigger('input'); wrapper.find('#deploy-freeze-start').trigger('input');
wrapper.find('#deploy-freeze-end').trigger('input'); wrapper.find('#deploy-freeze-end').trigger('input');
wrapper.find(DeployFreezeTimezoneDropdown).trigger('input'); wrapper.find(TimezoneDropdown).trigger('input');
}; };
afterEach(() => { afterEach(() => {
......
import Vuex from 'vuex'; import Vuex from 'vuex';
import DeployFreezeTimezoneDropdown from '~/deploy_freeze/components/deploy_freeze_timezone_dropdown.vue'; import TimezoneDropdown from '~/vue_shared/components/timezone_dropdown.vue';
import { shallowMount, createLocalVue } from '@vue/test-utils'; import { shallowMount, createLocalVue } from '@vue/test-utils';
import createStore from '~/deploy_freeze/store'; import createStore from '~/deploy_freeze/store';
import { mockTimezoneData } from '../mock_data'; import { mockTimezoneData } from '../mock_data';
import { GlDropdownItem } from '@gitlab/ui'; import { GlDropdownItem, GlNewDropdown } from '@gitlab/ui';
const localVue = createLocalVue(); const localVue = createLocalVue();
localVue.use(Vuex); localVue.use(Vuex);
...@@ -13,20 +13,21 @@ describe('Deploy freeze timezone dropdown', () => { ...@@ -13,20 +13,21 @@ describe('Deploy freeze timezone dropdown', () => {
let wrapper; let wrapper;
let store; let store;
const createComponent = term => { const createComponent = (searchTerm, selectedTimezone) => {
store = createStore({ store = createStore({
projectId: '8', projectId: '8',
timezoneData: mockTimezoneData, timezoneData: mockTimezoneData,
}); });
store.state.timezoneData = mockTimezoneData; wrapper = shallowMount(TimezoneDropdown, {
wrapper = shallowMount(DeployFreezeTimezoneDropdown, {
store, store,
localVue, localVue,
propsData: { propsData: {
value: term, value: selectedTimezone,
timezoneData: mockTimezoneData, timezoneData: mockTimezoneData,
}, },
}); });
wrapper.setData({ searchTerm });
}; };
const findAllDropdownItems = () => wrapper.findAll(GlDropdownItem); const findAllDropdownItems = () => wrapper.findAll(GlDropdownItem);
...@@ -37,7 +38,7 @@ describe('Deploy freeze timezone dropdown', () => { ...@@ -37,7 +38,7 @@ describe('Deploy freeze timezone dropdown', () => {
wrapper = null; wrapper = null;
}); });
describe('No enviroments found', () => { describe('No time zones found', () => {
beforeEach(() => { beforeEach(() => {
createComponent('UTC timezone'); createComponent('UTC timezone');
}); });
...@@ -68,13 +69,13 @@ describe('Deploy freeze timezone dropdown', () => { ...@@ -68,13 +69,13 @@ describe('Deploy freeze timezone dropdown', () => {
}); });
it('should not display empty results message', () => { it('should not display empty results message', () => {
expect(wrapper.find({ ref: 'noMatchingResults' }).exists()).toBe(false); expect(wrapper.find('[data-testid="noMatchingResults"]').exists()).toBe(false);
}); });
describe('Custom events', () => { describe('Custom events', () => {
it('should emit selectTimezone if an environment is clicked', () => { it('should emit input if a time zone is clicked', () => {
findDropdownItemByIndex(0).vm.$emit('click'); findDropdownItemByIndex(0).vm.$emit('click');
expect(wrapper.emitted('selectTimezone')).toEqual([ expect(wrapper.emitted('input')).toEqual([
[ [
{ {
formattedTimezone: '[UTC -8] Alaska', formattedTimezone: '[UTC -8] Alaska',
...@@ -85,4 +86,14 @@ describe('Deploy freeze timezone dropdown', () => { ...@@ -85,4 +86,14 @@ describe('Deploy freeze timezone dropdown', () => {
}); });
}); });
}); });
describe('Selected time zone', () => {
beforeEach(() => {
createComponent('', 'Alaska');
});
it('renders selected time zone as dropdown label', () => {
expect(wrapper.find(GlNewDropdown).vm.text).toBe('Alaska');
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment