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

work_item_detail_modal_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: f77d6c89035472db164dffe71e55a28067dd21db (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
import { GlAlert } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import waitForPromises from 'helpers/wait_for_promises';
import createMockApollo from 'helpers/mock_apollo_helper';
import { stubComponent } from 'helpers/stub_component';
import WorkItemDetailModal from '~/work_items/components/work_item_detail_modal.vue';
import deleteWorkItemMutation from '~/work_items/graphql/delete_work_item.mutation.graphql';
import WorkItemDetail from '~/work_items/components/work_item_detail.vue';
import { deleteWorkItemMutationErrorResponse, deleteWorkItemResponse } from '../mock_data';

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

  Vue.use(VueApollo);

  const workItemId = 'gid://gitlab/WorkItem/1';
  const hideModal = jest.fn();
  const GlModal = {
    template: `
    <div>
      <slot></slot>
    </div>
  `,
    methods: {
      hide: hideModal,
    },
  };

  const findModal = () => wrapper.findComponent(GlModal);
  const findAlert = () => wrapper.findComponent(GlAlert);
  const findWorkItemDetail = () => wrapper.findComponent(WorkItemDetail);

  const createComponent = ({
    deleteWorkItemMutationHandler = jest.fn().mockResolvedValue(deleteWorkItemResponse),
  } = {}) => {
    const apolloProvider = createMockApollo([
      [deleteWorkItemMutation, deleteWorkItemMutationHandler],
    ]);

    wrapper = shallowMount(WorkItemDetailModal, {
      apolloProvider,
      propsData: {
        workItemId,
        workItemIid: '1',
      },
      provide: {
        fullPath: 'group/project',
      },
      stubs: {
        GlModal,
        WorkItemDetail: stubComponent(WorkItemDetail),
      },
    });
  };

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

    expect(findWorkItemDetail().props()).toEqual({
      isModal: true,
      workItemIid: '1',
    });
  });

  it('renders alert if there is an error', async () => {
    createComponent({
      deleteWorkItemMutationHandler: jest.fn().mockRejectedValue({ message: 'message' }),
    });

    findWorkItemDetail().vm.$emit('deleteWorkItem');
    await waitForPromises();

    expect(findAlert().exists()).toBe(true);
  });

  it('does not render alert if there is no error', () => {
    createComponent();

    expect(findAlert().exists()).toBe(false);
  });

  it('dismisses the alert on `dismiss` emitted event', async () => {
    createComponent({
      deleteWorkItemMutationHandler: jest.fn().mockRejectedValue({ message: 'message' }),
    });

    findWorkItemDetail().vm.$emit('deleteWorkItem');
    await waitForPromises();

    findAlert().vm.$emit('dismiss');
    await nextTick();

    expect(findAlert().exists()).toBe(false);
  });

  it('emits `close` event on hiding the modal', () => {
    createComponent();
    findModal().vm.$emit('hide');

    expect(wrapper.emitted('close')).toHaveLength(1);
  });

  it('hides the modal when WorkItemDetail emits `close` event', () => {
    createComponent();

    findWorkItemDetail().vm.$emit('close');

    expect(hideModal).toHaveBeenCalled();
  });

  it('updates the work item when WorkItemDetail emits `update-modal` event', async () => {
    createComponent();

    findWorkItemDetail().vm.$emit('update-modal', undefined, { iid: 'updatedIid' });
    await nextTick();

    expect(findWorkItemDetail().props('workItemIid')).toBe('updatedIid');
  });

  describe('delete work item', () => {
    it('emits workItemDeleted and closes modal', async () => {
      const mutationMock = jest.fn().mockResolvedValue(deleteWorkItemResponse);
      createComponent({ deleteWorkItemMutationHandler: mutationMock });

      findWorkItemDetail().vm.$emit('deleteWorkItem');
      await waitForPromises();

      expect(wrapper.emitted('workItemDeleted')).toEqual([[workItemId]]);
      expect(hideModal).toHaveBeenCalled();
      expect(mutationMock).toHaveBeenCalledWith({ input: { id: workItemId } });
    });

    it.each`
      errorType                              | mutationMock                                                        | errorMessage
      ${'an error in the mutation response'} | ${jest.fn().mockResolvedValue(deleteWorkItemMutationErrorResponse)} | ${'Error'}
      ${'a network error'}                   | ${jest.fn().mockRejectedValue(new Error('GraphQL networkError'))}   | ${'GraphQL networkError'}
    `('shows an error message when there is $errorType', async ({ mutationMock, errorMessage }) => {
      createComponent({ deleteWorkItemMutationHandler: mutationMock });

      findWorkItemDetail().vm.$emit('deleteWorkItem');
      await waitForPromises();

      expect(wrapper.emitted('workItemDeleted')).toBeUndefined();
      expect(hideModal).not.toHaveBeenCalled();
      expect(findAlert().text()).toBe(errorMessage);
    });
  });
});