summaryrefslogtreecommitdiff
path: root/spec/javascripts/related_merge_requests/store/actions_spec.js
blob: 65e436fbb17b8dd99d6c2f6edc1e8514085feb1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import * as types from '~/related_merge_requests/store/mutation_types';
import actionsModule, * as actions from '~/related_merge_requests/store/actions';
import testAction from 'spec/helpers/vuex_action_helper';

describe('RelatedMergeRequest store actions', () => {
  let state;
  let flashSpy;
  let mock;

  beforeEach(() => {
    state = {
      apiEndpoint: '/api/related_merge_requests',
    };
    flashSpy = spyOnDependency(actionsModule, 'createFlash');
    mock = new MockAdapter(axios);
  });

  afterEach(() => {
    mock.restore();
  });

  describe('setInitialState', () => {
    it('commits types.SET_INITIAL_STATE with given props', done => {
      const props = { a: 1, b: 2 };

      testAction(
        actions.setInitialState,
        props,
        {},
        [{ type: types.SET_INITIAL_STATE, payload: props }],
        [],
        done,
      );
    });
  });

  describe('requestData', () => {
    it('commits types.REQUEST_DATA', done => {
      testAction(actions.requestData, null, {}, [{ type: types.REQUEST_DATA }], [], done);
    });
  });

  describe('receiveDataSuccess', () => {
    it('commits types.RECEIVE_DATA_SUCCESS with data', done => {
      const data = { a: 1, b: 2 };

      testAction(
        actions.receiveDataSuccess,
        data,
        {},
        [{ type: types.RECEIVE_DATA_SUCCESS, payload: data }],
        [],
        done,
      );
    });
  });

  describe('receiveDataError', () => {
    it('commits types.RECEIVE_DATA_ERROR', done => {
      testAction(
        actions.receiveDataError,
        null,
        {},
        [{ type: types.RECEIVE_DATA_ERROR }],
        [],
        done,
      );
    });
  });

  describe('fetchMergeRequests', () => {
    describe('for a successful request', () => {
      it('should dispatch success action', done => {
        const data = { a: 1 };
        mock.onGet(`${state.apiEndpoint}?per_page=100`).replyOnce(200, data, { 'x-total': 2 });

        testAction(
          actions.fetchMergeRequests,
          null,
          state,
          [],
          [{ type: 'requestData' }, { type: 'receiveDataSuccess', payload: { data, total: 2 } }],
          done,
        );
      });
    });

    describe('for a failing request', () => {
      it('should dispatch error action', done => {
        mock.onGet(`${state.apiEndpoint}?per_page=100`).replyOnce(400);

        testAction(
          actions.fetchMergeRequests,
          null,
          state,
          [],
          [{ type: 'requestData' }, { type: 'receiveDataError' }],
          () => {
            expect(flashSpy).toHaveBeenCalledTimes(1);
            expect(flashSpy).toHaveBeenCalledWith(jasmine.stringMatching('Something went wrong'));

            done();
          },
        );
      });
    });
  });
});