Welcome to mirror list, hosted at ThFree Co, Russian Federation.

work_item_title_spec.js « components « work_items « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f466bcf691692c2985ec24bcdd61249f3d01027 (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
154
155
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 ItemTitle from '~/work_items/components/item_title.vue';
import WorkItemTitle from '~/work_items/components/work_item_title.vue';
import { TRACKING_CATEGORY_SHOW } from '~/work_items/constants';
import updateWorkItemMutation from '~/work_items/graphql/update_work_item.mutation.graphql';
import updateWorkItemTaskMutation from '~/work_items/graphql/update_work_item_task.mutation.graphql';
import { updateWorkItemMutationResponse, workItemQueryResponse } from '../mock_data';

describe('WorkItemTitle component', () => {
  let wrapper;

  Vue.use(VueApollo);

  const mutationSuccessHandler = jest.fn().mockResolvedValue(updateWorkItemMutationResponse);

  const findItemTitle = () => wrapper.findComponent(ItemTitle);

  const createComponent = ({
    workItemParentId,
    mutationHandler = mutationSuccessHandler,
    canUpdate = true,
  } = {}) => {
    const { id, title, workItemType } = workItemQueryResponse.data.workItem;
    wrapper = shallowMount(WorkItemTitle, {
      apolloProvider: createMockApollo([
        [updateWorkItemMutation, mutationHandler],
        [updateWorkItemTaskMutation, mutationHandler],
      ]),
      propsData: {
        workItemId: id,
        workItemTitle: title,
        workItemType: workItemType.name,
        workItemParentId,
        canUpdate,
      },
    });
  };

  it('renders title', () => {
    createComponent();

    expect(findItemTitle().props('title')).toBe(workItemQueryResponse.data.workItem.title);
  });

  describe('item title disabled prop', () => {
    describe.each`
      description             | canUpdate | value
      ${'when cannot update'} | ${false}  | ${true}
      ${'when can update'}    | ${true}   | ${false}
    `('$description', ({ canUpdate, value }) => {
      it(`renders item title component with disabled=${value}`, () => {
        createComponent({ canUpdate });

        expect(findItemTitle().props('disabled')).toBe(value);
      });
    });
  });

  describe('when updating the title', () => {
    it('calls a mutation', () => {
      const title = 'new title!';

      createComponent();

      findItemTitle().vm.$emit('title-changed', title);

      expect(mutationSuccessHandler).toHaveBeenCalledWith({
        input: {
          id: workItemQueryResponse.data.workItem.id,
          title,
        },
      });
    });

    it('calls WorkItemTaskUpdate if passed workItemParentId prop', () => {
      const title = 'new title!';
      const workItemParentId = '1234';

      createComponent({
        workItemParentId,
      });

      findItemTitle().vm.$emit('title-changed', title);

      expect(mutationSuccessHandler).toHaveBeenCalledWith({
        input: {
          id: workItemParentId,
          taskData: {
            id: workItemQueryResponse.data.workItem.id,
            title,
          },
        },
      });
    });

    it('does not call a mutation when the title has not changed', () => {
      createComponent();

      findItemTitle().vm.$emit('title-changed', workItemQueryResponse.data.workItem.title);

      expect(mutationSuccessHandler).not.toHaveBeenCalled();
    });

    it('emits an error message when the mutation was unsuccessful', async () => {
      createComponent({ mutationHandler: jest.fn().mockRejectedValue('Error!') });

      findItemTitle().vm.$emit('title-changed', 'new title');
      await waitForPromises();

      expect(wrapper.emitted('error')).toEqual([
        ['Something went wrong while updating the task. Please try again.'],
      ]);
    });

    it('tracks editing the title', async () => {
      const trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);

      createComponent();

      findItemTitle().vm.$emit('title-changed', 'new title');
      await waitForPromises();

      expect(trackingSpy).toHaveBeenCalledWith(TRACKING_CATEGORY_SHOW, 'updated_title', {
        category: TRACKING_CATEGORY_SHOW,
        label: 'item_title',
        property: 'type_Task',
      });
    });

    describe('when title has more than 255 characters', () => {
      const title = new Array(257).join('a');

      it('does not call a mutation', () => {
        createComponent();

        findItemTitle().vm.$emit('title-changed', title);

        expect(mutationSuccessHandler).not.toHaveBeenCalled();
      });

      it('emits an error message', () => {
        createComponent();

        findItemTitle().vm.$emit('title-changed', title);

        expect(wrapper.emitted('error')).toEqual([['Title cannot have more than 255 characters.']]);
      });
    });
  });
});