actions_spec.js 6.9 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';
5
import store 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', () => {
17 18 19 20
  afterEach(() => {
    resetStore(store);
  });

F
Filipa Lacerda 已提交
21
  describe('setNotesData', () => {
22 23 24 25 26 27 28 29
    it('should set received notes data', done => {
      testAction(
        actions.setNotesData,
        notesDataMock,
        { notesData: {} },
        [{ type: 'SET_NOTES_DATA', payload: notesDataMock }],
        done,
      );
F
Filipa Lacerda 已提交
30 31 32
    });
  });

S
Simon Knox 已提交
33
  describe('setNoteableData', () => {
34 35 36 37 38 39 40 41
    it('should set received issue data', done => {
      testAction(
        actions.setNoteableData,
        noteableDataMock,
        { noteableData: {} },
        [{ type: 'SET_NOTEABLE_DATA', payload: noteableDataMock }],
        done,
      );
42
    });
F
Filipa Lacerda 已提交
43 44 45
  });

  describe('setUserData', () => {
46 47 48 49 50 51 52 53
    it('should set received user data', done => {
      testAction(
        actions.setUserData,
        userDataMock,
        { userData: {} },
        [{ type: 'SET_USER_DATA', payload: userDataMock }],
        done,
      );
54
    });
F
Filipa Lacerda 已提交
55 56 57
  });

  describe('setLastFetchedAt', () => {
58 59 60 61 62 63 64 65
    it('should set received timestamp', done => {
      testAction(
        actions.setLastFetchedAt,
        'timestamp',
        { lastFetchedAt: {} },
        [{ type: 'SET_LAST_FETCHED_AT', payload: 'timestamp' }],
        done,
      );
66
    });
F
Filipa Lacerda 已提交
67 68 69
  });

  describe('setInitialNotes', () => {
70 71 72 73 74 75 76 77
    it('should set initial notes', done => {
      testAction(
        actions.setInitialNotes,
        [individualNote],
        { notes: [] },
        [{ type: 'SET_INITIAL_NOTES', payload: [individualNote] }],
        done,
      );
F
Filipa Lacerda 已提交
78 79 80 81
    });
  });

  describe('setTargetNoteHash', () => {
82 83 84 85 86 87 88 89
    it('should set target note hash', done => {
      testAction(
        actions.setTargetNoteHash,
        'hash',
        { notes: [] },
        [{ type: 'SET_TARGET_NOTE_HASH', payload: 'hash' }],
        done,
      );
90
    });
F
Filipa Lacerda 已提交
91 92 93
  });

  describe('toggleDiscussion', () => {
94 95 96 97 98 99 100 101
    it('should toggle discussion', done => {
      testAction(
        actions.toggleDiscussion,
        { discussionId: discussionMock.id },
        { notes: [discussionMock] },
        [{ type: 'TOGGLE_DISCUSSION', payload: { discussionId: discussionMock.id } }],
        done,
      );
F
Filipa Lacerda 已提交
102 103
    });
  });
104 105 106

  describe('async methods', () => {
    const interceptor = (request, next) => {
107 108 109 110 111
      next(
        request.respondWith(JSON.stringify({}), {
          status: 200,
        }),
      );
112 113 114 115 116 117 118 119 120 121 122
    };

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

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

    describe('closeIssue', () => {
123 124 125
      it('sets state as closed', done => {
        store
          .dispatch('closeIssue', { notesData: { closeIssuePath: '' } })
126 127
          .then(() => {
            expect(store.state.noteableData.state).toEqual('closed');
128
            expect(store.state.isToggleStateButtonLoading).toEqual(false);
129 130 131 132 133 134 135
            done();
          })
          .catch(done.fail);
      });
    });

    describe('reopenIssue', () => {
136 137 138
      it('sets state as reopened', done => {
        store
          .dispatch('reopenIssue', { notesData: { reopenIssuePath: '' } })
139 140
          .then(() => {
            expect(store.state.noteableData.state).toEqual('reopened');
141
            expect(store.state.isToggleStateButtonLoading).toEqual(false);
142 143 144 145 146 147 148 149 150
            done();
          })
          .catch(done.fail);
      });
    });
  });

  describe('emitStateChangedEvent', () => {
    it('emits an event on the document', () => {
151
      document.addEventListener('issuable_vue_app:change', event => {
152
        expect(event.detail.data).toEqual({ id: '1', state: 'closed' });
F
Filipa Lacerda 已提交
153
        expect(event.detail.isClosed).toEqual(false);
154 155 156 157 158 159
      });

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

160
  describe('toggleStateButtonLoading', () => {
161 162 163 164 165 166 167 168
    it('should set loading as true', done => {
      testAction(
        actions.toggleStateButtonLoading,
        true,
        {},
        [{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: true }],
        done,
      );
169 170
    });

171 172 173 174 175 176 177 178
    it('should set loading as false', done => {
      testAction(
        actions.toggleStateButtonLoading,
        false,
        {},
        [{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: false }],
        done,
      );
179 180 181
    });
  });

182
  describe('toggleIssueLocalState', () => {
183 184
    it('sets issue state as closed', done => {
      testAction(actions.toggleIssueLocalState, 'closed', {}, [{ type: 'CLOSE_ISSUE' }], done);
185 186
    });

187 188
    it('sets issue state as reopened', done => {
      testAction(actions.toggleIssueLocalState, 'reopened', {}, [{ type: 'REOPEN_ISSUE' }], done);
189 190
    });
  });
191 192

  describe('poll', () => {
193
    beforeEach(done => {
194 195 196 197
      jasmine.clock().install();

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

198 199
      store
        .dispatch('setNotesData', notesDataMock)
200 201 202 203 204 205 206 207
        .then(done)
        .catch(done.fail);
    });

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

208
    it('calls service with last fetched state', done => {
209
      const interceptor = (request, next) => {
210 211 212 213 214 215 216 217 218 219 220 221 222 223
        next(
          request.respondWith(
            JSON.stringify({
              notes: [],
              last_fetched_at: '123456',
            }),
            {
              status: 200,
              headers: {
                'poll-interval': '1000',
              },
            },
          ),
        );
224 225 226 227 228
      };

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

229 230
      store
        .dispatch('poll')
231 232 233 234 235 236 237 238 239 240 241 242 243
        .then(() => new Promise(resolve => requestAnimationFrame(resolve)))
        .then(() => {
          expect(Vue.http.get).toHaveBeenCalledWith(jasmine.anything(), {
            url: jasmine.anything(),
            method: 'get',
            headers: {
              'X-Last-Fetched-At': undefined,
            },
          });
          expect(store.state.lastFetchedAt).toBe('123456');

          jasmine.clock().tick(1500);
        })
244 245 246 247 248 249
        .then(
          () =>
            new Promise(resolve => {
              requestAnimationFrame(resolve);
            }),
        )
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        .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 已提交
265
});