diff options
Diffstat (limited to 'spec/frontend/environments/environment_actions_spec.js')
-rw-r--r-- | spec/frontend/environments/environment_actions_spec.js | 131 |
1 files changed, 76 insertions, 55 deletions
diff --git a/spec/frontend/environments/environment_actions_spec.js b/spec/frontend/environments/environment_actions_spec.js index d305f5e90bd..cc5153d6eba 100644 --- a/spec/frontend/environments/environment_actions_spec.js +++ b/spec/frontend/environments/environment_actions_spec.js @@ -1,51 +1,69 @@ -import { shallowMount } from '@vue/test-utils'; +import { shallowMount, mount } from '@vue/test-utils'; import { TEST_HOST } from 'helpers/test_constants'; -import { GlLoadingIcon, GlIcon } from '@gitlab/ui'; +import { GlDropdown, GlDropdownItem, GlLoadingIcon, GlIcon } from '@gitlab/ui'; import { createMockDirective, getBinding } from 'helpers/vue_mock_directive'; import eventHub from '~/environments/event_hub'; import EnvironmentActions from '~/environments/components/environment_actions.vue'; +const scheduledJobAction = { + name: 'scheduled action', + playPath: `${TEST_HOST}/scheduled/job/action`, + playable: true, + scheduledAt: '2063-04-05T00:42:00Z', +}; + +const expiredJobAction = { + name: 'expired action', + playPath: `${TEST_HOST}/expired/job/action`, + playable: true, + scheduledAt: '2018-10-05T08:23:00Z', +}; + describe('EnvironmentActions Component', () => { - let vm; + let wrapper; - const findEnvironmentActionsButton = () => vm.find('[data-testid="environment-actions-button"]'); + const findEnvironmentActionsButton = () => + wrapper.find('[data-testid="environment-actions-button"]'); - beforeEach(() => { - vm = shallowMount(EnvironmentActions, { - propsData: { actions: [] }, + function createComponent(props, { mountFn = shallowMount } = {}) { + wrapper = mountFn(EnvironmentActions, { + propsData: { actions: [], ...props }, directives: { GlTooltip: createMockDirective(), }, }); - }); + } + + function createComponentWithScheduledJobs(opts = {}) { + return createComponent({ actions: [scheduledJobAction, expiredJobAction] }, opts); + } + + const findDropdownItem = action => { + const buttons = wrapper.findAll(GlDropdownItem); + return buttons.filter(button => button.text().startsWith(action.name)).at(0); + }; afterEach(() => { - vm.destroy(); + wrapper.destroy(); + wrapper = null; }); it('should render a dropdown button with 2 icons', () => { - expect(vm.find('.dropdown-new').findAll(GlIcon).length).toBe(2); + createComponent({}, { mountFn: mount }); + expect(wrapper.find(GlDropdown).findAll(GlIcon).length).toBe(2); }); it('should render a dropdown button with aria-label description', () => { - expect(vm.find('.dropdown-new').attributes('aria-label')).toEqual('Deploy to...'); + createComponent(); + expect(wrapper.find(GlDropdown).attributes('aria-label')).toBe('Deploy to...'); }); it('should render a tooltip', () => { + createComponent(); const tooltip = getBinding(findEnvironmentActionsButton().element, 'gl-tooltip'); expect(tooltip).toBeDefined(); }); - describe('is loading', () => { - beforeEach(() => { - vm.setData({ isLoading: true }); - }); - - it('should render a dropdown button with a loading icon', () => { - expect(vm.findAll(GlLoadingIcon).length).toBe(1); - }); - }); - describe('manual actions', () => { const actions = [ { @@ -64,68 +82,71 @@ describe('EnvironmentActions Component', () => { ]; beforeEach(() => { - vm.setProps({ actions }); + createComponent({ actions }); }); it('should render a dropdown with the provided list of actions', () => { - expect(vm.findAll('.dropdown-menu li').length).toEqual(actions.length); + expect(wrapper.findAll(GlDropdownItem)).toHaveLength(actions.length); }); it("should render a disabled action when it's not playable", () => { - expect(vm.find('.dropdown-menu li:last-child gl-button-stub').props('disabled')).toBe(true); + const dropdownItems = wrapper.findAll(GlDropdownItem); + const lastDropdownItem = dropdownItems.at(dropdownItems.length - 1); + expect(lastDropdownItem.attributes('disabled')).toBe('true'); }); }); describe('scheduled jobs', () => { - const scheduledJobAction = { - name: 'scheduled action', - playPath: `${TEST_HOST}/scheduled/job/action`, - playable: true, - scheduledAt: '2063-04-05T00:42:00Z', - }; - const expiredJobAction = { - name: 'expired action', - playPath: `${TEST_HOST}/expired/job/action`, - playable: true, - scheduledAt: '2018-10-05T08:23:00Z', - }; - const findDropdownItem = action => { - const buttons = vm.findAll('.dropdown-menu li gl-button-stub'); - return buttons.filter(button => button.text().startsWith(action.name)).at(0); + let emitSpy; + + const clickAndConfirm = async ({ confirm = true } = {}) => { + jest.spyOn(window, 'confirm').mockImplementation(() => confirm); + + findDropdownItem(scheduledJobAction).vm.$emit('click'); + await wrapper.vm.$nextTick(); }; beforeEach(() => { + emitSpy = jest.fn(); + eventHub.$on('postAction', emitSpy); jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime()); - vm.setProps({ actions: [scheduledJobAction, expiredJobAction] }); }); - it('emits postAction event after confirming', () => { - const emitSpy = jest.fn(); - eventHub.$on('postAction', emitSpy); - jest.spyOn(window, 'confirm').mockImplementation(() => true); + describe('when postAction event is confirmed', () => { + beforeEach(async () => { + createComponentWithScheduledJobs({ mountFn: mount }); + clickAndConfirm(); + }); - findDropdownItem(scheduledJobAction).vm.$emit('click'); + it('emits postAction event', () => { + expect(window.confirm).toHaveBeenCalled(); + expect(emitSpy).toHaveBeenCalledWith({ endpoint: scheduledJobAction.playPath }); + }); - expect(window.confirm).toHaveBeenCalled(); - expect(emitSpy).toHaveBeenCalledWith({ endpoint: scheduledJobAction.playPath }); + it('should render a dropdown button with a loading icon', () => { + expect(wrapper.find(GlLoadingIcon).isVisible()).toBe(true); + }); }); - it('does not emit postAction event if confirmation is cancelled', () => { - const emitSpy = jest.fn(); - eventHub.$on('postAction', emitSpy); - jest.spyOn(window, 'confirm').mockImplementation(() => false); - - findDropdownItem(scheduledJobAction).vm.$emit('click'); + describe('when postAction event is denied', () => { + beforeEach(() => { + createComponentWithScheduledJobs({ mountFn: mount }); + clickAndConfirm({ confirm: false }); + }); - expect(window.confirm).toHaveBeenCalled(); - expect(emitSpy).not.toHaveBeenCalled(); + it('does not emit postAction event if confirmation is cancelled', () => { + expect(window.confirm).toHaveBeenCalled(); + expect(emitSpy).not.toHaveBeenCalled(); + }); }); it('displays the remaining time in the dropdown', () => { + createComponentWithScheduledJobs(); expect(findDropdownItem(scheduledJobAction).text()).toContain('24:00:00'); }); it('displays 00:00:00 for expired jobs in the dropdown', () => { + createComponentWithScheduledJobs(); expect(findDropdownItem(expiredJobAction).text()).toContain('00:00:00'); }); }); |