BigW Consortium Gitlab

app_spec.js 9.54 KB
Newer Older
Regis Boudinot committed
1
import Vue from 'vue';
2 3
import '~/render_math';
import '~/render_gfm';
4
import issuableApp from '~/issue_show/components/app.vue';
5
import eventHub from '~/issue_show/event_hub';
6
import issueShowData from '../mock_data';
7

8 9 10 11
function formatText(text) {
  return text.trim().replace(/\s\s+/g, ' ');
}

12
describe('Issuable output', () => {
Filipa Lacerda committed
13 14
  let requestData = issueShowData.initialRequest;

Regis committed
15 16
  document.body.innerHTML = '<span id="task_status"></span>';

Filipa Lacerda committed
17 18 19 20 21 22
  const interceptor = (request, next) => {
    next(request.respondWith(JSON.stringify(requestData), {
      status: 200,
    }));
  };

23
  let vm;
Regis Boudinot committed
24

Filipa Lacerda committed
25
  beforeEach((done) => {
26
    spyOn(eventHub, '$emit');
27 28

    const IssuableDescriptionComponent = Vue.extend(issuableApp);
29

Filipa Lacerda committed
30 31 32
    requestData = issueShowData.initialRequest;
    Vue.http.interceptors.push(interceptor);

33
    vm = new IssuableDescriptionComponent({
Regis Boudinot committed
34
      propsData: {
35
        canUpdate: true,
36
        canDestroy: true,
37
        canMove: true,
38
        endpoint: '/gitlab-org/gitlab-shell/issues/9/realtime_changes',
39
        issuableRef: '#1',
40 41
        initialTitleHtml: '',
        initialTitleText: '',
42 43
        initialDescriptionHtml: '',
        initialDescriptionText: '',
44 45
        markdownPreviewUrl: '/',
        markdownDocs: '/',
46
        projectsAutocompleteUrl: '/',
47
        isConfidential: false,
48 49
        projectNamespace: '/',
        projectPath: '/',
Regis Boudinot committed
50 51
      },
    }).$mount();
Filipa Lacerda committed
52 53

    setTimeout(done);
54
  });
Regis Boudinot committed
55

Filipa Lacerda committed
56 57
  afterEach(() => {
    Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
58

Filipa Lacerda committed
59 60 61 62
    vm.poll.stop();
  });

  it('should render a title/description/edited and update title/description/edited on update', (done) => {
63 64 65 66 67 68
    let editedText;
    Vue.nextTick()
    .then(() => {
      editedText = vm.$el.querySelector('.edited-text');
    })
    .then(() => {
69
      expect(document.querySelector('title').innerText).toContain('this is a title (#1)');
70 71
      expect(vm.$el.querySelector('.title').innerHTML).toContain('<p>this is a title</p>');
      expect(vm.$el.querySelector('.wiki').innerHTML).toContain('<p>this is a description!</p>');
72
      expect(vm.$el.querySelector('.js-task-list-field').value).toContain('this is a description');
73 74 75
      expect(formatText(editedText.innerText)).toMatch(/Edited[\s\S]+?by Some User/);
      expect(editedText.querySelector('.author_link').href).toMatch(/\/some_user$/);
      expect(editedText.querySelector('time')).toBeTruthy();
76 77
    })
    .then(() => {
Filipa Lacerda committed
78 79
      requestData = issueShowData.secondRequest;
      vm.poll.makeRequest();
80
    })
Filipa Lacerda committed
81
    .then(() => new Promise(resolve => setTimeout(resolve)))
82 83 84 85 86 87 88 89 90 91 92 93
    .then(() => {
      expect(document.querySelector('title').innerText).toContain('2 (#1)');
      expect(vm.$el.querySelector('.title').innerHTML).toContain('<p>2</p>');
      expect(vm.$el.querySelector('.wiki').innerHTML).toContain('<p>42</p>');
      expect(vm.$el.querySelector('.js-task-list-field').value).toContain('42');
      expect(vm.$el.querySelector('.edited-text')).toBeTruthy();
      expect(formatText(vm.$el.querySelector('.edited-text').innerText)).toMatch(/Edited[\s\S]+?by Other User/);
      expect(editedText.querySelector('.author_link').href).toMatch(/\/other_user$/);
      expect(editedText.querySelector('time')).toBeTruthy();
    })
    .then(done)
    .catch(done.fail);
Regis Boudinot committed
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
  it('shows actions if permissions are correct', (done) => {
    vm.showForm = true;

    Vue.nextTick(() => {
      expect(
        vm.$el.querySelector('.btn'),
      ).not.toBeNull();

      done();
    });
  });

  it('does not show actions if permissions are incorrect', (done) => {
    vm.showForm = true;
    vm.canUpdate = false;

    Vue.nextTick(() => {
      expect(
        vm.$el.querySelector('.btn'),
      ).toBeNull();

      done();
    });
  });

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  it('does not update formState if form is already open', (done) => {
    vm.openForm();

    vm.state.titleText = 'testing 123';

    vm.openForm();

    Vue.nextTick(() => {
      expect(
        vm.store.formState.title,
      ).not.toBe('testing 123');

      done();
    });
  });

137
  describe('updateIssuable', () => {
138
    it('fetches new data after update', (done) => {
139
      spyOn(vm.service, 'getData').and.callThrough();
140 141 142 143 144
      spyOn(vm.service, 'updateIssuable').and.callFake(() => new Promise((resolve) => {
        resolve({
          json() {
            return {
              confidential: false,
145
              web_url: location.pathname,
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
            };
          },
        });
      }));

      vm.updateIssuable();

      setTimeout(() => {
        expect(
          vm.service.getData,
        ).toHaveBeenCalled();

        done();
      });
    });

162 163 164 165 166 167 168
    it('reloads the page if the confidential status has changed', (done) => {
      spyOn(gl.utils, 'visitUrl');
      spyOn(vm.service, 'updateIssuable').and.callFake(() => new Promise((resolve) => {
        resolve({
          json() {
            return {
              confidential: true,
169
              web_url: location.pathname,
170 171 172 173
            };
          },
        });
      }));
174

175
      vm.updateIssuable();
176

177 178 179
      setTimeout(() => {
        expect(
          gl.utils.visitUrl,
Phil Hughes committed
180
        ).toHaveBeenCalledWith(location.pathname);
181

182 183
        done();
      });
184 185
    });

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    it('correctly updates issuable data', (done) => {
      spyOn(vm.service, 'updateIssuable').and.callFake(() => new Promise((resolve) => {
        resolve();
      }));

      vm.updateIssuable();

      setTimeout(() => {
        expect(
          vm.service.updateIssuable,
        ).toHaveBeenCalledWith(vm.formState);
        expect(
          eventHub.$emit,
        ).toHaveBeenCalledWith('close.form');

        done();
      });
    });

205 206
    it('does not redirect if issue has not moved', (done) => {
      spyOn(gl.utils, 'visitUrl');
207 208 209 210
      spyOn(vm.service, 'updateIssuable').and.callFake(() => new Promise((resolve) => {
        resolve({
          json() {
            return {
211
              web_url: location.pathname,
Phil Hughes committed
212 213 214 215
              confidential: vm.isConfidential,
            };
          },
        });
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      }));

      vm.updateIssuable();

      setTimeout(() => {
        expect(
          gl.utils.visitUrl,
        ).not.toHaveBeenCalled();

        done();
      });
    });

    it('redirects if issue is moved', (done) => {
      spyOn(gl.utils, 'visitUrl');
      spyOn(vm.service, 'updateIssuable').and.callFake(() => new Promise((resolve) => {
        resolve({
          json() {
            return {
235
              web_url: '/testing-issue-move',
Phil Hughes committed
236
              confidential: vm.isConfidential,
237 238 239 240 241 242 243 244 245
            };
          },
        });
      }));

      vm.updateIssuable();

      setTimeout(() => {
        expect(
246 247
          gl.utils.visitUrl,
        ).toHaveBeenCalledWith('/testing-issue-move');
248 249 250 251 252

        done();
      });
    });

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    it('does not update issuable if project move confirm is false', (done) => {
      spyOn(window, 'confirm').and.returnValue(false);
      spyOn(vm.service, 'updateIssuable');

      vm.store.formState.move_to_project_id = 1;

      vm.updateIssuable();

      setTimeout(() => {
        expect(
          vm.service.updateIssuable,
        ).not.toHaveBeenCalled();

        done();
      });
    });

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    it('closes form on error', (done) => {
      spyOn(window, 'Flash').and.callThrough();
      spyOn(vm.service, 'updateIssuable').and.callFake(() => new Promise((resolve, reject) => {
        reject();
      }));

      vm.updateIssuable();

      setTimeout(() => {
        expect(
          eventHub.$emit,
        ).toHaveBeenCalledWith('close.form');
        expect(
          window.Flash,
        ).toHaveBeenCalledWith('Error updating issue');

        done();
      });
    });
  });

  describe('deleteIssuable', () => {
    it('changes URL when deleted', (done) => {
      spyOn(gl.utils, 'visitUrl');
      spyOn(vm.service, 'deleteIssuable').and.callFake(() => new Promise((resolve) => {
        resolve({
296
          json() {
297
            return { web_url: '/test' };
298
          },
299 300 301 302 303 304 305 306 307 308 309 310 311 312
        });
      }));

      vm.deleteIssuable();

      setTimeout(() => {
        expect(
          gl.utils.visitUrl,
        ).toHaveBeenCalledWith('/test');

        done();
      });
    });

313
    it('stops polling when deleting', (done) => {
314
      spyOn(gl.utils, 'visitUrl');
Filipa Lacerda committed
315
      spyOn(vm.poll, 'stop').and.callThrough();
316 317
      spyOn(vm.service, 'deleteIssuable').and.callFake(() => new Promise((resolve) => {
        resolve({
318
          json() {
319
            return { web_url: '/test' };
320
          },
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        });
      }));

      vm.deleteIssuable();

      setTimeout(() => {
        expect(
          vm.poll.stop,
        ).toHaveBeenCalledWith();

        done();
      });
    });

    it('closes form on error', (done) => {
      spyOn(window, 'Flash').and.callThrough();
      spyOn(vm.service, 'deleteIssuable').and.callFake(() => new Promise((resolve, reject) => {
        reject();
      }));

      vm.deleteIssuable();

      setTimeout(() => {
        expect(
          eventHub.$emit,
        ).toHaveBeenCalledWith('close.form');
        expect(
          window.Flash,
        ).toHaveBeenCalledWith('Error deleting issue');

        done();
      });
    });
  });
355 356 357 358 359 360 361

  describe('open form', () => {
    it('shows locked warning if form is open & data is different', (done) => {
      Vue.nextTick()
        .then(() => {
          vm.openForm();

Filipa Lacerda committed
362 363
          requestData = issueShowData.secondRequest;
          vm.poll.makeRequest();
364
        })
Filipa Lacerda committed
365
        .then(() => new Promise(resolve => setTimeout(resolve)))
366 367 368 369 370 371 372 373 374
        .then(() => {
          expect(
            vm.formState.lockedWarningVisible,
          ).toBeTruthy();

          expect(
            vm.$el.querySelector('.alert'),
          ).not.toBeNull();
        })
375
        .then(done)
376 377 378
        .catch(done.fail);
    });
  });
Regis Boudinot committed
379
});