summaryrefslogtreecommitdiff
path: root/spec/frontend/ide/stores/actions/merge_request_spec.js
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/ide/stores/actions/merge_request_spec.js')
-rw-r--r--spec/frontend/ide/stores/actions/merge_request_spec.js111
1 files changed, 89 insertions, 22 deletions
diff --git a/spec/frontend/ide/stores/actions/merge_request_spec.js b/spec/frontend/ide/stores/actions/merge_request_spec.js
index 9b17d95ea35..600bd5fe9e1 100644
--- a/spec/frontend/ide/stores/actions/merge_request_spec.js
+++ b/spec/frontend/ide/stores/actions/merge_request_spec.js
@@ -1,19 +1,33 @@
import MockAdapter from 'axios-mock-adapter';
-import axios from '~/lib/utils/axios_utils';
-import { createStore } from '~/ide/stores';
+import { range } from 'lodash';
+import { TEST_HOST } from 'helpers/test_constants';
+import testAction from 'helpers/vuex_action_helper';
import { deprecatedCreateFlash as createFlash } from '~/flash';
+import { leftSidebarViews, PERMISSION_READ_MR, MAX_MR_FILES_AUTO_OPEN } from '~/ide/constants';
+import service from '~/ide/services';
+import { createStore } from '~/ide/stores';
import {
getMergeRequestData,
getMergeRequestChanges,
getMergeRequestVersions,
+ openMergeRequestChanges,
openMergeRequest,
} from '~/ide/stores/actions/merge_request';
-import service from '~/ide/services';
-import { leftSidebarViews, PERMISSION_READ_MR } from '~/ide/constants';
+import * as types from '~/ide/stores/mutation_types';
+import axios from '~/lib/utils/axios_utils';
const TEST_PROJECT = 'abcproject';
const TEST_PROJECT_ID = 17;
+const createMergeRequestChange = (path) => ({
+ new_path: path,
+ path,
+});
+const createMergeRequestChangesCount = (n) =>
+ range(n).map((i) => createMergeRequestChange(`loremispum_${i}.md`));
+
+const testGetUrlForPath = (path) => `${TEST_HOST}/test/${path}`;
+
jest.mock('~/flash');
describe('IDE store merge request actions', () => {
@@ -353,6 +367,72 @@ describe('IDE store merge request actions', () => {
});
});
+ describe('openMergeRequestChanges', () => {
+ it.each`
+ desc | changes | entries
+ ${'with empty changes'} | ${[]} | ${{}}
+ ${'with changes not matching entries'} | ${[{ new_path: '123.md' }]} | ${{ '456.md': {} }}
+ `('$desc, does nothing', ({ changes, entries }) => {
+ const state = { entries };
+
+ return testAction({
+ action: openMergeRequestChanges,
+ state,
+ payload: changes,
+ expectedActions: [],
+ expectedMutations: [],
+ });
+ });
+
+ it('updates views and opens mr changes', () => {
+ // This is the payload sent to the action
+ const changesPayload = createMergeRequestChangesCount(15);
+
+ // Remove some items from the payload to use for entries
+ const changes = changesPayload.slice(1, 14);
+
+ const entries = changes.reduce(
+ (acc, { path }) => Object.assign(acc, { [path]: path, type: 'blob' }),
+ {},
+ );
+ const pathsToOpen = changes.slice(0, MAX_MR_FILES_AUTO_OPEN).map((x) => x.new_path);
+
+ return testAction({
+ action: openMergeRequestChanges,
+ state: { entries, getUrlForPath: testGetUrlForPath },
+ payload: changesPayload,
+ expectedActions: [
+ { type: 'updateActivityBarView', payload: leftSidebarViews.review.name },
+ // Only activates first file
+ { type: 'router/push', payload: testGetUrlForPath(pathsToOpen[0]) },
+ { type: 'setFileActive', payload: pathsToOpen[0] },
+ // Fetches data for other files
+ ...pathsToOpen.slice(1).map((path) => ({
+ type: 'getFileData',
+ payload: { path, makeFileActive: false },
+ })),
+ ...pathsToOpen.slice(1).map((path) => ({
+ type: 'getRawFileData',
+ payload: { path },
+ })),
+ ],
+ expectedMutations: [
+ ...changes.map((change) => ({
+ type: types.SET_FILE_MERGE_REQUEST_CHANGE,
+ payload: {
+ file: entries[change.new_path],
+ mrChange: change,
+ },
+ })),
+ ...pathsToOpen.map((path) => ({
+ type: types.TOGGLE_FILE_OPEN,
+ payload: path,
+ })),
+ ],
+ });
+ });
+ });
+
describe('openMergeRequest', () => {
const mr = {
projectId: TEST_PROJECT,
@@ -409,7 +489,6 @@ describe('IDE store merge request actions', () => {
case 'getFiles':
case 'getMergeRequestVersions':
case 'getBranchData':
- case 'setFileMrChange':
return Promise.resolve();
default:
return originalDispatch(type, payload);
@@ -445,6 +524,7 @@ describe('IDE store merge request actions', () => {
],
['getMergeRequestVersions', mr],
['getMergeRequestChanges', mr],
+ ['openMergeRequestChanges', testMergeRequestChanges.changes],
]);
})
.then(done)
@@ -454,9 +534,11 @@ describe('IDE store merge request actions', () => {
it('updates activity bar view and gets file data, if changes are found', (done) => {
store.state.entries.foo = {
type: 'blob',
+ path: 'foo',
};
store.state.entries.bar = {
type: 'blob',
+ path: 'bar',
};
testMergeRequestChanges.changes = [
@@ -467,24 +549,9 @@ describe('IDE store merge request actions', () => {
openMergeRequest({ state: store.state, dispatch: store.dispatch, getters: mockGetters }, mr)
.then(() => {
expect(store.dispatch).toHaveBeenCalledWith(
- 'updateActivityBarView',
- leftSidebarViews.review.name,
+ 'openMergeRequestChanges',
+ testMergeRequestChanges.changes,
);
-
- testMergeRequestChanges.changes.forEach((change, i) => {
- expect(store.dispatch).toHaveBeenCalledWith('setFileMrChange', {
- file: store.state.entries[change.new_path],
- mrChange: change,
- });
-
- expect(store.dispatch).toHaveBeenCalledWith('getFileData', {
- path: change.new_path,
- makeFileActive: i === 0,
- openFile: true,
- });
- });
-
- expect(store.state.openFiles.length).toBe(testMergeRequestChanges.changes.length);
})
.then(done)
.catch(done.fail);