summaryrefslogtreecommitdiff
path: root/spec/frontend/__helpers__/vuex_action_helper_spec.js
blob: 182aea9c1c5bbcdeac3f592b5bb5499eac347c37 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import MockAdapter from 'axios-mock-adapter';
import { TEST_HOST } from 'helpers/test_constants';
import axios from '~/lib/utils/axios_utils';
import testActionFn from './vuex_action_helper';

const testActionFnWithOptionsArg = (...args) => {
  const [action, payload, state, expectedMutations, expectedActions] = args;
  return testActionFn({ action, payload, state, expectedMutations, expectedActions });
};

describe.each([testActionFn, testActionFnWithOptionsArg])(
  'VueX test helper (testAction)',
  (testAction) => {
    let originalExpect;
    let assertion;
    let mock;

    beforeEach(() => {
      mock = new MockAdapter(axios);
      /**
       * In order to test the helper properly, we need to overwrite the Jest
       * `expect` helper.  We test that the testAction helper properly passes the
       * dispatched actions/committed mutations to the Jest helper.
       */
      originalExpect = expect;
      assertion = null;
      global.expect = (actual) => ({
        toEqual: () => {
          originalExpect(actual).toEqual(assertion);
        },
      });
    });

    afterEach(() => {
      mock.restore();
      global.expect = originalExpect;
    });

    it('properly passes state and payload to action', () => {
      const exampleState = { FOO: 12, BAR: 3 };
      const examplePayload = { BAZ: 73, BIZ: 55 };

      const action = ({ state }, payload) => {
        originalExpect(state).toEqual(exampleState);
        originalExpect(payload).toEqual(examplePayload);
      };

      assertion = { mutations: [], actions: [] };

      return testAction(action, examplePayload, exampleState);
    });

    describe('given a sync action', () => {
      it('mocks committing mutations', () => {
        const action = ({ commit }) => {
          commit('MUTATION');
        };

        assertion = { mutations: [{ type: 'MUTATION' }], actions: [] };

        return testAction(action, null, {}, assertion.mutations, assertion.actions);
      });

      it('mocks dispatching actions', () => {
        const action = ({ dispatch }) => {
          dispatch('ACTION');
        };

        assertion = { actions: [{ type: 'ACTION' }], mutations: [] };

        return testAction(action, null, {}, assertion.mutations, assertion.actions);
      });

      it('returns a promise', () => {
        assertion = { mutations: [], actions: [] };

        const promise = testAction(() => {}, null, {}, assertion.mutations, assertion.actions);

        originalExpect(promise instanceof Promise).toBe(true);

        return promise;
      });
    });

    describe('given an async action (returning a promise)', () => {
      const data = { FOO: 'BAR' };

      const asyncAction = ({ commit, dispatch }) => {
        dispatch('ACTION');

        return axios
          .get(TEST_HOST)
          .catch((error) => {
            commit('ERROR');
            throw error;
          })
          .then(() => {
            commit('SUCCESS');
            return data;
          });
      };

      it('returns original data of successful promise while checking actions/mutations', async () => {
        mock.onGet(TEST_HOST).replyOnce(200, 42);

        assertion = { mutations: [{ type: 'SUCCESS' }], actions: [{ type: 'ACTION' }] };

        const res = await testAction(asyncAction, null, {}, assertion.mutations, assertion.actions);
        originalExpect(res).toEqual(data);
      });

      it('returns original error of rejected promise while checking actions/mutations', async () => {
        mock.onGet(TEST_HOST).replyOnce(500, '');

        assertion = { mutations: [{ type: 'ERROR' }], actions: [{ type: 'ACTION' }] };

        const err = testAction(asyncAction, null, {}, assertion.mutations, assertion.actions);
        await originalExpect(err).rejects.toEqual(new Error('Request failed with status code 500'));
      });
    });

    it('works with actions not returning promises', () => {
      const data = { FOO: 'BAR' };

      const asyncAction = ({ commit, dispatch }) => {
        dispatch('ACTION');

        axios
          .get(TEST_HOST)
          .then(() => {
            commit('SUCCESS');
            return data;
          })
          .catch((error) => {
            commit('ERROR');
            throw error;
          });
      };

      mock.onGet(TEST_HOST).replyOnce(200, 42);

      assertion = { mutations: [{ type: 'SUCCESS' }], actions: [{ type: 'ACTION' }] };

      return testAction(asyncAction, null, {}, assertion.mutations, assertion.actions);
    });
  },
);