Commit 1772216f authored by Sean Arnold's avatar Sean Arnold

Add specs for update metric image JS changes

parent 36e1e7cb
...@@ -25,7 +25,7 @@ export default { ...@@ -25,7 +25,7 @@ export default {
state.isUploadingImage = false; state.isUploadingImage = false;
const metricIndex = state.metricImages.findIndex((img) => img.id === image.id); const metricIndex = state.metricImages.findIndex((img) => img.id === image.id);
if (metricIndex >= 0) { if (metricIndex >= 0) {
state.metricImages.splice(metricIndex, 1, image) state.metricImages.splice(metricIndex, 1, image);
} }
}, },
[types.RECEIVE_METRIC_DELETE_SUCCESS](state, imageId) { [types.RECEIVE_METRIC_DELETE_SUCCESS](state, imageId) {
......
import Api from 'ee/api'; import Api from 'ee/api';
import { getMetricImages, uploadMetricImage } from 'ee/issues/show/components/incidents/service'; import {
getMetricImages,
uploadMetricImage,
updateMetricImage,
} from 'ee/issues/show/components/incidents/service';
import { fileList, fileListRaw } from './mock_data'; import { fileList, fileListRaw } from './mock_data';
jest.mock('ee/api', () => ({ jest.mock('ee/api', () => ({
fetchIssueMetricImages: jest.fn(), fetchIssueMetricImages: jest.fn(),
uploadIssueMetricImage: jest.fn(), uploadIssueMetricImage: jest.fn(),
updateIssueMetricImage: jest.fn(),
})); }));
describe('Incidents service', () => { describe('Incidents service', () => {
...@@ -23,4 +28,12 @@ describe('Incidents service', () => { ...@@ -23,4 +28,12 @@ describe('Incidents service', () => {
expect(Api.uploadIssueMetricImage).toHaveBeenCalled(); expect(Api.uploadIssueMetricImage).toHaveBeenCalled();
expect(result).toEqual(fileList[0]); expect(result).toEqual(fileList[0]);
}); });
it('updates a metric image', async () => {
Api.updateIssueMetricImage.mockResolvedValue({ data: fileListRaw[0] });
const result = await updateMetricImage();
expect(Api.updateIssueMetricImage).toHaveBeenCalled();
expect(result).toEqual(fileList[0]);
});
}); });
...@@ -3,6 +3,7 @@ import Vuex from 'vuex'; ...@@ -3,6 +3,7 @@ import Vuex from 'vuex';
import { import {
getMetricImages, getMetricImages,
uploadMetricImage, uploadMetricImage,
updateMetricImage,
deleteMetricImage, deleteMetricImage,
} from 'ee/issues/show/components/incidents/service'; } from 'ee/issues/show/components/incidents/service';
import createStore from 'ee/issues/show/components/incidents/store'; import createStore from 'ee/issues/show/components/incidents/store';
...@@ -17,6 +18,7 @@ jest.mock('~/flash'); ...@@ -17,6 +18,7 @@ jest.mock('~/flash');
jest.mock('ee/issues/show/components/incidents/service', () => ({ jest.mock('ee/issues/show/components/incidents/service', () => ({
getMetricImages: jest.fn(), getMetricImages: jest.fn(),
uploadMetricImage: jest.fn(), uploadMetricImage: jest.fn(),
updateMetricImage: jest.fn(),
deleteMetricImage: jest.fn(), deleteMetricImage: jest.fn(),
})); }));
...@@ -104,6 +106,37 @@ describe('Metrics tab store actions', () => { ...@@ -104,6 +106,37 @@ describe('Metrics tab store actions', () => {
}); });
}); });
describe('updating metric images', () => {
const payload = {
url: 'test_url',
urlText: 'url text',
};
it('should call success action when updating an image', () => {
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 () => {
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', () => { describe('deleting a metric image', () => {
const payload = fileList[0].id; const payload = fileList[0].id;
......
...@@ -101,6 +101,25 @@ describe('Metric images mutations', () => { ...@@ -101,6 +101,25 @@ describe('Metric images mutations', () => {
}); });
}); });
describe('RECEIVE_METRIC_UPDATE_SUCCESS', () => {
const initialImage = testImages[0];
var newImage = Object.assign({}, 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', () => { describe('RECEIVE_METRIC_DELETE_SUCCESS', () => {
const deletedImageId = testImages[1].id; const deletedImageId = testImages[1].id;
const expectedResult = [testImages[0], testImages[2]]; const expectedResult = [testImages[0], testImages[2]];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment