2022-04-04 11:22:00 +05:30
|
|
|
import Vue, { nextTick } from 'vue';
|
2022-11-25 23:54:43 +05:30
|
|
|
import { mount } from '@vue/test-utils';
|
2020-04-22 19:07:51 +05:30
|
|
|
import { projectData, branches } from 'jest/ide/mock_data';
|
2022-11-25 23:54:43 +05:30
|
|
|
import CommitActions from '~/ide/components/commit_sidebar/actions.vue';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { createStore } from '~/ide/stores';
|
|
|
|
import {
|
|
|
|
COMMIT_TO_NEW_BRANCH,
|
|
|
|
COMMIT_TO_CURRENT_BRANCH,
|
|
|
|
} from '~/ide/stores/modules/commit/constants';
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
const ACTION_UPDATE_COMMIT_ACTION = 'commit/updateCommitAction';
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
const BRANCH_DEFAULT = 'main';
|
2020-04-22 19:07:51 +05:30
|
|
|
const BRANCH_PROTECTED = 'protected/access';
|
|
|
|
const BRANCH_PROTECTED_NO_ACCESS = 'protected/no-access';
|
|
|
|
const BRANCH_REGULAR = 'regular';
|
|
|
|
const BRANCH_REGULAR_NO_ACCESS = 'regular/no-access';
|
|
|
|
|
|
|
|
describe('IDE commit sidebar actions', () => {
|
|
|
|
let store;
|
2022-11-25 23:54:43 +05:30
|
|
|
let wrapper;
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
const createComponent = ({ hasMR = false, currentBranchId = 'main', emptyRepo = false } = {}) => {
|
2022-11-25 23:54:43 +05:30
|
|
|
store.state.currentBranchId = currentBranchId;
|
|
|
|
store.state.currentProjectId = 'abcproject';
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
const proj = { ...projectData };
|
2021-03-08 18:12:59 +05:30
|
|
|
proj.branches[currentBranchId] = branches.find((branch) => branch.name === currentBranchId);
|
2020-04-22 19:07:51 +05:30
|
|
|
proj.empty_repo = emptyRepo;
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
Vue.set(store.state.projects, 'abcproject', proj);
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
if (hasMR) {
|
2022-11-25 23:54:43 +05:30
|
|
|
store.state.currentMergeRequestId = '1';
|
|
|
|
store.state.projects[store.state.currentProjectId].mergeRequests[
|
2020-04-22 19:07:51 +05:30
|
|
|
store.state.currentMergeRequestId
|
|
|
|
] = { foo: 'bar' };
|
|
|
|
}
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper = mount(CommitActions, { store });
|
|
|
|
return wrapper;
|
2020-04-22 19:07:51 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
store = createStore();
|
|
|
|
jest.spyOn(store, 'dispatch').mockImplementation(() => {});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper.destroy();
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
const findText = () => wrapper.text();
|
|
|
|
const findRadios = () => wrapper.findAll('input[type="radio"]');
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
it('renders 2 groups', () => {
|
|
|
|
createComponent();
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(findRadios()).toHaveLength(2);
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('renders current branch text', () => {
|
|
|
|
createComponent();
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(findText()).toContain('Commit to main branch');
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it('hides merge request option when project merge requests are disabled', async () => {
|
2020-04-22 19:07:51 +05:30
|
|
|
createComponent({ hasMR: false });
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
|
|
|
expect(findRadios().length).toBe(2);
|
|
|
|
expect(findText()).not.toContain('Create a new branch and merge request');
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('escapes current branch name', () => {
|
|
|
|
const injectedSrc = '<img src="x" />';
|
|
|
|
const escapedSrc = '<img src="x" />';
|
|
|
|
createComponent({ currentBranchId: injectedSrc });
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.text()).not.toContain(injectedSrc);
|
|
|
|
expect(wrapper.text).not.toContain(escapedSrc);
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('updateSelectedCommitAction', () => {
|
|
|
|
it('does not return anything if currentBranch does not exist', () => {
|
|
|
|
createComponent({ currentBranchId: null });
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(store.dispatch).not.toHaveBeenCalled();
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('is not called on mount if there is already a selected commitAction', () => {
|
|
|
|
store.state.commitAction = '1';
|
|
|
|
createComponent({ currentBranchId: null });
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(store.dispatch).not.toHaveBeenCalled();
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it('calls again after staged changes', async () => {
|
2020-04-22 19:07:51 +05:30
|
|
|
createComponent({ currentBranchId: null });
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
store.state.currentBranchId = 'main';
|
|
|
|
store.state.changedFiles.push({});
|
|
|
|
store.state.stagedFiles.push({});
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(store.dispatch).toHaveBeenCalledWith(ACTION_UPDATE_COMMIT_ACTION, expect.anything());
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
input | expectedOption
|
2021-03-11 19:13:27 +05:30
|
|
|
${{ currentBranchId: BRANCH_DEFAULT }} | ${COMMIT_TO_NEW_BRANCH}
|
|
|
|
${{ currentBranchId: BRANCH_DEFAULT, emptyRepo: true }} | ${COMMIT_TO_CURRENT_BRANCH}
|
|
|
|
${{ currentBranchId: BRANCH_PROTECTED, hasMR: true }} | ${COMMIT_TO_CURRENT_BRANCH}
|
|
|
|
${{ currentBranchId: BRANCH_PROTECTED, hasMR: false }} | ${COMMIT_TO_CURRENT_BRANCH}
|
|
|
|
${{ currentBranchId: BRANCH_PROTECTED_NO_ACCESS, hasMR: true }} | ${COMMIT_TO_NEW_BRANCH}
|
|
|
|
${{ currentBranchId: BRANCH_PROTECTED_NO_ACCESS, hasMR: false }} | ${COMMIT_TO_NEW_BRANCH}
|
|
|
|
${{ currentBranchId: BRANCH_REGULAR, hasMR: true }} | ${COMMIT_TO_CURRENT_BRANCH}
|
|
|
|
${{ currentBranchId: BRANCH_REGULAR, hasMR: false }} | ${COMMIT_TO_CURRENT_BRANCH}
|
|
|
|
${{ currentBranchId: BRANCH_REGULAR_NO_ACCESS, hasMR: true }} | ${COMMIT_TO_NEW_BRANCH}
|
|
|
|
${{ currentBranchId: BRANCH_REGULAR_NO_ACCESS, hasMR: false }} | ${COMMIT_TO_NEW_BRANCH}
|
2020-04-22 19:07:51 +05:30
|
|
|
`(
|
|
|
|
'with $input, it dispatches update commit action with $expectedOption',
|
|
|
|
({ input, expectedOption }) => {
|
|
|
|
createComponent(input);
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(store.dispatch.mock.calls).toEqual([[ACTION_UPDATE_COMMIT_ACTION, expectedOption]]);
|
2020-04-22 19:07:51 +05:30
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when empty project', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent({ emptyRepo: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('only renders commit to current branch', () => {
|
|
|
|
expect(findRadios().length).toBe(1);
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(findText()).toContain('Commit to main branch');
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|