-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathChartEditorComponent.ts
More file actions
297 lines (258 loc) · 8.37 KB
/
ChartEditorComponent.ts
File metadata and controls
297 lines (258 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* ChartEditorComponent - Reusable component for chart/tile editor
* Used for creating and configuring dashboard tiles and chart explorer
*/
import { DisplayType } from '@hyperdx/common-utils/dist/types';
import { Locator, Page } from '@playwright/test';
import { getSqlEditor } from '../utils/locators';
import { WebhookAlertModalComponent } from './WebhookAlertModalComponent';
export class ChartEditorComponent {
readonly page: Page;
readonly addNewWebhookButton: Locator;
readonly webhookAlertModal: WebhookAlertModalComponent;
private readonly chartNameInput: Locator;
private readonly chartTypeInput: Locator;
private readonly sourceSelector: Locator;
private readonly metricSelector: Locator;
private readonly aggFnSelect: Locator;
private readonly addOrRemoveAlertButton: Locator;
private readonly webhookSelector: Locator;
private readonly runQueryButton: Locator;
private readonly saveButton: Locator;
constructor(page: Page) {
this.page = page;
this.chartNameInput = page.getByTestId('chart-name-input');
this.chartTypeInput = page.getByTestId('chart-type-input');
this.sourceSelector = page.getByTestId('source-selector');
this.metricSelector = page.getByTestId('metric-name-selector');
this.aggFnSelect = page.getByTestId('agg-fn-select');
this.addOrRemoveAlertButton = page.getByTestId('alert-button');
this.webhookSelector = page.getByTestId('select-webhook');
this.addNewWebhookButton = page.getByTestId('add-new-webhook-button');
this.webhookAlertModal = new WebhookAlertModalComponent(page);
this.runQueryButton = page.getByTestId('chart-run-query-button');
this.saveButton = page.getByTestId('chart-save-button');
}
/**
* Set chart name
*/
async setChartName(name: string) {
await this.chartNameInput.fill(name);
}
/**
* Set chart type using data-testid
*/
async setChartType(type: DisplayType) {
await this.page.getByTestId(`chart-type-${type}`).click();
}
/**
* Set group by expression
*/
async setGroupBy(expression: string) {
const groupByInput = getSqlEditor(this.page, 'SQL Columns');
await groupByInput.click();
await this.page.keyboard.type(expression);
}
/**
* Select a data source
*/
async selectSource(sourceName: string) {
await this.sourceSelector.click();
// Use getByRole for more reliable selection
const sourceOption = this.page.getByRole('option', { name: sourceName });
if ((await sourceOption.getAttribute('data-combobox-active')) != 'true') {
await sourceOption.click({ timeout: 5000 });
}
}
/**
* Select a metric by name
*/
async selectMetric(metricName: string, metricValue?: string) {
// Wait for metric selector to be visible
await this.metricSelector.waitFor({ state: 'visible', timeout: 5000 });
// Click to open dropdown
await this.metricSelector.click();
// Type to filter
await this.metricSelector.fill(metricName);
// If a specific metric value is provided, wait for and click it
if (metricValue) {
// Use attribute selector for combobox options
const targetMetricOption = this.page.locator(
`[data-combobox-option="true"][value="${metricValue}"]`,
);
await targetMetricOption.waitFor({ state: 'visible', timeout: 5000 });
await targetMetricOption.click({ timeout: 5000 });
} else {
// Otherwise just press Enter to select the first match
await this.page.keyboard.press('Enter');
}
}
/**
* Select an aggregation function from the dropdown
*/
async selectAggFn(label: string) {
await this.aggFnSelect.click();
await this.page.getByRole('option', { name: label }).click();
}
/**
* Get the currently selected aggregation function value
*/
async getSelectedAggFn(): Promise<string | null> {
return this.aggFnSelect.inputValue();
}
/**
* Check if an aggregation function option is available in the dropdown
*/
async isAggFnOptionAvailable(label: string): Promise<boolean> {
await this.aggFnSelect.click();
const option = this.page.getByRole('option', { name: label });
const visible = await option.isVisible().catch(() => false);
// Close the dropdown
await this.page.keyboard.press('Escape');
return visible;
}
async clickAddAlert() {
await this.addOrRemoveAlertButton.click();
this.addNewWebhookButton.waitFor({
state: 'visible',
timeout: 2000,
});
}
async clickRemoveAlert() {
await this.addOrRemoveAlertButton.click();
this.addNewWebhookButton.waitFor({
state: 'hidden',
timeout: 2000,
});
}
async selectWebhook(webhookName: string) {
// Click to open dropdown
await this.webhookSelector.click();
// Type to filter
await this.webhookSelector.fill(webhookName);
// Use getByRole for more reliable selection
const sourceOption = this.page.getByRole('option', { name: webhookName });
if ((await sourceOption.getAttribute('data-combobox-active')) != 'true') {
await sourceOption.click({ timeout: 5000 });
}
}
/**
* Run the query and wait for it to complete
*/
async runQuery(waitForRecharts: boolean = true) {
await this.runQueryButton.click();
if (waitForRecharts) {
// need to wait for the recharts graph to render
await this.page
.locator('.recharts-responsive-container')
.first()
.waitFor({ state: 'visible', timeout: 10000 });
}
}
/**
* Switch the chart editor from Builder to SQL mode.
*/
async switchToSqlMode() {
const sqlLabel = this.page.locator(
'.mantine-SegmentedControl-label:has-text("SQL")',
);
await sqlLabel.waitFor({ state: 'visible', timeout: 5000 });
await sqlLabel.click();
}
/**
* Type a SQL query into the CodeMirror SQL editor.
* Call switchToSqlMode() first to make the SQL editor visible.
*/
async typeSqlQuery(sql: string) {
// Target the cm-content (editable area) inside the SQL template editor.
// Use first() because the "Generated SQL" accordion may add another
// cm-editor further down the DOM.
const sqlContent = this.page.locator('.cm-editor .cm-content').first();
await sqlContent.click();
await this.page.keyboard.type(sql);
}
/**
* Save the chart/tile and wait for modal to close
*/
async save() {
await this.saveButton.click();
// Wait for save button to disappear (modal closes)
await this.saveButton.waitFor({ state: 'hidden', timeout: 2000 });
}
/**
* Wait for chart editor data to load (sources, metrics, etc.)
*/
async waitForDataToLoad() {
await this.runQueryButton.waitFor({ state: 'visible', timeout: 2000 });
await this.page.waitForLoadState('networkidle');
}
/**
* Complete workflow: create a basic chart with name and save
*/
async createBasicChart(name: string) {
// Wait for data sources to load before interacting
await this.waitForDataToLoad();
await this.setChartName(name);
await this.runQuery();
await this.save();
}
/**
* Complete workflow: create a chart with specific source and metric
*/
async createChartWithMetric(
chartName: string,
sourceName: string,
metricName: string,
metricValue?: string,
) {
// Wait for data sources to load before interacting
await this.waitForDataToLoad();
await this.selectSource(sourceName);
await this.selectMetric(metricName, metricValue);
await this.runQuery();
await this.save();
}
/**
* Complete workflow: create a chart with specific source and metric
*/
async createTable({
chartName,
sourceName,
groupBy,
}: {
chartName: string;
sourceName: string;
groupBy?: string;
}) {
// Wait for data sources to load before interacting
await this.waitForDataToLoad();
const tableButton = this.page.getByRole('tab', { name: 'Table' });
await tableButton.click();
await this.setChartName(chartName);
await this.selectSource(sourceName);
if (groupBy) await this.setGroupBy(groupBy);
await this.save();
}
// Getters for assertions
get nameInput() {
return this.chartNameInput;
}
get source() {
return this.sourceSelector;
}
get metric() {
return this.metricSelector;
}
get aggFn() {
return this.aggFnSelect;
}
get alertButton() {
return this.addOrRemoveAlertButton;
}
get runButton() {
return this.runQueryButton;
}
get saveBtn() {
return this.saveButton;
}
}