Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mutations_spec.js « store « metric_images « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 754f729e65709d3c9a08d1fcc142dbad1a9544b8 (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 { cloneDeep } from 'lodash';
import * as types from '~/vue_shared/components/metric_images/store/mutation_types';
import mutations from '~/vue_shared/components/metric_images/store/mutations';
import { initialData } from '../mock_data';

const defaultState = {
  metricImages: [],
  isLoadingMetricImages: false,
  isUploadingImage: false,
};

const testImages = [
  { filename: 'test.filename', id: 5, filePath: 'test/file/path', url: null },
  { filename: 'second.filename', id: 6, filePath: 'second/file/path', url: 'test/url' },
  { filename: 'third.filename', id: 7, filePath: 'third/file/path', url: 'test/url' },
];

describe('Metric images mutations', () => {
  let state;

  const createState = (customState = {}) => {
    state = {
      ...cloneDeep(defaultState),
      ...customState,
    };
  };

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

  describe('REQUEST_METRIC_IMAGES', () => {
    beforeEach(() => {
      mutations[types.REQUEST_METRIC_IMAGES](state);
    });

    it('should set the loading state', () => {
      expect(state.isLoadingMetricImages).toBe(true);
    });
  });

  describe('RECEIVE_METRIC_IMAGES_SUCCESS', () => {
    beforeEach(() => {
      mutations[types.RECEIVE_METRIC_IMAGES_SUCCESS](state, testImages);
    });

    it('should unset the loading state', () => {
      expect(state.isLoadingMetricImages).toBe(false);
    });

    it('should set the metric images', () => {
      expect(state.metricImages).toEqual(testImages);
    });
  });

  describe('RECEIVE_METRIC_IMAGES_ERROR', () => {
    beforeEach(() => {
      mutations[types.RECEIVE_METRIC_IMAGES_ERROR](state);
    });

    it('should unset the loading state', () => {
      expect(state.isLoadingMetricImages).toBe(false);
    });
  });

  describe('REQUEST_METRIC_UPLOAD', () => {
    beforeEach(() => {
      mutations[types.REQUEST_METRIC_UPLOAD](state);
    });

    it('should set the loading state', () => {
      expect(state.isUploadingImage).toBe(true);
    });
  });

  describe('RECEIVE_METRIC_UPLOAD_SUCCESS', () => {
    const initialImage = testImages[0];
    const newImage = testImages[1];

    beforeEach(() => {
      createState({ metricImages: [initialImage] });
      mutations[types.RECEIVE_METRIC_UPLOAD_SUCCESS](state, newImage);
    });

    it('should unset the loading state', () => {
      expect(state.isUploadingImage).toBe(false);
    });

    it('should add the new metric image after the existing one', () => {
      expect(state.metricImages).toMatchObject([initialImage, newImage]);
    });
  });

  describe('RECEIVE_METRIC_UPLOAD_ERROR', () => {
    beforeEach(() => {
      mutations[types.RECEIVE_METRIC_UPLOAD_ERROR](state);
    });

    it('should unset the loading state', () => {
      expect(state.isUploadingImage).toBe(false);
    });
  });

  describe('RECEIVE_METRIC_UPDATE_SUCCESS', () => {
    const initialImage = testImages[0];
    const newImage = testImages[0];
    newImage.url = 'https://www.gitlab.com';

    beforeEach(() => {
      createState({ metricImages: [initialImage] });
      mutations[types.RECEIVE_METRIC_UPDATE_SUCCESS](state, newImage);
    });

    it('should unset the loading state', () => {
      expect(state.isUploadingImage).toBe(false);
    });

    it('should replace the existing image with the new one', () => {
      expect(state.metricImages).toMatchObject([newImage]);
    });
  });

  describe('RECEIVE_METRIC_DELETE_SUCCESS', () => {
    const deletedImageId = testImages[1].id;
    const expectedResult = [testImages[0], testImages[2]];

    beforeEach(() => {
      createState({ metricImages: [...testImages] });
      mutations[types.RECEIVE_METRIC_DELETE_SUCCESS](state, deletedImageId);
    });

    it('should remove the correct metric image', () => {
      expect(state.metricImages).toEqual(expectedResult);
    });
  });

  describe('SET_INITIAL_DATA', () => {
    beforeEach(() => {
      mutations[types.SET_INITIAL_DATA](state, initialData);
    });

    it('should unset the loading state', () => {
      expect(state.modelIid).toBe(initialData.modelIid);
      expect(state.projectId).toBe(initialData.projectId);
    });
  });
});