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

board_new_issue_deprecated_spec.js « boards « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3beaf870bf58becdb4c436d62f4775d204363e0c (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* global List */

import { mount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import Vue from 'vue';
import Vuex from 'vuex';
import boardNewIssue from '~/boards/components/board_new_issue_deprecated.vue';
import boardsStore from '~/boards/stores/boards_store';
import axios from '~/lib/utils/axios_utils';

import '~/boards/models/list';
import { listObj, boardsMockInterceptor } from './mock_data';

Vue.use(Vuex);

describe('Issue boards new issue form', () => {
  let wrapper;
  let vm;
  let list;
  let mock;
  let newIssueMock;
  const promiseReturn = {
    data: {
      iid: 100,
    },
  };

  const submitIssue = () => {
    const dummySubmitEvent = {
      preventDefault() {},
    };
    wrapper.vm.$refs.submitButton = wrapper.find({ ref: 'submitButton' });
    return wrapper.vm.submit(dummySubmitEvent);
  };

  beforeEach(() => {
    const BoardNewIssueComp = Vue.extend(boardNewIssue);

    mock = new MockAdapter(axios);
    mock.onAny().reply(boardsMockInterceptor);

    boardsStore.create();

    list = new List(listObj);

    newIssueMock = Promise.resolve(promiseReturn);
    jest.spyOn(list, 'newIssue').mockImplementation(() => newIssueMock);

    const store = new Vuex.Store({
      getters: { isGroupBoard: () => false },
    });

    wrapper = mount(BoardNewIssueComp, {
      propsData: {
        disabled: false,
        list,
      },
      store,
      provide: {
        groupId: null,
      },
    });

    vm = wrapper.vm;

    return Vue.nextTick();
  });

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

  it('calls submit if submit button is clicked', () => {
    jest.spyOn(wrapper.vm, 'submit').mockImplementation();
    vm.title = 'Testing Title';

    return Vue.nextTick()
      .then(submitIssue)
      .then(() => {
        expect(wrapper.vm.submit).toHaveBeenCalled();
      });
  });

  it('disables submit button if title is empty', () => {
    expect(wrapper.find({ ref: 'submitButton' }).props().disabled).toBe(true);
  });

  it('enables submit button if title is not empty', () => {
    wrapper.setData({ title: 'Testing Title' });

    return Vue.nextTick().then(() => {
      expect(wrapper.find({ ref: 'input' }).element.value).toBe('Testing Title');
      expect(wrapper.find({ ref: 'submitButton' }).props().disabled).toBe(false);
    });
  });

  it('clears title after clicking cancel', () => {
    wrapper.find({ ref: 'cancelButton' }).trigger('click');

    return Vue.nextTick().then(() => {
      expect(vm.title).toBe('');
    });
  });

  it('does not create new issue if title is empty', () => {
    return submitIssue().then(() => {
      expect(list.newIssue).not.toHaveBeenCalled();
    });
  });

  describe('submit success', () => {
    it('creates new issue', () => {
      wrapper.setData({ title: 'create issue' });

      return Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(list.newIssue).toHaveBeenCalled();
        });
    });

    it('enables button after submit', () => {
      jest.spyOn(wrapper.vm, 'submit').mockImplementation();
      wrapper.setData({ title: 'create issue' });

      return Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(wrapper.vm.$refs.submitButton.props().disabled).toBe(false);
        });
    });

    it('clears title after submit', () => {
      wrapper.setData({ title: 'create issue' });

      return Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(vm.title).toBe('');
        });
    });

    it('sets detail issue after submit', () => {
      expect(boardsStore.detail.issue.title).toBe(undefined);
      wrapper.setData({ title: 'create issue' });

      return Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(boardsStore.detail.issue.title).toBe('create issue');
        });
    });

    it('sets detail list after submit', () => {
      wrapper.setData({ title: 'create issue' });

      return Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(boardsStore.detail.list.id).toBe(list.id);
        });
    });

    it('sets detail weight after submit', () => {
      boardsStore.weightFeatureAvailable = true;
      wrapper.setData({ title: 'create issue' });

      return Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(boardsStore.detail.list.weight).toBe(list.weight);
        });
    });

    it('does not set detail weight after submit', () => {
      boardsStore.weightFeatureAvailable = false;
      wrapper.setData({ title: 'create issue' });

      return Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(boardsStore.detail.list.weight).toBe(list.weight);
        });
    });
  });

  describe('submit error', () => {
    beforeEach(() => {
      newIssueMock = Promise.reject(new Error('My hovercraft is full of eels!'));
      vm.title = 'error';
    });

    it('removes issue', () => {
      const lengthBefore = list.issues.length;
      return Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(list.issues.length).toBe(lengthBefore);
        });
    });

    it('shows error', () => {
      return Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(vm.error).toBe(true);
        });
    });
  });
});