diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-31 15:11:37 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-31 15:11:37 +0300 |
commit | c62dd5ae440db9ad4696000a863fbcb53fe83498 (patch) | |
tree | 3e4c18cd7a475e6d81f80335d173f1eb3f9510d8 /spec/frontend/vue_merge_request_widget | |
parent | aa88b9053d72f3f9b69ae5365b77595dcae5c0c3 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend/vue_merge_request_widget')
-rw-r--r-- | spec/frontend/vue_merge_request_widget/components/states/mr_widget_wip_spec.js | 42 | ||||
-rw-r--r-- | spec/frontend/vue_merge_request_widget/components/states/work_in_progress_spec.js | 181 |
2 files changed, 181 insertions, 42 deletions
diff --git a/spec/frontend/vue_merge_request_widget/components/states/mr_widget_wip_spec.js b/spec/frontend/vue_merge_request_widget/components/states/mr_widget_wip_spec.js deleted file mode 100644 index 82aeac1a47d..00000000000 --- a/spec/frontend/vue_merge_request_widget/components/states/mr_widget_wip_spec.js +++ /dev/null @@ -1,42 +0,0 @@ -import { mount } from '@vue/test-utils'; -import WorkInProgress from '~/vue_merge_request_widget/components/states/work_in_progress.vue'; - -let wrapper; - -const createComponent = (updateMergeRequest = true) => { - wrapper = mount(WorkInProgress, { - propsData: { - mr: {}, - }, - data() { - return { - userPermissions: { - updateMergeRequest, - }, - }; - }, - }); -}; - -describe('Merge request widget draft state component', () => { - afterEach(() => { - wrapper.destroy(); - }); - - describe('template', () => { - it('should have correct elements', () => { - createComponent(true); - - expect(wrapper.text()).toContain( - "Merge blocked: merge request must be marked as ready. It's still marked as draft.", - ); - expect(wrapper.find('[data-testid="removeWipButton"]').text()).toContain('Mark as ready'); - }); - - it('should not show removeWIP button is user cannot update MR', () => { - createComponent(false); - - expect(wrapper.find('[data-testid="removeWipButton"]').exists()).toBe(false); - }); - }); -}); diff --git a/spec/frontend/vue_merge_request_widget/components/states/work_in_progress_spec.js b/spec/frontend/vue_merge_request_widget/components/states/work_in_progress_spec.js new file mode 100644 index 00000000000..72c61b99cf4 --- /dev/null +++ b/spec/frontend/vue_merge_request_widget/components/states/work_in_progress_spec.js @@ -0,0 +1,181 @@ +import Vue from 'vue'; +import VueApollo from 'vue-apollo'; +import getStateQueryResponse from 'test_fixtures/graphql/merge_requests/get_state.query.graphql.json'; +import { createAlert } from '~/flash'; +import WorkInProgress, { + MSG_SOMETHING_WENT_WRONG, + MSG_MERGE_BLOCKED, + MSG_MARK_READY, +} from '~/vue_merge_request_widget/components/states/work_in_progress.vue'; +import draftQuery from '~/vue_merge_request_widget/queries/states/draft.query.graphql'; +import getStateQuery from '~/vue_merge_request_widget/queries/get_state.query.graphql'; +import removeDraftMutation from '~/vue_merge_request_widget/queries/toggle_draft.mutation.graphql'; +import MergeRequest from '~/merge_request'; +import { mountExtended } from 'helpers/vue_test_utils_helper'; +import createMockApollo from 'helpers/mock_apollo_helper'; +import waitForPromises from 'helpers/wait_for_promises'; + +Vue.use(VueApollo); + +const TEST_PROJECT_ID = getStateQueryResponse.data.project.id; +const TEST_MR_ID = getStateQueryResponse.data.project.mergeRequest.id; +const TEST_MR_IID = '23'; +const TEST_MR_TITLE = 'Test MR Title'; +const TEST_PROJECT_PATH = 'lorem/ipsum'; + +jest.mock('~/flash'); +jest.mock('~/merge_request'); + +describe('~/vue_merge_request_widget/components/states/work_in_progress.vue', () => { + let wrapper; + let apolloProvider; + + let draftQuerySpy; + let removeDraftMutationSpy; + + const findWIPButton = () => wrapper.findByTestId('removeWipButton'); + + const createDraftQueryResponse = (canUpdateMergeRequest) => ({ + data: { + project: { + __typename: 'Project', + id: TEST_PROJECT_ID, + mergeRequest: { + __typename: 'MergeRequest', + id: TEST_MR_ID, + userPermissions: { + updateMergeRequest: canUpdateMergeRequest, + }, + }, + }, + }, + }); + const createRemoveDraftMutationResponse = () => ({ + data: { + mergeRequestSetDraft: { + __typename: 'MergeRequestSetWipPayload', + errors: [], + mergeRequest: { + __typename: 'MergeRequest', + id: TEST_MR_ID, + title: TEST_MR_TITLE, + draft: false, + mergeableDiscussionsState: true, + }, + }, + }, + }); + + const createComponent = async () => { + wrapper = mountExtended(WorkInProgress, { + apolloProvider, + propsData: { + mr: { + issuableId: TEST_MR_ID, + title: TEST_MR_TITLE, + iid: TEST_MR_IID, + targetProjectFullPath: TEST_PROJECT_PATH, + }, + }, + }); + + await waitForPromises(); + + // why: work_in_progress.vue has some coupling that this query has been read before + // for some reason this has to happen **after** the component has mounted + // or apollo throws errors. + apolloProvider.defaultClient.cache.writeQuery({ + query: getStateQuery, + variables: { + projectPath: TEST_PROJECT_PATH, + iid: TEST_MR_IID, + }, + data: getStateQueryResponse.data, + }); + }; + + beforeEach(() => { + draftQuerySpy = jest.fn().mockResolvedValue(createDraftQueryResponse(true)); + removeDraftMutationSpy = jest.fn().mockResolvedValue(createRemoveDraftMutationResponse()); + + apolloProvider = createMockApollo([ + [draftQuery, draftQuerySpy], + [removeDraftMutation, removeDraftMutationSpy], + ]); + }); + + describe('when user can update MR', () => { + beforeEach(async () => { + await createComponent(); + }); + + it('renders text', () => { + expect(wrapper.text()).toContain(MSG_MERGE_BLOCKED); + }); + + it('renders mark ready button', () => { + expect(findWIPButton().text()).toBe(MSG_MARK_READY); + }); + + it('does not call remove draft mutation', () => { + expect(removeDraftMutationSpy).not.toHaveBeenCalled(); + }); + + describe('when mark ready button is clicked', () => { + beforeEach(async () => { + findWIPButton().vm.$emit('click'); + + await waitForPromises(); + }); + + it('calls mutation spy', () => { + expect(removeDraftMutationSpy).toHaveBeenCalledWith({ + draft: false, + iid: TEST_MR_IID, + projectPath: TEST_PROJECT_PATH, + }); + }); + + it('does not create alert', () => { + expect(createAlert).not.toHaveBeenCalled(); + }); + + it('calls toggleDraftStatus', () => { + expect(MergeRequest.toggleDraftStatus).toHaveBeenCalledWith(TEST_MR_TITLE, true); + }); + }); + + describe('when mutation fails and ready button is clicked', () => { + beforeEach(async () => { + removeDraftMutationSpy.mockRejectedValue(new Error('TEST FAIL')); + findWIPButton().vm.$emit('click'); + + await waitForPromises(); + }); + + it('creates alert', () => { + expect(createAlert).toHaveBeenCalledWith({ + message: MSG_SOMETHING_WENT_WRONG, + }); + }); + + it('does not call toggleDraftStatus', () => { + expect(MergeRequest.toggleDraftStatus).not.toHaveBeenCalled(); + }); + }); + }); + + describe('when user cannot update MR', () => { + beforeEach(async () => { + draftQuerySpy.mockResolvedValue(createDraftQueryResponse(false)); + + createComponent(); + + await waitForPromises(); + }); + + it('does not render mark ready button', () => { + expect(findWIPButton().exists()).toBe(false); + }); + }); +}); |