2020-12-01 19:09:45 -05:00
|
|
|
import {
|
|
|
|
findAllByText,
|
|
|
|
fireEvent,
|
|
|
|
getByLabelText,
|
|
|
|
findByTestId,
|
|
|
|
getByText,
|
|
|
|
screen,
|
2020-12-10 04:09:32 -05:00
|
|
|
findByText,
|
2020-12-01 19:09:45 -05:00
|
|
|
} from '@testing-library/dom';
|
2020-09-29 14:09:52 -04:00
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
const isFolderRowOpen = (row) => row.matches('.folder.is-open');
|
2020-09-29 14:09:52 -04:00
|
|
|
|
|
|
|
const getLeftSidebar = () => screen.getByTestId('left-sidebar');
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
export const switchLeftSidebarTab = (name) => {
|
2020-09-29 14:09:52 -04:00
|
|
|
const sidebar = getLeftSidebar();
|
|
|
|
|
|
|
|
const button = getByLabelText(sidebar, name);
|
|
|
|
|
|
|
|
button.click();
|
|
|
|
};
|
|
|
|
|
2020-12-01 19:09:45 -05:00
|
|
|
export const getStatusBar = () => document.querySelector('.ide-status-bar');
|
|
|
|
|
|
|
|
export const waitForMonacoEditor = () =>
|
2020-12-23 13:10:19 -05:00
|
|
|
new Promise((resolve) => window.monaco.editor.onDidCreateEditor(resolve));
|
2020-12-01 19:09:45 -05:00
|
|
|
|
2021-02-18 13:10:41 -05:00
|
|
|
export const waitForEditorModelChange = (instance) =>
|
|
|
|
new Promise((resolve) => instance.onDidChangeModel(resolve));
|
|
|
|
|
2020-11-19 04:09:38 -05:00
|
|
|
export const findMonacoEditor = () =>
|
2020-12-10 04:09:32 -05:00
|
|
|
screen.findAllByLabelText(/Editor content;/).then(([x]) => x.closest('.monaco-editor'));
|
|
|
|
|
|
|
|
export const findMonacoDiffEditor = () =>
|
|
|
|
screen.findAllByLabelText(/Editor content;/).then(([x]) => x.closest('.monaco-diff-editor'));
|
2020-09-29 14:09:52 -04:00
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
export const findAndSetEditorValue = async (value) => {
|
2020-09-29 14:09:52 -04:00
|
|
|
const editor = await findMonacoEditor();
|
|
|
|
const uri = editor.getAttribute('data-uri');
|
|
|
|
|
|
|
|
window.monaco.editor.getModel(uri).setValue(value);
|
|
|
|
};
|
|
|
|
|
2020-11-19 04:09:38 -05:00
|
|
|
export const getEditorValue = async () => {
|
|
|
|
const editor = await findMonacoEditor();
|
|
|
|
const uri = editor.getAttribute('data-uri');
|
|
|
|
|
|
|
|
return window.monaco.editor.getModel(uri).getValue();
|
|
|
|
};
|
|
|
|
|
2020-12-17 13:10:14 -05:00
|
|
|
const findTreeBody = () => screen.findByTestId('ide-tree-body');
|
2020-09-29 14:09:52 -04:00
|
|
|
|
2020-12-17 13:10:14 -05:00
|
|
|
const findRootActions = () => screen.findByTestId('ide-root-actions');
|
2020-10-29 05:08:38 -04:00
|
|
|
|
2020-09-29 14:09:52 -04:00
|
|
|
const findFileRowContainer = (row = null) =>
|
|
|
|
row ? Promise.resolve(row.parentElement) : findTreeBody();
|
|
|
|
|
|
|
|
const findFileChild = async (row, name, index = 0) => {
|
|
|
|
const container = await findFileRowContainer(row);
|
|
|
|
const children = await findAllByText(container, name, { selector: '.file-row-name' });
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
return children
|
|
|
|
.map((x) => x.closest('.file-row'))
|
|
|
|
.find((x) => x.dataset.level === index.toString());
|
2020-09-29 14:09:52 -04:00
|
|
|
};
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
const openFileRow = (row) => {
|
2020-10-29 05:08:38 -04:00
|
|
|
if (!row || isFolderRowOpen(row)) {
|
2020-09-29 14:09:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
row.click();
|
|
|
|
};
|
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
export const findAndTraverseToPath = async (path, index = 0, row = null) => {
|
2020-09-29 14:09:52 -04:00
|
|
|
if (!path) {
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [name, ...restOfPath] = path.split('/');
|
|
|
|
|
|
|
|
openFileRow(row);
|
|
|
|
|
|
|
|
const child = await findFileChild(row, name, index);
|
|
|
|
|
|
|
|
return findAndTraverseToPath(restOfPath.join('/'), index + 1, child);
|
|
|
|
};
|
|
|
|
|
|
|
|
const clickFileRowAction = (row, name) => {
|
|
|
|
fireEvent.mouseOver(row);
|
|
|
|
|
|
|
|
const dropdownButton = getByLabelText(row, 'Create new file or directory');
|
|
|
|
dropdownButton.click();
|
|
|
|
|
|
|
|
const dropdownAction = getByLabelText(dropdownButton.parentNode, name);
|
|
|
|
dropdownAction.click();
|
|
|
|
};
|
|
|
|
|
2020-12-01 19:09:45 -05:00
|
|
|
const fillFileNameModal = async (value, submitText = 'Create file') => {
|
|
|
|
const modal = await screen.findByTestId('ide-new-entry');
|
|
|
|
|
|
|
|
const nameField = await findByTestId(modal, 'file-name-field');
|
2020-09-29 14:09:52 -04:00
|
|
|
fireEvent.input(nameField, { target: { value } });
|
|
|
|
|
2020-12-01 19:09:45 -05:00
|
|
|
const createButton = getByText(modal, submitText, { selector: 'button' });
|
2020-09-29 14:09:52 -04:00
|
|
|
createButton.click();
|
|
|
|
};
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
const findAndClickRootAction = async (name) => {
|
2020-10-29 05:08:38 -04:00
|
|
|
const container = await findRootActions();
|
|
|
|
const button = getByLabelText(container, name);
|
|
|
|
|
|
|
|
button.click();
|
|
|
|
};
|
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
/**
|
|
|
|
* Drop leading "/-/ide" and file path from the current URL
|
|
|
|
*/
|
|
|
|
export const getBaseRoute = (url = window.location.pathname) =>
|
|
|
|
url.replace(/^\/-\/ide/, '').replace(/\/-\/.*$/, '');
|
|
|
|
|
2020-12-01 19:09:45 -05:00
|
|
|
export const clickPreviewMarkdown = () => {
|
|
|
|
screen.getByText('Preview Markdown').click();
|
|
|
|
};
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
export const openFile = async (path) => {
|
2020-10-29 05:08:38 -04:00
|
|
|
const row = await findAndTraverseToPath(path);
|
|
|
|
|
|
|
|
openFileRow(row);
|
|
|
|
};
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
export const waitForTabToOpen = (fileName) =>
|
2020-12-10 04:09:32 -05:00
|
|
|
findByText(document.querySelector('.multi-file-edit-pane'), fileName);
|
|
|
|
|
2020-09-29 14:09:52 -04:00
|
|
|
export const createFile = async (path, content) => {
|
2020-12-23 07:10:26 -05:00
|
|
|
const parentPath = path.split('/').slice(0, -1).join('/');
|
2020-09-29 14:09:52 -04:00
|
|
|
|
|
|
|
const parentRow = await findAndTraverseToPath(parentPath);
|
2020-10-29 05:08:38 -04:00
|
|
|
|
|
|
|
if (parentRow) {
|
|
|
|
clickFileRowAction(parentRow, 'New file');
|
|
|
|
} else {
|
|
|
|
await findAndClickRootAction('New file');
|
|
|
|
}
|
2020-09-29 14:09:52 -04:00
|
|
|
|
2020-12-01 19:09:45 -05:00
|
|
|
await fillFileNameModal(path);
|
2020-09-29 14:09:52 -04:00
|
|
|
await findAndSetEditorValue(content);
|
|
|
|
};
|
|
|
|
|
2021-01-13 22:10:47 -05:00
|
|
|
export const updateFile = async (path, content) => {
|
|
|
|
await openFile(path);
|
|
|
|
await findAndSetEditorValue(content);
|
|
|
|
};
|
|
|
|
|
2020-11-19 04:09:38 -05:00
|
|
|
export const getFilesList = () => {
|
2020-12-23 13:10:19 -05:00
|
|
|
return screen.getAllByTestId('file-row-name-container').map((e) => e.textContent.trim());
|
2020-11-19 04:09:38 -05:00
|
|
|
};
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
export const deleteFile = async (path) => {
|
2020-09-29 14:09:52 -04:00
|
|
|
const row = await findAndTraverseToPath(path);
|
|
|
|
clickFileRowAction(row, 'Delete');
|
|
|
|
};
|
|
|
|
|
2020-12-01 19:09:45 -05:00
|
|
|
export const renameFile = async (path, newPath) => {
|
|
|
|
const row = await findAndTraverseToPath(path);
|
|
|
|
clickFileRowAction(row, 'Rename/Move');
|
|
|
|
|
|
|
|
await fillFileNameModal(newPath, 'Rename file');
|
|
|
|
};
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
export const closeFile = async (path) => {
|
2020-12-01 19:09:45 -05:00
|
|
|
const button = await screen.getByLabelText(`Close ${path}`, {
|
|
|
|
selector: '.multi-file-tabs button',
|
|
|
|
});
|
|
|
|
|
|
|
|
button.click();
|
|
|
|
};
|
|
|
|
|
2021-01-13 22:10:47 -05:00
|
|
|
/**
|
|
|
|
* Fill out and submit the commit form in the Web IDE
|
|
|
|
*
|
|
|
|
* @param {Object} options - Used to fill out the commit form in the IDE
|
|
|
|
* @param {Boolean} options.newBranch - Flag for the "Create a new branch" radio.
|
|
|
|
* @param {Boolean} options.newMR - Flag for the "Start a new merge request" checkbox.
|
|
|
|
* @param {String} options.newBranchName - Value to put in the new branch name input field. The Web IDE supports leaving this field blank.
|
|
|
|
*/
|
|
|
|
export const commit = async ({ newBranch = false, newMR = false, newBranchName = '' } = {}) => {
|
2020-12-10 04:09:32 -05:00
|
|
|
switchLeftSidebarTab('Commit');
|
2020-09-29 14:09:52 -04:00
|
|
|
screen.getByTestId('begin-commit-button').click();
|
|
|
|
|
2021-01-13 22:10:47 -05:00
|
|
|
if (!newBranch) {
|
|
|
|
const option = await screen.findByLabelText(/Commit to .+ branch/);
|
|
|
|
option.click();
|
|
|
|
} else {
|
|
|
|
const option = await screen.findByLabelText('Create a new branch');
|
|
|
|
option.click();
|
|
|
|
|
|
|
|
const branchNameInput = await screen.findByTestId('ide-new-branch-name');
|
|
|
|
fireEvent.input(branchNameInput, { target: { value: newBranchName } });
|
|
|
|
|
|
|
|
const mrCheck = await screen.findByLabelText('Start a new merge request');
|
|
|
|
if (Boolean(mrCheck.checked) !== newMR) {
|
|
|
|
mrCheck.click();
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 14:09:52 -04:00
|
|
|
|
|
|
|
screen.getByText('Commit').click();
|
|
|
|
};
|