actions_spec.js 14.1 KB
Newer Older
1
import Vue from 'vue';
2
import $ from 'jquery';
3
import _ from 'underscore';
4
import { headersInterceptor } from 'spec/helpers/vue_resource_helper';
F
Filipa Lacerda 已提交
5
import * as actions from '~/notes/stores/actions';
F
Felipe Artur 已提交
6
import createStore from '~/notes/stores';
7
import mrWidgetEventHub from '~/vue_merge_request_widget/event_hub';
8
import testAction from '../../helpers/vuex_action_helper';
9
import { resetStore } from '../helpers';
10 11 12 13 14 15 16
import {
  discussionMock,
  notesDataMock,
  userDataMock,
  noteableDataMock,
  individualNote,
} from '../mock_data';
17

F
Filipa Lacerda 已提交
18
describe('Actions Notes Store', () => {
F
Felipe Artur 已提交
19 20 21 22 23 24
  let store;

  beforeEach(() => {
    store = createStore();
  });

25 26 27 28
  afterEach(() => {
    resetStore(store);
  });

F
Filipa Lacerda 已提交
29
  describe('setNotesData', () => {
30 31 32 33 34 35
    it('should set received notes data', done => {
      testAction(
        actions.setNotesData,
        notesDataMock,
        { notesData: {} },
        [{ type: 'SET_NOTES_DATA', payload: notesDataMock }],
36
        [],
37 38
        done,
      );
F
Filipa Lacerda 已提交
39 40 41
    });
  });

S
Simon Knox 已提交
42
  describe('setNoteableData', () => {
43 44 45 46 47 48
    it('should set received issue data', done => {
      testAction(
        actions.setNoteableData,
        noteableDataMock,
        { noteableData: {} },
        [{ type: 'SET_NOTEABLE_DATA', payload: noteableDataMock }],
49
        [],
50 51
        done,
      );
52
    });
F
Filipa Lacerda 已提交
53 54 55
  });

  describe('setUserData', () => {
56 57 58 59 60 61
    it('should set received user data', done => {
      testAction(
        actions.setUserData,
        userDataMock,
        { userData: {} },
        [{ type: 'SET_USER_DATA', payload: userDataMock }],
62
        [],
63 64
        done,
      );
65
    });
F
Filipa Lacerda 已提交
66 67 68
  });

  describe('setLastFetchedAt', () => {
69 70 71 72 73 74
    it('should set received timestamp', done => {
      testAction(
        actions.setLastFetchedAt,
        'timestamp',
        { lastFetchedAt: {} },
        [{ type: 'SET_LAST_FETCHED_AT', payload: 'timestamp' }],
75
        [],
76 77
        done,
      );
78
    });
F
Filipa Lacerda 已提交
79 80 81
  });

  describe('setInitialNotes', () => {
82 83 84 85 86
    it('should set initial notes', done => {
      testAction(
        actions.setInitialNotes,
        [individualNote],
        { notes: [] },
F
Felipe Artur 已提交
87
        [{ type: 'SET_INITIAL_DISCUSSIONS', payload: [individualNote] }],
88
        [],
89 90
        done,
      );
F
Filipa Lacerda 已提交
91 92 93 94
    });
  });

  describe('setTargetNoteHash', () => {
95 96 97 98 99 100
    it('should set target note hash', done => {
      testAction(
        actions.setTargetNoteHash,
        'hash',
        { notes: [] },
        [{ type: 'SET_TARGET_NOTE_HASH', payload: 'hash' }],
101
        [],
102 103
        done,
      );
104
    });
F
Filipa Lacerda 已提交
105 106 107
  });

  describe('toggleDiscussion', () => {
108 109 110 111 112 113
    it('should toggle discussion', done => {
      testAction(
        actions.toggleDiscussion,
        { discussionId: discussionMock.id },
        { notes: [discussionMock] },
        [{ type: 'TOGGLE_DISCUSSION', payload: { discussionId: discussionMock.id } }],
114
        [],
115 116
        done,
      );
F
Filipa Lacerda 已提交
117 118
    });
  });
119

F
Felipe Artur 已提交
120 121 122 123 124 125 126
  describe('expandDiscussion', () => {
    it('should expand discussion', done => {
      testAction(
        actions.expandDiscussion,
        { discussionId: discussionMock.id },
        { notes: [discussionMock] },
        [{ type: 'EXPAND_DISCUSSION', payload: { discussionId: discussionMock.id } }],
127
        [{ type: 'diffs/renderFileForDiscussionId', payload: discussionMock.id }],
F
Felipe Artur 已提交
128 129 130 131 132
        done,
      );
    });
  });

133 134 135 136 137 138 139 140 141 142 143 144 145
  describe('collapseDiscussion', () => {
    it('should commit collapse discussion', done => {
      testAction(
        actions.collapseDiscussion,
        { discussionId: discussionMock.id },
        { notes: [discussionMock] },
        [{ type: 'COLLAPSE_DISCUSSION', payload: { discussionId: discussionMock.id } }],
        [],
        done,
      );
    });
  });

146 147
  describe('async methods', () => {
    const interceptor = (request, next) => {
148 149 150 151 152
      next(
        request.respondWith(JSON.stringify({}), {
          status: 200,
        }),
      );
153 154 155 156 157 158 159 160 161 162 163
    };

    beforeEach(() => {
      Vue.http.interceptors.push(interceptor);
    });

    afterEach(() => {
      Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
    });

    describe('closeIssue', () => {
164 165 166
      it('sets state as closed', done => {
        store
          .dispatch('closeIssue', { notesData: { closeIssuePath: '' } })
167 168
          .then(() => {
            expect(store.state.noteableData.state).toEqual('closed');
169
            expect(store.state.isToggleStateButtonLoading).toEqual(false);
170 171 172 173 174 175 176
            done();
          })
          .catch(done.fail);
      });
    });

    describe('reopenIssue', () => {
177 178 179
      it('sets state as reopened', done => {
        store
          .dispatch('reopenIssue', { notesData: { reopenIssuePath: '' } })
180 181
          .then(() => {
            expect(store.state.noteableData.state).toEqual('reopened');
182
            expect(store.state.isToggleStateButtonLoading).toEqual(false);
183 184 185 186 187 188 189 190 191
            done();
          })
          .catch(done.fail);
      });
    });
  });

  describe('emitStateChangedEvent', () => {
    it('emits an event on the document', () => {
192
      document.addEventListener('issuable_vue_app:change', event => {
193
        expect(event.detail.data).toEqual({ id: '1', state: 'closed' });
F
Filipa Lacerda 已提交
194
        expect(event.detail.isClosed).toEqual(false);
195 196 197 198 199 200
      });

      store.dispatch('emitStateChangedEvent', { id: '1', state: 'closed' });
    });
  });

201
  describe('toggleStateButtonLoading', () => {
202 203 204 205 206 207
    it('should set loading as true', done => {
      testAction(
        actions.toggleStateButtonLoading,
        true,
        {},
        [{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: true }],
208
        [],
209 210
        done,
      );
211 212
    });

213 214 215 216 217 218
    it('should set loading as false', done => {
      testAction(
        actions.toggleStateButtonLoading,
        false,
        {},
        [{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: false }],
219
        [],
220 221
        done,
      );
222 223 224
    });
  });

225
  describe('toggleIssueLocalState', () => {
226
    it('sets issue state as closed', done => {
227
      testAction(actions.toggleIssueLocalState, 'closed', {}, [{ type: 'CLOSE_ISSUE' }], [], done);
228 229
    });

230
    it('sets issue state as reopened', done => {
F
Felipe Artur 已提交
231 232 233 234 235 236 237 238
      testAction(
        actions.toggleIssueLocalState,
        'reopened',
        {},
        [{ type: 'REOPEN_ISSUE' }],
        [],
        done,
      );
239 240
    });
  });
241 242

  describe('poll', () => {
243
    beforeEach(done => {
244 245 246 247
      jasmine.clock().install();

      spyOn(Vue.http, 'get').and.callThrough();

248 249
      store
        .dispatch('setNotesData', notesDataMock)
250 251 252 253 254 255 256 257
        .then(done)
        .catch(done.fail);
    });

    afterEach(() => {
      jasmine.clock().uninstall();
    });

258
    it('calls service with last fetched state', done => {
259
      const interceptor = (request, next) => {
260 261 262 263 264 265 266 267 268 269 270 271 272 273
        next(
          request.respondWith(
            JSON.stringify({
              notes: [],
              last_fetched_at: '123456',
            }),
            {
              status: 200,
              headers: {
                'poll-interval': '1000',
              },
            },
          ),
        );
274 275 276 277 278
      };

      Vue.http.interceptors.push(interceptor);
      Vue.http.interceptors.push(headersInterceptor);

279 280
      store
        .dispatch('poll')
281 282
        .then(() => new Promise(resolve => requestAnimationFrame(resolve)))
        .then(() => {
F
Felipe Artur 已提交
283
          expect(Vue.http.get).toHaveBeenCalled();
284 285 286 287
          expect(store.state.lastFetchedAt).toBe('123456');

          jasmine.clock().tick(1500);
        })
288 289 290 291 292 293
        .then(
          () =>
            new Promise(resolve => {
              requestAnimationFrame(resolve);
            }),
        )
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        .then(() => {
          expect(Vue.http.get.calls.count()).toBe(2);
          expect(Vue.http.get.calls.mostRecent().args[1].headers).toEqual({
            'X-Last-Fetched-At': '123456',
          });
        })
        .then(() => store.dispatch('stopPolling'))
        .then(() => {
          Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
          Vue.http.interceptors = _.without(Vue.http.interceptors, headersInterceptor);
        })
        .then(done)
        .catch(done.fail);
    });
  });
309 310 311 312 313 314 315 316 317 318 319 320 321

  describe('setNotesFetchedState', () => {
    it('should set notes fetched state', done => {
      testAction(
        actions.setNotesFetchedState,
        true,
        {},
        [{ type: 'SET_NOTES_FETCHED_STATE', payload: true }],
        [],
        done,
      );
    });
  });
322 323 324 325 326 327 328 329 330 331 332 333

  describe('deleteNote', () => {
    const interceptor = (request, next) => {
      next(
        request.respondWith(JSON.stringify({}), {
          status: 200,
        }),
      );
    };

    beforeEach(() => {
      Vue.http.interceptors.push(interceptor);
334 335

      $('body').attr('data-page', '');
336 337 338 339
    });

    afterEach(() => {
      Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
340 341

      $('body').attr('data-page', '');
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    });

    it('commits DELETE_NOTE and dispatches updateMergeRequestWidget', done => {
      const note = { path: `${gl.TEST_HOST}`, id: 1 };

      testAction(
        actions.deleteNote,
        note,
        store.state,
        [
          {
            type: 'DELETE_NOTE',
            payload: note,
          },
        ],
        [
          {
            type: 'updateMergeRequestWidget',
          },
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
          {
            type: 'updateResolvableDiscussonsCounts',
          },
        ],
        done,
      );
    });

    it('dispatches removeDiscussionsFromDiff on merge request page', done => {
      const note = { path: `${gl.TEST_HOST}`, id: 1 };

      $('body').attr('data-page', 'projects:merge_requests:show');

      testAction(
        actions.deleteNote,
        note,
        store.state,
        [
          {
            type: 'DELETE_NOTE',
            payload: note,
          },
        ],
        [
          {
            type: 'updateMergeRequestWidget',
          },
          {
            type: 'updateResolvableDiscussonsCounts',
          },
          {
            type: 'diffs/removeDiscussionsFromDiff',
          },
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
        ],
        done,
      );
    });
  });

  describe('createNewNote', () => {
    describe('success', () => {
      const res = {
        id: 1,
        valid: true,
      };
      const interceptor = (request, next) => {
        next(
          request.respondWith(JSON.stringify(res), {
            status: 200,
          }),
        );
      };

      beforeEach(() => {
        Vue.http.interceptors.push(interceptor);
      });

      afterEach(() => {
        Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
      });

      it('commits ADD_NEW_NOTE and dispatches updateMergeRequestWidget', done => {
        testAction(
          actions.createNewNote,
          { endpoint: `${gl.TEST_HOST}`, data: {} },
          store.state,
          [
            {
              type: 'ADD_NEW_NOTE',
              payload: res,
            },
          ],
          [
            {
              type: 'updateMergeRequestWidget',
            },
P
Phil Hughes 已提交
437 438 439
            {
              type: 'startTaskList',
            },
440 441 442
            {
              type: 'updateResolvableDiscussonsCounts',
            },
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
          ],
          done,
        );
      });
    });

    describe('error', () => {
      const res = {
        errors: ['error'],
      };
      const interceptor = (request, next) => {
        next(
          request.respondWith(JSON.stringify(res), {
            status: 200,
          }),
        );
      };

      beforeEach(() => {
        Vue.http.interceptors.push(interceptor);
      });

      afterEach(() => {
        Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
      });

      it('does not commit ADD_NEW_NOTE or dispatch updateMergeRequestWidget', done => {
        testAction(
          actions.createNewNote,
          { endpoint: `${gl.TEST_HOST}`, data: {} },
          store.state,
          [],
          [],
          done,
        );
      });
    });
  });

  describe('toggleResolveNote', () => {
    const res = {
      resolved: true,
    };
    const interceptor = (request, next) => {
      next(
        request.respondWith(JSON.stringify(res), {
          status: 200,
        }),
      );
    };

    beforeEach(() => {
      Vue.http.interceptors.push(interceptor);
    });

    afterEach(() => {
      Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
    });

    describe('as note', () => {
      it('commits UPDATE_NOTE and dispatches updateMergeRequestWidget', done => {
        testAction(
          actions.toggleResolveNote,
          { endpoint: `${gl.TEST_HOST}`, isResolved: true, discussion: false },
          store.state,
          [
            {
              type: 'UPDATE_NOTE',
              payload: res,
            },
          ],
          [
515 516 517
            {
              type: 'updateResolvableDiscussonsCounts',
            },
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
            {
              type: 'updateMergeRequestWidget',
            },
          ],
          done,
        );
      });
    });

    describe('as discussion', () => {
      it('commits UPDATE_DISCUSSION and dispatches updateMergeRequestWidget', done => {
        testAction(
          actions.toggleResolveNote,
          { endpoint: `${gl.TEST_HOST}`, isResolved: true, discussion: true },
          store.state,
          [
            {
              type: 'UPDATE_DISCUSSION',
              payload: res,
            },
          ],
          [
540 541 542
            {
              type: 'updateResolvableDiscussonsCounts',
            },
543 544 545 546 547 548 549 550 551 552 553 554
            {
              type: 'updateMergeRequestWidget',
            },
          ],
          done,
        );
      });
    });
  });

  describe('updateMergeRequestWidget', () => {
    it('calls mrWidget checkStatus', () => {
555
      spyOn(mrWidgetEventHub, '$emit');
556 557 558

      actions.updateMergeRequestWidget();

559
      expect(mrWidgetEventHub.$emit).toHaveBeenCalledWith('mr.discussion.updated');
560 561
    });
  });
562 563 564 565 566 567 568 569 570 571 572 573 574

  describe('setCommentsDisabled', () => {
    it('should set comments disabled state', done => {
      testAction(
        actions.setCommentsDisabled,
        true,
        null,
        [{ type: 'DISABLE_COMMENTS', payload: true }],
        [],
        done,
      );
    });
  });
575 576 577 578 579 580 581 582 583 584 585 586 587

  describe('updateResolvableDiscussonsCounts', () => {
    it('commits UPDATE_RESOLVABLE_DISCUSSIONS_COUNTS', done => {
      testAction(
        actions.updateResolvableDiscussonsCounts,
        null,
        {},
        [{ type: 'UPDATE_RESOLVABLE_DISCUSSIONS_COUNTS' }],
        [],
        done,
      );
    });
  });
F
Filipa Lacerda 已提交
588
});