debian-mirror-gitlab/spec/frontend/badges/store/actions_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

537 lines
16 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import MockAdapter from 'axios-mock-adapter';
2020-04-08 14:13:33 +05:30
import testAction from 'helpers/vuex_action_helper';
2021-03-11 19:13:27 +05:30
import { TEST_HOST } from 'spec/test_constants';
2018-05-09 12:01:36 +05:30
import actions, { transformBackendBadge } from '~/badges/store/actions';
import mutationTypes from '~/badges/store/mutation_types';
import createState from '~/badges/store/state';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2023-04-23 21:23:45 +05:30
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
2018-05-09 12:01:36 +05:30
import { createDummyBadge, createDummyBadgeResponse } from '../dummy_badge';
describe('Badges store actions', () => {
const dummyEndpointUrl = `${TEST_HOST}/badges/endpoint`;
2021-03-08 18:12:59 +05:30
const dummyBadges = [
{ ...createDummyBadge(), id: 5 },
{ ...createDummyBadge(), id: 6 },
];
2018-05-09 12:01:36 +05:30
let axiosMock;
let badgeId;
let state;
beforeEach(() => {
axiosMock = new MockAdapter(axios);
state = {
...createState(),
apiEndpointUrl: dummyEndpointUrl,
badges: dummyBadges,
};
badgeId = state.badges[0].id;
});
afterEach(() => {
axiosMock.restore();
});
describe('requestNewBadge', () => {
2022-06-21 17:19:12 +05:30
it('commits REQUEST_NEW_BADGE', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.requestNewBadge,
null,
state,
[{ type: mutationTypes.REQUEST_NEW_BADGE }],
[],
);
});
});
describe('receiveNewBadge', () => {
2022-06-21 17:19:12 +05:30
it('commits RECEIVE_NEW_BADGE', () => {
2018-05-09 12:01:36 +05:30
const newBadge = createDummyBadge();
2022-06-21 17:19:12 +05:30
return testAction(
2018-05-09 12:01:36 +05:30
actions.receiveNewBadge,
newBadge,
state,
[{ type: mutationTypes.RECEIVE_NEW_BADGE, payload: newBadge }],
[],
);
});
});
describe('receiveNewBadgeError', () => {
2022-06-21 17:19:12 +05:30
it('commits RECEIVE_NEW_BADGE_ERROR', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.receiveNewBadgeError,
null,
state,
[{ type: mutationTypes.RECEIVE_NEW_BADGE_ERROR }],
[],
);
});
});
describe('addBadge', () => {
let badgeInAddForm;
let dispatch;
let endpointMock;
beforeEach(() => {
endpointMock = axiosMock.onPost(dummyEndpointUrl);
2020-04-08 14:13:33 +05:30
dispatch = jest.fn();
2018-05-09 12:01:36 +05:30
badgeInAddForm = createDummyBadge();
state = {
...state,
badgeInAddForm,
};
});
2022-06-21 17:19:12 +05:30
it('dispatches requestNewBadge and receiveNewBadge for successful response', async () => {
2018-05-09 12:01:36 +05:30
const dummyResponse = createDummyBadgeResponse();
2021-03-08 18:12:59 +05:30
endpointMock.replyOnce((req) => {
2018-05-09 12:01:36 +05:30
expect(req.data).toBe(
JSON.stringify({
2020-01-01 13:55:28 +05:30
name: 'TestBadge',
2018-05-09 12:01:36 +05:30
image_url: badgeInAddForm.imageUrl,
link_url: badgeInAddForm.linkUrl,
}),
);
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([['requestNewBadge']]);
dispatch.mockClear();
2023-04-23 21:23:45 +05:30
return [HTTP_STATUS_OK, dummyResponse];
2018-05-09 12:01:36 +05:30
});
const dummyBadge = transformBackendBadge(dummyResponse);
2022-06-21 17:19:12 +05:30
await actions.addBadge({ state, dispatch });
expect(dispatch.mock.calls).toEqual([['receiveNewBadge', dummyBadge]]);
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestNewBadge and receiveNewBadgeError for error response', async () => {
2021-03-08 18:12:59 +05:30
endpointMock.replyOnce((req) => {
2018-05-09 12:01:36 +05:30
expect(req.data).toBe(
JSON.stringify({
2020-01-01 13:55:28 +05:30
name: 'TestBadge',
2018-05-09 12:01:36 +05:30
image_url: badgeInAddForm.imageUrl,
link_url: badgeInAddForm.linkUrl,
}),
);
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([['requestNewBadge']]);
dispatch.mockClear();
2023-04-23 21:23:45 +05:30
return [HTTP_STATUS_INTERNAL_SERVER_ERROR, ''];
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
await expect(actions.addBadge({ state, dispatch })).rejects.toThrow();
expect(dispatch.mock.calls).toEqual([['receiveNewBadgeError']]);
2018-05-09 12:01:36 +05:30
});
});
describe('requestDeleteBadge', () => {
2022-06-21 17:19:12 +05:30
it('commits REQUEST_DELETE_BADGE', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.requestDeleteBadge,
badgeId,
state,
[{ type: mutationTypes.REQUEST_DELETE_BADGE, payload: badgeId }],
[],
);
});
});
describe('receiveDeleteBadge', () => {
2022-06-21 17:19:12 +05:30
it('commits RECEIVE_DELETE_BADGE', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.receiveDeleteBadge,
badgeId,
state,
[{ type: mutationTypes.RECEIVE_DELETE_BADGE, payload: badgeId }],
[],
);
});
});
describe('receiveDeleteBadgeError', () => {
2022-06-21 17:19:12 +05:30
it('commits RECEIVE_DELETE_BADGE_ERROR', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.receiveDeleteBadgeError,
badgeId,
state,
[{ type: mutationTypes.RECEIVE_DELETE_BADGE_ERROR, payload: badgeId }],
[],
);
});
});
describe('deleteBadge', () => {
let dispatch;
let endpointMock;
beforeEach(() => {
endpointMock = axiosMock.onDelete(`${dummyEndpointUrl}/${badgeId}`);
2020-04-08 14:13:33 +05:30
dispatch = jest.fn();
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestDeleteBadge and receiveDeleteBadge for successful response', async () => {
2018-05-09 12:01:36 +05:30
endpointMock.replyOnce(() => {
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([['requestDeleteBadge', badgeId]]);
dispatch.mockClear();
2023-04-23 21:23:45 +05:30
return [HTTP_STATUS_OK, ''];
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
await actions.deleteBadge({ state, dispatch }, { id: badgeId });
expect(dispatch.mock.calls).toEqual([['receiveDeleteBadge', badgeId]]);
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestDeleteBadge and receiveDeleteBadgeError for error response', async () => {
2018-05-09 12:01:36 +05:30
endpointMock.replyOnce(() => {
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([['requestDeleteBadge', badgeId]]);
dispatch.mockClear();
2023-04-23 21:23:45 +05:30
return [HTTP_STATUS_INTERNAL_SERVER_ERROR, ''];
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
await expect(actions.deleteBadge({ state, dispatch }, { id: badgeId })).rejects.toThrow();
expect(dispatch.mock.calls).toEqual([['receiveDeleteBadgeError', badgeId]]);
2018-05-09 12:01:36 +05:30
});
});
describe('editBadge', () => {
2022-06-21 17:19:12 +05:30
it('commits START_EDITING', () => {
2018-05-09 12:01:36 +05:30
const dummyBadge = createDummyBadge();
2022-06-21 17:19:12 +05:30
return testAction(
2018-05-09 12:01:36 +05:30
actions.editBadge,
dummyBadge,
state,
[{ type: mutationTypes.START_EDITING, payload: dummyBadge }],
[],
);
});
});
describe('requestLoadBadges', () => {
2022-06-21 17:19:12 +05:30
it('commits REQUEST_LOAD_BADGES', () => {
2018-05-09 12:01:36 +05:30
const dummyData = 'this is not real data';
2022-06-21 17:19:12 +05:30
return testAction(
2018-05-09 12:01:36 +05:30
actions.requestLoadBadges,
dummyData,
state,
[{ type: mutationTypes.REQUEST_LOAD_BADGES, payload: dummyData }],
[],
);
});
});
describe('receiveLoadBadges', () => {
2022-06-21 17:19:12 +05:30
it('commits RECEIVE_LOAD_BADGES', () => {
2018-05-09 12:01:36 +05:30
const badges = dummyBadges;
2022-06-21 17:19:12 +05:30
return testAction(
2018-05-09 12:01:36 +05:30
actions.receiveLoadBadges,
badges,
state,
[{ type: mutationTypes.RECEIVE_LOAD_BADGES, payload: badges }],
[],
);
});
});
describe('receiveLoadBadgesError', () => {
2022-06-21 17:19:12 +05:30
it('commits RECEIVE_LOAD_BADGES_ERROR', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.receiveLoadBadgesError,
null,
state,
[{ type: mutationTypes.RECEIVE_LOAD_BADGES_ERROR }],
[],
);
});
});
describe('loadBadges', () => {
let dispatch;
let endpointMock;
beforeEach(() => {
endpointMock = axiosMock.onGet(dummyEndpointUrl);
2020-04-08 14:13:33 +05:30
dispatch = jest.fn();
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestLoadBadges and receiveLoadBadges for successful response', async () => {
2018-05-09 12:01:36 +05:30
const dummyData = 'this is just some data';
const dummyReponse = [
createDummyBadgeResponse(),
createDummyBadgeResponse(),
createDummyBadgeResponse(),
];
endpointMock.replyOnce(() => {
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([['requestLoadBadges', dummyData]]);
dispatch.mockClear();
2023-04-23 21:23:45 +05:30
return [HTTP_STATUS_OK, dummyReponse];
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
await actions.loadBadges({ state, dispatch }, dummyData);
const badges = dummyReponse.map(transformBackendBadge);
2018-12-13 13:39:08 +05:30
2022-06-21 17:19:12 +05:30
expect(dispatch.mock.calls).toEqual([['receiveLoadBadges', badges]]);
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestLoadBadges and receiveLoadBadgesError for error response', async () => {
2018-05-09 12:01:36 +05:30
const dummyData = 'this is just some data';
endpointMock.replyOnce(() => {
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([['requestLoadBadges', dummyData]]);
dispatch.mockClear();
2023-04-23 21:23:45 +05:30
return [HTTP_STATUS_INTERNAL_SERVER_ERROR, ''];
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
await expect(actions.loadBadges({ state, dispatch }, dummyData)).rejects.toThrow();
expect(dispatch.mock.calls).toEqual([['receiveLoadBadgesError']]);
2018-05-09 12:01:36 +05:30
});
});
describe('requestRenderedBadge', () => {
2022-06-21 17:19:12 +05:30
it('commits REQUEST_RENDERED_BADGE', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.requestRenderedBadge,
null,
state,
[{ type: mutationTypes.REQUEST_RENDERED_BADGE }],
[],
);
});
});
describe('receiveRenderedBadge', () => {
2022-06-21 17:19:12 +05:30
it('commits RECEIVE_RENDERED_BADGE', () => {
2018-05-09 12:01:36 +05:30
const dummyBadge = createDummyBadge();
2022-06-21 17:19:12 +05:30
return testAction(
2018-05-09 12:01:36 +05:30
actions.receiveRenderedBadge,
dummyBadge,
state,
[{ type: mutationTypes.RECEIVE_RENDERED_BADGE, payload: dummyBadge }],
[],
);
});
});
describe('receiveRenderedBadgeError', () => {
2022-06-21 17:19:12 +05:30
it('commits RECEIVE_RENDERED_BADGE_ERROR', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.receiveRenderedBadgeError,
null,
state,
[{ type: mutationTypes.RECEIVE_RENDERED_BADGE_ERROR }],
[],
);
});
});
describe('renderBadge', () => {
let dispatch;
let endpointMock;
let badgeInForm;
beforeEach(() => {
badgeInForm = createDummyBadge();
state = {
...state,
badgeInAddForm: badgeInForm,
};
const urlParameters = [
`link_url=${encodeURIComponent(badgeInForm.linkUrl)}`,
`image_url=${encodeURIComponent(badgeInForm.imageUrl)}`,
].join('&');
endpointMock = axiosMock.onGet(`${dummyEndpointUrl}/render?${urlParameters}`);
2020-04-08 14:13:33 +05:30
dispatch = jest.fn();
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('returns immediately if imageUrl is empty', async () => {
2020-04-08 14:13:33 +05:30
jest.spyOn(axios, 'get').mockImplementation(() => {});
2018-05-09 12:01:36 +05:30
badgeInForm.imageUrl = '';
2022-06-21 17:19:12 +05:30
await actions.renderBadge({ state, dispatch });
expect(axios.get).not.toHaveBeenCalled();
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('returns immediately if linkUrl is empty', async () => {
2020-04-08 14:13:33 +05:30
jest.spyOn(axios, 'get').mockImplementation(() => {});
2018-05-09 12:01:36 +05:30
badgeInForm.linkUrl = '';
2022-06-21 17:19:12 +05:30
await actions.renderBadge({ state, dispatch });
expect(axios.get).not.toHaveBeenCalled();
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('escapes user input', async () => {
2020-04-08 14:13:33 +05:30
jest
.spyOn(axios, 'get')
.mockImplementation(() => Promise.resolve({ data: createDummyBadgeResponse() }));
2019-07-07 11:18:12 +05:30
badgeInForm.imageUrl = '&make-sandwich=true';
2018-05-09 12:01:36 +05:30
badgeInForm.linkUrl = '<script>I am dangerous!</script>';
2022-06-21 17:19:12 +05:30
await actions.renderBadge({ state, dispatch });
expect(axios.get.mock.calls.length).toBe(1);
const url = axios.get.mock.calls[0][0];
2020-04-08 14:13:33 +05:30
2022-06-21 17:19:12 +05:30
expect(url).toMatch(new RegExp(`^${dummyEndpointUrl}/render?`));
2022-07-16 23:28:13 +05:30
expect(url).toMatch(/\\?link_url=%3Cscript%3EI%20am%20dangerous!%3C%2Fscript%3E&/);
expect(url).toMatch(/&image_url=%26make-sandwich%3Dtrue$/);
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestRenderedBadge and receiveRenderedBadge for successful response', async () => {
2018-05-09 12:01:36 +05:30
const dummyReponse = createDummyBadgeResponse();
endpointMock.replyOnce(() => {
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([['requestRenderedBadge']]);
dispatch.mockClear();
2023-04-23 21:23:45 +05:30
return [HTTP_STATUS_OK, dummyReponse];
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
await actions.renderBadge({ state, dispatch });
const renderedBadge = transformBackendBadge(dummyReponse);
2018-12-13 13:39:08 +05:30
2022-06-21 17:19:12 +05:30
expect(dispatch.mock.calls).toEqual([['receiveRenderedBadge', renderedBadge]]);
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestRenderedBadge and receiveRenderedBadgeError for error response', async () => {
2018-05-09 12:01:36 +05:30
endpointMock.replyOnce(() => {
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([['requestRenderedBadge']]);
dispatch.mockClear();
2023-04-23 21:23:45 +05:30
return [HTTP_STATUS_INTERNAL_SERVER_ERROR, ''];
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
await expect(actions.renderBadge({ state, dispatch })).rejects.toThrow();
expect(dispatch.mock.calls).toEqual([['receiveRenderedBadgeError']]);
2018-05-09 12:01:36 +05:30
});
});
describe('requestUpdatedBadge', () => {
2022-06-21 17:19:12 +05:30
it('commits REQUEST_UPDATED_BADGE', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.requestUpdatedBadge,
null,
state,
[{ type: mutationTypes.REQUEST_UPDATED_BADGE }],
[],
);
});
});
describe('receiveUpdatedBadge', () => {
2022-06-21 17:19:12 +05:30
it('commits RECEIVE_UPDATED_BADGE', () => {
2018-05-09 12:01:36 +05:30
const updatedBadge = createDummyBadge();
2022-06-21 17:19:12 +05:30
return testAction(
2018-05-09 12:01:36 +05:30
actions.receiveUpdatedBadge,
updatedBadge,
state,
[{ type: mutationTypes.RECEIVE_UPDATED_BADGE, payload: updatedBadge }],
[],
);
});
});
describe('receiveUpdatedBadgeError', () => {
2022-06-21 17:19:12 +05:30
it('commits RECEIVE_UPDATED_BADGE_ERROR', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.receiveUpdatedBadgeError,
null,
state,
[{ type: mutationTypes.RECEIVE_UPDATED_BADGE_ERROR }],
[],
);
});
});
describe('saveBadge', () => {
let badgeInEditForm;
let dispatch;
let endpointMock;
beforeEach(() => {
badgeInEditForm = createDummyBadge();
state = {
...state,
badgeInEditForm,
};
endpointMock = axiosMock.onPut(`${dummyEndpointUrl}/${badgeInEditForm.id}`);
2020-04-08 14:13:33 +05:30
dispatch = jest.fn();
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestUpdatedBadge and receiveUpdatedBadge for successful response', async () => {
2018-05-09 12:01:36 +05:30
const dummyResponse = createDummyBadgeResponse();
2021-03-08 18:12:59 +05:30
endpointMock.replyOnce((req) => {
2018-05-09 12:01:36 +05:30
expect(req.data).toBe(
JSON.stringify({
2020-01-01 13:55:28 +05:30
name: 'TestBadge',
2018-05-09 12:01:36 +05:30
image_url: badgeInEditForm.imageUrl,
link_url: badgeInEditForm.linkUrl,
}),
);
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([['requestUpdatedBadge']]);
dispatch.mockClear();
2023-04-23 21:23:45 +05:30
return [HTTP_STATUS_OK, dummyResponse];
2018-05-09 12:01:36 +05:30
});
const updatedBadge = transformBackendBadge(dummyResponse);
2022-06-21 17:19:12 +05:30
await actions.saveBadge({ state, dispatch });
expect(dispatch.mock.calls).toEqual([['receiveUpdatedBadge', updatedBadge]]);
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestUpdatedBadge and receiveUpdatedBadgeError for error response', async () => {
2021-03-08 18:12:59 +05:30
endpointMock.replyOnce((req) => {
2018-05-09 12:01:36 +05:30
expect(req.data).toBe(
JSON.stringify({
2020-01-01 13:55:28 +05:30
name: 'TestBadge',
2018-05-09 12:01:36 +05:30
image_url: badgeInEditForm.imageUrl,
link_url: badgeInEditForm.linkUrl,
}),
);
2018-12-13 13:39:08 +05:30
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([['requestUpdatedBadge']]);
dispatch.mockClear();
2023-04-23 21:23:45 +05:30
return [HTTP_STATUS_INTERNAL_SERVER_ERROR, ''];
2018-05-09 12:01:36 +05:30
});
2022-06-21 17:19:12 +05:30
await expect(actions.saveBadge({ state, dispatch })).rejects.toThrow();
expect(dispatch.mock.calls).toEqual([['receiveUpdatedBadgeError']]);
2018-05-09 12:01:36 +05:30
});
});
describe('stopEditing', () => {
2022-06-21 17:19:12 +05:30
it('commits STOP_EDITING', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.stopEditing,
null,
state,
[{ type: mutationTypes.STOP_EDITING }],
[],
);
});
});
describe('updateBadgeInForm', () => {
2022-06-21 17:19:12 +05:30
it('commits UPDATE_BADGE_IN_FORM', () => {
2018-05-09 12:01:36 +05:30
const dummyBadge = createDummyBadge();
2022-06-21 17:19:12 +05:30
return testAction(
2018-05-09 12:01:36 +05:30
actions.updateBadgeInForm,
dummyBadge,
state,
[{ type: mutationTypes.UPDATE_BADGE_IN_FORM, payload: dummyBadge }],
[],
);
});
describe('updateBadgeInModal', () => {
2022-06-21 17:19:12 +05:30
it('commits UPDATE_BADGE_IN_MODAL', () => {
2018-05-09 12:01:36 +05:30
const dummyBadge = createDummyBadge();
2022-06-21 17:19:12 +05:30
return testAction(
2018-05-09 12:01:36 +05:30
actions.updateBadgeInModal,
dummyBadge,
state,
[{ type: mutationTypes.UPDATE_BADGE_IN_MODAL, payload: dummyBadge }],
[],
);
});
});
});
});