debian-mirror-gitlab/spec/frontend/notes/stores/actions_spec.js

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

1538 lines
44 KiB
JavaScript
Raw Normal View History

2020-01-01 13:55:28 +05:30
import AxiosMockAdapter from 'axios-mock-adapter';
2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2021-03-08 18:12:59 +05:30
import testAction from 'helpers/vuex_action_helper';
2021-03-11 19:13:27 +05:30
import { TEST_HOST } from 'spec/test_constants';
2020-01-01 13:55:28 +05:30
import Api from '~/api';
2023-03-04 22:38:38 +05:30
import { createAlert } from '~/flash';
2022-10-11 01:57:18 +05:30
import toast from '~/vue_shared/plugins/global_toast';
2021-04-17 20:07:23 +05:30
import { EVENT_ISSUABLE_VUE_APP_CHANGE } from '~/issuable/constants';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2019-07-07 11:18:12 +05:30
import * as notesConstants from '~/notes/constants';
2018-11-08 19:23:39 +05:30
import createStore from '~/notes/stores';
2021-03-11 19:13:27 +05:30
import * as actions from '~/notes/stores/actions';
import * as mutationTypes from '~/notes/stores/mutation_types';
import mutations from '~/notes/stores/mutations';
import * as utils from '~/notes/stores/utils';
2023-03-04 22:38:38 +05:30
import updateIssueLockMutation from '~/sidebar/queries/update_issue_lock.mutation.graphql';
import updateMergeRequestLockMutation from '~/sidebar/queries/update_merge_request_lock.mutation.graphql';
2022-10-11 01:57:18 +05:30
import promoteTimelineEvent from '~/notes/graphql/promote_timeline_event.mutation.graphql';
2018-12-05 23:21:45 +05:30
import mrWidgetEventHub from '~/vue_merge_request_widget/event_hub';
2022-10-11 01:57:18 +05:30
import notesEventHub from '~/notes/event_hub';
2022-08-13 15:12:31 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2018-03-27 19:54:05 +05:30
import { resetStore } from '../helpers';
2018-05-09 12:01:36 +05:30
import {
discussionMock,
notesDataMock,
userDataMock,
noteableDataMock,
individualNote,
2020-06-23 00:09:42 +05:30
batchSuggestionsInfoMock,
2018-05-09 12:01:36 +05:30
} from '../mock_data';
2018-03-17 18:26:18 +05:30
2019-07-31 22:56:46 +05:30
const TEST_ERROR_MESSAGE = 'Test error message';
2023-03-04 22:38:38 +05:30
const mockAlertDismiss = jest.fn();
jest.mock('~/flash', () => ({
createAlert: jest.fn().mockImplementation(() => ({
dismiss: mockAlertDismiss,
})),
}));
2019-07-31 22:56:46 +05:30
2022-10-11 01:57:18 +05:30
jest.mock('~/vue_shared/plugins/global_toast');
2018-03-17 18:26:18 +05:30
describe('Actions Notes Store', () => {
2019-07-31 22:56:46 +05:30
let commit;
let dispatch;
let state;
2018-11-08 19:23:39 +05:30
let store;
2019-12-04 20:38:33 +05:30
let axiosMock;
2018-11-08 19:23:39 +05:30
beforeEach(() => {
store = createStore();
2020-04-08 14:13:33 +05:30
commit = jest.fn();
dispatch = jest.fn();
2019-07-31 22:56:46 +05:30
state = {};
2019-12-04 20:38:33 +05:30
axiosMock = new AxiosMockAdapter(axios);
2020-05-24 23:13:21 +05:30
// This is necessary as we query Close issue button at the top of issue page when clicking bottom button
2022-07-16 23:28:13 +05:30
setHTMLFixture(
2020-05-24 23:13:21 +05:30
'<div class="detail-page-header-actions"><button class="btn-close btn-grouped"></button></div>',
);
2018-11-08 19:23:39 +05:30
});
2018-03-27 19:54:05 +05:30
afterEach(() => {
resetStore(store);
2019-12-04 20:38:33 +05:30
axiosMock.restore();
2022-07-16 23:28:13 +05:30
resetHTMLFixture();
2018-03-27 19:54:05 +05:30
});
2018-03-17 18:26:18 +05:30
describe('setNotesData', () => {
2022-06-21 17:19:12 +05:30
it('should set received notes data', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.setNotesData,
notesDataMock,
{ notesData: {} },
[{ type: 'SET_NOTES_DATA', payload: notesDataMock }],
[],
);
2018-03-17 18:26:18 +05:30
});
});
describe('setNoteableData', () => {
2022-06-21 17:19:12 +05:30
it('should set received issue data', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.setNoteableData,
noteableDataMock,
{ noteableData: {} },
[{ type: 'SET_NOTEABLE_DATA', payload: noteableDataMock }],
[],
);
2018-03-17 18:26:18 +05:30
});
});
describe('setUserData', () => {
2022-06-21 17:19:12 +05:30
it('should set received user data', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.setUserData,
userDataMock,
{ userData: {} },
[{ type: 'SET_USER_DATA', payload: userDataMock }],
[],
);
2018-03-17 18:26:18 +05:30
});
});
describe('setLastFetchedAt', () => {
2022-06-21 17:19:12 +05:30
it('should set received timestamp', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.setLastFetchedAt,
'timestamp',
{ lastFetchedAt: {} },
[{ type: 'SET_LAST_FETCHED_AT', payload: 'timestamp' }],
[],
);
2018-03-17 18:26:18 +05:30
});
});
describe('setInitialNotes', () => {
2022-06-21 17:19:12 +05:30
it('should set initial notes', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.setInitialNotes,
[individualNote],
{ notes: [] },
2021-12-11 22:18:48 +05:30
[{ type: 'ADD_OR_UPDATE_DISCUSSIONS', payload: [individualNote] }],
2018-05-09 12:01:36 +05:30
[],
);
2018-03-17 18:26:18 +05:30
});
});
describe('setTargetNoteHash', () => {
2022-06-21 17:19:12 +05:30
it('should set target note hash', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.setTargetNoteHash,
'hash',
{ notes: [] },
[{ type: 'SET_TARGET_NOTE_HASH', payload: 'hash' }],
[],
);
2018-03-17 18:26:18 +05:30
});
});
describe('toggleDiscussion', () => {
2022-06-21 17:19:12 +05:30
it('should toggle discussion', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.toggleDiscussion,
{ discussionId: discussionMock.id },
{ notes: [discussionMock] },
[{ type: 'TOGGLE_DISCUSSION', payload: { discussionId: discussionMock.id } }],
[],
);
2018-03-17 18:26:18 +05:30
});
});
2018-03-27 19:54:05 +05:30
2018-11-08 19:23:39 +05:30
describe('expandDiscussion', () => {
2022-06-21 17:19:12 +05:30
it('should expand discussion', () => {
return testAction(
2018-11-08 19:23:39 +05:30
actions.expandDiscussion,
{ discussionId: discussionMock.id },
{ notes: [discussionMock] },
[{ type: 'EXPAND_DISCUSSION', payload: { discussionId: discussionMock.id } }],
2019-02-15 15:39:39 +05:30
[{ type: 'diffs/renderFileForDiscussionId', payload: discussionMock.id }],
2018-11-08 19:23:39 +05:30
);
});
});
describe('collapseDiscussion', () => {
2022-06-21 17:19:12 +05:30
it('should commit collapse discussion', () => {
return testAction(
2018-11-08 19:23:39 +05:30
actions.collapseDiscussion,
{ discussionId: discussionMock.id },
{ notes: [discussionMock] },
[{ type: 'COLLAPSE_DISCUSSION', payload: { discussionId: discussionMock.id } }],
[],
);
});
});
2018-03-27 19:54:05 +05:30
describe('async methods', () => {
beforeEach(() => {
2019-12-04 20:38:33 +05:30
axiosMock.onAny().reply(200, {});
2018-03-27 19:54:05 +05:30
});
2021-02-22 17:27:13 +05:30
describe('closeMergeRequest', () => {
2022-06-21 17:19:12 +05:30
it('sets state as closed', async () => {
await store.dispatch('closeIssuable', { notesData: { closeIssuePath: '' } });
expect(store.state.noteableData.state).toEqual('closed');
expect(store.state.isToggleStateButtonLoading).toEqual(false);
2018-03-27 19:54:05 +05:30
});
});
2021-02-22 17:27:13 +05:30
describe('reopenMergeRequest', () => {
2022-06-21 17:19:12 +05:30
it('sets state as reopened', async () => {
await store.dispatch('reopenIssuable', { notesData: { reopenIssuePath: '' } });
expect(store.state.noteableData.state).toEqual('reopened');
expect(store.state.isToggleStateButtonLoading).toEqual(false);
2018-03-27 19:54:05 +05:30
});
});
});
describe('emitStateChangedEvent', () => {
it('emits an event on the document', () => {
2021-04-17 20:07:23 +05:30
document.addEventListener(EVENT_ISSUABLE_VUE_APP_CHANGE, (event) => {
2018-03-27 19:54:05 +05:30
expect(event.detail.data).toEqual({ id: '1', state: 'closed' });
expect(event.detail.isClosed).toEqual(false);
});
store.dispatch('emitStateChangedEvent', { id: '1', state: 'closed' });
});
});
describe('toggleStateButtonLoading', () => {
2022-06-21 17:19:12 +05:30
it('should set loading as true', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.toggleStateButtonLoading,
true,
{},
[{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: true }],
[],
);
2018-03-27 19:54:05 +05:30
});
2022-06-21 17:19:12 +05:30
it('should set loading as false', () => {
return testAction(
2018-05-09 12:01:36 +05:30
actions.toggleStateButtonLoading,
false,
{},
[{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: false }],
[],
);
2018-03-27 19:54:05 +05:30
});
});
describe('toggleIssueLocalState', () => {
2022-06-21 17:19:12 +05:30
it('sets issue state as closed', () => {
return testAction(actions.toggleIssueLocalState, 'closed', {}, [{ type: 'CLOSE_ISSUE' }], []);
2018-03-27 19:54:05 +05:30
});
2022-06-21 17:19:12 +05:30
it('sets issue state as reopened', () => {
return testAction(
2018-11-08 19:23:39 +05:30
actions.toggleIssueLocalState,
'reopened',
{},
[{ type: 'REOPEN_ISSUE' }],
[],
);
2018-03-27 19:54:05 +05:30
});
});
2020-05-24 23:13:21 +05:30
describe('poll', () => {
2021-09-04 01:27:46 +05:30
const pollInterval = 6000;
const pollResponse = { notes: [], last_fetched_at: '123456' };
const pollHeaders = { 'poll-interval': `${pollInterval}` };
const successMock = () =>
axiosMock.onGet(notesDataMock.notesPath).reply(200, pollResponse, pollHeaders);
const failureMock = () => axiosMock.onGet(notesDataMock.notesPath).reply(500);
const advanceAndRAF = async (time) => {
if (time) {
jest.advanceTimersByTime(time);
}
2022-08-13 15:12:31 +05:30
return waitForPromises();
2021-09-04 01:27:46 +05:30
};
const advanceXMoreIntervals = async (number) => {
const timeoutLength = pollInterval * number;
return advanceAndRAF(timeoutLength);
};
const startPolling = async () => {
await store.dispatch('poll');
await advanceAndRAF(2);
};
const cleanUp = async () => {
jest.clearAllTimers();
return store.dispatch('stopPolling');
};
2018-03-27 19:54:05 +05:30
2022-06-21 17:19:12 +05:30
beforeEach(() => {
return store.dispatch('setNotesData', notesDataMock);
2018-03-27 19:54:05 +05:30
});
2021-09-04 01:27:46 +05:30
afterEach(() => {
return cleanUp();
});
it('calls service with last fetched state', async () => {
successMock();
await startPolling();
expect(store.state.lastFetchedAt).toBe('123456');
await advanceXMoreIntervals(1);
expect(axiosMock.history.get).toHaveLength(2);
expect(axiosMock.history.get[1].headers).toMatchObject({
'X-Last-Fetched-At': '123456',
});
});
describe('polling side effects', () => {
it('retries twice', async () => {
failureMock();
await startPolling();
// This is the first request, not a retry
expect(axiosMock.history.get).toHaveLength(1);
await advanceXMoreIntervals(1);
// Retry #1
expect(axiosMock.history.get).toHaveLength(2);
await advanceXMoreIntervals(1);
// Retry #2
expect(axiosMock.history.get).toHaveLength(3);
await advanceXMoreIntervals(10);
// There are no more retries
expect(axiosMock.history.get).toHaveLength(3);
});
it('shows the error display on the second failure', async () => {
failureMock();
await startPolling();
expect(axiosMock.history.get).toHaveLength(1);
2023-03-04 22:38:38 +05:30
expect(createAlert).not.toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
await advanceXMoreIntervals(1);
expect(axiosMock.history.get).toHaveLength(2);
2023-03-04 22:38:38 +05:30
expect(createAlert).toHaveBeenCalled();
expect(createAlert).toHaveBeenCalledTimes(1);
2021-09-04 01:27:46 +05:30
});
it('resets the failure counter on success', async () => {
// We can't get access to the actual counter in the polling closure.
// So we can infer that it's reset by ensuring that the error is only
// shown when we cause two failures in a row - no successes between
axiosMock
.onGet(notesDataMock.notesPath)
.replyOnce(500) // cause one error
.onGet(notesDataMock.notesPath)
.replyOnce(200, pollResponse, pollHeaders) // then a success
.onGet(notesDataMock.notesPath)
.reply(500); // and then more errors
await startPolling(); // Failure #1
await advanceXMoreIntervals(1); // Success #1
await advanceXMoreIntervals(1); // Failure #2
// That was the first failure AFTER a success, so we should NOT see the error displayed
2023-03-04 22:38:38 +05:30
expect(createAlert).not.toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
// Now we'll allow another failure
await advanceXMoreIntervals(1); // Failure #3
// Since this is the second failure in a row, the error should happen
2023-03-04 22:38:38 +05:30
expect(createAlert).toHaveBeenCalledTimes(1);
2021-09-04 01:27:46 +05:30
});
it('hides the error display if it exists on success', async () => {
failureMock();
await startPolling();
await advanceXMoreIntervals(2);
// After two errors, the error should be displayed
2023-03-04 22:38:38 +05:30
expect(createAlert).toHaveBeenCalledTimes(1);
2021-09-04 01:27:46 +05:30
axiosMock.reset();
successMock();
await advanceXMoreIntervals(1);
2023-03-04 22:38:38 +05:30
expect(mockAlertDismiss).toHaveBeenCalledTimes(1);
2021-09-04 01:27:46 +05:30
});
2018-03-27 19:54:05 +05:30
});
});
2018-11-08 19:23:39 +05:30
describe('setNotesFetchedState', () => {
2022-06-21 17:19:12 +05:30
it('should set notes fetched state', () => {
return testAction(
2018-11-08 19:23:39 +05:30
actions.setNotesFetchedState,
true,
{},
[{ type: 'SET_NOTES_FETCHED_STATE', payload: true }],
[],
);
});
});
2018-12-05 23:21:45 +05:30
2019-12-04 20:38:33 +05:30
describe('removeNote', () => {
2019-09-30 21:07:59 +05:30
const endpoint = `${TEST_HOST}/note`;
2018-12-05 23:21:45 +05:30
beforeEach(() => {
2019-09-30 21:07:59 +05:30
axiosMock.onDelete(endpoint).replyOnce(200, {});
2019-02-15 15:39:39 +05:30
2022-07-23 23:45:48 +05:30
document.body.dataset.page = '';
2018-12-05 23:21:45 +05:30
});
afterEach(() => {
2019-09-30 21:07:59 +05:30
axiosMock.restore();
2019-02-15 15:39:39 +05:30
2022-07-23 23:45:48 +05:30
document.body.dataset.page = '';
2018-12-05 23:21:45 +05:30
});
2022-06-21 17:19:12 +05:30
it('commits DELETE_NOTE and dispatches updateMergeRequestWidget', () => {
2019-09-30 21:07:59 +05:30
const note = { path: endpoint, id: 1 };
2018-12-05 23:21:45 +05:30
2022-06-21 17:19:12 +05:30
return testAction(
2019-12-04 20:38:33 +05:30
actions.removeNote,
2018-12-05 23:21:45 +05:30
note,
store.state,
[
{
type: 'DELETE_NOTE',
payload: note,
},
],
[
{
type: 'updateMergeRequestWidget',
},
2019-02-15 15:39:39 +05:30
{
2019-09-30 21:07:59 +05:30
type: 'updateResolvableDiscussionsCounts',
2019-02-15 15:39:39 +05:30
},
],
);
});
2022-06-21 17:19:12 +05:30
it('dispatches removeDiscussionsFromDiff on merge request page', () => {
2019-09-30 21:07:59 +05:30
const note = { path: endpoint, id: 1 };
2019-02-15 15:39:39 +05:30
2022-07-23 23:45:48 +05:30
document.body.dataset.page = 'projects:merge_requests:show';
2019-02-15 15:39:39 +05:30
2022-06-21 17:19:12 +05:30
return testAction(
2019-12-04 20:38:33 +05:30
actions.removeNote,
2019-02-15 15:39:39 +05:30
note,
store.state,
[
{
type: 'DELETE_NOTE',
payload: note,
},
],
[
{
type: 'updateMergeRequestWidget',
},
{
2019-09-30 21:07:59 +05:30
type: 'updateResolvableDiscussionsCounts',
2019-02-15 15:39:39 +05:30
},
{
type: 'diffs/removeDiscussionsFromDiff',
},
2018-12-05 23:21:45 +05:30
],
);
});
});
2019-12-04 20:38:33 +05:30
describe('deleteNote', () => {
const endpoint = `${TEST_HOST}/note`;
beforeEach(() => {
axiosMock.onDelete(endpoint).replyOnce(200, {});
2022-07-23 23:45:48 +05:30
document.body.dataset.page = '';
2019-12-04 20:38:33 +05:30
});
afterEach(() => {
axiosMock.restore();
2022-07-23 23:45:48 +05:30
document.body.dataset.page = '';
2019-12-04 20:38:33 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches removeNote', () => {
2019-12-04 20:38:33 +05:30
const note = { path: endpoint, id: 1 };
2022-06-21 17:19:12 +05:30
return testAction(
2019-12-04 20:38:33 +05:30
actions.deleteNote,
note,
{},
[],
[
{
type: 'removeNote',
payload: {
id: 1,
path: 'http://test.host/note',
},
},
],
);
});
});
2018-12-05 23:21:45 +05:30
describe('createNewNote', () => {
describe('success', () => {
const res = {
id: 1,
valid: true,
};
beforeEach(() => {
2019-12-04 20:38:33 +05:30
axiosMock.onAny().reply(200, res);
2018-12-05 23:21:45 +05:30
});
2022-06-21 17:19:12 +05:30
it('commits ADD_NEW_NOTE and dispatches updateMergeRequestWidget', () => {
return testAction(
2018-12-05 23:21:45 +05:30
actions.createNewNote,
2020-07-28 23:09:34 +05:30
{ endpoint: `${TEST_HOST}`, data: {} },
2018-12-05 23:21:45 +05:30
store.state,
[
{
type: 'ADD_NEW_NOTE',
payload: res,
},
],
[
{
type: 'updateMergeRequestWidget',
},
2019-02-15 15:39:39 +05:30
{
type: 'startTaskList',
},
{
2019-09-30 21:07:59 +05:30
type: 'updateResolvableDiscussionsCounts',
2019-02-15 15:39:39 +05:30
},
2018-12-05 23:21:45 +05:30
],
);
});
});
describe('error', () => {
const res = {
errors: ['error'],
};
beforeEach(() => {
2019-12-04 20:38:33 +05:30
axiosMock.onAny().replyOnce(200, res);
2018-12-05 23:21:45 +05:30
});
2022-06-21 17:19:12 +05:30
it('does not commit ADD_NEW_NOTE or dispatch updateMergeRequestWidget', () => {
return testAction(
2018-12-05 23:21:45 +05:30
actions.createNewNote,
2020-07-28 23:09:34 +05:30
{ endpoint: `${TEST_HOST}`, data: {} },
2018-12-05 23:21:45 +05:30
store.state,
[],
[],
);
});
});
});
describe('toggleResolveNote', () => {
const res = {
resolved: true,
};
beforeEach(() => {
2019-12-04 20:38:33 +05:30
axiosMock.onAny().reply(200, res);
2018-12-05 23:21:45 +05:30
});
describe('as note', () => {
2022-06-21 17:19:12 +05:30
it('commits UPDATE_NOTE and dispatches updateMergeRequestWidget', () => {
return testAction(
2018-12-05 23:21:45 +05:30
actions.toggleResolveNote,
2020-07-28 23:09:34 +05:30
{ endpoint: `${TEST_HOST}`, isResolved: true, discussion: false },
2018-12-05 23:21:45 +05:30
store.state,
[
{
type: 'UPDATE_NOTE',
payload: res,
},
],
[
2019-02-15 15:39:39 +05:30
{
2019-09-30 21:07:59 +05:30
type: 'updateResolvableDiscussionsCounts',
2019-02-15 15:39:39 +05:30
},
2018-12-05 23:21:45 +05:30
{
type: 'updateMergeRequestWidget',
},
],
);
});
});
describe('as discussion', () => {
2022-06-21 17:19:12 +05:30
it('commits UPDATE_DISCUSSION and dispatches updateMergeRequestWidget', () => {
return testAction(
2018-12-05 23:21:45 +05:30
actions.toggleResolveNote,
2020-07-28 23:09:34 +05:30
{ endpoint: `${TEST_HOST}`, isResolved: true, discussion: true },
2018-12-05 23:21:45 +05:30
store.state,
[
{
type: 'UPDATE_DISCUSSION',
payload: res,
},
],
[
2019-02-15 15:39:39 +05:30
{
2019-09-30 21:07:59 +05:30
type: 'updateResolvableDiscussionsCounts',
2019-02-15 15:39:39 +05:30
},
2018-12-05 23:21:45 +05:30
{
type: 'updateMergeRequestWidget',
},
],
);
});
});
});
describe('updateMergeRequestWidget', () => {
it('calls mrWidget checkStatus', () => {
2020-04-08 14:13:33 +05:30
jest.spyOn(mrWidgetEventHub, '$emit').mockImplementation(() => {});
2018-12-05 23:21:45 +05:30
actions.updateMergeRequestWidget();
expect(mrWidgetEventHub.$emit).toHaveBeenCalledWith('mr.discussion.updated');
});
});
2018-12-13 13:39:08 +05:30
describe('setCommentsDisabled', () => {
2022-06-21 17:19:12 +05:30
it('should set comments disabled state', () => {
return testAction(
2018-12-13 13:39:08 +05:30
actions.setCommentsDisabled,
true,
null,
[{ type: 'DISABLE_COMMENTS', payload: true }],
[],
);
});
});
2019-02-15 15:39:39 +05:30
2019-09-30 21:07:59 +05:30
describe('updateResolvableDiscussionsCounts', () => {
2022-06-21 17:19:12 +05:30
it('commits UPDATE_RESOLVABLE_DISCUSSIONS_COUNTS', () => {
return testAction(
2019-09-30 21:07:59 +05:30
actions.updateResolvableDiscussionsCounts,
2019-02-15 15:39:39 +05:30
null,
{},
[{ type: 'UPDATE_RESOLVABLE_DISCUSSIONS_COUNTS' }],
[],
);
});
});
2019-03-02 22:35:43 +05:30
describe('convertToDiscussion', () => {
2022-06-21 17:19:12 +05:30
it('commits CONVERT_TO_DISCUSSION with noteId', () => {
2019-03-02 22:35:43 +05:30
const noteId = 'dummy-note-id';
2022-06-21 17:19:12 +05:30
return testAction(
2019-03-02 22:35:43 +05:30
actions.convertToDiscussion,
noteId,
{},
[{ type: 'CONVERT_TO_DISCUSSION', payload: noteId }],
[],
);
});
});
2019-07-07 11:18:12 +05:30
describe('updateOrCreateNotes', () => {
2020-11-24 15:15:51 +05:30
it('Prevents `fetchDiscussions` being called multiple times within time limit', () => {
const note = { id: 1234, type: notesConstants.DIFF_NOTE };
const getters = { notesById: {} };
state = { discussions: [note], notesData: { discussionsPath: '' } };
commit.mockImplementation((type, value) => {
if (type === mutationTypes.SET_FETCHING_DISCUSSIONS) {
mutations[type](state, value);
}
});
actions.updateOrCreateNotes({ commit, state, getters, dispatch }, [note]);
actions.updateOrCreateNotes({ commit, state, getters, dispatch }, [note]);
jest.runAllTimers();
actions.updateOrCreateNotes({ commit, state, getters, dispatch }, [note]);
expect(dispatch).toHaveBeenCalledTimes(2);
});
2019-07-07 11:18:12 +05:30
it('Updates existing note', () => {
const note = { id: 1234 };
const getters = { notesById: { 1234: note } };
actions.updateOrCreateNotes({ commit, state, getters, dispatch }, [note]);
2020-04-08 14:13:33 +05:30
expect(commit.mock.calls).toEqual([[mutationTypes.UPDATE_NOTE, note]]);
2019-07-07 11:18:12 +05:30
});
it('Creates a new note if none exisits', () => {
const note = { id: 1234 };
const getters = { notesById: {} };
actions.updateOrCreateNotes({ commit, state, getters, dispatch }, [note]);
2020-04-08 14:13:33 +05:30
expect(commit.mock.calls).toEqual([[mutationTypes.ADD_NEW_NOTE, note]]);
2019-07-07 11:18:12 +05:30
});
describe('Discussion notes', () => {
let note;
let getters;
beforeEach(() => {
note = { id: 1234 };
getters = { notesById: {} };
});
it('Adds a reply to an existing discussion', () => {
state = { discussions: [note] };
const discussionNote = {
...note,
type: notesConstants.DISCUSSION_NOTE,
discussion_id: 1234,
};
actions.updateOrCreateNotes({ commit, state, getters, dispatch }, [discussionNote]);
2020-04-08 14:13:33 +05:30
expect(commit.mock.calls).toEqual([
2019-07-07 11:18:12 +05:30
[mutationTypes.ADD_NEW_REPLY_TO_DISCUSSION, discussionNote],
]);
});
it('fetches discussions for diff notes', () => {
state = { discussions: [], notesData: { discussionsPath: 'Hello world' } };
const diffNote = { ...note, type: notesConstants.DIFF_NOTE, discussion_id: 1234 };
actions.updateOrCreateNotes({ commit, state, getters, dispatch }, [diffNote]);
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([
2019-07-07 11:18:12 +05:30
['fetchDiscussions', { path: state.notesData.discussionsPath }],
]);
});
it('Adds a new note', () => {
state = { discussions: [] };
const discussionNote = {
...note,
type: notesConstants.DISCUSSION_NOTE,
discussion_id: 1234,
};
actions.updateOrCreateNotes({ commit, state, getters, dispatch }, [discussionNote]);
2020-04-08 14:13:33 +05:30
expect(commit.mock.calls).toEqual([[mutationTypes.ADD_NEW_NOTE, discussionNote]]);
2019-07-07 11:18:12 +05:30
});
});
});
describe('replyToDiscussion', () => {
const payload = { endpoint: TEST_HOST, data: {} };
2022-06-21 17:19:12 +05:30
it('updates discussion if response contains disussion', () => {
2019-12-04 20:38:33 +05:30
const discussion = { notes: [] };
axiosMock.onAny().reply(200, { discussion });
2022-06-21 17:19:12 +05:30
return testAction(
2019-07-07 11:18:12 +05:30
actions.replyToDiscussion,
payload,
{
notesById: {},
},
2019-12-04 20:38:33 +05:30
[{ type: mutationTypes.UPDATE_DISCUSSION, payload: discussion }],
2019-07-07 11:18:12 +05:30
[
{ type: 'updateMergeRequestWidget' },
{ type: 'startTaskList' },
2019-09-30 21:07:59 +05:30
{ type: 'updateResolvableDiscussionsCounts' },
2019-07-07 11:18:12 +05:30
],
);
});
2022-06-21 17:19:12 +05:30
it('adds a reply to a discussion', () => {
2019-12-04 20:38:33 +05:30
const res = {};
axiosMock.onAny().reply(200, res);
2019-07-07 11:18:12 +05:30
2022-06-21 17:19:12 +05:30
return testAction(
2019-07-07 11:18:12 +05:30
actions.replyToDiscussion,
payload,
{
notesById: {},
},
[{ type: mutationTypes.ADD_NEW_REPLY_TO_DISCUSSION, payload: res }],
[],
);
});
});
describe('removeConvertedDiscussion', () => {
2022-06-21 17:19:12 +05:30
it('commits CONVERT_TO_DISCUSSION with noteId', () => {
2019-07-07 11:18:12 +05:30
const noteId = 'dummy-id';
2022-06-21 17:19:12 +05:30
return testAction(
2019-07-07 11:18:12 +05:30
actions.removeConvertedDiscussion,
noteId,
{},
[{ type: 'REMOVE_CONVERTED_DISCUSSION', payload: noteId }],
[],
);
});
});
2019-07-31 22:56:46 +05:30
describe('resolveDiscussion', () => {
let getters;
let discussionId;
beforeEach(() => {
discussionId = discussionMock.id;
state.discussions = [discussionMock];
getters = {
isDiscussionResolved: () => false,
};
});
2022-06-21 17:19:12 +05:30
it('when unresolved, dispatches action', () => {
return testAction(
2019-07-31 22:56:46 +05:30
actions.resolveDiscussion,
{ discussionId },
{ ...state, ...getters },
[],
[
{
type: 'toggleResolveNote',
payload: {
endpoint: discussionMock.resolve_path,
isResolved: false,
discussion: true,
},
},
],
);
});
2022-06-21 17:19:12 +05:30
it('when resolved, does nothing', () => {
2021-03-08 18:12:59 +05:30
getters.isDiscussionResolved = (id) => id === discussionId;
2019-07-31 22:56:46 +05:30
2022-06-21 17:19:12 +05:30
return testAction(
2019-07-31 22:56:46 +05:30
actions.resolveDiscussion,
{ discussionId },
{ ...state, ...getters },
[],
[],
);
});
});
2019-09-04 21:01:54 +05:30
describe('saveNote', () => {
2020-01-01 13:55:28 +05:30
const flashContainer = {};
const payload = { endpoint: TEST_HOST, data: { 'note[note]': 'some text' }, flashContainer };
2019-09-04 21:01:54 +05:30
describe('if response contains errors', () => {
const res = { errors: { something: ['went wrong'] } };
2020-01-01 13:55:28 +05:30
const error = { message: 'Unprocessable entity', response: { data: res } };
2019-09-04 21:01:54 +05:30
2022-06-21 17:19:12 +05:30
it('throws an error', async () => {
await expect(
actions.saveNote(
2019-09-04 21:01:54 +05:30
{
commit() {},
2020-01-01 13:55:28 +05:30
dispatch: () => Promise.reject(error),
2019-09-04 21:01:54 +05:30
},
payload,
2022-06-21 17:19:12 +05:30
),
).rejects.toEqual(error);
2023-03-04 22:38:38 +05:30
expect(createAlert).not.toHaveBeenCalled();
2020-01-01 13:55:28 +05:30
});
});
describe('if response contains errors.base', () => {
const res = { errors: { base: ['something went wrong'] } };
const error = { message: 'Unprocessable entity', response: { data: res } };
2022-06-21 17:19:12 +05:30
it('sets flash alert using errors.base message', async () => {
const resp = await actions.saveNote(
{
commit() {},
dispatch: () => Promise.reject(error),
},
{ ...payload, flashContainer },
);
2023-03-04 22:38:38 +05:30
expect(resp.hasAlert).toBe(true);
expect(createAlert).toHaveBeenCalledWith({
2022-06-21 17:19:12 +05:30
message: 'Your comment could not be submitted because something went wrong',
parent: flashContainer,
});
2019-09-04 21:01:54 +05:30
});
});
describe('if response contains no errors', () => {
const res = { valid: true };
2022-06-21 17:19:12 +05:30
it('returns the response', async () => {
const data = await actions.saveNote(
{
commit() {},
dispatch: () => Promise.resolve(res),
},
payload,
);
expect(data).toBe(res);
2023-03-04 22:38:38 +05:30
expect(createAlert).not.toHaveBeenCalled();
2019-09-04 21:01:54 +05:30
});
});
});
2019-07-31 22:56:46 +05:30
describe('submitSuggestion', () => {
const discussionId = 'discussion-id';
const noteId = 'note-id';
const suggestionId = 'suggestion-id';
let flashContainer;
beforeEach(() => {
2020-04-08 14:13:33 +05:30
jest.spyOn(Api, 'applySuggestion').mockReturnValue(Promise.resolve());
dispatch.mockReturnValue(Promise.resolve());
2019-07-31 22:56:46 +05:30
flashContainer = {};
});
2022-06-21 17:19:12 +05:30
const testSubmitSuggestion = async (expectFn) => {
await actions.submitSuggestion(
{ commit, dispatch },
{ discussionId, noteId, suggestionId, flashContainer },
);
expectFn();
2019-07-31 22:56:46 +05:30
};
2022-06-21 17:19:12 +05:30
it('when service success, commits and resolves discussion', () => {
testSubmitSuggestion(() => {
2020-04-08 14:13:33 +05:30
expect(commit.mock.calls).toEqual([
2021-02-22 17:27:13 +05:30
[mutationTypes.SET_RESOLVING_DISCUSSION, true],
[mutationTypes.SET_RESOLVING_DISCUSSION, false],
2019-07-31 22:56:46 +05:30
]);
2021-02-22 17:27:13 +05:30
expect(dispatch.mock.calls).toEqual([
['stopPolling'],
['resolveDiscussion', { discussionId }],
['restartPolling'],
]);
2023-03-04 22:38:38 +05:30
expect(createAlert).not.toHaveBeenCalled();
2019-07-31 22:56:46 +05:30
});
});
2022-06-21 17:19:12 +05:30
it('when service fails, flashes error message', () => {
2019-07-31 22:56:46 +05:30
const response = { response: { data: { message: TEST_ERROR_MESSAGE } } };
2020-04-08 14:13:33 +05:30
Api.applySuggestion.mockReturnValue(Promise.reject(response));
2019-07-31 22:56:46 +05:30
2022-06-21 17:19:12 +05:30
return testSubmitSuggestion(() => {
2021-02-22 17:27:13 +05:30
expect(commit.mock.calls).toEqual([
[mutationTypes.SET_RESOLVING_DISCUSSION, true],
[mutationTypes.SET_RESOLVING_DISCUSSION, false],
]);
expect(dispatch.mock.calls).toEqual([['stopPolling'], ['restartPolling']]);
2023-03-04 22:38:38 +05:30
expect(createAlert).toHaveBeenCalledWith({
2021-09-30 23:02:18 +05:30
message: TEST_ERROR_MESSAGE,
parent: flashContainer,
});
2020-06-23 00:09:42 +05:30
});
});
2022-06-21 17:19:12 +05:30
it('when service fails, and no error message available, uses default message', () => {
2020-06-23 00:09:42 +05:30
const response = { response: 'foo' };
Api.applySuggestion.mockReturnValue(Promise.reject(response));
2022-06-21 17:19:12 +05:30
return testSubmitSuggestion(() => {
2021-02-22 17:27:13 +05:30
expect(commit.mock.calls).toEqual([
[mutationTypes.SET_RESOLVING_DISCUSSION, true],
[mutationTypes.SET_RESOLVING_DISCUSSION, false],
]);
expect(dispatch.mock.calls).toEqual([['stopPolling'], ['restartPolling']]);
2023-03-04 22:38:38 +05:30
expect(createAlert).toHaveBeenCalledWith({
2021-09-30 23:02:18 +05:30
message: 'Something went wrong while applying the suggestion. Please try again.',
parent: flashContainer,
});
2019-07-31 22:56:46 +05:30
});
});
2022-06-21 17:19:12 +05:30
it('when resolve discussion fails, fail gracefully', () => {
2020-04-08 14:13:33 +05:30
dispatch.mockReturnValue(Promise.reject());
2019-07-31 22:56:46 +05:30
2022-06-21 17:19:12 +05:30
return testSubmitSuggestion(() => {
2023-03-04 22:38:38 +05:30
expect(createAlert).not.toHaveBeenCalled();
2019-07-31 22:56:46 +05:30
});
});
});
2019-10-12 21:52:04 +05:30
2020-06-23 00:09:42 +05:30
describe('submitSuggestionBatch', () => {
const discussionIds = batchSuggestionsInfoMock.map(({ discussionId }) => discussionId);
const batchSuggestionsInfo = batchSuggestionsInfoMock;
let flashContainer;
beforeEach(() => {
jest.spyOn(Api, 'applySuggestionBatch');
dispatch.mockReturnValue(Promise.resolve());
Api.applySuggestionBatch.mockReturnValue(Promise.resolve());
state = { batchSuggestionsInfo };
flashContainer = {};
});
2022-06-21 17:19:12 +05:30
const testSubmitSuggestionBatch = async (expectFn) => {
await actions.submitSuggestionBatch({ commit, dispatch, state }, { flashContainer });
expectFn();
2020-06-23 00:09:42 +05:30
};
2022-06-21 17:19:12 +05:30
it('when service succeeds, commits, resolves discussions, resets batch and applying batch state', () => {
testSubmitSuggestionBatch(() => {
2020-06-23 00:09:42 +05:30
expect(commit.mock.calls).toEqual([
[mutationTypes.SET_APPLYING_BATCH_STATE, true],
2021-02-22 17:27:13 +05:30
[mutationTypes.SET_RESOLVING_DISCUSSION, true],
2020-06-23 00:09:42 +05:30
[mutationTypes.CLEAR_SUGGESTION_BATCH],
[mutationTypes.SET_APPLYING_BATCH_STATE, false],
2021-02-22 17:27:13 +05:30
[mutationTypes.SET_RESOLVING_DISCUSSION, false],
2020-06-23 00:09:42 +05:30
]);
expect(dispatch.mock.calls).toEqual([
2021-02-22 17:27:13 +05:30
['stopPolling'],
2020-06-23 00:09:42 +05:30
['resolveDiscussion', { discussionId: discussionIds[0] }],
['resolveDiscussion', { discussionId: discussionIds[1] }],
2021-02-22 17:27:13 +05:30
['restartPolling'],
2020-06-23 00:09:42 +05:30
]);
2023-03-04 22:38:38 +05:30
expect(createAlert).not.toHaveBeenCalled();
2020-06-23 00:09:42 +05:30
});
});
2022-06-21 17:19:12 +05:30
it('when service fails, flashes error message, resets applying batch state', () => {
2020-06-23 00:09:42 +05:30
const response = { response: { data: { message: TEST_ERROR_MESSAGE } } };
Api.applySuggestionBatch.mockReturnValue(Promise.reject(response));
2022-06-21 17:19:12 +05:30
testSubmitSuggestionBatch(() => {
2020-06-23 00:09:42 +05:30
expect(commit.mock.calls).toEqual([
[mutationTypes.SET_APPLYING_BATCH_STATE, true],
2021-02-22 17:27:13 +05:30
[mutationTypes.SET_RESOLVING_DISCUSSION, true],
2020-06-23 00:09:42 +05:30
[mutationTypes.SET_APPLYING_BATCH_STATE, false],
2021-02-22 17:27:13 +05:30
[mutationTypes.SET_RESOLVING_DISCUSSION, false],
2020-06-23 00:09:42 +05:30
]);
2021-02-22 17:27:13 +05:30
expect(dispatch.mock.calls).toEqual([['stopPolling'], ['restartPolling']]);
2023-03-04 22:38:38 +05:30
expect(createAlert).toHaveBeenCalledWith({
2021-09-30 23:02:18 +05:30
message: TEST_ERROR_MESSAGE,
parent: flashContainer,
});
2020-06-23 00:09:42 +05:30
});
});
2022-06-21 17:19:12 +05:30
it('when service fails, and no error message available, uses default message', () => {
2020-06-23 00:09:42 +05:30
const response = { response: 'foo' };
Api.applySuggestionBatch.mockReturnValue(Promise.reject(response));
2022-06-21 17:19:12 +05:30
testSubmitSuggestionBatch(() => {
2020-06-23 00:09:42 +05:30
expect(commit.mock.calls).toEqual([
[mutationTypes.SET_APPLYING_BATCH_STATE, true],
2021-02-22 17:27:13 +05:30
[mutationTypes.SET_RESOLVING_DISCUSSION, true],
2020-06-23 00:09:42 +05:30
[mutationTypes.SET_APPLYING_BATCH_STATE, false],
2021-02-22 17:27:13 +05:30
[mutationTypes.SET_RESOLVING_DISCUSSION, false],
2020-06-23 00:09:42 +05:30
]);
2021-02-22 17:27:13 +05:30
expect(dispatch.mock.calls).toEqual([['stopPolling'], ['restartPolling']]);
2023-03-04 22:38:38 +05:30
expect(createAlert).toHaveBeenCalledWith({
2021-09-30 23:02:18 +05:30
message:
'Something went wrong while applying the batch of suggestions. Please try again.',
parent: flashContainer,
});
2020-06-23 00:09:42 +05:30
});
});
2022-06-21 17:19:12 +05:30
it('when resolve discussions fails, fails gracefully, resets batch and applying batch state', () => {
2020-06-23 00:09:42 +05:30
dispatch.mockReturnValue(Promise.reject());
2022-06-21 17:19:12 +05:30
testSubmitSuggestionBatch(() => {
2020-06-23 00:09:42 +05:30
expect(commit.mock.calls).toEqual([
[mutationTypes.SET_APPLYING_BATCH_STATE, true],
2021-02-22 17:27:13 +05:30
[mutationTypes.SET_RESOLVING_DISCUSSION, true],
2020-06-23 00:09:42 +05:30
[mutationTypes.CLEAR_SUGGESTION_BATCH],
[mutationTypes.SET_APPLYING_BATCH_STATE, false],
2021-02-22 17:27:13 +05:30
[mutationTypes.SET_RESOLVING_DISCUSSION, false],
2020-06-23 00:09:42 +05:30
]);
2023-03-04 22:38:38 +05:30
expect(createAlert).not.toHaveBeenCalled();
2020-06-23 00:09:42 +05:30
});
});
});
describe('addSuggestionInfoToBatch', () => {
const suggestionInfo = batchSuggestionsInfoMock[0];
2022-06-21 17:19:12 +05:30
it("adds a suggestion's info to the current batch", () => {
return testAction(
2020-06-23 00:09:42 +05:30
actions.addSuggestionInfoToBatch,
suggestionInfo,
{ batchSuggestionsInfo: [] },
[{ type: 'ADD_SUGGESTION_TO_BATCH', payload: suggestionInfo }],
[],
);
});
});
describe('removeSuggestionInfoFromBatch', () => {
const suggestionInfo = batchSuggestionsInfoMock[0];
2022-06-21 17:19:12 +05:30
it("removes a suggestion's info the current batch", () => {
return testAction(
2020-06-23 00:09:42 +05:30
actions.removeSuggestionInfoFromBatch,
suggestionInfo.suggestionId,
{ batchSuggestionsInfo: [suggestionInfo] },
[{ type: 'REMOVE_SUGGESTION_FROM_BATCH', payload: suggestionInfo.suggestionId }],
[],
);
});
});
2019-10-12 21:52:04 +05:30
describe('filterDiscussion', () => {
const path = 'some-discussion-path';
const filter = 0;
beforeEach(() => {
2020-04-08 14:13:33 +05:30
dispatch.mockReturnValue(new Promise(() => {}));
2019-10-12 21:52:04 +05:30
});
2022-01-26 12:08:38 +05:30
it('clears existing discussions', () => {
actions.filterDiscussion({ commit, dispatch }, { path, filter, persistFilter: false });
expect(commit.mock.calls).toEqual([[mutationTypes.CLEAR_DISCUSSIONS]]);
});
2019-10-12 21:52:04 +05:30
it('fetches discussions with filter and persistFilter false', () => {
2022-01-26 12:08:38 +05:30
actions.filterDiscussion({ commit, dispatch }, { path, filter, persistFilter: false });
2019-10-12 21:52:04 +05:30
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([
2019-10-12 21:52:04 +05:30
['setLoadingState', true],
['fetchDiscussions', { path, filter, persistFilter: false }],
]);
});
it('fetches discussions with filter and persistFilter true', () => {
2022-01-26 12:08:38 +05:30
actions.filterDiscussion({ commit, dispatch }, { path, filter, persistFilter: true });
2019-10-12 21:52:04 +05:30
2020-04-08 14:13:33 +05:30
expect(dispatch.mock.calls).toEqual([
2019-10-12 21:52:04 +05:30
['setLoadingState', true],
['fetchDiscussions', { path, filter, persistFilter: true }],
]);
});
});
2020-04-22 19:07:51 +05:30
describe('setDiscussionSortDirection', () => {
2022-06-21 17:19:12 +05:30
it('calls the correct mutation with the correct args', () => {
return testAction(
2020-04-22 19:07:51 +05:30
actions.setDiscussionSortDirection,
2021-01-03 14:25:43 +05:30
{ direction: notesConstants.DESC, persist: false },
2020-04-22 19:07:51 +05:30
{},
2021-01-03 14:25:43 +05:30
[
{
type: mutationTypes.SET_DISCUSSIONS_SORT,
payload: { direction: notesConstants.DESC, persist: false },
},
],
2020-04-22 19:07:51 +05:30
[],
);
});
});
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
describe('setSelectedCommentPosition', () => {
2022-06-21 17:19:12 +05:30
it('calls the correct mutation with the correct args', () => {
return testAction(
2020-07-28 23:09:34 +05:30
actions.setSelectedCommentPosition,
{},
{},
[{ type: mutationTypes.SET_SELECTED_COMMENT_POSITION, payload: {} }],
[],
);
});
});
2020-06-23 00:09:42 +05:30
describe('softDeleteDescriptionVersion', () => {
const endpoint = '/path/to/diff/1';
const payload = {
endpoint,
startingVersion: undefined,
versionId: 1,
};
describe('if response contains no errors', () => {
2022-06-21 17:19:12 +05:30
it('dispatches requestDeleteDescriptionVersion', () => {
2020-06-23 00:09:42 +05:30
axiosMock.onDelete(endpoint).replyOnce(200);
2022-06-21 17:19:12 +05:30
return testAction(
2020-06-23 00:09:42 +05:30
actions.softDeleteDescriptionVersion,
payload,
{},
[],
[
{
type: 'requestDeleteDescriptionVersion',
},
{
type: 'receiveDeleteDescriptionVersion',
payload: payload.versionId,
},
],
);
});
});
describe('if response contains errors', () => {
const errorMessage = 'Request failed with status code 503';
2022-06-21 17:19:12 +05:30
it('dispatches receiveDeleteDescriptionVersionError and throws an error', async () => {
2020-06-23 00:09:42 +05:30
axiosMock.onDelete(endpoint).replyOnce(503);
2022-06-21 17:19:12 +05:30
await expect(
testAction(
actions.softDeleteDescriptionVersion,
payload,
{},
[],
[
{
type: 'requestDeleteDescriptionVersion',
},
{
type: 'receiveDeleteDescriptionVersionError',
payload: new Error(errorMessage),
},
],
),
).rejects.toEqual(new Error());
2023-03-04 22:38:38 +05:30
expect(createAlert).toHaveBeenCalled();
2020-06-23 00:09:42 +05:30
});
});
});
2020-07-28 23:09:34 +05:30
describe('setConfidentiality', () => {
it('calls the correct mutation with the correct args', () => {
testAction(actions.setConfidentiality, true, { noteableData: { confidential: false } }, [
{ type: mutationTypes.SET_ISSUE_CONFIDENTIAL, payload: true },
]);
});
});
2020-06-23 00:09:42 +05:30
describe('updateAssignees', () => {
2022-06-21 17:19:12 +05:30
it('update the assignees state', () => {
return testAction(
2020-06-23 00:09:42 +05:30
actions.updateAssignees,
[userDataMock.id],
{ state: noteableDataMock },
[{ type: mutationTypes.UPDATE_ASSIGNEES, payload: [userDataMock.id] }],
[],
);
});
});
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
describe.each`
issuableType
${'issue'} | ${'merge_request'}
`('updateLockedAttribute for issuableType=$issuableType', ({ issuableType }) => {
// Payload for mutation query
state = { noteableData: { discussion_locked: false } };
const targetType = issuableType;
const getters = { getNoteableData: { iid: '1', targetType } };
// Target state after mutation
const locked = true;
const actionArgs = { fullPath: 'full/path', locked };
const input = { iid: '1', projectPath: 'full/path', locked: true };
// Helper functions
const targetMutation = () => {
return targetType === 'issue' ? updateIssueLockMutation : updateMergeRequestLockMutation;
};
const mockResolvedValue = () => {
return targetType === 'issue'
? { data: { issueSetLocked: { issue: { discussionLocked: locked } } } }
: { data: { mergeRequestSetLocked: { mergeRequest: { discussionLocked: locked } } } };
};
beforeEach(() => {
jest.spyOn(utils.gqClient, 'mutate').mockResolvedValue(mockResolvedValue());
});
it('calls gqClient mutation one time', () => {
actions.updateLockedAttribute({ commit: () => {}, state, getters }, actionArgs);
expect(utils.gqClient.mutate).toHaveBeenCalledTimes(1);
});
it('calls gqClient mutation with the correct values', () => {
actions.updateLockedAttribute({ commit: () => {}, state, getters }, actionArgs);
expect(utils.gqClient.mutate).toHaveBeenCalledWith({
mutation: targetMutation(),
variables: { input },
});
});
describe('on success of mutation', () => {
it('calls commit with the correct values', () => {
const commitSpy = jest.fn();
return actions
.updateLockedAttribute({ commit: commitSpy, state, getters }, actionArgs)
.then(() => {
expect(commitSpy).toHaveBeenCalledWith(mutationTypes.SET_ISSUABLE_LOCK, locked);
});
});
});
});
describe('updateDiscussionPosition', () => {
2022-06-21 17:19:12 +05:30
it('update the assignees state', () => {
2020-10-24 23:57:45 +05:30
const updatedPosition = { discussionId: 1, position: { test: true } };
2022-06-21 17:19:12 +05:30
return testAction(
2020-10-24 23:57:45 +05:30
actions.updateDiscussionPosition,
updatedPosition,
{ state: { discussions: [] } },
[{ type: mutationTypes.UPDATE_DISCUSSION_POSITION, payload: updatedPosition }],
[],
);
});
});
2021-03-11 19:13:27 +05:30
2022-10-11 01:57:18 +05:30
describe('promoteCommentToTimelineEvent', () => {
const actionArgs = {
noteId: '1',
addError: 'addError: Create error',
addGenericError: 'addGenericError',
};
const commitSpy = jest.fn();
describe('for successful request', () => {
const timelineEventSuccessResponse = {
data: {
timelineEventPromoteFromNote: {
timelineEvent: {
id: 'gid://gitlab/IncidentManagement::TimelineEvent/19',
},
errors: [],
},
},
};
beforeEach(() => {
jest.spyOn(utils.gqClient, 'mutate').mockResolvedValue(timelineEventSuccessResponse);
});
it('calls gqClient mutation with the correct values', () => {
actions.promoteCommentToTimelineEvent({ commit: () => {} }, actionArgs);
expect(utils.gqClient.mutate).toHaveBeenCalledTimes(1);
expect(utils.gqClient.mutate).toHaveBeenCalledWith({
mutation: promoteTimelineEvent,
variables: {
input: {
noteId: 'gid://gitlab/Note/1',
},
},
});
});
it('returns success response', () => {
jest.spyOn(notesEventHub, '$emit').mockImplementation(() => {});
return actions.promoteCommentToTimelineEvent({ commit: commitSpy }, actionArgs).then(() => {
expect(notesEventHub.$emit).toHaveBeenLastCalledWith(
'comment-promoted-to-timeline-event',
);
expect(toast).toHaveBeenCalledWith('Comment added to the timeline.');
expect(commitSpy).toHaveBeenCalledWith(
mutationTypes.SET_PROMOTE_COMMENT_TO_TIMELINE_PROGRESS,
false,
);
});
});
});
describe('for failing request', () => {
const errorResponse = {
data: {
timelineEventPromoteFromNote: {
timelineEvent: null,
errors: ['Create error'],
},
},
};
it.each`
mockReject | message | captureError | error
${true} | ${'addGenericError'} | ${true} | ${new Error()}
${false} | ${'addError: Create error'} | ${false} | ${null}
`(
'should show an error when submission fails',
({ mockReject, message, captureError, error }) => {
const expectedAlertArgs = {
captureError,
error,
message,
};
if (mockReject) {
jest.spyOn(utils.gqClient, 'mutate').mockRejectedValueOnce(new Error());
} else {
jest.spyOn(utils.gqClient, 'mutate').mockResolvedValue(errorResponse);
}
return actions
.promoteCommentToTimelineEvent({ commit: commitSpy }, actionArgs)
.then(() => {
2023-03-04 22:38:38 +05:30
expect(createAlert).toHaveBeenCalledWith(expectedAlertArgs);
2022-10-11 01:57:18 +05:30
expect(commitSpy).toHaveBeenCalledWith(
mutationTypes.SET_PROMOTE_COMMENT_TO_TIMELINE_PROGRESS,
false,
);
});
},
);
});
});
2021-03-11 19:13:27 +05:30
describe('setFetchingState', () => {
2022-06-21 17:19:12 +05:30
it('commits SET_NOTES_FETCHING_STATE', () => {
return testAction(
2021-03-11 19:13:27 +05:30
actions.setFetchingState,
true,
null,
[{ type: mutationTypes.SET_NOTES_FETCHING_STATE, payload: true }],
[],
);
});
});
2021-12-11 22:18:48 +05:30
describe('fetchDiscussions', () => {
const discussion = { notes: [] };
afterEach(() => {
window.gon = {};
});
2022-06-21 17:19:12 +05:30
it('updates the discussions and dispatches `updateResolvableDiscussionsCounts`', () => {
2021-12-11 22:18:48 +05:30
axiosMock.onAny().reply(200, { discussion });
2022-06-21 17:19:12 +05:30
return testAction(
2021-12-11 22:18:48 +05:30
actions.fetchDiscussions,
{},
2023-03-17 16:20:25 +05:30
{ noteableType: notesConstants.EPIC_NOTEABLE_TYPE },
2021-12-11 22:18:48 +05:30
[
{ type: mutationTypes.ADD_OR_UPDATE_DISCUSSIONS, payload: { discussion } },
{ type: mutationTypes.SET_FETCHING_DISCUSSIONS, payload: false },
],
[{ type: 'updateResolvableDiscussionsCounts' }],
);
});
2022-08-13 15:12:31 +05:30
it('dispatches `fetchDiscussionsBatch` action if noteable is an Issue', () => {
2022-06-21 17:19:12 +05:30
return testAction(
2021-12-11 22:18:48 +05:30
actions.fetchDiscussions,
{ path: 'test-path', filter: 'test-filter', persistFilter: 'test-persist-filter' },
2022-08-13 15:12:31 +05:30
{ noteableType: notesConstants.ISSUE_NOTEABLE_TYPE },
2021-12-11 22:18:48 +05:30
[],
[
{
type: 'fetchDiscussionsBatch',
payload: {
config: {
params: { notes_filter: 'test-filter', persist_filter: 'test-persist-filter' },
},
path: 'test-path',
perPage: 20,
},
},
],
);
});
2022-07-23 23:45:48 +05:30
2023-03-17 16:20:25 +05:30
it('dispatches `fetchDiscussionsBatch` action if noteable is a MergeRequest', () => {
2022-07-23 23:45:48 +05:30
return testAction(
actions.fetchDiscussions,
{ path: 'test-path', filter: 'test-filter', persistFilter: 'test-persist-filter' },
2022-08-13 15:12:31 +05:30
{ noteableType: notesConstants.MERGE_REQUEST_NOTEABLE_TYPE },
2022-07-23 23:45:48 +05:30
[],
[
{
type: 'fetchDiscussionsBatch',
payload: {
config: {
params: { notes_filter: 'test-filter', persist_filter: 'test-persist-filter' },
},
path: 'test-path',
perPage: 20,
},
},
],
);
});
2021-12-11 22:18:48 +05:30
});
describe('fetchDiscussionsBatch', () => {
const discussion = { notes: [] };
const config = {
params: { notes_filter: 'test-filter', persist_filter: 'test-persist-filter' },
};
const actionPayload = { config, path: 'test-path', perPage: 20 };
2022-06-21 17:19:12 +05:30
it('updates the discussions and dispatches `updateResolvableDiscussionsCounts if there are no headers', () => {
2021-12-11 22:18:48 +05:30
axiosMock.onAny().reply(200, { discussion }, {});
2022-06-21 17:19:12 +05:30
return testAction(
2021-12-11 22:18:48 +05:30
actions.fetchDiscussionsBatch,
actionPayload,
null,
[
{ type: mutationTypes.ADD_OR_UPDATE_DISCUSSIONS, payload: { discussion } },
2022-07-23 23:45:48 +05:30
{ type: mutationTypes.SET_DONE_FETCHING_BATCH_DISCUSSIONS, payload: true },
2021-12-11 22:18:48 +05:30
{ type: mutationTypes.SET_FETCHING_DISCUSSIONS, payload: false },
],
[{ type: 'updateResolvableDiscussionsCounts' }],
);
});
2022-06-21 17:19:12 +05:30
it('dispatches itself if there is `x-next-page-cursor` header', () => {
2021-12-11 22:18:48 +05:30
axiosMock.onAny().reply(200, { discussion }, { 'x-next-page-cursor': 1 });
2022-06-21 17:19:12 +05:30
return testAction(
2021-12-11 22:18:48 +05:30
actions.fetchDiscussionsBatch,
actionPayload,
null,
[{ type: mutationTypes.ADD_OR_UPDATE_DISCUSSIONS, payload: { discussion } }],
[
{
type: 'fetchDiscussionsBatch',
payload: { ...actionPayload, perPage: 30, cursor: 1 },
},
],
);
});
});
2018-03-17 18:26:18 +05:30
});