debian-mirror-gitlab/spec/frontend_integration/ide/helpers/ide_helper.js

174 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-02-22 17:27:13 +05:30
import {
findAllByText,
fireEvent,
getByLabelText,
findByTestId,
getByText,
screen,
findByText,
} from '@testing-library/dom';
2021-01-03 14:25:43 +05:30
const isFolderRowOpen = row => row.matches('.folder.is-open');
const getLeftSidebar = () => screen.getByTestId('left-sidebar');
2021-02-22 17:27:13 +05:30
export const switchLeftSidebarTab = name => {
2021-01-03 14:25:43 +05:30
const sidebar = getLeftSidebar();
const button = getByLabelText(sidebar, name);
button.click();
};
2021-02-22 17:27:13 +05:30
export const getStatusBar = () => document.querySelector('.ide-status-bar');
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
export const waitForMonacoEditor = () =>
new Promise(resolve => window.monaco.editor.onDidCreateEditor(resolve));
export const findMonacoEditor = () =>
screen.findAllByLabelText(/Editor content;/).then(([x]) => x.closest('.monaco-editor'));
export const findMonacoDiffEditor = () =>
screen.findAllByLabelText(/Editor content;/).then(([x]) => x.closest('.monaco-diff-editor'));
export const findAndSetEditorValue = async value => {
2021-01-03 14:25:43 +05:30
const editor = await findMonacoEditor();
const uri = editor.getAttribute('data-uri');
window.monaco.editor.getModel(uri).setValue(value);
};
2021-02-22 17:27:13 +05:30
export const getEditorValue = async () => {
const editor = await findMonacoEditor();
const uri = editor.getAttribute('data-uri');
return window.monaco.editor.getModel(uri).getValue();
};
2021-01-03 14:25:43 +05:30
const findTreeBody = () => screen.findByTestId('ide-tree-body', {}, { timeout: 5000 });
const findRootActions = () => screen.findByTestId('ide-root-actions', {}, { timeout: 7000 });
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' });
return children.map(x => x.closest('.file-row')).find(x => x.dataset.level === index.toString());
};
const openFileRow = row => {
if (!row || isFolderRowOpen(row)) {
return;
}
row.click();
};
const findAndTraverseToPath = async (path, index = 0, row = null) => {
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();
};
2021-02-22 17:27:13 +05:30
const fillFileNameModal = async (value, submitText = 'Create file') => {
const modal = await screen.findByTestId('ide-new-entry');
const nameField = await findByTestId(modal, 'file-name-field');
2021-01-03 14:25:43 +05:30
fireEvent.input(nameField, { target: { value } });
2021-02-22 17:27:13 +05:30
const createButton = getByText(modal, submitText, { selector: 'button' });
2021-01-03 14:25:43 +05:30
createButton.click();
};
const findAndClickRootAction = async name => {
const container = await findRootActions();
const button = getByLabelText(container, name);
button.click();
};
2021-02-22 17:27:13 +05:30
export const clickPreviewMarkdown = () => {
screen.getByText('Preview Markdown').click();
};
2021-01-03 14:25:43 +05:30
export const openFile = async path => {
const row = await findAndTraverseToPath(path);
openFileRow(row);
};
2021-02-22 17:27:13 +05:30
export const waitForTabToOpen = fileName =>
findByText(document.querySelector('.multi-file-edit-pane'), fileName);
2021-01-03 14:25:43 +05:30
export const createFile = async (path, content) => {
const parentPath = path
.split('/')
.slice(0, -1)
.join('/');
const parentRow = await findAndTraverseToPath(parentPath);
if (parentRow) {
clickFileRowAction(parentRow, 'New file');
} else {
await findAndClickRootAction('New file');
}
2021-02-22 17:27:13 +05:30
await fillFileNameModal(path);
2021-01-03 14:25:43 +05:30
await findAndSetEditorValue(content);
};
2021-02-22 17:27:13 +05:30
export const getFilesList = () => {
return screen.getAllByTestId('file-row-name-container').map(e => e.textContent.trim());
};
2021-01-03 14:25:43 +05:30
export const deleteFile = async path => {
const row = await findAndTraverseToPath(path);
clickFileRowAction(row, 'Delete');
};
2021-02-22 17:27:13 +05:30
export const renameFile = async (path, newPath) => {
const row = await findAndTraverseToPath(path);
clickFileRowAction(row, 'Rename/Move');
await fillFileNameModal(newPath, 'Rename file');
};
export const closeFile = async path => {
const button = await screen.getByLabelText(`Close ${path}`, {
selector: '.multi-file-tabs button',
});
button.click();
};
2021-01-03 14:25:43 +05:30
export const commit = async () => {
2021-02-22 17:27:13 +05:30
switchLeftSidebarTab('Commit');
2021-01-03 14:25:43 +05:30
screen.getByTestId('begin-commit-button').click();
await screen.findByLabelText(/Commit to .+ branch/).then(x => x.click());
screen.getByText('Commit').click();
};