summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/metric_images/store/actions_spec.js
blob: 518cf3546754030238c5920ceb6fb558d5736549 (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
import Vue from 'vue';
import Vuex from 'vuex';
import actionsFactory from '~/vue_shared/components/metric_images/store/actions';
import * as types from '~/vue_shared/components/metric_images/store/mutation_types';
import createStore from '~/vue_shared/components/metric_images/store';
import testAction from 'helpers/vuex_action_helper';
import createFlash from '~/flash';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import { fileList, initialData } from '../mock_data';

jest.mock('~/flash');
const service = {
  getMetricImages: jest.fn(),
  uploadMetricImage: jest.fn(),
  updateMetricImage: jest.fn(),
  deleteMetricImage: jest.fn(),
};

const actions = actionsFactory(service);

const defaultState = {
  issueIid: 1,
  projectId: '2',
};

Vue.use(Vuex);

describe('Metrics tab store actions', () => {
  let store;
  let state;

  beforeEach(() => {
    store = createStore(defaultState);
    state = store.state;
  });

  afterEach(() => {
    createFlash.mockClear();
  });

  describe('fetching metric images', () => {
    it('should call success action when fetching metric images', () => {
      service.getMetricImages.mockImplementation(() => Promise.resolve(fileList));

      testAction(actions.fetchImages, null, state, [
        { type: types.REQUEST_METRIC_IMAGES },
        {
          type: types.RECEIVE_METRIC_IMAGES_SUCCESS,
          payload: convertObjectPropsToCamelCase(fileList, { deep: true }),
        },
      ]);
    });

    it('should call error action when fetching metric images with an error', async () => {
      service.getMetricImages.mockImplementation(() => Promise.reject());

      await testAction(
        actions.fetchImages,
        null,
        state,
        [{ type: types.REQUEST_METRIC_IMAGES }, { type: types.RECEIVE_METRIC_IMAGES_ERROR }],
        [],
      );
      expect(createFlash).toHaveBeenCalled();
    });
  });

  describe('uploading metric images', () => {
    const payload = {
      // mock the FileList api
      files: {
        item() {
          return fileList[0];
        },
      },
      url: 'test_url',
    };

    it('should call success action when uploading an image', () => {
      service.uploadMetricImage.mockImplementation(() => Promise.resolve(fileList[0]));

      testAction(actions.uploadImage, payload, state, [
        { type: types.REQUEST_METRIC_UPLOAD },
        {
          type: types.RECEIVE_METRIC_UPLOAD_SUCCESS,
          payload: fileList[0],
        },
      ]);
    });

    it('should call error action when failing to upload an image', async () => {
      service.uploadMetricImage.mockImplementation(() => Promise.reject());

      await testAction(
        actions.uploadImage,
        payload,
        state,
        [{ type: types.REQUEST_METRIC_UPLOAD }, { type: types.RECEIVE_METRIC_UPLOAD_ERROR }],
        [],
      );
      expect(createFlash).toHaveBeenCalled();
    });
  });

  describe('updating metric images', () => {
    const payload = {
      url: 'test_url',
      urlText: 'url text',
    };

    it('should call success action when updating an image', () => {
      service.updateMetricImage.mockImplementation(() => Promise.resolve());

      testAction(actions.updateImage, payload, state, [
        { type: types.REQUEST_METRIC_UPLOAD },
        {
          type: types.RECEIVE_METRIC_UPDATE_SUCCESS,
        },
      ]);
    });

    it('should call error action when failing to update an image', async () => {
      service.updateMetricImage.mockImplementation(() => Promise.reject());

      await testAction(
        actions.updateImage,
        payload,
        state,
        [{ type: types.REQUEST_METRIC_UPLOAD }, { type: types.RECEIVE_METRIC_UPLOAD_ERROR }],
        [],
      );
      expect(createFlash).toHaveBeenCalled();
    });
  });

  describe('deleting a metric image', () => {
    const payload = fileList[0].id;

    it('should call success action when deleting an image', () => {
      service.deleteMetricImage.mockImplementation(() => Promise.resolve());

      testAction(actions.deleteImage, payload, state, [
        {
          type: types.RECEIVE_METRIC_DELETE_SUCCESS,
          payload,
        },
      ]);
    });
  });

  describe('initial data', () => {
    it('should set the initial data correctly', () => {
      testAction(actions.setInitialData, initialData, state, [
        { type: types.SET_INITIAL_DATA, payload: initialData },
      ]);
    });
  });
});