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

work_item_state_toggle_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: a210bd50422486642abee0bbdf7e1acd273bf6e5 (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
import { GlButton } from '@gitlab/ui';
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 WorkItemStateToggle from '~/work_items/components/work_item_state_toggle.vue';
import {
  STATE_OPEN,
  STATE_CLOSED,
  STATE_EVENT_CLOSE,
  STATE_EVENT_REOPEN,
  TRACKING_CATEGORY_SHOW,
} from '~/work_items/constants';
import updateWorkItemMutation from '~/work_items/graphql/update_work_item.mutation.graphql';
import { updateWorkItemMutationResponse, workItemQueryResponse } from '../mock_data';

describe('Work Item State toggle button component', () => {
  let wrapper;

  Vue.use(VueApollo);

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

  const findStateToggleButton = () => wrapper.findComponent(GlButton);

  const { id } = workItemQueryResponse.data.workItem;

  const createComponent = ({
    mutationHandler = mutationSuccessHandler,
    canUpdate = true,
    workItemState = STATE_OPEN,
    workItemType = 'Task',
  } = {}) => {
    wrapper = shallowMount(WorkItemStateToggle, {
      apolloProvider: createMockApollo([[updateWorkItemMutation, mutationHandler]]),
      propsData: {
        workItemId: id,
        workItemState,
        workItemType,
        canUpdate,
      },
    });
  };

  describe('work item State button text', () => {
    it.each`
      workItemState   | workItemType    | buttonText
      ${STATE_OPEN}   | ${'Task'}       | ${'Close task'}
      ${STATE_CLOSED} | ${'Task'}       | ${'Reopen task'}
      ${STATE_OPEN}   | ${'Objective'}  | ${'Close objective'}
      ${STATE_CLOSED} | ${'Objective'}  | ${'Reopen objective'}
      ${STATE_OPEN}   | ${'Key result'} | ${'Close key result'}
      ${STATE_CLOSED} | ${'Key result'} | ${'Reopen key result'}
    `(
      'is "$buttonText" when "$workItemType" state is "$workItemState"',
      ({ workItemState, workItemType, buttonText }) => {
        createComponent({ workItemState, workItemType });

        expect(findStateToggleButton().text()).toBe(buttonText);
      },
    );
  });

  describe('when updating the state', () => {
    it('calls a mutation', () => {
      createComponent();

      findStateToggleButton().vm.$emit('click');

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

    it('calls a mutation with REOPEN', () => {
      createComponent({
        workItemState: STATE_CLOSED,
      });

      findStateToggleButton().vm.$emit('click');

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

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

      findStateToggleButton().vm.$emit('click');
      await waitForPromises();

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

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

      createComponent();

      findStateToggleButton().vm.$emit('click');
      await waitForPromises();

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