diff options
Diffstat (limited to 'spec/frontend/work_items/components')
5 files changed, 271 insertions, 97 deletions
diff --git a/spec/frontend/work_items/components/item_state_spec.js b/spec/frontend/work_items/components/item_state_spec.js new file mode 100644 index 00000000000..79b76f3c061 --- /dev/null +++ b/spec/frontend/work_items/components/item_state_spec.js @@ -0,0 +1,54 @@ +import { mount } from '@vue/test-utils'; +import { STATE_OPEN, STATE_CLOSED } from '~/work_items/constants'; +import ItemState from '~/work_items/components/item_state.vue'; + +describe('ItemState', () => { + let wrapper; + + const findLabel = () => wrapper.find('label').text(); + const selectedValue = () => wrapper.find('option:checked').element.value; + + const clickOpen = () => wrapper.findAll('option').at(0).setSelected(); + + const createComponent = ({ state = STATE_OPEN, disabled = false } = {}) => { + wrapper = mount(ItemState, { + propsData: { + state, + disabled, + }, + }); + }; + + afterEach(() => { + wrapper.destroy(); + }); + + it('renders label and dropdown', () => { + createComponent(); + + expect(findLabel()).toBe('Status'); + expect(selectedValue()).toBe(STATE_OPEN); + }); + + it('renders dropdown for closed', () => { + createComponent({ state: STATE_CLOSED }); + + expect(selectedValue()).toBe(STATE_CLOSED); + }); + + it('emits changed event', async () => { + createComponent({ state: STATE_CLOSED }); + + await clickOpen(); + + expect(wrapper.emitted('changed')).toEqual([[STATE_OPEN]]); + }); + + it('does not emits changed event if clicking selected value', async () => { + createComponent({ state: STATE_OPEN }); + + await clickOpen(); + + expect(wrapper.emitted('changed')).toBeUndefined(); + }); +}); diff --git a/spec/frontend/work_items/components/work_item_actions_spec.js b/spec/frontend/work_items/components/work_item_actions_spec.js index d0e9cfee353..137a0a7326d 100644 --- a/spec/frontend/work_items/components/work_item_actions_spec.js +++ b/spec/frontend/work_items/components/work_item_actions_spec.js @@ -1,30 +1,18 @@ import { GlDropdownItem, GlModal } from '@gitlab/ui'; import { shallowMount } from '@vue/test-utils'; -import Vue from 'vue'; -import VueApollo from 'vue-apollo'; -import waitForPromises from 'helpers/wait_for_promises'; -import createMockApollo from 'helpers/mock_apollo_helper'; import WorkItemActions from '~/work_items/components/work_item_actions.vue'; -import deleteWorkItem from '~/work_items/graphql/delete_work_item.mutation.graphql'; -import { deleteWorkItemResponse, deleteWorkItemFailureResponse } from '../mock_data'; describe('WorkItemActions component', () => { let wrapper; let glModalDirective; - Vue.use(VueApollo); - const findModal = () => wrapper.findComponent(GlModal); const findDeleteButton = () => wrapper.findComponent(GlDropdownItem); - const createComponent = ({ - canUpdate = true, - deleteWorkItemHandler = jest.fn().mockResolvedValue(deleteWorkItemResponse), - } = {}) => { + const createComponent = ({ canDelete = true } = {}) => { glModalDirective = jest.fn(); wrapper = shallowMount(WorkItemActions, { - apolloProvider: createMockApollo([[deleteWorkItem, deleteWorkItemHandler]]), - propsData: { workItemId: '123', canUpdate }, + propsData: { workItemId: '123', canDelete }, directives: { glModal: { bind(_, { value }) { @@ -54,48 +42,17 @@ describe('WorkItemActions component', () => { expect(glModalDirective).toHaveBeenCalled(); }); - it('calls delete mutation when clicking OK button', () => { - const deleteWorkItemHandler = jest.fn().mockResolvedValue(deleteWorkItemResponse); - - createComponent({ - deleteWorkItemHandler, - }); - - findModal().vm.$emit('ok'); - - expect(deleteWorkItemHandler).toHaveBeenCalled(); - expect(wrapper.emitted('error')).toBeUndefined(); - }); - - it('emits event after delete success', async () => { + it('emits event when clicking OK button', () => { createComponent(); findModal().vm.$emit('ok'); - await waitForPromises(); - - expect(wrapper.emitted('workItemDeleted')).not.toBeUndefined(); - expect(wrapper.emitted('error')).toBeUndefined(); - }); - - it('emits error event after delete failure', async () => { - createComponent({ - deleteWorkItemHandler: jest.fn().mockResolvedValue(deleteWorkItemFailureResponse), - }); - - findModal().vm.$emit('ok'); - - await waitForPromises(); - - expect(wrapper.emitted('error')[0]).toEqual([ - "The resource that you are attempting to access does not exist or you don't have permission to perform this action", - ]); - expect(wrapper.emitted('workItemDeleted')).toBeUndefined(); + expect(wrapper.emitted('deleteWorkItem')).toEqual([[]]); }); - it('does not render when canUpdate is false', () => { + it('does not render when canDelete is false', () => { createComponent({ - canUpdate: false, + canDelete: false, }); expect(wrapper.html()).toBe(''); diff --git a/spec/frontend/work_items/components/work_item_detail_modal_spec.js b/spec/frontend/work_items/components/work_item_detail_modal_spec.js index 9f35ccb853b..aaabdbc82d9 100644 --- a/spec/frontend/work_items/components/work_item_detail_modal_spec.js +++ b/spec/frontend/work_items/components/work_item_detail_modal_spec.js @@ -1,23 +1,57 @@ -import { GlModal } from '@gitlab/ui'; +import { GlAlert } from '@gitlab/ui'; import { shallowMount } from '@vue/test-utils'; -import Vue from 'vue'; +import Vue, { nextTick } from 'vue'; import VueApollo from 'vue-apollo'; +import waitForPromises from 'helpers/wait_for_promises'; +import createMockApollo from 'helpers/mock_apollo_helper'; import WorkItemDetail from '~/work_items/components/work_item_detail.vue'; import WorkItemDetailModal from '~/work_items/components/work_item_detail_modal.vue'; -import WorkItemActions from '~/work_items/components/work_item_actions.vue'; +import deleteWorkItemFromTaskMutation from '~/work_items/graphql/delete_task_from_work_item.mutation.graphql'; describe('WorkItemDetailModal component', () => { let wrapper; Vue.use(VueApollo); + const hideModal = jest.fn(); + const GlModal = { + template: ` + <div> + <slot></slot> + </div> + `, + methods: { + hide: hideModal, + }, + }; + const findModal = () => wrapper.findComponent(GlModal); - const findWorkItemActions = () => wrapper.findComponent(WorkItemActions); + const findAlert = () => wrapper.findComponent(GlAlert); const findWorkItemDetail = () => wrapper.findComponent(WorkItemDetail); - const createComponent = ({ visible = true, workItemId = '1', canUpdate = false } = {}) => { + const createComponent = ({ workItemId = '1', error = false } = {}) => { + const apolloProvider = createMockApollo([ + [ + deleteWorkItemFromTaskMutation, + jest.fn().mockResolvedValue({ + data: { + workItemDeleteTask: { + workItem: { id: 123, descriptionHtml: 'updated work item desc' }, + errors: [], + }, + }, + }), + ], + ]); + wrapper = shallowMount(WorkItemDetailModal, { - propsData: { visible, workItemId, canUpdate }, + apolloProvider, + propsData: { workItemId }, + data() { + return { + error, + }; + }, stubs: { GlModal, }, @@ -28,31 +62,59 @@ describe('WorkItemDetailModal component', () => { wrapper.destroy(); }); - describe.each([true, false])('when visible=%s', (visible) => { - it(`${visible ? 'renders' : 'does not render'} modal`, () => { - createComponent({ visible }); + it('renders WorkItemDetail', () => { + createComponent(); - expect(findModal().props('visible')).toBe(visible); + expect(findWorkItemDetail().props()).toEqual({ + workItemId: '1', }); }); - it('renders heading', () => { + it('renders alert if there is an error', () => { + createComponent({ error: true }); + + expect(findAlert().exists()).toBe(true); + }); + + it('does not render alert if there is no error', () => { createComponent(); - expect(wrapper.find('h2').text()).toBe('Work Item'); + expect(findAlert().exists()).toBe(false); }); - it('renders WorkItemDetail', () => { + it('dismisses the alert on `dismiss` emitted event', async () => { + createComponent({ error: true }); + findAlert().vm.$emit('dismiss'); + await nextTick(); + + expect(findAlert().exists()).toBe(false); + }); + + it('emits `close` event on hiding the modal', () => { createComponent(); + findModal().vm.$emit('hide'); - expect(findWorkItemDetail().props()).toEqual({ workItemId: '1' }); + expect(wrapper.emitted('close')).toBeTruthy(); }); - it('shows work item actions', () => { - createComponent({ - canUpdate: true, - }); + it('emits `workItemUpdated` event on updating work item', () => { + createComponent(); + findWorkItemDetail().vm.$emit('workItemUpdated'); + + expect(wrapper.emitted('workItemUpdated')).toBeTruthy(); + }); + + describe('delete work item', () => { + it('emits workItemDeleted and closes modal', async () => { + createComponent(); + const newDesc = 'updated work item desc'; + + findWorkItemDetail().vm.$emit('deleteWorkItem'); - expect(findWorkItemActions().exists()).toBe(true); + await waitForPromises(); + + expect(wrapper.emitted('workItemDeleted')).toEqual([[newDesc]]); + expect(hideModal).toHaveBeenCalled(); + }); }); }); diff --git a/spec/frontend/work_items/components/work_item_state_spec.js b/spec/frontend/work_items/components/work_item_state_spec.js new file mode 100644 index 00000000000..9e48f56d9e9 --- /dev/null +++ b/spec/frontend/work_items/components/work_item_state_spec.js @@ -0,0 +1,117 @@ +import { shallowMount } from '@vue/test-utils'; +import Vue from 'vue'; +import VueApollo from 'vue-apollo'; +import createMockApollo from 'helpers/mock_apollo_helper'; +import { mockTracking } from 'helpers/tracking_helper'; +import waitForPromises from 'helpers/wait_for_promises'; +import ItemState from '~/work_items/components/item_state.vue'; +import WorkItemState from '~/work_items/components/work_item_state.vue'; +import { + i18n, + STATE_OPEN, + STATE_CLOSED, + STATE_EVENT_CLOSE, + STATE_EVENT_REOPEN, +} from '~/work_items/constants'; +import updateWorkItemMutation from '~/work_items/graphql/update_work_item.mutation.graphql'; +import { updateWorkItemMutationResponse, workItemQueryResponse } from '../mock_data'; + +describe('WorkItemState component', () => { + let wrapper; + + Vue.use(VueApollo); + + const mutationSuccessHandler = jest.fn().mockResolvedValue(updateWorkItemMutationResponse); + + const findItemState = () => wrapper.findComponent(ItemState); + + const createComponent = ({ + state = STATE_OPEN, + mutationHandler = mutationSuccessHandler, + } = {}) => { + const { id, workItemType } = workItemQueryResponse.data.workItem; + wrapper = shallowMount(WorkItemState, { + apolloProvider: createMockApollo([[updateWorkItemMutation, mutationHandler]]), + propsData: { + workItem: { + id, + state, + workItemType, + }, + }, + }); + }; + + afterEach(() => { + wrapper.destroy(); + }); + + it('renders state', () => { + createComponent(); + + expect(findItemState().props('state')).toBe(workItemQueryResponse.data.workItem.state); + }); + + describe('when updating the state', () => { + it('calls a mutation', () => { + createComponent(); + + findItemState().vm.$emit('changed', STATE_CLOSED); + + expect(mutationSuccessHandler).toHaveBeenCalledWith({ + input: { + id: workItemQueryResponse.data.workItem.id, + stateEvent: STATE_EVENT_CLOSE, + }, + }); + }); + + it('calls a mutation with REOPEN', () => { + createComponent({ + state: STATE_CLOSED, + }); + + findItemState().vm.$emit('changed', STATE_OPEN); + + expect(mutationSuccessHandler).toHaveBeenCalledWith({ + input: { + id: workItemQueryResponse.data.workItem.id, + stateEvent: STATE_EVENT_REOPEN, + }, + }); + }); + + it('emits updated event', async () => { + createComponent(); + + findItemState().vm.$emit('changed', STATE_CLOSED); + await waitForPromises(); + + expect(wrapper.emitted('updated')).toEqual([[]]); + }); + + it('emits an error message when the mutation was unsuccessful', async () => { + createComponent({ mutationHandler: jest.fn().mockRejectedValue('Error!') }); + + findItemState().vm.$emit('changed', STATE_CLOSED); + await waitForPromises(); + + expect(wrapper.emitted('error')).toEqual([[i18n.updateError]]); + }); + + it('tracks editing the state', async () => { + const trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn); + + createComponent(); + + findItemState().vm.$emit('changed', STATE_CLOSED); + await waitForPromises(); + + expect(trackingSpy).toHaveBeenCalledWith('workItems:show', 'updated_state', { + category: 'workItems:show', + label: 'item_state', + property: 'type_Task', + }); + }); + }); +}); diff --git a/spec/frontend/work_items/components/work_item_title_spec.js b/spec/frontend/work_items/components/work_item_title_spec.js index 9b1ef2d14e4..19b56362ac0 100644 --- a/spec/frontend/work_items/components/work_item_title_spec.js +++ b/spec/frontend/work_items/components/work_item_title_spec.js @@ -1,4 +1,3 @@ -import { GlLoadingIcon } from '@gitlab/ui'; import { shallowMount } from '@vue/test-utils'; import Vue from 'vue'; import VueApollo from 'vue-apollo'; @@ -18,15 +17,13 @@ describe('WorkItemTitle component', () => { const mutationSuccessHandler = jest.fn().mockResolvedValue(updateWorkItemMutationResponse); - const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon); const findItemTitle = () => wrapper.findComponent(ItemTitle); - const createComponent = ({ loading = false, mutationHandler = mutationSuccessHandler } = {}) => { + const createComponent = ({ mutationHandler = mutationSuccessHandler } = {}) => { const { id, title, workItemType } = workItemQueryResponse.data.workItem; wrapper = shallowMount(WorkItemTitle, { apolloProvider: createMockApollo([[updateWorkItemMutation, mutationHandler]]), propsData: { - loading, workItemId: id, workItemTitle: title, workItemType: workItemType.name, @@ -38,32 +35,10 @@ describe('WorkItemTitle component', () => { wrapper.destroy(); }); - describe('when loading', () => { - beforeEach(() => { - createComponent({ loading: true }); - }); - - it('renders loading spinner', () => { - expect(findLoadingIcon().exists()).toBe(true); - }); - - it('does not render title', () => { - expect(findItemTitle().exists()).toBe(false); - }); - }); - - describe('when loaded', () => { - beforeEach(() => { - createComponent({ loading: false }); - }); - - it('does not render loading spinner', () => { - expect(findLoadingIcon().exists()).toBe(false); - }); + it('renders title', () => { + createComponent(); - it('renders title', () => { - expect(findItemTitle().props('title')).toBe(workItemQueryResponse.data.workItem.title); - }); + expect(findItemTitle().props('title')).toBe(workItemQueryResponse.data.workItem.title); }); describe('when updating the title', () => { @@ -82,6 +57,15 @@ describe('WorkItemTitle component', () => { }); }); + it('emits updated event', async () => { + createComponent(); + + findItemTitle().vm.$emit('title-changed', 'new title'); + await waitForPromises(); + + expect(wrapper.emitted('updated')).toEqual([[]]); + }); + it('does not call a mutation when the title has not changed', () => { createComponent(); |