Files
actual/packages/desktop-client/e2e/page-models/rules-page.js
2023-02-26 15:25:53 +00:00

79 lines
1.8 KiB
JavaScript

export class RulesPage {
constructor(page) {
this.page = page;
}
/**
* Create a new rule
*/
async createRule(data) {
await this.page
.getByRole('button', {
name: 'Create new rule',
})
.click();
await this._fillRuleFields(data);
await this.page.getByRole('button', { name: 'Save' }).click();
}
/**
* Retrieve the data for the nth-rule.
* 0-based index
*/
async getNthRule(index) {
const row = this.page.getByTestId('table').getByTestId('row').nth(index);
return {
conditions: await row.getByTestId('conditions').textContent(),
actions: await row.getByTestId('actions').textContent(),
};
}
async _fillRuleFields(data) {
if (data.conditions) {
await this._fillEditorFields(
data.conditions,
this.page.getByTestId('condition-list'),
);
}
if (data.actions) {
await this._fillEditorFields(
data.actions,
this.page.getByTestId('action-list'),
);
}
}
async _fillEditorFields(data, rootElement) {
for (const idx in data) {
const { field, op, value } = data[idx];
const row = rootElement.getByTestId('editor-row').nth(idx);
if (!(await row.isVisible())) {
await rootElement.getByRole('button', { name: 'Add entry' }).click();
}
if (field) {
await row.getByRole('button').first().click();
await this.page
.getByRole('option', { exact: true, name: field })
.click();
}
if (op) {
await row.getByRole('button', { name: 'is' }).click();
await this.page.getByRole('option', { name: op }).click();
}
if (value) {
await row.getByRole('combobox').fill(value);
await this.page.keyboard.press('Enter');
}
}
}
}