summaryrefslogtreecommitdiff
path: root/spec/frontend/ref/stores/actions_spec.js
blob: 11acec27165959b35c5047d35d2156807ebba270 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import testAction from 'helpers/vuex_action_helper';
import * as actions from '~/ref/stores/actions';
import * as types from '~/ref/stores/mutation_types';
import createState from '~/ref/stores/state';

let mockBranchesReturnValue;
let mockTagsReturnValue;
let mockCommitReturnValue;

jest.mock('~/api', () => ({
  // `__esModule: true` is required when mocking modules with default exports:
  // https://jestjs.io/docs/en/jest-object#jestmockmodulename-factory-options
  __esModule: true,
  default: {
    branches: () => mockBranchesReturnValue,
    tags: () => mockTagsReturnValue,
    commit: () => mockCommitReturnValue,
  },
}));

describe('Ref selector Vuex store actions', () => {
  let state;

  beforeEach(() => {
    state = createState();
  });

  describe('setProjectId', () => {
    it(`commits ${types.SET_PROJECT_ID} with the new project ID`, () => {
      const projectId = '4';
      testAction(actions.setProjectId, projectId, state, [
        { type: types.SET_PROJECT_ID, payload: projectId },
      ]);
    });
  });

  describe('setSelectedRef', () => {
    it(`commits ${types.SET_SELECTED_REF} with the new selected ref name`, () => {
      const selectedRef = 'v1.2.3';
      testAction(actions.setSelectedRef, selectedRef, state, [
        { type: types.SET_SELECTED_REF, payload: selectedRef },
      ]);
    });
  });

  describe('search', () => {
    it(`commits ${types.SET_QUERY} with the new search query`, () => {
      const query = 'hello';
      testAction(
        actions.search,
        query,
        state,
        [{ type: types.SET_QUERY, payload: query }],
        [{ type: 'searchBranches' }, { type: 'searchTags' }, { type: 'searchCommits' }],
      );
    });
  });

  describe('searchBranches', () => {
    describe('when the search is successful', () => {
      const branchesApiResponse = { data: [{ name: 'my-feature-branch' }] };

      beforeEach(() => {
        mockBranchesReturnValue = Promise.resolve(branchesApiResponse);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_BRANCHES_SUCCESS} with the response from the API, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.searchBranches, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_BRANCHES_SUCCESS, payload: branchesApiResponse },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });

    describe('when the search fails', () => {
      const error = new Error('Something went wrong!');

      beforeEach(() => {
        mockBranchesReturnValue = Promise.reject(error);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_BRANCHES_ERROR} with the error object, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.searchBranches, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_BRANCHES_ERROR, payload: error },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });
  });

  describe('searchTags', () => {
    describe('when the search is successful', () => {
      const tagsApiResponse = { data: [{ name: 'v1.2.3' }] };

      beforeEach(() => {
        mockTagsReturnValue = Promise.resolve(tagsApiResponse);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_TAGS_SUCCESS} with the response from the API, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.searchTags, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_TAGS_SUCCESS, payload: tagsApiResponse },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });

    describe('when the search fails', () => {
      const error = new Error('Something went wrong!');

      beforeEach(() => {
        mockTagsReturnValue = Promise.reject(error);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_TAGS_ERROR} with the error object, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.searchTags, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_TAGS_ERROR, payload: error },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });
  });

  describe('searchCommits', () => {
    describe('when the search query potentially matches a commit SHA', () => {
      beforeEach(() => {
        state.isQueryPossiblyASha = true;
      });

      describe('when the search is successful', () => {
        const commitApiResponse = { data: [{ id: 'abcd1234' }] };

        beforeEach(() => {
          mockCommitReturnValue = Promise.resolve(commitApiResponse);
        });

        it(`commits ${types.REQUEST_START}, ${types.RECEIVE_COMMITS_SUCCESS} with the response from the API, and ${types.REQUEST_FINISH}`, () => {
          return testAction(actions.searchCommits, undefined, state, [
            { type: types.REQUEST_START },
            { type: types.RECEIVE_COMMITS_SUCCESS, payload: commitApiResponse },
            { type: types.REQUEST_FINISH },
          ]);
        });
      });

      describe('when the search fails', () => {
        const error = new Error('Something went wrong!');

        beforeEach(() => {
          mockCommitReturnValue = Promise.reject(error);
        });

        describe('when the search query might match a commit SHA', () => {
          it(`commits ${types.REQUEST_START}, ${types.RECEIVE_COMMITS_ERROR} with the error object, and ${types.REQUEST_FINISH}`, () => {
            return testAction(actions.searchCommits, undefined, state, [
              { type: types.REQUEST_START },
              { type: types.RECEIVE_COMMITS_ERROR, payload: error },
              { type: types.REQUEST_FINISH },
            ]);
          });
        });
      });
    });

    describe('when the search query will not match a commit SHA', () => {
      beforeEach(() => {
        state.isQueryPossiblyASha = false;
      });

      it(`commits ${types.RESET_COMMIT_MATCHES}`, () => {
        return testAction(actions.searchCommits, undefined, state, [
          { type: types.RESET_COMMIT_MATCHES },
        ]);
      });
    });
  });
});