diff options
Diffstat (limited to 'spec/javascripts/ide')
-rw-r--r-- | spec/javascripts/ide/components/commit_sidebar/actions_spec.js | 188 | ||||
-rw-r--r-- | spec/javascripts/ide/components/commit_sidebar/new_merge_request_option_spec.js | 160 | ||||
-rw-r--r-- | spec/javascripts/ide/components/repo_editor_spec.js | 8 | ||||
-rw-r--r-- | spec/javascripts/ide/mock_data.js | 34 | ||||
-rw-r--r-- | spec/javascripts/ide/stores/getters_spec.js | 32 | ||||
-rw-r--r-- | spec/javascripts/ide/stores/modules/commit/actions_spec.js | 181 | ||||
-rw-r--r-- | spec/javascripts/ide/stores/modules/commit/getters_spec.js | 162 | ||||
-rw-r--r-- | spec/javascripts/ide/stores/mutations/file_spec.js | 42 | ||||
-rw-r--r-- | spec/javascripts/ide/stores/utils_spec.js | 35 |
9 files changed, 640 insertions, 202 deletions
diff --git a/spec/javascripts/ide/components/commit_sidebar/actions_spec.js b/spec/javascripts/ide/components/commit_sidebar/actions_spec.js index b903abe63fc..a3db3ee1b18 100644 --- a/spec/javascripts/ide/components/commit_sidebar/actions_spec.js +++ b/spec/javascripts/ide/components/commit_sidebar/actions_spec.js @@ -1,30 +1,28 @@ import Vue from 'vue'; -import store from '~/ide/stores'; -import consts from '~/ide/stores/modules/commit/constants'; +import { createStore } from '~/ide/stores'; import commitActions from '~/ide/components/commit_sidebar/actions.vue'; +import consts from '~/ide/stores/modules/commit/constants'; import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; -import { resetStore } from 'spec/ide/helpers'; -import { projectData } from 'spec/ide/mock_data'; +import { projectData, branches } from 'spec/ide/mock_data'; + +const ACTION_UPDATE_COMMIT_ACTION = 'commit/updateCommitAction'; describe('IDE commit sidebar actions', () => { + let store; let vm; - const createComponent = ({ - hasMR = false, - commitAction = consts.COMMIT_TO_NEW_BRANCH, - mergeRequestsEnabled = true, - currentBranchId = 'master', - shouldCreateMR = false, - } = {}) => { + + const createComponent = ({ hasMR = false, currentBranchId = 'master' } = {}) => { const Component = Vue.extend(commitActions); vm = createComponentWithStore(Component, store); vm.$store.state.currentBranchId = currentBranchId; vm.$store.state.currentProjectId = 'abcproject'; - vm.$store.state.commit.commitAction = commitAction; - Vue.set(vm.$store.state.projects, 'abcproject', { ...projectData }); - vm.$store.state.projects.abcproject.merge_requests_enabled = mergeRequestsEnabled; - vm.$store.state.commit.shouldCreateMR = shouldCreateMR; + + const proj = { ...projectData }; + proj.branches[currentBranchId] = branches.find(branch => branch.name === currentBranchId); + + Vue.set(vm.$store.state.projects, 'abcproject', proj); if (hasMR) { vm.$store.state.currentMergeRequestId = '1'; @@ -33,13 +31,19 @@ describe('IDE commit sidebar actions', () => { ] = { foo: 'bar' }; } - return vm.$mount(); + vm.$mount(); + + return vm; }; + beforeEach(() => { + store = createStore(); + spyOn(store, 'dispatch'); + }); + afterEach(() => { vm.$destroy(); - - resetStore(vm.$store); + vm = null; }); it('renders 2 groups', () => { @@ -73,4 +77,152 @@ describe('IDE commit sidebar actions', () => { expect(vm.commitToCurrentBranchText).not.toContain(injectedSrc); }); }); + + describe('updateSelectedCommitAction', () => { + it('does not return anything if currentBranch does not exist', () => { + createComponent({ currentBranchId: null }); + + expect(vm.$store.dispatch).not.toHaveBeenCalled(); + }); + + it('calls again after staged changes', done => { + createComponent({ currentBranchId: null }); + + vm.$store.state.currentBranchId = 'master'; + vm.$store.state.changedFiles.push({}); + vm.$store.state.stagedFiles.push({}); + + vm.$nextTick() + .then(() => { + expect(vm.$store.dispatch).toHaveBeenCalledWith( + ACTION_UPDATE_COMMIT_ACTION, + jasmine.anything(), + ); + }) + .then(done) + .catch(done.fail); + }); + + describe('default branch', () => { + it('dispatches correct action for default branch', () => { + createComponent({ + currentBranchId: 'master', + }); + + expect(vm.$store.dispatch).toHaveBeenCalledTimes(1); + expect(vm.$store.dispatch).toHaveBeenCalledWith( + ACTION_UPDATE_COMMIT_ACTION, + consts.COMMIT_TO_NEW_BRANCH, + ); + }); + }); + + describe('protected branch', () => { + describe('with write access', () => { + it('dispatches correct action when MR exists', () => { + createComponent({ + hasMR: true, + currentBranchId: 'protected/access', + }); + + expect(vm.$store.dispatch).toHaveBeenCalledWith( + ACTION_UPDATE_COMMIT_ACTION, + consts.COMMIT_TO_CURRENT_BRANCH, + ); + }); + + it('dispatches correct action when MR does not exists', () => { + createComponent({ + hasMR: false, + currentBranchId: 'protected/access', + }); + + expect(vm.$store.dispatch).toHaveBeenCalledWith( + ACTION_UPDATE_COMMIT_ACTION, + consts.COMMIT_TO_CURRENT_BRANCH, + ); + }); + }); + + describe('without write access', () => { + it('dispatches correct action when MR exists', () => { + createComponent({ + hasMR: true, + currentBranchId: 'protected/no-access', + }); + + expect(vm.$store.dispatch).toHaveBeenCalledWith( + ACTION_UPDATE_COMMIT_ACTION, + consts.COMMIT_TO_NEW_BRANCH, + ); + }); + + it('dispatches correct action when MR does not exists', () => { + createComponent({ + hasMR: false, + currentBranchId: 'protected/no-access', + }); + + expect(vm.$store.dispatch).toHaveBeenCalledWith( + ACTION_UPDATE_COMMIT_ACTION, + consts.COMMIT_TO_NEW_BRANCH, + ); + }); + }); + }); + + describe('regular branch', () => { + describe('with write access', () => { + it('dispatches correct action when MR exists', () => { + createComponent({ + hasMR: true, + currentBranchId: 'regular', + }); + + expect(vm.$store.dispatch).toHaveBeenCalledWith( + ACTION_UPDATE_COMMIT_ACTION, + consts.COMMIT_TO_CURRENT_BRANCH, + ); + }); + + it('dispatches correct action when MR does not exists', () => { + createComponent({ + hasMR: false, + currentBranchId: 'regular', + }); + + expect(vm.$store.dispatch).toHaveBeenCalledWith( + ACTION_UPDATE_COMMIT_ACTION, + consts.COMMIT_TO_CURRENT_BRANCH, + ); + }); + }); + + describe('without write access', () => { + it('dispatches correct action when MR exists', () => { + createComponent({ + hasMR: true, + currentBranchId: 'regular/no-access', + }); + + expect(vm.$store.dispatch).toHaveBeenCalledWith( + ACTION_UPDATE_COMMIT_ACTION, + consts.COMMIT_TO_NEW_BRANCH, + ); + }); + + it('dispatches correct action when MR does not exists', () => { + createComponent({ + hasMR: false, + currentBranchId: 'regular/no-access', + }); + + expect(vm.$store.dispatch).toHaveBeenCalledWith( + ACTION_UPDATE_COMMIT_ACTION, + consts.COMMIT_TO_NEW_BRANCH, + ); + }); + }); + }); + }); }); diff --git a/spec/javascripts/ide/components/commit_sidebar/new_merge_request_option_spec.js b/spec/javascripts/ide/components/commit_sidebar/new_merge_request_option_spec.js index 7017bfcd6a6..5f2db695241 100644 --- a/spec/javascripts/ide/components/commit_sidebar/new_merge_request_option_spec.js +++ b/spec/javascripts/ide/components/commit_sidebar/new_merge_request_option_spec.js @@ -1,33 +1,36 @@ import Vue from 'vue'; import store from '~/ide/stores'; -import consts from '~/ide/stores/modules/commit/constants'; import NewMergeRequestOption from '~/ide/components/commit_sidebar/new_merge_request_option.vue'; import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; -import { projectData } from 'spec/ide/mock_data'; +import { projectData, branches } from 'spec/ide/mock_data'; import { resetStore } from 'spec/ide/helpers'; +import consts from '../../../../../app/assets/javascripts/ide/stores/modules/commit/constants'; describe('create new MR checkbox', () => { let vm; - const createComponent = ({ - hasMR = false, - commitAction = consts.COMMIT_TO_NEW_BRANCH, - currentBranchId = 'master', - } = {}) => { + const setMR = () => { + vm.$store.state.currentMergeRequestId = '1'; + vm.$store.state.projects[store.state.currentProjectId].mergeRequests[ + store.state.currentMergeRequestId + ] = { foo: 'bar' }; + }; + + const createComponent = ({ currentBranchId = 'master', createNewBranch = false } = {}) => { const Component = Vue.extend(NewMergeRequestOption); vm = createComponentWithStore(Component, store); + vm.$store.state.commit.commitAction = createNewBranch + ? consts.COMMIT_TO_NEW_BRANCH + : consts.COMMIT_TO_CURRENT_BRANCH; + vm.$store.state.currentBranchId = currentBranchId; vm.$store.state.currentProjectId = 'abcproject'; - vm.$store.state.commit.commitAction = commitAction; - Vue.set(vm.$store.state.projects, 'abcproject', { ...projectData }); - if (hasMR) { - vm.$store.state.currentMergeRequestId = '1'; - vm.$store.state.projects[store.state.currentProjectId].mergeRequests[ - store.state.currentMergeRequestId - ] = { foo: 'bar' }; - } + const proj = JSON.parse(JSON.stringify(projectData)); + proj.branches[currentBranchId] = branches.find(branch => branch.name === currentBranchId); + + Vue.set(vm.$store.state.projects, 'abcproject', proj); return vm.$mount(); }; @@ -38,30 +41,131 @@ describe('create new MR checkbox', () => { resetStore(vm.$store); }); - it('is hidden when an MR already exists and committing to current branch', () => { - createComponent({ - hasMR: true, - commitAction: consts.COMMIT_TO_CURRENT_BRANCH, - currentBranchId: 'feature', + describe('for default branch', () => { + describe('is rendered when pushing to a new branch', () => { + beforeEach(() => { + createComponent({ + currentBranchId: 'master', + createNewBranch: true, + }); + }); + + it('has NO new MR', () => { + expect(vm.$el.textContent).not.toBe(''); + }); + + it('has new MR', done => { + setMR(); + + vm.$nextTick() + .then(() => { + expect(vm.$el.textContent).not.toBe(''); + }) + .then(done) + .catch(done.fail); + }); }); - expect(vm.$el.textContent).toBe(''); + describe('is NOT rendered when pushing to the same branch', () => { + beforeEach(() => { + createComponent({ + currentBranchId: 'master', + createNewBranch: false, + }); + }); + + it('has NO new MR', () => { + expect(vm.$el.textContent).toBe(''); + }); + + it('has new MR', done => { + setMR(); + + vm.$nextTick() + .then(() => { + expect(vm.$el.textContent).toBe(''); + }) + .then(done) + .catch(done.fail); + }); + }); }); - it('does not hide checkbox if MR does not exist', () => { - createComponent({ hasMR: false }); + describe('for protected branch', () => { + describe('when user does not have the write access', () => { + beforeEach(() => { + createComponent({ + currentBranchId: 'protected/no-access', + }); + }); + + it('is rendered if MR does not exists', () => { + expect(vm.$el.textContent).not.toBe(''); + }); + + it('is rendered if MR exists', done => { + setMR(); - expect(vm.$el.querySelector('input[type="checkbox"]').hidden).toBe(false); + vm.$nextTick() + .then(() => { + expect(vm.$el.textContent).not.toBe(''); + }) + .then(done) + .catch(done.fail); + }); + }); + + describe('when user has the write access', () => { + beforeEach(() => { + createComponent({ + currentBranchId: 'protected/access', + }); + }); + + it('is rendered if MR does not exist', () => { + expect(vm.$el.textContent).not.toBe(''); + }); + + it('is hidden if MR exists', done => { + setMR(); + + vm.$nextTick() + .then(() => { + expect(vm.$el.textContent).toBe(''); + }) + .then(done) + .catch(done.fail); + }); + }); }); - it('does not hide checkbox when creating a new branch', () => { - createComponent({ commitAction: consts.COMMIT_TO_NEW_BRANCH }); + describe('for regular branch', () => { + beforeEach(() => { + createComponent({ + currentBranchId: 'regular', + }); + }); - expect(vm.$el.querySelector('input[type="checkbox"]').hidden).toBe(false); + it('is rendered if no MR exists', () => { + expect(vm.$el.textContent).not.toBe(''); + }); + + it('is hidden if MR exists', done => { + setMR(); + + vm.$nextTick() + .then(() => { + expect(vm.$el.textContent).toBe(''); + }) + .then(done) + .catch(done.fail); + }); }); it('dispatches toggleShouldCreateMR when clicking checkbox', () => { - createComponent(); + createComponent({ + currentBranchId: 'regular', + }); const el = vm.$el.querySelector('input[type="checkbox"]'); spyOn(vm.$store, 'dispatch'); el.dispatchEvent(new Event('change')); diff --git a/spec/javascripts/ide/components/repo_editor_spec.js b/spec/javascripts/ide/components/repo_editor_spec.js index 7dc5cb24981..0701b773e17 100644 --- a/spec/javascripts/ide/components/repo_editor_spec.js +++ b/spec/javascripts/ide/components/repo_editor_spec.js @@ -5,7 +5,7 @@ import axios from '~/lib/utils/axios_utils'; import store from '~/ide/stores'; import repoEditor from '~/ide/components/repo_editor.vue'; import Editor from '~/ide/lib/editor'; -import { activityBarViews } from '~/ide/constants'; +import { activityBarViews, FILE_VIEW_MODE_EDITOR, FILE_VIEW_MODE_PREVIEW } from '~/ide/constants'; import { createComponentWithStore } from '../../helpers/vue_mount_component_helper'; import setTimeoutPromise from '../../helpers/set_timeout_promise_helper'; import { file, resetStore } from '../helpers'; @@ -16,7 +16,7 @@ describe('RepoEditor', () => { beforeEach(done => { const f = { ...file(), - viewMode: 'editor', + viewMode: FILE_VIEW_MODE_EDITOR, }; const RepoEditor = Vue.extend(repoEditor); @@ -370,7 +370,7 @@ describe('RepoEditor', () => { describe('when files view mode is preview', () => { beforeEach(done => { spyOn(vm.editor, 'updateDimensions'); - vm.file.viewMode = 'preview'; + vm.file.viewMode = FILE_VIEW_MODE_PREVIEW; vm.$nextTick(done); }); @@ -392,7 +392,7 @@ describe('RepoEditor', () => { describe('when file view mode changes to editor', () => { beforeEach(done => { - vm.file.viewMode = 'editor'; + vm.file.viewMode = FILE_VIEW_MODE_EDITOR; // one tick to trigger watch vm.$nextTick() diff --git a/spec/javascripts/ide/mock_data.js b/spec/javascripts/ide/mock_data.js index 570a396c5e3..c02c7e5d45e 100644 --- a/spec/javascripts/ide/mock_data.js +++ b/spec/javascripts/ide/mock_data.js @@ -176,23 +176,51 @@ export const branches = [ committed_date: '2018-08-01T00:20:05Z', }, can_push: true, + protected: true, + default: true, }, { id: 2, - name: 'feature/lorem-ipsum', + name: 'protected/no-access', commit: { message: 'Update some stuff', committed_date: '2018-08-02T00:00:05Z', }, - can_push: true, + can_push: false, + protected: true, + default: false, }, { id: 3, - name: 'feature/dolar-amit', + name: 'protected/access', + commit: { + message: 'Update some stuff', + committed_date: '2018-08-02T00:00:05Z', + }, + can_push: true, + protected: true, + default: false, + }, + { + id: 4, + name: 'regular', commit: { message: 'Update some more stuff', committed_date: '2018-06-30T00:20:05Z', }, can_push: true, + protected: false, + default: false, + }, + { + id: 5, + name: 'regular/no-access', + commit: { + message: 'Update some more stuff', + committed_date: '2018-06-30T00:20:05Z', + }, + can_push: false, + protected: false, + default: false, }, ]; diff --git a/spec/javascripts/ide/stores/getters_spec.js b/spec/javascripts/ide/stores/getters_spec.js index 735bbd47f55..73a8d993a13 100644 --- a/spec/javascripts/ide/stores/getters_spec.js +++ b/spec/javascripts/ide/stores/getters_spec.js @@ -221,4 +221,36 @@ describe('IDE store getters', () => { }); }); }); + + describe('canPushToBranch', () => { + it('returns false when no currentBranch exists', () => { + const localGetters = { + currentProject: undefined, + }; + + expect(getters.canPushToBranch({}, localGetters)).toBeFalsy(); + }); + + it('returns true when can_push to currentBranch', () => { + const localGetters = { + currentProject: { + default_branch: 'master', + }, + 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(); + }); + }); }); diff --git a/spec/javascripts/ide/stores/modules/commit/actions_spec.js b/spec/javascripts/ide/stores/modules/commit/actions_spec.js index 8a3c132972e..091b454c0d2 100644 --- a/spec/javascripts/ide/stores/modules/commit/actions_spec.js +++ b/spec/javascripts/ide/stores/modules/commit/actions_spec.js @@ -57,6 +57,44 @@ describe('IDE commit module actions', () => { .then(done) .catch(done.fail); }); + + it('sets shouldCreateMR to true if "Create new MR" option is visible', done => { + store.state.shouldHideNewMrOption = false; + + testAction( + actions.updateCommitAction, + {}, + store.state, + [ + { + type: mutationTypes.UPDATE_COMMIT_ACTION, + payload: { commitAction: jasmine.anything() }, + }, + { type: mutationTypes.TOGGLE_SHOULD_CREATE_MR, payload: true }, + ], + [], + done, + ); + }); + + it('sets shouldCreateMR to false if "Create new MR" option is hidden', done => { + store.state.shouldHideNewMrOption = true; + + testAction( + actions.updateCommitAction, + {}, + store.state, + [ + { + type: mutationTypes.UPDATE_COMMIT_ACTION, + payload: { commitAction: jasmine.anything() }, + }, + { type: mutationTypes.TOGGLE_SHOULD_CREATE_MR, payload: false }, + ], + [], + done, + ); + }); }); describe('updateBranchName', () => { @@ -245,7 +283,7 @@ describe('IDE commit module actions', () => { master: { workingReference: '1', commit: { - short_id: TEST_COMMIT_SHA, + id: TEST_COMMIT_SHA, }, }, }, @@ -411,7 +449,7 @@ describe('IDE commit module actions', () => { expect(visitUrl).toHaveBeenCalledWith( `webUrl/merge_requests/new?merge_request[source_branch]=${ store.getters['commit/placeholderBranchName'] - }&merge_request[target_branch]=master`, + }&merge_request[target_branch]=master&nav_source=webide`, ); done(); @@ -541,147 +579,10 @@ describe('IDE commit module actions', () => { actions.toggleShouldCreateMR, {}, store.state, - [ - { type: mutationTypes.TOGGLE_SHOULD_CREATE_MR }, - { type: mutationTypes.INTERACT_WITH_NEW_MR }, - ], + [{ type: mutationTypes.TOGGLE_SHOULD_CREATE_MR }], [], done, ); }); }); - - describe('setShouldCreateMR', () => { - beforeEach(() => { - store.state.projects = { - project: { - default_branch: 'master', - branches: { - master: { - name: 'master', - }, - feature: { - name: 'feature', - }, - }, - }, - }; - - store.state.currentProjectId = 'project'; - }); - - it('sets to false when the current branch already has an MR', done => { - store.state.commit.currentMergeRequestId = 1; - store.state.commit.commitAction = consts.COMMIT_TO_CURRENT_BRANCH; - store.state.currentMergeRequestId = '1'; - store.state.currentBranchId = 'feature'; - spyOn(store, 'commit').and.callThrough(); - - store - .dispatch('commit/setShouldCreateMR') - .then(() => { - expect(store.commit.calls.allArgs()[0]).toEqual( - jasmine.arrayContaining([`commit/${mutationTypes.TOGGLE_SHOULD_CREATE_MR}`, false]), - ); - done(); - }) - .catch(done.fail); - }); - - it('changes to false when current branch is the default branch and user has not interacted', done => { - store.state.commit.interactedWithNewMR = false; - store.state.currentBranchId = 'master'; - store.state.commit.commitAction = consts.COMMIT_TO_CURRENT_BRANCH; - spyOn(store, 'commit').and.callThrough(); - - store - .dispatch('commit/setShouldCreateMR') - .then(() => { - expect(store.commit.calls.allArgs()[0]).toEqual( - jasmine.arrayContaining([`commit/${mutationTypes.TOGGLE_SHOULD_CREATE_MR}`, false]), - ); - done(); - }) - .catch(done.fail); - }); - - it('changes to true when "create new branch" is selected and user has not interacted', done => { - store.state.commit.commitAction = consts.COMMIT_TO_NEW_BRANCH; - store.state.commit.interactedWithNewMR = false; - spyOn(store, 'commit').and.callThrough(); - - store - .dispatch('commit/setShouldCreateMR') - .then(() => { - expect(store.commit.calls.allArgs()[0]).toEqual( - jasmine.arrayContaining([`commit/${mutationTypes.TOGGLE_SHOULD_CREATE_MR}`, true]), - ); - done(); - }) - .catch(done.fail); - }); - - it('does not change anything if user has interacted and comitting to new branch', done => { - store.state.commit.commitAction = consts.COMMIT_TO_NEW_BRANCH; - store.state.commit.interactedWithNewMR = true; - spyOn(store, 'commit').and.callThrough(); - - store - .dispatch('commit/setShouldCreateMR') - .then(() => { - expect(store.commit).not.toHaveBeenCalled(); - done(); - }) - .catch(done.fail); - }); - - it('does not change anything if user has interacted and comitting to branch without MR', done => { - store.state.commit.commitAction = consts.COMMIT_TO_CURRENT_BRANCH; - store.state.commit.currentMergeRequestId = null; - store.state.commit.interactedWithNewMR = true; - spyOn(store, 'commit').and.callThrough(); - - store - .dispatch('commit/setShouldCreateMR') - .then(() => { - expect(store.commit).not.toHaveBeenCalled(); - done(); - }) - .catch(done.fail); - }); - - it('still changes to false if hiding the checkbox', done => { - store.state.currentBranchId = 'feature'; - store.state.commit.commitAction = consts.COMMIT_TO_CURRENT_BRANCH; - store.state.currentMergeRequestId = '1'; - store.state.commit.interactedWithNewMR = true; - spyOn(store, 'commit').and.callThrough(); - - store - .dispatch('commit/setShouldCreateMR') - .then(() => { - expect(store.commit.calls.allArgs()[0]).toEqual( - jasmine.arrayContaining([`commit/${mutationTypes.TOGGLE_SHOULD_CREATE_MR}`, false]), - ); - done(); - }) - .catch(done.fail); - }); - - it('does not change to false when on master and user has interacted even if MR exists', done => { - store.state.currentBranchId = 'master'; - store.state.commit.commitAction = consts.COMMIT_TO_CURRENT_BRANCH; - store.state.currentMergeRequestId = '1'; - store.state.commit.interactedWithNewMR = true; - spyOn(store, 'commit').and.callThrough(); - - store - .dispatch('commit/setShouldCreateMR') - .then(() => { - expect(store.commit).not.toHaveBeenCalled(); - done(); - }) - .catch(done.fail); - }); - }); }); diff --git a/spec/javascripts/ide/stores/modules/commit/getters_spec.js b/spec/javascripts/ide/stores/modules/commit/getters_spec.js index 6e71a790deb..07445c22917 100644 --- a/spec/javascripts/ide/stores/modules/commit/getters_spec.js +++ b/spec/javascripts/ide/stores/modules/commit/getters_spec.js @@ -1,6 +1,6 @@ import commitState from '~/ide/stores/modules/commit/state'; -import consts from '~/ide/stores/modules/commit/constants'; import * as getters from '~/ide/stores/modules/commit/getters'; +import consts from '~/ide/stores/modules/commit/constants'; describe('IDE commit module getters', () => { let state; @@ -55,15 +55,15 @@ describe('IDE commit module getters', () => { }); }); - it('defualts to currentBranchId', () => { - expect(getters.branchName(state, null, rootState)).toBe('master'); + it('defaults to currentBranchId when not committing to a new branch', () => { + localGetters.isCreatingNewBranch = false; + + expect(getters.branchName(state, localGetters, rootState)).toBe('master'); }); - describe('COMMIT_TO_NEW_BRANCH', () => { + describe('commit to a new branch', () => { beforeEach(() => { - Object.assign(state, { - commitAction: consts.COMMIT_TO_NEW_BRANCH, - }); + localGetters.isCreatingNewBranch = true; }); it('uses newBranchName when not empty', () => { @@ -144,4 +144,152 @@ describe('IDE commit module getters', () => { }); }); }); + + describe('isCreatingNewBranch', () => { + it('returns false if NOT creating a new branch', () => { + state.commitAction = consts.COMMIT_TO_CURRENT_BRANCH; + + expect(getters.isCreatingNewBranch(state)).toBeFalsy(); + }); + + it('returns true if creating a new branch', () => { + state.commitAction = consts.COMMIT_TO_NEW_BRANCH; + + expect(getters.isCreatingNewBranch(state)).toBeTruthy(); + }); + }); + + describe('shouldHideNewMrOption', () => { + let localGetters = {}; + let rootGetters = {}; + + beforeEach(() => { + localGetters = { + isCreatingNewBranch: null, + }; + rootGetters = { + isOnDefaultBranch: null, + hasMergeRequest: null, + canPushToBranch: null, + }; + }); + + describe('NO existing MR for the branch', () => { + beforeEach(() => { + rootGetters.hasMergeRequest = false; + }); + + it('should never hide "New MR" option', () => { + expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy(); + }); + }); + + describe('existing MR for the branch', () => { + beforeEach(() => { + rootGetters.hasMergeRequest = true; + }); + + it('should NOT hide "New MR" option if user can NOT push to the current branch', () => { + rootGetters.canPushToBranch = false; + + expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy(); + }); + + it('should hide "New MR" option if user can push to the current branch', () => { + rootGetters.canPushToBranch = true; + + expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeTruthy(); + }); + }); + + describe('user can NOT push the branch', () => { + beforeEach(() => { + rootGetters.canPushToBranch = false; + }); + + it('should never hide "New MR" option', () => { + expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy(); + }); + }); + + describe('user can push to the branch', () => { + beforeEach(() => { + rootGetters.canPushToBranch = true; + }); + + it('should NOT hide "New MR" option if there is NO existing MR for the current branch', () => { + rootGetters.hasMergeRequest = false; + + expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy(); + }); + + it('should hide "New MR" option if there is existing MR for the current branch', () => { + rootGetters.hasMergeRequest = true; + + expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeTruthy(); + }); + }); + + describe('default branch', () => { + beforeEach(() => { + rootGetters.isOnDefaultBranch = true; + }); + + describe('committing to the same branch', () => { + beforeEach(() => { + localGetters.isCreatingNewBranch = false; + rootGetters.canPushToBranch = true; + }); + + it('should hide "New MR" when there is an existing MR', () => { + rootGetters.hasMergeRequest = true; + + expect( + getters.shouldHideNewMrOption(state, localGetters, null, rootGetters), + ).toBeTruthy(); + }); + + it('should hide "New MR" when there is no existing MR', () => { + rootGetters.hasMergeRequest = false; + + expect( + getters.shouldHideNewMrOption(state, localGetters, null, rootGetters), + ).toBeTruthy(); + }); + }); + + describe('creating a new branch', () => { + beforeEach(() => { + localGetters.isCreatingNewBranch = true; + }); + + it('should NOT hide "New MR" option no matter existence of an MR or write access', () => { + rootGetters.hasMergeRequest = false; + rootGetters.canPushToBranch = true; + + expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy(); + + rootGetters.hasMergeRequest = true; + rootGetters.canPushToBranch = true; + + expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy(); + + rootGetters.hasMergeRequest = false; + rootGetters.canPushToBranch = false; + + expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy(); + }); + }); + }); + + it('should never hide "New MR" option when creating a new branch', () => { + localGetters.isCreatingNewBranch = true; + + rootGetters.isOnDefaultBranch = false; + rootGetters.hasMergeRequest = true; + rootGetters.canPushToBranch = true; + + expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy(); + }); + }); }); diff --git a/spec/javascripts/ide/stores/mutations/file_spec.js b/spec/javascripts/ide/stores/mutations/file_spec.js index 7714f66c9a4..064e66cef64 100644 --- a/spec/javascripts/ide/stores/mutations/file_spec.js +++ b/spec/javascripts/ide/stores/mutations/file_spec.js @@ -1,5 +1,6 @@ import mutations from '~/ide/stores/mutations/file'; import state from '~/ide/stores/state'; +import { FILE_VIEW_MODE_PREVIEW } from '~/ide/constants'; import { file } from '../../helpers'; describe('IDE store file mutations', () => { @@ -103,6 +104,43 @@ describe('IDE store file mutations', () => { expect(localState.openFiles[0].rawPath).toEqual(rawPath); expect(localFile.rawPath).toEqual(rawPath); }); + + it('does not mutate certain props on the file', () => { + const path = 'New Path'; + const name = 'New Name'; + localFile.path = path; + localFile.name = name; + + localState.stagedFiles = [localFile]; + localState.changedFiles = [localFile]; + localState.openFiles = [localFile]; + + mutations.SET_FILE_DATA(localState, { + data: { + path: 'Old Path', + name: 'Old Name', + raw: 'Old Raw', + base_raw: 'Old Base Raw', + }, + file: localFile, + }); + + [ + localState.stagedFiles[0], + localState.changedFiles[0], + localState.openFiles[0], + localFile, + ].forEach(f => { + expect(f).toEqual( + jasmine.objectContaining({ + path, + name, + raw: null, + baseRaw: null, + }), + ); + }); + }); }); describe('SET_FILE_RAW_DATA', () => { @@ -388,10 +426,10 @@ describe('IDE store file mutations', () => { it('updates file view mode', () => { mutations.SET_FILE_VIEWMODE(localState, { file: localFile, - viewMode: 'preview', + viewMode: FILE_VIEW_MODE_PREVIEW, }); - expect(localFile.viewMode).toBe('preview'); + expect(localFile.viewMode).toBe(FILE_VIEW_MODE_PREVIEW); }); }); diff --git a/spec/javascripts/ide/stores/utils_spec.js b/spec/javascripts/ide/stores/utils_spec.js index bceb3a8db91..0fc9519a6bf 100644 --- a/spec/javascripts/ide/stores/utils_spec.js +++ b/spec/javascripts/ide/stores/utils_spec.js @@ -261,6 +261,41 @@ describe('Multi-file store utils', () => { }, ]); }); + + it('filters out folders from the list', () => { + const files = [ + { + path: 'a', + type: 'blob', + deleted: true, + }, + { + path: 'c', + type: 'tree', + deleted: true, + }, + { + path: 'c/d', + type: 'blob', + deleted: true, + }, + ]; + + const flattendFiles = utils.getCommitFiles(files); + + expect(flattendFiles).toEqual([ + { + path: 'a', + type: 'blob', + deleted: true, + }, + { + path: 'c/d', + type: 'blob', + deleted: true, + }, + ]); + }); }); describe('mergeTrees', () => { |