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

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

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

23 24 25 26
  afterEach(() => {
    resetStore(store);
  });

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          jasmine.clock().tick(1500);
        })
286 287 288 289 290 291
        .then(
          () =>
            new Promise(resolve => {
              requestAnimationFrame(resolve);
            }),
        )
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        .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);
    });
  });
307 308 309 310 311 312 313 314 315 316 317 318 319

  describe('setNotesFetchedState', () => {
    it('should set notes fetched state', done => {
      testAction(
        actions.setNotesFetchedState,
        true,
        {},
        [{ type: 'SET_NOTES_FETCHED_STATE', payload: true }],
        [],
        done,
      );
    });
  });
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 355 356 357 358 359 360 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 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 437 438 439 440 441 442 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

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

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

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

    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',
          },
        ],
        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',
            },
          ],
          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,
            },
          ],
          [
            {
              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,
            },
          ],
          [
            {
              type: 'updateMergeRequestWidget',
            },
          ],
          done,
        );
      });
    });
  });

  describe('updateMergeRequestWidget', () => {
    it('calls mrWidget checkStatus', () => {
      gl.mrWidget = {
        checkStatus: jasmine.createSpy('checkStatus'),
      };

      actions.updateMergeRequestWidget();

      expect(gl.mrWidget.checkStatus).toHaveBeenCalled();
    });
  });
F
Filipa Lacerda 已提交
513
});