Commit 222ed135 authored by Denys Mishunov's avatar Denys Mishunov

Re-factored openBranch action in Web IDE

- shortcut for rendering empty state right away
- do not attempt to fetch files alongside with
branchData as branch might not exist at all
- catch potential errors on both: fetching
branchData and getFiles
parent c5cdcadb
Pipeline #63395119 passed with stages
in 56 minutes and 15 seconds
......@@ -92,43 +92,57 @@ export const showEmptyState = ({ commit, state }, { projectId, branchId }) => {
});
};
export const openBranch = ({ dispatch, state }, { projectId, branchId, basePath }) => {
export const openBranch = ({ dispatch, state, getters }, { projectId, branchId, basePath }) => {
dispatch('setCurrentBranchId', branchId);
if (state.projects[projectId].empty_repo) {
if (getters.emptyRepo) {
return dispatch('showEmptyState', { projectId, branchId });
}
dispatch('getBranchData', {
projectId,
branchId,
});
return dispatch('getFiles', {
return dispatch('getBranchData', {
projectId,
branchId,
})
.then(() => {
if (basePath) {
const path = basePath.slice(-1) === '/' ? basePath.slice(0, -1) : basePath;
const treeEntryKey = Object.keys(state.entries).find(
key => key === path && !state.entries[key].pending,
);
const treeEntry = state.entries[treeEntryKey];
if (treeEntry) {
dispatch('handleTreeEntryAction', treeEntry);
} else {
dispatch('createTempEntry', {
name: path,
type: 'blob',
});
}
}
})
.then(() => {
dispatch('getMergeRequestsForBranch', {
projectId,
branchId,
});
dispatch('getFiles', {
projectId,
branchId,
})
.then(() => {
if (basePath) {
const path = basePath.slice(-1) === '/' ? basePath.slice(0, -1) : basePath;
const treeEntryKey = Object.keys(state.entries).find(
key => key === path && !state.entries[key].pending,
);
const treeEntry = state.entries[treeEntryKey];
if (treeEntry) {
dispatch('handleTreeEntryAction', treeEntry);
} else {
dispatch('createTempEntry', {
name: path,
type: 'blob',
});
}
}
})
.catch(
() =>
new Error(
sprintf(
__('An error occurred whilst getting files for - %{branchId}'),
{
branchId: `<strong>${_.escape(projectId)}/${_.escape(branchId)}</strong>`,
},
false,
),
),
);
})
.catch(() => {
dispatch('showBranchNotFoundError', branchId);
});
};
---
title: Empty project state for Web IDE
merge_request: 26556
author:
type: added
......@@ -958,6 +958,9 @@ msgstr ""
msgid "An error occurred whilst fetching the latest pipeline."
msgstr ""
msgid "An error occurred whilst getting files for - %{branchId}"
msgstr ""
msgid "An error occurred whilst loading all the files."
msgstr ""
......@@ -2987,6 +2990,9 @@ msgstr ""
msgid "Create a new branch"
msgstr ""
msgid "Create a new file as there are no files yet. Afterwards, you'll be able to commit your changes."
msgstr ""
msgid "Create a new issue"
msgstr ""
......@@ -6422,6 +6428,9 @@ msgstr ""
msgid "No file selected"
msgstr ""
msgid "No files"
msgstr ""
msgid "No files found."
msgstr ""
......@@ -8576,6 +8585,9 @@ msgstr ""
msgid "Select Archive Format"
msgstr ""
msgid "Select a file from the left sidebar to begin editing. Afterwards, you'll be able to commit your changes."
msgstr ""
msgid "Select a group to invite"
msgstr ""
......
......@@ -4,9 +4,8 @@ import {
refreshLastCommitData,
showBranchNotFoundError,
createNewBranchFromDefault,
// getBranchData,
openBranch,
showEmptyState,
openBranch,
} from '~/ide/stores/actions';
import store from '~/ide/stores';
import service from '~/ide/services';
......@@ -197,41 +196,16 @@ describe('IDE store project actions', () => {
});
});
// describe('getBranchData', () => {
// describe('error', () => {
// it('dispatches branch not found action when response is 404', done => {
// const dispatch = jasmine.createSpy('dispatchSpy');
//
// mock.onGet(/(.*)/).replyOnce(404);
//
// getBranchData(
// {
// commit() {},
// dispatch,
// state: store.state,
// },
// {
// projectId: 'abc/def',
// branchId: 'master-testing',
// },
// )
// .then(done.fail)
// .catch(() => {
// expect(dispatch.calls.argsFor(0)).toEqual([
// 'showBranchNotFoundError',
// 'master-testing',
// ]);
// done();
// });
// });
// });
// });
describe('showEmptyState', () => {
it('calls actions and commits proper mutations', done => {
it('commits proper mutations when supplied error is 404', done => {
testAction(
showEmptyState,
{
err: {
response: {
status: 404,
},
},
projectId: 'abc/def',
branchId: 'master',
},
......@@ -259,7 +233,7 @@ describe('IDE store project actions', () => {
describe('openBranch', () => {
const branch = {
projectId: 'feature/lorem-ipsum',
projectId: 'abc/def',
branchId: '123-lorem',
};
......@@ -269,35 +243,38 @@ describe('IDE store project actions', () => {
'foo/bar-pending': { pending: true },
'foo/bar': { pending: false },
};
spyOn(store, 'dispatch').and.returnValue(Promise.resolve());
});
describe('empty repo', () => {
beforeEach(() => {
store.state.projects['feature/lorem-ipsum'] = {
spyOn(store, 'dispatch').and.returnValue(Promise.resolve());
store.state.currentProjectId = 'abc/def';
store.state.projects['abc/def'] = {
empty_repo: true,
};
});
it('dispatches empty state action', done => {
afterEach(() => {
resetStore(store);
});
it('dispatches showEmptyState action right away', done => {
openBranch(store, branch)
.then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([
['setCurrentBranchId', branch.branchId],
['showEmptyState', branch],
]);
})
.then(done)
.catch(done.fail);
.then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([
['setCurrentBranchId', branch.branchId],
['showEmptyState', branch],
]);
done();
})
.catch(done.fail);
});
});
describe('non-empty repo', () => {
describe('existing branch', () => {
beforeEach(() => {
store.state.projects['feature/lorem-ipsum'] = {
empty_repo: false,
};
spyOn(store, 'dispatch').and.returnValue(Promise.resolve());
});
it('dispatches branch actions', done => {
......@@ -306,8 +283,8 @@ describe('IDE store project actions', () => {
expect(store.dispatch.calls.allArgs()).toEqual([
['setCurrentBranchId', branch.branchId],
['getBranchData', branch],
['getFiles', branch],
['getMergeRequestsForBranch', branch],
['getFiles', branch],
]);
})
.then(done)
......@@ -355,5 +332,24 @@ describe('IDE store project actions', () => {
.catch(done.fail);
});
});
describe('non-existent branch', () => {
beforeEach(() => {
spyOn(store, 'dispatch').and.returnValue(Promise.reject());
});
it('dispatches correct branch actions', done => {
openBranch(store, branch)
.then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([
['setCurrentBranchId', branch.branchId],
['getBranchData', branch],
['showBranchNotFoundError', branch.branchId],
]);
})
.then(done)
.catch(done.fail);
});
});
});
});
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