summaryrefslogtreecommitdiff
path: root/spec/frontend/work_items/components/work_item_links/work_item_links_form_spec.js
blob: 434c1db8a2ce79abe69defd7bdc314d72d8eac24 (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
import Vue from 'vue';
import { GlForm, GlFormInput, GlFormCombobox } from '@gitlab/ui';
import VueApollo from 'vue-apollo';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import WorkItemLinksForm from '~/work_items/components/work_item_links/work_item_links_form.vue';
import projectWorkItemsQuery from '~/work_items/graphql/project_work_items.query.graphql';
import projectWorkItemTypesQuery from '~/work_items/graphql/project_work_item_types.query.graphql';
import createWorkItemMutation from '~/work_items/graphql/create_work_item.mutation.graphql';
import updateWorkItemMutation from '~/work_items/graphql/update_work_item.mutation.graphql';
import {
  availableWorkItemsResponse,
  projectWorkItemTypesQueryResponse,
  createWorkItemMutationResponse,
  updateWorkItemMutationResponse,
} from '../../mock_data';

Vue.use(VueApollo);

describe('WorkItemLinksForm', () => {
  let wrapper;

  const updateMutationResolver = jest.fn().mockResolvedValue(updateWorkItemMutationResponse);
  const createMutationResolver = jest.fn().mockResolvedValue(createWorkItemMutationResponse);

  const createComponent = async ({
    listResponse = availableWorkItemsResponse,
    typesResponse = projectWorkItemTypesQueryResponse,
    parentConfidential = false,
  } = {}) => {
    wrapper = shallowMountExtended(WorkItemLinksForm, {
      apolloProvider: createMockApollo([
        [projectWorkItemsQuery, jest.fn().mockResolvedValue(listResponse)],
        [projectWorkItemTypesQuery, jest.fn().mockResolvedValue(typesResponse)],
        [updateWorkItemMutation, updateMutationResolver],
        [createWorkItemMutation, createMutationResolver],
      ]),
      propsData: { issuableGid: 'gid://gitlab/WorkItem/1', parentConfidential },
      provide: {
        projectPath: 'project/path',
      },
    });

    await waitForPromises();
  };

  const findForm = () => wrapper.findComponent(GlForm);
  const findCombobox = () => wrapper.findComponent(GlFormCombobox);
  const findInput = () => wrapper.findComponent(GlFormInput);
  const findAddChildButton = () => wrapper.findByTestId('add-child-button');

  beforeEach(async () => {
    await createComponent();
  });

  afterEach(() => {
    wrapper.destroy();
  });

  it('renders form', () => {
    expect(findForm().exists()).toBe(true);
  });

  it('creates child task in non confidential parent', async () => {
    findInput().vm.$emit('input', 'Create task test');

    findForm().vm.$emit('submit', {
      preventDefault: jest.fn(),
    });
    await waitForPromises();
    expect(createMutationResolver).toHaveBeenCalledWith({
      input: {
        title: 'Create task test',
        projectPath: 'project/path',
        workItemTypeId: 'gid://gitlab/WorkItems::Type/3',
        hierarchyWidget: {
          parentId: 'gid://gitlab/WorkItem/1',
        },
        confidential: false,
      },
    });
  });

  it('creates child task in confidential parent', async () => {
    await createComponent({ parentConfidential: true });

    findInput().vm.$emit('input', 'Create confidential task');

    findForm().vm.$emit('submit', {
      preventDefault: jest.fn(),
    });
    await waitForPromises();
    expect(createMutationResolver).toHaveBeenCalledWith({
      input: {
        title: 'Create confidential task',
        projectPath: 'project/path',
        workItemTypeId: 'gid://gitlab/WorkItems::Type/3',
        hierarchyWidget: {
          parentId: 'gid://gitlab/WorkItem/1',
        },
        confidential: true,
      },
    });
  });

  // Follow up issue to turn this functionality back on https://gitlab.com/gitlab-org/gitlab/-/issues/368757
  // eslint-disable-next-line jest/no-disabled-tests
  it.skip('selects and add child', async () => {
    findCombobox().vm.$emit('input', availableWorkItemsResponse.data.workspace.workItems.edges[0]);

    findAddChildButton().vm.$emit('click');
    await waitForPromises();
    expect(updateMutationResolver).toHaveBeenCalled();
  });

  // eslint-disable-next-line jest/no-disabled-tests
  describe.skip('when typing in combobox', () => {
    beforeEach(async () => {
      findCombobox().vm.$emit('input', 'Task');
      await waitForPromises();
      await jest.runOnlyPendingTimers();
    });

    it('passes available work items as prop', () => {
      expect(findCombobox().exists()).toBe(true);
      expect(findCombobox().props('tokenList').length).toBe(2);
    });

    it('passes action to create task', () => {
      expect(findCombobox().props('actionList').length).toBe(1);
    });
  });
});