actions_spec.js 7.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
  describe('async methods', () => {
    const interceptor = (request, next) => {
133 134 135 136 137
      next(
        request.respondWith(JSON.stringify({}), {
          status: 200,
        }),
      );
138 139 140 141 142 143 144 145 146 147 148
    };

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

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

    describe('closeIssue', () => {
149 150 151
      it('sets state as closed', done => {
        store
          .dispatch('closeIssue', { notesData: { closeIssuePath: '' } })
152 153
          .then(() => {
            expect(store.state.noteableData.state).toEqual('closed');
154
            expect(store.state.isToggleStateButtonLoading).toEqual(false);
155 156 157 158 159 160 161
            done();
          })
          .catch(done.fail);
      });
    });

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

  describe('emitStateChangedEvent', () => {
    it('emits an event on the document', () => {
177
      document.addEventListener('issuable_vue_app:change', event => {
178
        expect(event.detail.data).toEqual({ id: '1', state: 'closed' });
F
Filipa Lacerda 已提交
179
        expect(event.detail.isClosed).toEqual(false);
180 181 182 183 184 185
      });

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

186
  describe('toggleStateButtonLoading', () => {
187 188 189 190 191 192
    it('should set loading as true', done => {
      testAction(
        actions.toggleStateButtonLoading,
        true,
        {},
        [{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: true }],
193
        [],
194 195
        done,
      );
196 197
    });

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

210
  describe('toggleIssueLocalState', () => {
211
    it('sets issue state as closed', done => {
212
      testAction(actions.toggleIssueLocalState, 'closed', {}, [{ type: 'CLOSE_ISSUE' }], [], done);
213 214
    });

215
    it('sets issue state as reopened', done => {
F
Felipe Artur 已提交
216 217 218 219 220 221 222 223
      testAction(
        actions.toggleIssueLocalState,
        'reopened',
        {},
        [{ type: 'REOPEN_ISSUE' }],
        [],
        done,
      );
224 225
    });
  });
226 227

  describe('poll', () => {
228
    beforeEach(done => {
229 230 231 232
      jasmine.clock().install();

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

233 234
      store
        .dispatch('setNotesData', notesDataMock)
235 236 237 238 239 240 241 242
        .then(done)
        .catch(done.fail);
    });

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

243
    it('calls service with last fetched state', done => {
244
      const interceptor = (request, next) => {
245 246 247 248 249 250 251 252 253 254 255 256 257 258
        next(
          request.respondWith(
            JSON.stringify({
              notes: [],
              last_fetched_at: '123456',
            }),
            {
              status: 200,
              headers: {
                'poll-interval': '1000',
              },
            },
          ),
        );
259 260 261 262 263
      };

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

264 265
      store
        .dispatch('poll')
266 267
        .then(() => new Promise(resolve => requestAnimationFrame(resolve)))
        .then(() => {
F
Felipe Artur 已提交
268
          expect(Vue.http.get).toHaveBeenCalled();
269 270 271 272
          expect(store.state.lastFetchedAt).toBe('123456');

          jasmine.clock().tick(1500);
        })
273 274 275 276 277 278
        .then(
          () =>
            new Promise(resolve => {
              requestAnimationFrame(resolve);
            }),
        )
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
        .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);
    });
  });
F
Filipa Lacerda 已提交
294
});