Commit 9fd88264 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '27915-fix-ide-empty-repo' into 'master'

Fix IDE on empty repo

Closes #27915

See merge request gitlab-org/gitlab!25463
parents c0bc7b5b bd54b41c
...@@ -18,7 +18,7 @@ export default { ...@@ -18,7 +18,7 @@ export default {
computed: { computed: {
...mapState(['currentBranchId', 'changedFiles', 'stagedFiles']), ...mapState(['currentBranchId', 'changedFiles', 'stagedFiles']),
...mapCommitState(['commitAction']), ...mapCommitState(['commitAction']),
...mapGetters(['currentBranch']), ...mapGetters(['currentBranch', 'emptyRepo', 'canPushToBranch']),
commitToCurrentBranchText() { commitToCurrentBranchText() {
return sprintf( return sprintf(
s__('IDE|Commit to %{branchName} branch'), s__('IDE|Commit to %{branchName} branch'),
...@@ -29,6 +29,13 @@ export default { ...@@ -29,6 +29,13 @@ export default {
containsStagedChanges() { containsStagedChanges() {
return this.changedFiles.length > 0 && this.stagedFiles.length > 0; return this.changedFiles.length > 0 && this.stagedFiles.length > 0;
}, },
shouldDefaultToCurrentBranch() {
if (this.emptyRepo) {
return true;
}
return this.canPushToBranch && !this.currentBranch?.default;
},
}, },
watch: { watch: {
containsStagedChanges() { containsStagedChanges() {
...@@ -43,13 +50,11 @@ export default { ...@@ -43,13 +50,11 @@ export default {
methods: { methods: {
...mapCommitActions(['updateCommitAction']), ...mapCommitActions(['updateCommitAction']),
updateSelectedCommitAction() { updateSelectedCommitAction() {
if (!this.currentBranch) { if (!this.currentBranch && !this.emptyRepo) {
return; return;
} }
const { can_push: canPush = false, default: isDefault = false } = this.currentBranch; if (this.shouldDefaultToCurrentBranch) {
if (canPush && !isDefault) {
this.updateCommitAction(consts.COMMIT_TO_CURRENT_BRANCH); this.updateCommitAction(consts.COMMIT_TO_CURRENT_BRANCH);
} else { } else {
this.updateCommitAction(consts.COMMIT_TO_NEW_BRANCH); this.updateCommitAction(consts.COMMIT_TO_NEW_BRANCH);
...@@ -68,7 +73,7 @@ export default { ...@@ -68,7 +73,7 @@ export default {
<div class="append-bottom-15 ide-commit-options"> <div class="append-bottom-15 ide-commit-options">
<radio-group <radio-group
:value="$options.commitToCurrentBranch" :value="$options.commitToCurrentBranch"
:disabled="currentBranch && !currentBranch.can_push" :disabled="!canPushToBranch"
:title="$options.currentBranchPermissionsTooltip" :title="$options.currentBranchPermissionsTooltip"
> >
<span <span
...@@ -77,11 +82,13 @@ export default { ...@@ -77,11 +82,13 @@ export default {
v-html="commitToCurrentBranchText" v-html="commitToCurrentBranchText"
></span> ></span>
</radio-group> </radio-group>
<radio-group <template v-if="!emptyRepo">
:value="$options.commitToNewBranch" <radio-group
:label="__('Create a new branch')" :value="$options.commitToNewBranch"
:show-input="true" :label="__('Create a new branch')"
/> :show-input="true"
<new-merge-request-option /> />
<new-merge-request-option />
</template>
</div> </div>
</template> </template>
...@@ -10,6 +10,7 @@ export const FILE_VIEW_MODE_PREVIEW = 'preview'; ...@@ -10,6 +10,7 @@ export const FILE_VIEW_MODE_PREVIEW = 'preview';
export const PERMISSION_CREATE_MR = 'createMergeRequestIn'; export const PERMISSION_CREATE_MR = 'createMergeRequestIn';
export const PERMISSION_READ_MR = 'readMergeRequest'; export const PERMISSION_READ_MR = 'readMergeRequest';
export const PERMISSION_PUSH_CODE = 'pushCode';
export const viewerTypes = { export const viewerTypes = {
mr: 'mrdiff', mr: 'mrdiff',
......
...@@ -2,7 +2,8 @@ query getUserPermissions($projectPath: ID!) { ...@@ -2,7 +2,8 @@ query getUserPermissions($projectPath: ID!) {
project(fullPath: $projectPath) { project(fullPath: $projectPath) {
userPermissions { userPermissions {
createMergeRequestIn, createMergeRequestIn,
readMergeRequest readMergeRequest,
pushCode
} }
} }
} }
...@@ -83,10 +83,14 @@ export const showBranchNotFoundError = ({ dispatch }, branchId) => { ...@@ -83,10 +83,14 @@ export const showBranchNotFoundError = ({ dispatch }, branchId) => {
}); });
}; };
export const showEmptyState = ({ commit, state, dispatch }, { projectId, branchId }) => { export const loadEmptyBranch = ({ commit, state }, { projectId, branchId }) => {
const treePath = `${projectId}/${branchId}`; const treePath = `${projectId}/${branchId}`;
const currentTree = state.trees[`${projectId}/${branchId}`];
dispatch('setCurrentBranchId', branchId); // If we already have a tree, let's not recreate an empty one
if (currentTree) {
return;
}
commit(types.CREATE_TREE, { treePath }); commit(types.CREATE_TREE, { treePath });
commit(types.TOGGLE_LOADING, { commit(types.TOGGLE_LOADING, {
...@@ -114,8 +118,16 @@ export const loadFile = ({ dispatch, state }, { basePath }) => { ...@@ -114,8 +118,16 @@ export const loadFile = ({ dispatch, state }, { basePath }) => {
} }
}; };
export const loadBranch = ({ dispatch, getters }, { projectId, branchId }) => export const loadBranch = ({ dispatch, getters, state }, { projectId, branchId }) => {
dispatch('getBranchData', { const currentProject = state.projects[projectId];
if (currentProject?.branches?.[branchId]) {
return Promise.resolve();
} else if (getters.emptyRepo) {
return dispatch('loadEmptyBranch', { projectId, branchId });
}
return dispatch('getBranchData', {
projectId, projectId,
branchId, branchId,
}) })
...@@ -137,29 +149,23 @@ export const loadBranch = ({ dispatch, getters }, { projectId, branchId }) => ...@@ -137,29 +149,23 @@ export const loadBranch = ({ dispatch, getters }, { projectId, branchId }) =>
dispatch('showBranchNotFoundError', branchId); dispatch('showBranchNotFoundError', branchId);
throw err; throw err;
}); });
};
export const openBranch = ({ dispatch, state, getters }, { projectId, branchId, basePath }) => { export const openBranch = ({ dispatch }, { projectId, branchId, basePath }) => {
const currentProject = state.projects[projectId]; dispatch('setCurrentBranchId', branchId);
if (getters.emptyRepo) {
return dispatch('showEmptyState', { projectId, branchId }); return dispatch('loadBranch', { projectId, branchId })
} .then(() => dispatch('loadFile', { basePath }))
if (!currentProject || !currentProject.branches[branchId]) { .catch(
dispatch('setCurrentBranchId', branchId); () =>
new Error(
return dispatch('loadBranch', { projectId, branchId }) sprintf(
.then(() => dispatch('loadFile', { basePath })) __('An error occurred while getting files for - %{branchId}'),
.catch( {
() => branchId: `<strong>${esc(projectId)}/${esc(branchId)}</strong>`,
new Error( },
sprintf( false,
__('An error occurred while getting files for - %{branchId}'),
{
branchId: `<strong>${esc(projectId)}/${esc(branchId)}</strong>`,
},
false,
),
), ),
); ),
} );
return Promise.resolve(dispatch('loadFile', { basePath }));
}; };
...@@ -4,6 +4,7 @@ import { ...@@ -4,6 +4,7 @@ import {
packageJsonPath, packageJsonPath,
PERMISSION_READ_MR, PERMISSION_READ_MR,
PERMISSION_CREATE_MR, PERMISSION_CREATE_MR,
PERMISSION_PUSH_CODE,
} from '../constants'; } from '../constants';
export const activeFile = state => state.openFiles.find(file => file.active) || null; export const activeFile = state => state.openFiles.find(file => file.active) || null;
...@@ -120,8 +121,9 @@ export const packageJson = state => state.entries[packageJsonPath]; ...@@ -120,8 +121,9 @@ export const packageJson = state => state.entries[packageJsonPath];
export const isOnDefaultBranch = (_state, getters) => export const isOnDefaultBranch = (_state, getters) =>
getters.currentProject && getters.currentProject.default_branch === getters.branchName; getters.currentProject && getters.currentProject.default_branch === getters.branchName;
export const canPushToBranch = (_state, getters) => export const canPushToBranch = (_state, getters) => {
getters.currentBranch && getters.currentBranch.can_push; return Boolean(getters.currentBranch ? getters.currentBranch.can_push : getters.canPushCode);
};
export const isFileDeletedAndReadded = (state, getters) => path => { export const isFileDeletedAndReadded = (state, getters) => path => {
const stagedFile = getters.getStagedFile(path); const stagedFile = getters.getStagedFile(path);
...@@ -157,5 +159,8 @@ export const canReadMergeRequests = (state, getters) => ...@@ -157,5 +159,8 @@ export const canReadMergeRequests = (state, getters) =>
export const canCreateMergeRequests = (state, getters) => export const canCreateMergeRequests = (state, getters) =>
Boolean(getters.findProjectPermissions(state.currentProjectId)[PERMISSION_CREATE_MR]); Boolean(getters.findProjectPermissions(state.currentProjectId)[PERMISSION_CREATE_MR]);
export const canPushCode = (state, getters) =>
Boolean(getters.findProjectPermissions(state.currentProjectId)[PERMISSION_PUSH_CODE]);
// prevent babel-plugin-rewire from generating an invalid default during karma tests // prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {}; export default () => {};
...@@ -106,6 +106,9 @@ export const updateFilesAfterCommit = ({ commit, dispatch, rootState, rootGetter ...@@ -106,6 +106,9 @@ export const updateFilesAfterCommit = ({ commit, dispatch, rootState, rootGetter
}; };
export const commitChanges = ({ commit, state, getters, dispatch, rootState, rootGetters }) => { export const commitChanges = ({ commit, state, getters, dispatch, rootState, rootGetters }) => {
// Pull commit options out because they could change
// During some of the pre and post commit processing
const { shouldCreateMR, isCreatingNewBranch, branchName } = getters;
const newBranch = state.commitAction !== consts.COMMIT_TO_CURRENT_BRANCH; const newBranch = state.commitAction !== consts.COMMIT_TO_CURRENT_BRANCH;
const stageFilesPromise = rootState.stagedFiles.length const stageFilesPromise = rootState.stagedFiles.length
? Promise.resolve() ? Promise.resolve()
...@@ -116,7 +119,7 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState, roo ...@@ -116,7 +119,7 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState, roo
return stageFilesPromise return stageFilesPromise
.then(() => { .then(() => {
const payload = createCommitPayload({ const payload = createCommitPayload({
branch: getters.branchName, branch: branchName,
newBranch, newBranch,
getters, getters,
state, state,
...@@ -149,7 +152,7 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState, roo ...@@ -149,7 +152,7 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState, roo
dispatch('updateCommitMessage', ''); dispatch('updateCommitMessage', '');
return dispatch('updateFilesAfterCommit', { return dispatch('updateFilesAfterCommit', {
data, data,
branch: getters.branchName, branch: branchName,
}) })
.then(() => { .then(() => {
commit(rootTypes.CLEAR_STAGED_CHANGES, null, { root: true }); commit(rootTypes.CLEAR_STAGED_CHANGES, null, { root: true });
...@@ -158,15 +161,15 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState, roo ...@@ -158,15 +161,15 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState, roo
commit(rootTypes.SET_LAST_COMMIT_MSG, '', { root: true }); commit(rootTypes.SET_LAST_COMMIT_MSG, '', { root: true });
}, 5000); }, 5000);
if (getters.shouldCreateMR) { if (shouldCreateMR) {
const { currentProject } = rootGetters; const { currentProject } = rootGetters;
const targetBranch = getters.isCreatingNewBranch const targetBranch = isCreatingNewBranch
? rootState.currentBranchId ? rootState.currentBranchId
: currentProject.default_branch; : currentProject.default_branch;
dispatch( dispatch(
'redirectToUrl', 'redirectToUrl',
createNewMergeRequestUrl(currentProject.web_url, getters.branchName, targetBranch), createNewMergeRequestUrl(currentProject.web_url, branchName, targetBranch),
{ root: true }, { root: true },
); );
} }
...@@ -194,7 +197,7 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState, roo ...@@ -194,7 +197,7 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState, roo
if (rootGetters.activeFile) { if (rootGetters.activeFile) {
router.push( router.push(
`/project/${rootState.currentProjectId}/blob/${getters.branchName}/-/${rootGetters.activeFile.path}`, `/project/${rootState.currentProjectId}/blob/${branchName}/-/${rootGetters.activeFile.path}`,
); );
} }
} }
......
...@@ -55,7 +55,7 @@ export const shouldHideNewMrOption = (_state, getters, _rootState, rootGetters) ...@@ -55,7 +55,7 @@ export const shouldHideNewMrOption = (_state, getters, _rootState, rootGetters)
rootGetters.canPushToBranch; rootGetters.canPushToBranch;
export const shouldDisableNewMrOption = (state, getters, rootState, rootGetters) => export const shouldDisableNewMrOption = (state, getters, rootState, rootGetters) =>
!rootGetters.canCreateMergeRequests; !rootGetters.canCreateMergeRequests || rootGetters.emptyRepo;
export const shouldCreateMR = (state, getters) => export const shouldCreateMR = (state, getters) =>
state.shouldCreateMR && !getters.shouldDisableNewMrOption; state.shouldCreateMR && !getters.shouldDisableNewMrOption;
......
---
title: Fix some Web IDE bugs with empty projects
merge_request: 25463
author:
type: fixed
...@@ -17,7 +17,11 @@ describe('IDE commit sidebar actions', () => { ...@@ -17,7 +17,11 @@ describe('IDE commit sidebar actions', () => {
let store; let store;
let vm; let vm;
const createComponent = ({ hasMR = false, currentBranchId = 'master' } = {}) => { const createComponent = ({
hasMR = false,
currentBranchId = 'master',
emptyRepo = false,
} = {}) => {
const Component = Vue.extend(commitActions); const Component = Vue.extend(commitActions);
vm = createComponentWithStore(Component, store); vm = createComponentWithStore(Component, store);
...@@ -27,6 +31,7 @@ describe('IDE commit sidebar actions', () => { ...@@ -27,6 +31,7 @@ describe('IDE commit sidebar actions', () => {
const proj = { ...projectData }; const proj = { ...projectData };
proj.branches[currentBranchId] = branches.find(branch => branch.name === currentBranchId); proj.branches[currentBranchId] = branches.find(branch => branch.name === currentBranchId);
proj.empty_repo = emptyRepo;
Vue.set(vm.$store.state.projects, 'abcproject', proj); Vue.set(vm.$store.state.projects, 'abcproject', proj);
...@@ -52,24 +57,27 @@ describe('IDE commit sidebar actions', () => { ...@@ -52,24 +57,27 @@ describe('IDE commit sidebar actions', () => {
vm = null; vm = null;
}); });
const findText = () => vm.$el.textContent;
const findRadios = () => Array.from(vm.$el.querySelectorAll('input[type="radio"]'));
it('renders 2 groups', () => { it('renders 2 groups', () => {
createComponent(); createComponent();
expect(vm.$el.querySelectorAll('input[type="radio"]').length).toBe(2); expect(findRadios().length).toBe(2);
}); });
it('renders current branch text', () => { it('renders current branch text', () => {
createComponent(); createComponent();
expect(vm.$el.textContent).toContain('Commit to master branch'); expect(findText()).toContain('Commit to master branch');
}); });
it('hides merge request option when project merge requests are disabled', done => { it('hides merge request option when project merge requests are disabled', done => {
createComponent({ mergeRequestsEnabled: false }); createComponent({ hasMR: false });
vm.$nextTick(() => { vm.$nextTick(() => {
expect(vm.$el.querySelectorAll('input[type="radio"]').length).toBe(2); expect(findRadios().length).toBe(2);
expect(vm.$el.textContent).not.toContain('Create a new branch and merge request'); expect(findText()).not.toContain('Create a new branch and merge request');
done(); done();
}); });
...@@ -119,6 +127,7 @@ describe('IDE commit sidebar actions', () => { ...@@ -119,6 +127,7 @@ describe('IDE commit sidebar actions', () => {
it.each` it.each`
input | expectedOption input | expectedOption
${{ currentBranchId: BRANCH_DEFAULT }} | ${consts.COMMIT_TO_NEW_BRANCH} ${{ currentBranchId: BRANCH_DEFAULT }} | ${consts.COMMIT_TO_NEW_BRANCH}
${{ currentBranchId: BRANCH_DEFAULT, emptyRepo: true }} | ${consts.COMMIT_TO_CURRENT_BRANCH}
${{ currentBranchId: BRANCH_PROTECTED, hasMR: true }} | ${consts.COMMIT_TO_CURRENT_BRANCH} ${{ currentBranchId: BRANCH_PROTECTED, hasMR: true }} | ${consts.COMMIT_TO_CURRENT_BRANCH}
${{ currentBranchId: BRANCH_PROTECTED, hasMR: false }} | ${consts.COMMIT_TO_CURRENT_BRANCH} ${{ currentBranchId: BRANCH_PROTECTED, hasMR: false }} | ${consts.COMMIT_TO_CURRENT_BRANCH}
${{ currentBranchId: BRANCH_PROTECTED_NO_ACCESS, hasMR: true }} | ${consts.COMMIT_TO_NEW_BRANCH} ${{ currentBranchId: BRANCH_PROTECTED_NO_ACCESS, hasMR: true }} | ${consts.COMMIT_TO_NEW_BRANCH}
...@@ -138,4 +147,15 @@ describe('IDE commit sidebar actions', () => { ...@@ -138,4 +147,15 @@ describe('IDE commit sidebar actions', () => {
}, },
); );
}); });
describe('when empty project', () => {
beforeEach(() => {
createComponent({ emptyRepo: true });
});
it('only renders commit to current branch', () => {
expect(findRadios().length).toBe(1);
expect(findText()).toContain('Commit to master branch');
});
});
}); });
...@@ -280,39 +280,21 @@ describe('IDE store getters', () => { ...@@ -280,39 +280,21 @@ describe('IDE store getters', () => {
}); });
describe('canPushToBranch', () => { describe('canPushToBranch', () => {
it('returns false when no currentBranch exists', () => { it.each`
const localGetters = { currentBranch | canPushCode | expectedValue
currentProject: undefined, ${undefined} | ${undefined} | ${false}
}; ${{ can_push: true }} | ${false} | ${true}
${{ can_push: true }} | ${true} | ${true}
expect(getters.canPushToBranch({}, localGetters)).toBeFalsy(); ${{ can_push: false }} | ${false} | ${false}
}); ${{ can_push: false }} | ${true} | ${false}
${undefined} | ${true} | ${true}
it('returns true when can_push to currentBranch', () => { ${undefined} | ${false} | ${false}
const localGetters = { `(
currentProject: { 'with currentBranch ($currentBranch) and canPushCode ($canPushCode), it is $expectedValue',
default_branch: 'master', ({ currentBranch, canPushCode, expectedValue }) => {
}, expect(getters.canPushToBranch({}, { currentBranch, canPushCode })).toBe(expectedValue);
currentBranch: { },
can_push: true, );
},
};
expect(getters.canPushToBranch({}, localGetters)).toBeTruthy();
});
it('returns false when !can_push to currentBranch', () => {
const localGetters = {
currentProject: {
default_branch: 'master',
},
currentBranch: {
can_push: false,
},
};
expect(getters.canPushToBranch({}, localGetters)).toBeFalsy();
});
}); });
describe('isFileDeletedAndReadded', () => { describe('isFileDeletedAndReadded', () => {
...@@ -422,6 +404,7 @@ describe('IDE store getters', () => { ...@@ -422,6 +404,7 @@ describe('IDE store getters', () => {
getterName | permissionKey getterName | permissionKey
${'canReadMergeRequests'} | ${'readMergeRequest'} ${'canReadMergeRequests'} | ${'readMergeRequest'}
${'canCreateMergeRequests'} | ${'createMergeRequestIn'} ${'canCreateMergeRequests'} | ${'createMergeRequestIn'}
${'canPushCode'} | ${'pushCode'}
`('$getterName', ({ getterName, permissionKey }) => { `('$getterName', ({ getterName, permissionKey }) => {
it.each([true, false])('finds permission for current project (%s)', val => { it.each([true, false])('finds permission for current project (%s)', val => {
localState.projects[TEST_PROJECT_ID] = { localState.projects[TEST_PROJECT_ID] = {
......
...@@ -292,4 +292,15 @@ describe('IDE commit module getters', () => { ...@@ -292,4 +292,15 @@ describe('IDE commit module getters', () => {
expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy(); expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy();
}); });
}); });
describe('shouldDisableNewMrOption', () => {
it.each`
rootGetters | expectedValue
${{ canCreateMergeRequests: false, emptyRepo: false }} | ${true}
${{ canCreateMergeRequests: true, emptyRepo: true }} | ${true}
${{ canCreateMergeRequests: true, emptyRepo: false }} | ${false}
`('with $rootGetters, it is $expectedValue', ({ rootGetters, expectedValue }) => {
expect(getters.shouldDisableNewMrOption(state, getters, {}, rootGetters)).toBe(expectedValue);
});
});
}); });
...@@ -4,7 +4,7 @@ import { ...@@ -4,7 +4,7 @@ import {
refreshLastCommitData, refreshLastCommitData,
showBranchNotFoundError, showBranchNotFoundError,
createNewBranchFromDefault, createNewBranchFromDefault,
showEmptyState, loadEmptyBranch,
openBranch, openBranch,
loadFile, loadFile,
loadBranch, loadBranch,
...@@ -16,6 +16,8 @@ import router from '~/ide/ide_router'; ...@@ -16,6 +16,8 @@ import router from '~/ide/ide_router';
import { resetStore } from '../../helpers'; import { resetStore } from '../../helpers';
import testAction from '../../../helpers/vuex_action_helper'; import testAction from '../../../helpers/vuex_action_helper';
const TEST_PROJECT_ID = 'abc/def';
describe('IDE store project actions', () => { describe('IDE store project actions', () => {
let mock; let mock;
let store; let store;
...@@ -24,7 +26,7 @@ describe('IDE store project actions', () => { ...@@ -24,7 +26,7 @@ describe('IDE store project actions', () => {
store = createStore(); store = createStore();
mock = new MockAdapter(axios); mock = new MockAdapter(axios);
store.state.projects['abc/def'] = { store.state.projects[TEST_PROJECT_ID] = {
branches: {}, branches: {},
}; };
}); });
...@@ -83,7 +85,7 @@ describe('IDE store project actions', () => { ...@@ -83,7 +85,7 @@ describe('IDE store project actions', () => {
{ {
type: 'SET_BRANCH_COMMIT', type: 'SET_BRANCH_COMMIT',
payload: { payload: {
projectId: 'abc/def', projectId: TEST_PROJECT_ID,
branchId: 'master', branchId: 'master',
commit: { id: '123' }, commit: { id: '123' },
}, },
...@@ -200,17 +202,17 @@ describe('IDE store project actions', () => { ...@@ -200,17 +202,17 @@ describe('IDE store project actions', () => {
}); });
}); });
describe('showEmptyState', () => { describe('loadEmptyBranch', () => {
it('creates a blank tree and sets loading state to false', done => { it('creates a blank tree and sets loading state to false', done => {
testAction( testAction(
showEmptyState, loadEmptyBranch,
{ projectId: 'abc/def', branchId: 'master' }, { projectId: TEST_PROJECT_ID, branchId: 'master' },
store.state, store.state,
[ [
{ type: 'CREATE_TREE', payload: { treePath: 'abc/def/master' } }, { type: 'CREATE_TREE', payload: { treePath: `${TEST_PROJECT_ID}/master` } },
{ {
type: 'TOGGLE_LOADING', type: 'TOGGLE_LOADING',
payload: { entry: store.state.trees['abc/def/master'], forceValue: false }, payload: { entry: store.state.trees[`${TEST_PROJECT_ID}/master`], forceValue: false },
}, },
], ],
jasmine.any(Object), jasmine.any(Object),
...@@ -218,13 +220,15 @@ describe('IDE store project actions', () => { ...@@ -218,13 +220,15 @@ describe('IDE store project actions', () => {
); );
}); });
it('sets the currentBranchId to the branchId that was passed', done => { it('does nothing, if tree already exists', done => {
const trees = { [`${TEST_PROJECT_ID}/master`]: [] };
testAction( testAction(
showEmptyState, loadEmptyBranch,
{ projectId: 'abc/def', branchId: 'master' }, { projectId: TEST_PROJECT_ID, branchId: 'master' },
store.state, { trees },
jasmine.any(Object), [],
[{ type: 'setCurrentBranchId', payload: 'master' }], [],
done, done,
); );
}); });
...@@ -278,10 +282,29 @@ describe('IDE store project actions', () => { ...@@ -278,10 +282,29 @@ describe('IDE store project actions', () => {
}); });
describe('loadBranch', () => { describe('loadBranch', () => {
const projectId = 'abc/def'; const projectId = TEST_PROJECT_ID;
const branchId = '123-lorem'; const branchId = '123-lorem';
const ref = 'abcd2322'; const ref = 'abcd2322';
it('when empty repo, loads empty branch', done => {
const mockGetters = { emptyRepo: true };
testAction(
loadBranch,
{ projectId, branchId },
{ ...store.state, ...mockGetters },
[],
[{ type: 'loadEmptyBranch', payload: { projectId, branchId } }],
done,
);
});
it('when branch already exists, does nothing', done => {
store.state.projects[projectId].branches[branchId] = {};
testAction(loadBranch, { projectId, branchId }, store.state, [], [], done);
});
it('fetches branch data', done => { it('fetches branch data', done => {
const mockGetters = { findBranch: () => ({ commit: { id: ref } }) }; const mockGetters = { findBranch: () => ({ commit: { id: ref } }) };
spyOn(store, 'dispatch').and.returnValue(Promise.resolve()); spyOn(store, 'dispatch').and.returnValue(Promise.resolve());
...@@ -317,7 +340,7 @@ describe('IDE store project actions', () => { ...@@ -317,7 +340,7 @@ describe('IDE store project actions', () => {
}); });
describe('openBranch', () => { describe('openBranch', () => {
const projectId = 'abc/def'; const projectId = TEST_PROJECT_ID;
const branchId = '123-lorem'; const branchId = '123-lorem';
const branch = { const branch = {
...@@ -335,55 +358,6 @@ describe('IDE store project actions', () => { ...@@ -335,55 +358,6 @@ describe('IDE store project actions', () => {
}); });
}); });
it('loads file right away if the branch has already been fetched', done => {
spyOn(store, 'dispatch');
Object.assign(store.state, {
projects: {
[projectId]: {
branches: {
[branchId]: { foo: 'bar' },
},
},
},
});
openBranch(store, branch)
.then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([['loadFile', { basePath: undefined }]]);
})
.then(done)
.catch(done.fail);
});
describe('empty repo', () => {
beforeEach(() => {
spyOn(store, 'dispatch').and.returnValue(Promise.resolve());
Object.assign(store.state, {
currentProjectId: 'abc/def',
projects: {
'abc/def': {
empty_repo: true,
},
},
});
});
afterEach(() => {
resetStore(store);
});
it('dispatches showEmptyState action right away', done => {
openBranch(store, branch)
.then(() => {
expect(store.dispatch.calls.allArgs()).toEqual([['showEmptyState', branch]]);
done();
})
.catch(done.fail);
});
});
describe('existing branch', () => { describe('existing branch', () => {
beforeEach(() => { beforeEach(() => {
spyOn(store, 'dispatch').and.returnValue(Promise.resolve()); spyOn(store, 'dispatch').and.returnValue(Promise.resolve());
...@@ -410,11 +384,17 @@ describe('IDE store project actions', () => { ...@@ -410,11 +384,17 @@ describe('IDE store project actions', () => {
it('dispatches correct branch actions', done => { it('dispatches correct branch actions', done => {
openBranch(store, branch) openBranch(store, branch)
.then(() => { .then(val => {
expect(store.dispatch.calls.allArgs()).toEqual([ expect(store.dispatch.calls.allArgs()).toEqual([
['setCurrentBranchId', branchId], ['setCurrentBranchId', branchId],
['loadBranch', { projectId, branchId }], ['loadBranch', { projectId, branchId }],
]); ]);
expect(val).toEqual(
new Error(
`An error occurred while getting files for - <strong>${projectId}/${branchId}</strong>`,
),
);
}) })
.then(done) .then(done)
.catch(done.fail); .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