debian-mirror-gitlab/spec/javascripts/ide/components/commit_sidebar/actions_spec.js

229 lines
6.4 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import Vue from 'vue';
2020-01-01 13:55:28 +05:30
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import { projectData, branches } from 'spec/ide/mock_data';
2019-12-04 20:38:33 +05:30
import { createStore } from '~/ide/stores';
2018-05-09 12:01:36 +05:30
import commitActions from '~/ide/components/commit_sidebar/actions.vue';
2019-12-04 20:38:33 +05:30
import consts from '~/ide/stores/modules/commit/constants';
const ACTION_UPDATE_COMMIT_ACTION = 'commit/updateCommitAction';
2018-05-09 12:01:36 +05:30
describe('IDE commit sidebar actions', () => {
2019-12-04 20:38:33 +05:30
let store;
2018-05-09 12:01:36 +05:30
let vm;
2019-12-04 20:38:33 +05:30
const createComponent = ({ hasMR = false, currentBranchId = 'master' } = {}) => {
2018-05-09 12:01:36 +05:30
const Component = Vue.extend(commitActions);
vm = createComponentWithStore(Component, store);
2019-07-07 11:18:12 +05:30
vm.$store.state.currentBranchId = currentBranchId;
2018-11-08 19:23:39 +05:30
vm.$store.state.currentProjectId = 'abcproject';
2019-12-04 20:38:33 +05:30
const proj = { ...projectData };
proj.branches[currentBranchId] = branches.find(branch => branch.name === currentBranchId);
Vue.set(vm.$store.state.projects, 'abcproject', proj);
2018-05-09 12:01:36 +05:30
2019-07-07 11:18:12 +05:30
if (hasMR) {
vm.$store.state.currentMergeRequestId = '1';
vm.$store.state.projects[store.state.currentProjectId].mergeRequests[
store.state.currentMergeRequestId
] = { foo: 'bar' };
}
2018-05-09 12:01:36 +05:30
2019-12-04 20:38:33 +05:30
vm.$mount();
return vm;
2019-07-07 11:18:12 +05:30
};
2018-05-09 12:01:36 +05:30
2019-12-04 20:38:33 +05:30
beforeEach(() => {
store = createStore();
spyOn(store, 'dispatch');
});
2018-05-09 12:01:36 +05:30
afterEach(() => {
vm.$destroy();
2019-12-04 20:38:33 +05:30
vm = null;
2018-05-09 12:01:36 +05:30
});
2019-07-07 11:18:12 +05:30
it('renders 2 groups', () => {
createComponent();
expect(vm.$el.querySelectorAll('input[type="radio"]').length).toBe(2);
2018-05-09 12:01:36 +05:30
});
it('renders current branch text', () => {
2019-07-07 11:18:12 +05:30
createComponent();
2018-05-09 12:01:36 +05:30
expect(vm.$el.textContent).toContain('Commit to master branch');
});
2018-10-15 14:42:47 +05:30
2018-11-08 19:23:39 +05:30
it('hides merge request option when project merge requests are disabled', done => {
2019-07-07 11:18:12 +05:30
createComponent({ mergeRequestsEnabled: false });
2018-11-08 19:23:39 +05:30
vm.$nextTick(() => {
expect(vm.$el.querySelectorAll('input[type="radio"]').length).toBe(2);
expect(vm.$el.textContent).not.toContain('Create a new branch and merge request');
done();
});
});
2018-10-15 14:42:47 +05:30
describe('commitToCurrentBranchText', () => {
it('escapes current branch', () => {
2019-07-07 11:18:12 +05:30
const injectedSrc = '<img src="x" />';
createComponent({ currentBranchId: injectedSrc });
expect(vm.commitToCurrentBranchText).not.toContain(injectedSrc);
});
});
2019-12-04 20:38:33 +05:30
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,
);
});
});
});
});
2018-05-09 12:01:36 +05:30
});