summaryrefslogtreecommitdiff
path: root/spec/frontend/sidebar/components/move/move_issue_button_spec.js
blob: e2f5414056a9e84347733daeb9539c7838201a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { visitUrl } from '~/lib/utils/url_utility';
import { createAlert } from '~/alert';
import ProjectSelect from '~/sidebar/components/move/issuable_move_dropdown.vue';
import MoveIssueButton from '~/sidebar/components/move/move_issue_button.vue';
import moveIssueMutation from '~/sidebar/queries/move_issue.mutation.graphql';

Vue.use(VueApollo);

jest.mock('~/alert');
jest.mock('~/lib/utils/url_utility', () => ({
  visitUrl: jest.fn(),
}));

const projectFullPath = 'flight/FlightJS';
const projectsAutocompleteEndpoint = '/-/autocomplete/projects?project_id=1';
const issueIid = '15';

const mockDestinationProject = {
  full_path: 'gitlab-org/GitLabTest',
};

const mockWebUrl = `${mockDestinationProject.full_path}/issues/${issueIid}`;

const mockMutationErrorMessage = 'Example error message';

const resolvedMutationWithoutErrorsMock = jest.fn().mockResolvedValue({
  data: {
    issueMove: {
      issue: {
        id: issueIid,
        webUrl: mockWebUrl,
      },
      errors: [],
    },
  },
});

const resolvedMutationWithErrorsMock = jest.fn().mockResolvedValue({
  data: {
    issueMove: {
      errors: [{ message: mockMutationErrorMessage }],
    },
  },
});

const rejectedMutationMock = jest.fn().mockRejectedValue({});

describe('MoveIssueButton', () => {
  let wrapper;
  let fakeApollo;

  const findProjectSelect = () => wrapper.findComponent(ProjectSelect);
  const emitProjectSelectEvent = () => {
    findProjectSelect().vm.$emit('move-issuable', mockDestinationProject);
  };
  const createComponent = (mutationResolverMock = rejectedMutationMock) => {
    fakeApollo = createMockApollo([[moveIssueMutation, mutationResolverMock]]);

    wrapper = shallowMount(MoveIssueButton, {
      provide: {
        projectFullPath,
        projectsAutocompleteEndpoint,
        issueIid,
      },
      apolloProvider: fakeApollo,
    });
  };

  it('renders the project select dropdown', () => {
    createComponent();

    expect(findProjectSelect().props()).toMatchObject({
      projectsFetchPath: projectsAutocompleteEndpoint,
      dropdownButtonTitle: MoveIssueButton.i18n.title,
      dropdownHeaderTitle: MoveIssueButton.i18n.title,
      moveInProgress: false,
    });
  });

  describe('when the project is selected', () => {
    it('sets loading state and dropdown button text when issue is moving', async () => {
      createComponent();
      expect(findProjectSelect().props()).toMatchObject({
        dropdownButtonTitle: MoveIssueButton.i18n.title,
        moveInProgress: false,
      });

      emitProjectSelectEvent();
      await nextTick();

      expect(findProjectSelect().props()).toMatchObject({
        dropdownButtonTitle: MoveIssueButton.i18n.titleInProgress,
        moveInProgress: true,
      });
    });

    it.each`
      condition                      | mutation
      ${'a mutation returns errors'} | ${resolvedMutationWithErrorsMock}
      ${'a mutation is rejected'}    | ${rejectedMutationMock}
    `('sets loading state to false when $condition', async ({ mutation }) => {
      createComponent(mutation);
      emitProjectSelectEvent();

      await nextTick();
      expect(findProjectSelect().props('moveInProgress')).toBe(true);

      await waitForPromises();
      expect(findProjectSelect().props('moveInProgress')).toBe(false);
    });

    it('creates an alert and logs errors when a mutation returns errors', async () => {
      createComponent(resolvedMutationWithErrorsMock);
      emitProjectSelectEvent();

      await waitForPromises();

      expect(createAlert).toHaveBeenCalledWith({
        message: MoveIssueButton.i18n.moveErrorMessage,
        captureError: true,
        error: expect.any(Object),
      });
    });

    it('calls a mutation for the selected issue', async () => {
      createComponent(resolvedMutationWithoutErrorsMock);
      emitProjectSelectEvent();

      await waitForPromises();

      expect(resolvedMutationWithoutErrorsMock).toHaveBeenCalledWith({
        moveIssueInput: {
          projectPath: projectFullPath,
          iid: issueIid,
          targetProjectPath: mockDestinationProject.full_path,
        },
      });
    });

    it('redirects to the correct page when the mutation succeeds', async () => {
      createComponent(resolvedMutationWithoutErrorsMock);
      emitProjectSelectEvent();
      await waitForPromises();

      expect(visitUrl).toHaveBeenCalledWith(mockWebUrl);
    });
  });
});