debian-mirror-gitlab/spec/frontend/ide/components/ide_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

215 lines
5.5 KiB
JavaScript
Raw Normal View History

2022-04-04 11:22:00 +05:30
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
2021-03-11 19:13:27 +05:30
import Vuex from 'vuex';
2021-02-22 17:27:13 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2022-08-13 15:12:31 +05:30
import { stubPerformanceWebAPI } from 'helpers/performance';
2023-01-13 00:05:48 +05:30
import { __ } from '~/locale';
2021-04-29 21:17:54 +05:30
import CannotPushCodeAlert from '~/ide/components/cannot_push_code_alert.vue';
2021-01-29 00:20:46 +05:30
import ErrorMessage from '~/ide/components/error_message.vue';
2021-03-11 19:13:27 +05:30
import Ide from '~/ide/components/ide.vue';
2023-01-13 00:05:48 +05:30
import eventHub from '~/ide/eventhub';
2021-04-29 21:17:54 +05:30
import { MSG_CANNOT_PUSH_CODE_GO_TO_FORK, MSG_GO_TO_FORK } from '~/ide/messages';
2021-03-11 19:13:27 +05:30
import { createStore } from '~/ide/stores';
2020-10-24 23:57:45 +05:30
import { file } from '../helpers';
2018-10-15 14:42:47 +05:30
import { projectData } from '../mock_data';
2018-05-09 12:01:36 +05:30
2022-04-04 11:22:00 +05:30
Vue.use(Vuex);
2020-06-23 00:09:42 +05:30
2021-04-29 21:17:54 +05:30
const TEST_FORK_IDE_PATH = '/test/ide/path';
2023-01-13 00:05:48 +05:30
const MSG_ARE_YOU_SURE = __('Are you sure you want to lose unsaved changes?');
2021-04-29 21:17:54 +05:30
2021-01-29 00:20:46 +05:30
describe('WebIDE', () => {
const emptyProjData = { ...projectData, empty_repo: true, branches: {} };
2020-06-23 00:09:42 +05:30
2021-04-29 21:17:54 +05:30
let store;
2021-01-29 00:20:46 +05:30
let wrapper;
2019-09-04 21:01:54 +05:30
2021-03-11 19:13:27 +05:30
const createComponent = ({ projData = emptyProjData, state = {} } = {}) => {
2021-01-29 00:20:46 +05:30
store.state.currentProjectId = 'abcproject';
2021-09-04 01:27:46 +05:30
store.state.currentBranchId = 'main';
2021-03-11 19:13:27 +05:30
store.state.projects.abcproject = projData && { ...projData };
2021-09-04 01:27:46 +05:30
store.state.trees['abcproject/main'] = {
2021-01-29 00:20:46 +05:30
tree: [],
loading: false,
};
2021-03-08 18:12:59 +05:30
Object.keys(state).forEach((key) => {
2021-01-29 00:20:46 +05:30
store.state[key] = state[key];
});
2018-05-09 12:01:36 +05:30
2021-03-11 19:13:27 +05:30
wrapper = shallowMount(Ide, {
2021-01-29 00:20:46 +05:30
store,
});
2021-03-11 19:13:27 +05:30
};
2021-04-29 21:17:54 +05:30
const findAlert = () => wrapper.findComponent(CannotPushCodeAlert);
2023-01-13 00:05:48 +05:30
const callOnBeforeUnload = (e = {}) => window.onbeforeunload(e);
2023-07-09 08:55:56 +05:30
beforeAll(() => {
// HACK: Workaround readonly property in Jest
Object.defineProperty(window, 'onbeforeunload', {
writable: true,
});
});
2021-04-29 21:17:54 +05:30
beforeEach(() => {
2022-08-13 15:12:31 +05:30
stubPerformanceWebAPI();
2021-04-29 21:17:54 +05:30
store = createStore();
});
2018-05-09 12:01:36 +05:30
2019-09-04 21:01:54 +05:30
afterEach(() => {
2023-01-13 00:05:48 +05:30
window.onbeforeunload = null;
2019-09-04 21:01:54 +05:30
});
2018-10-15 14:42:47 +05:30
2021-01-29 00:20:46 +05:30
describe('ide component, empty repo', () => {
beforeEach(() => {
2021-03-11 19:13:27 +05:30
createComponent({
2021-01-29 00:20:46 +05:30
projData: {
empty_repo: true,
},
});
2019-09-04 21:01:54 +05:30
});
2023-06-20 00:43:36 +05:30
it('renders "New file" button in empty repo', () => {
2021-01-29 00:20:46 +05:30
expect(wrapper.find('[title="New file"]').exists()).toBe(true);
});
2018-05-09 12:01:36 +05:30
});
2021-01-29 00:20:46 +05:30
describe('ide component, non-empty repo', () => {
describe('error message', () => {
2021-02-22 17:27:13 +05:30
it.each`
errorMessage | exists
${null} | ${false}
${{ text: 'error' }} | ${true}
`(
'should error message exists=$exists when errorMessage=$errorMessage',
async ({ errorMessage, exists }) => {
2021-03-11 19:13:27 +05:30
createComponent({
2021-02-22 17:27:13 +05:30
state: {
errorMessage,
2021-01-29 00:20:46 +05:30
},
2021-02-22 17:27:13 +05:30
});
2018-05-09 12:01:36 +05:30
2021-02-22 17:27:13 +05:30
await waitForPromises();
2022-08-27 11:52:29 +05:30
expect(wrapper.findComponent(ErrorMessage).exists()).toBe(exists);
2021-02-22 17:27:13 +05:30
},
);
2018-05-09 12:01:36 +05:30
});
2018-10-15 14:42:47 +05:30
2021-01-29 00:20:46 +05:30
describe('onBeforeUnload', () => {
it('returns undefined when no staged files or changed files', () => {
2021-03-11 19:13:27 +05:30
createComponent();
2023-01-13 00:05:48 +05:30
expect(callOnBeforeUnload()).toBe(undefined);
2021-01-29 00:20:46 +05:30
});
2018-11-18 11:00:15 +05:30
2021-01-29 00:20:46 +05:30
it('returns warning text when their are changed files', () => {
2021-03-11 19:13:27 +05:30
createComponent({
2021-01-29 00:20:46 +05:30
state: {
changedFiles: [file()],
},
});
2018-11-18 11:00:15 +05:30
2023-01-13 00:05:48 +05:30
const e = {};
expect(callOnBeforeUnload(e)).toBe(MSG_ARE_YOU_SURE);
expect(e.returnValue).toBe(MSG_ARE_YOU_SURE);
2021-01-29 00:20:46 +05:30
});
2018-11-18 11:00:15 +05:30
2021-01-29 00:20:46 +05:30
it('returns warning text when their are staged files', () => {
2021-03-11 19:13:27 +05:30
createComponent({
2021-01-29 00:20:46 +05:30
state: {
stagedFiles: [file()],
},
});
2018-11-18 11:00:15 +05:30
2023-01-13 00:05:48 +05:30
const e = {};
expect(callOnBeforeUnload(e)).toBe(MSG_ARE_YOU_SURE);
expect(e.returnValue).toBe(MSG_ARE_YOU_SURE);
2021-01-29 00:20:46 +05:30
});
2018-11-18 11:00:15 +05:30
2023-01-13 00:05:48 +05:30
it('returns undefined once after "skip-beforeunload" was emitted', () => {
2021-03-11 19:13:27 +05:30
createComponent({
2021-01-29 00:20:46 +05:30
state: {
stagedFiles: [file()],
},
});
2018-11-18 11:00:15 +05:30
2023-01-13 00:05:48 +05:30
eventHub.$emit('skip-beforeunload');
const e = {};
expect(callOnBeforeUnload()).toBe(undefined);
expect(e.returnValue).toBe(undefined);
2018-11-18 11:00:15 +05:30
2023-01-13 00:05:48 +05:30
expect(callOnBeforeUnload(e)).toBe(MSG_ARE_YOU_SURE);
expect(e.returnValue).toBe(MSG_ARE_YOU_SURE);
2021-01-29 00:20:46 +05:30
});
2018-11-18 11:00:15 +05:30
});
2021-01-29 00:20:46 +05:30
describe('non-existent branch', () => {
it('does not render "New file" button for non-existent branch when repo is not empty', () => {
2021-03-11 19:13:27 +05:30
createComponent({
2021-01-29 00:20:46 +05:30
state: {
projects: {},
},
});
expect(wrapper.find('[title="New file"]').exists()).toBe(false);
2019-09-04 21:01:54 +05:30
});
});
2018-11-08 19:23:39 +05:30
2021-01-29 00:20:46 +05:30
describe('branch with files', () => {
beforeEach(() => {
2021-03-11 19:13:27 +05:30
createComponent({
2021-01-29 00:20:46 +05:30
projData: {
empty_repo: false,
},
});
});
2018-11-08 19:23:39 +05:30
2021-01-29 00:20:46 +05:30
it('does not render "New file" button', () => {
expect(wrapper.find('[title="New file"]').exists()).toBe(false);
2019-09-04 21:01:54 +05:30
});
2018-11-08 19:23:39 +05:30
});
});
2021-03-11 19:13:27 +05:30
2023-06-20 00:43:36 +05:30
it('when user cannot push code, shows an alert', () => {
2021-04-29 21:17:54 +05:30
store.state.links = {
forkInfo: {
ide_path: TEST_FORK_IDE_PATH,
},
};
2021-03-11 19:13:27 +05:30
createComponent({
projData: {
userPermissions: {
pushCode: false,
},
},
});
expect(findAlert().props()).toMatchObject({
2021-04-29 21:17:54 +05:30
message: MSG_CANNOT_PUSH_CODE_GO_TO_FORK,
action: {
href: TEST_FORK_IDE_PATH,
text: MSG_GO_TO_FORK,
},
2021-03-11 19:13:27 +05:30
});
});
it.each`
desc | projData
${'when user can push code'} | ${{ userPermissions: { pushCode: true } }}
${'when project is not ready'} | ${null}
`('$desc, no alert is shown', ({ projData }) => {
createComponent({
projData,
});
expect(findAlert().exists()).toBe(false);
});
2018-05-09 12:01:36 +05:30
});