actions_spec.js 8.0 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,
      );
    });
  });
F
Filipa Lacerda 已提交
320
});