diff options
Diffstat (limited to 'spec/frontend/deploy_freeze/components')
4 files changed, 302 insertions, 0 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 new file mode 100644 index 00000000000..9ecf6bf375b --- /dev/null +++ b/spec/frontend/deploy_freeze/components/deploy_freeze_modal_spec.js @@ -0,0 +1,92 @@ +import Vuex from 'vuex'; +import { createLocalVue, shallowMount } from '@vue/test-utils'; +import { GlButton, GlModal } from '@gitlab/ui'; +import DeployFreezeModal from '~/deploy_freeze/components/deploy_freeze_modal.vue'; +import TimezoneDropdown from '~/vue_shared/components/timezone_dropdown.vue'; +import createStore from '~/deploy_freeze/store'; + +const localVue = createLocalVue(); +localVue.use(Vuex); + +describe('Deploy freeze modal', () => { + let wrapper; + let store; + const freezePeriodsFixture = getJSONFixture('/api/freeze-periods/freeze_periods.json'); + const timezoneDataFixture = getJSONFixture('/api/freeze-periods/timezone_data.json'); + + beforeEach(() => { + store = createStore({ + projectId: '8', + timezoneData: timezoneDataFixture, + }); + wrapper = shallowMount(DeployFreezeModal, { + attachToDocument: true, + stubs: { + GlModal, + }, + localVue, + store, + }); + }); + + const findModal = () => wrapper.find(GlModal); + const addDeployFreezeButton = () => + findModal() + .findAll(GlButton) + .at(1); + + const setInput = (freezeStartCron, freezeEndCron, selectedTimezone) => { + store.state.freezeStartCron = freezeStartCron; + store.state.freezeEndCron = freezeEndCron; + store.state.selectedTimezone = selectedTimezone; + + wrapper.find('#deploy-freeze-start').trigger('input'); + wrapper.find('#deploy-freeze-end').trigger('input'); + wrapper.find(TimezoneDropdown).trigger('input'); + }; + + afterEach(() => { + wrapper.destroy(); + wrapper = null; + }); + + describe('Basic interactions', () => { + it('button is disabled when freeze period is invalid', () => { + expect(addDeployFreezeButton().attributes('disabled')).toBeTruthy(); + }); + }); + + describe('Adding a new deploy freeze', () => { + 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(); + }); + }); + + describe('Validations', () => { + describe('when the cron state is invalid', () => { + beforeEach(() => { + setInput('invalid cron', 'invalid cron', 'invalid timezone'); + }); + + it('disables the add deploy freeze button', () => { + expect(addDeployFreezeButton().attributes('disabled')).toBeTruthy(); + }); + }); + + describe('when the cron state is valid', () => { + beforeEach(() => { + const { freeze_start, freeze_end, cron_timezone } = freezePeriodsFixture[0]; + setInput(freeze_start, freeze_end, cron_timezone); + }); + + it('does not disable the submit button', () => { + expect(addDeployFreezeButton().attributes('disabled')).toBeFalsy(); + }); + }); + }); +}); diff --git a/spec/frontend/deploy_freeze/components/deploy_freeze_settings_spec.js b/spec/frontend/deploy_freeze/components/deploy_freeze_settings_spec.js new file mode 100644 index 00000000000..d40df7de7d1 --- /dev/null +++ b/spec/frontend/deploy_freeze/components/deploy_freeze_settings_spec.js @@ -0,0 +1,42 @@ +import Vuex from 'vuex'; +import { createLocalVue, shallowMount } from '@vue/test-utils'; +import DeployFreezeSettings from '~/deploy_freeze/components/deploy_freeze_settings.vue'; +import DeployFreezeTable from '~/deploy_freeze/components/deploy_freeze_table.vue'; +import DeployFreezeModal from '~/deploy_freeze/components/deploy_freeze_modal.vue'; +import createStore from '~/deploy_freeze/store'; + +const localVue = createLocalVue(); +localVue.use(Vuex); + +describe('Deploy freeze settings', () => { + let wrapper; + let store; + const timezoneDataFixture = getJSONFixture('/api/freeze-periods/timezone_data.json'); + + beforeEach(() => { + store = createStore({ + projectId: '8', + timezoneData: timezoneDataFixture, + }); + jest.spyOn(store, 'dispatch').mockImplementation(); + wrapper = shallowMount(DeployFreezeSettings, { + localVue, + store, + }); + }); + + afterEach(() => { + wrapper.destroy(); + wrapper = null; + }); + + describe('Deploy freeze table contains components', () => { + it('contains deploy freeze table', () => { + expect(wrapper.find(DeployFreezeTable).exists()).toBe(true); + }); + + it('contains deploy freeze modal', () => { + expect(wrapper.find(DeployFreezeModal).exists()).toBe(true); + }); + }); +}); diff --git a/spec/frontend/deploy_freeze/components/deploy_freeze_table_spec.js b/spec/frontend/deploy_freeze/components/deploy_freeze_table_spec.js new file mode 100644 index 00000000000..383ffa90b22 --- /dev/null +++ b/spec/frontend/deploy_freeze/components/deploy_freeze_table_spec.js @@ -0,0 +1,70 @@ +import Vuex from 'vuex'; +import { createLocalVue, mount } from '@vue/test-utils'; +import DeployFreezeTable from '~/deploy_freeze/components/deploy_freeze_table.vue'; +import createStore from '~/deploy_freeze/store'; + +const localVue = createLocalVue(); +localVue.use(Vuex); + +describe('Deploy freeze table', () => { + let wrapper; + let store; + const timezoneDataFixture = getJSONFixture('/api/freeze-periods/timezone_data.json'); + + const createComponent = () => { + store = createStore({ + projectId: '8', + timezoneData: timezoneDataFixture, + }); + jest.spyOn(store, 'dispatch').mockImplementation(); + wrapper = mount(DeployFreezeTable, { + attachToDocument: true, + localVue, + store, + }); + }; + + const findEmptyFreezePeriods = () => wrapper.find('[data-testid="empty-freeze-periods"]'); + const findAddDeployFreezeButton = () => wrapper.find('[data-testid="add-deploy-freeze"]'); + const findDeployFreezeTable = () => wrapper.find('[data-testid="deploy-freeze-table"]'); + + beforeEach(() => { + createComponent(); + }); + + afterEach(() => { + wrapper.destroy(); + wrapper = null; + }); + + it('dispatches fetchFreezePeriods when mounted', () => { + expect(store.dispatch).toHaveBeenCalledWith('fetchFreezePeriods'); + }); + + describe('Renders correct data', () => { + 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', + ); + }); + + it('displays data', () => { + const freezePeriodsFixture = getJSONFixture('/api/freeze-periods/freeze_periods.json'); + store.state.freezePeriods = freezePeriodsFixture; + + return wrapper.vm.$nextTick(() => { + const tableRows = findDeployFreezeTable().findAll('tbody tr'); + expect(tableRows.length).toBe(freezePeriodsFixture.length); + expect(findEmptyFreezePeriods().exists()).toBe(false); + }); + }); + }); + + describe('Table click actions', () => { + it('displays add deploy freeze button', () => { + expect(findAddDeployFreezeButton().exists()).toBe(true); + expect(findAddDeployFreezeButton().text()).toBe('Add deploy freeze'); + }); + }); +}); diff --git a/spec/frontend/deploy_freeze/components/timezone_dropdown_spec.js b/spec/frontend/deploy_freeze/components/timezone_dropdown_spec.js new file mode 100644 index 00000000000..644cd0b5f27 --- /dev/null +++ b/spec/frontend/deploy_freeze/components/timezone_dropdown_spec.js @@ -0,0 +1,98 @@ +import Vuex from 'vuex'; +import { shallowMount, createLocalVue } from '@vue/test-utils'; +import { GlDeprecatedDropdownItem, GlNewDropdown } from '@gitlab/ui'; +import TimezoneDropdown from '~/vue_shared/components/timezone_dropdown.vue'; +import createStore from '~/deploy_freeze/store'; + +const localVue = createLocalVue(); +localVue.use(Vuex); + +describe('Deploy freeze timezone dropdown', () => { + let wrapper; + let store; + const timezoneDataFixture = getJSONFixture('/api/freeze-periods/timezone_data.json'); + + const createComponent = (searchTerm, selectedTimezone) => { + store = createStore({ + projectId: '8', + timezoneData: timezoneDataFixture, + }); + wrapper = shallowMount(TimezoneDropdown, { + store, + localVue, + propsData: { + value: selectedTimezone, + timezoneData: timezoneDataFixture, + }, + }); + + wrapper.setData({ searchTerm }); + }; + + const findAllDropdownItems = () => wrapper.findAll(GlDeprecatedDropdownItem); + const findDropdownItemByIndex = index => wrapper.findAll(GlDeprecatedDropdownItem).at(index); + + afterEach(() => { + wrapper.destroy(); + wrapper = null; + }); + + describe('No time zones found', () => { + beforeEach(() => { + createComponent('UTC timezone'); + }); + + it('renders empty results message', () => { + expect(findDropdownItemByIndex(0).text()).toBe('No matching results'); + }); + }); + + describe('Search term is empty', () => { + beforeEach(() => { + createComponent(''); + }); + + it('renders all timezones when search term is empty', () => { + expect(findAllDropdownItems()).toHaveLength(timezoneDataFixture.length); + }); + }); + + describe('Time zones found', () => { + beforeEach(() => { + createComponent('Alaska'); + }); + + it('renders only the time zone searched for', () => { + expect(findAllDropdownItems()).toHaveLength(1); + expect(findDropdownItemByIndex(0).text()).toBe('[UTC -8] Alaska'); + }); + + it('should not display empty results message', () => { + expect(wrapper.find('[data-testid="noMatchingResults"]').exists()).toBe(false); + }); + + describe('Custom events', () => { + it('should emit input if a time zone is clicked', () => { + findDropdownItemByIndex(0).vm.$emit('click'); + expect(wrapper.emitted('input')).toEqual([ + [ + { + formattedTimezone: '[UTC -8] Alaska', + identifier: 'America/Juneau', + }, + ], + ]); + }); + }); + }); + + describe('Selected time zone', () => { + beforeEach(() => { + createComponent('', 'Alaska'); + }); + + it('renders selected time zone as dropdown label', () => { + expect(wrapper.find(GlNewDropdown).vm.text).toBe('Alaska'); + }); + }); +}); |