This commit is contained in:
Victor Marin
2025-11-05 15:45:08 +02:00
parent da84148dc5
commit f116bb783f
@@ -48,166 +48,167 @@ describe('PanelGroupByAction', () => {
deactivate();
});
describe('GroupBy variable integration', () => {
it('should access variable through scene graph', () => {
const { action, variable } = buildTestScene();
describe('GroupBy variable integration', () => {
it('should access variable through scene graph', () => {
const { action, variable } = buildTestScene();
jest.spyOn(variable, 'validateAndUpdate').mockReturnValue(
of({
origin: variable,
state: variable.state,
})
);
jest.spyOn(variable, 'validateAndUpdate').mockReturnValue(
of({
origin: variable,
state: variable.state,
})
);
const deactivate = action.activate();
const deactivate = action.activate();
const foundVariable = action.getGroupByVariable();
expect(foundVariable).toBe(variable);
expect(foundVariable?.state.type).toBe('groupby');
const foundVariable = action.getGroupByVariable();
expect(foundVariable).toBe(variable);
expect(foundVariable?.state.type).toBe('groupby');
deactivate();
});
it('should handle missing GroupByVariable gracefully', () => {
const action = new PanelGroupByAction();
const panel = new VizPanel({
title: 'Panel A',
pluginId: 'table',
key: 'panel-1',
headerActions: [action],
$data: new SceneQueryRunner({
datasource: { uid: 'my-uid' },
queries: [{ query: 'QueryA', refId: 'A' }],
}),
deactivate();
});
new DashboardScene({
uid: 'dash-1',
$variables: new SceneVariableSet({
variables: [], // No GroupByVariable
}),
body: DefaultGridLayoutManager.fromVizPanels([panel]),
it('should handle missing GroupByVariable gracefully', () => {
const action = new PanelGroupByAction();
const panel = new VizPanel({
title: 'Panel A',
pluginId: 'table',
key: 'panel-1',
headerActions: [action],
$data: new SceneQueryRunner({
datasource: { uid: 'my-uid' },
queries: [{ query: 'QueryA', refId: 'A' }],
}),
});
new DashboardScene({
uid: 'dash-1',
$variables: new SceneVariableSet({
variables: [], // No GroupByVariable
}),
body: DefaultGridLayoutManager.fromVizPanels([panel]),
});
const deactivate = action.activate();
expect(action.getGroupByVariable()).toBeUndefined();
deactivate();
});
});
describe('Panel parent requirement', () => {
it('should throw error if parent is not VizPanel', () => {
const action = new PanelGroupByAction();
expect(() => {
action.activate();
}).toThrow('PanelGroupByAction can be used only for VizPanel');
});
});
describe('getGroupByOptions', () => {
it('should return empty array when variable has no options', async () => {
const { action, variable } = buildTestScene();
jest.spyOn(variable, 'validateAndUpdate').mockReturnValue(
of({
origin: variable,
state: {
...variable.state,
options: [],
},
})
);
const deactivate = action.activate();
const options = await action.getGroupByOptions();
expect(options).toEqual([]);
deactivate();
});
const deactivate = action.activate();
it('should filter options based on applicability', async () => {
const { action, variable, panel } = buildTestScene();
expect(action.getGroupByVariable()).toBeUndefined();
variable.setState({
options: [
{ label: 'field1', value: 'field1' },
{ label: 'field2', value: 'field2' },
{ label: 'field3', value: 'field3' },
],
});
deactivate();
});
});
describe('Panel parent requirement', () => {
it('should throw error if parent is not VizPanel', () => {
const action = new PanelGroupByAction();
expect(() => {
action.activate();
}).toThrow('PanelGroupByAction can be used only for VizPanel');
});
});
describe('getGroupByOptions', () => {
it('should return empty array when variable has no options', async () => {
const { action, variable } = buildTestScene();
jest.spyOn(variable, 'validateAndUpdate').mockReturnValue(
of({
origin: variable,
state: {
...variable.state,
options: [],
const queryRunner = panel.state.$data as SceneQueryRunner;
queryRunner.setState({
data: {
state: 'Done',
series: [],
timeRange: {} as any,
request: {
targets: [{ refId: 'A', datasource: { uid: 'test-uid', type: 'prometheus' } }],
},
},
})
);
});
const deactivate = action.activate();
jest.spyOn(variable, 'validateAndUpdate').mockReturnValue(
of({
origin: variable,
state: variable.state,
})
);
const options = await action.getGroupByOptions();
expect(options).toEqual([]);
jest.spyOn(variable, 'getGroupByApplicabilityForQueries').mockResolvedValue([
{ key: 'field1', applicable: true },
{ key: 'field2', applicable: false },
{ key: 'field3', applicable: true },
]);
deactivate();
});
const deactivate = action.activate();
it('should filter options based on applicability', async () => {
const { action, variable, panel } = buildTestScene();
variable.setState({
options: [
const options = await action.getGroupByOptions();
expect(options).toHaveLength(2);
expect(options).toEqual([
{ label: 'field1', value: 'field1' },
{ label: 'field2', value: 'field2' },
{ label: 'field3', value: 'field3' },
],
]);
deactivate();
});
const queryRunner = panel.state.$data as SceneQueryRunner;
queryRunner.setState({
data: {
state: 'Done',
series: [],
timeRange: {} as any,
request: {
targets: [{ refId: 'A', datasource: { uid: 'test-uid', type: 'prometheus' } }],
},
},
});
jest.spyOn(variable, 'validateAndUpdate').mockReturnValue(
of({
origin: variable,
state: variable.state,
})
);
jest.spyOn(variable, 'getGroupByApplicabilityForQueries').mockResolvedValue([
{ key: 'field1', applicable: true },
{ key: 'field2', applicable: false },
{ key: 'field3', applicable: true },
]);
const deactivate = action.activate();
const options = await action.getGroupByOptions();
expect(options).toHaveLength(2);
expect(options).toEqual([
{ label: 'field1', value: 'field1' },
{ label: 'field3', value: 'field3' },
]);
deactivate();
});
});
function buildTestScene() {
const variable = new GroupByVariable({
name: 'A',
label: 'A',
description: 'A',
type: 'groupby',
value: 'Text',
});
const action = new PanelGroupByAction();
const panel = new VizPanel({
title: 'Panel A',
pluginId: 'table',
key: 'panel-12',
headerActions: [action],
$data: new SceneQueryRunner({
datasource: { uid: 'my-uid' },
queries: [{ query: 'QueryA', refId: 'A' }],
}),
});
const dashboard = new DashboardScene({
uid: 'A',
$variables: new SceneVariableSet({
variables: [variable],
}),
body: DefaultGridLayoutManager.fromVizPanels([panel]),
});
return { action, panel, dashboard, variable };
}
});
function buildTestScene() {
const variable = new GroupByVariable({
name: 'A',
label: 'A',
description: 'A',
type: 'groupby',
value: 'Text',
});
const action = new PanelGroupByAction();
const panel = new VizPanel({
title: 'Panel A',
pluginId: 'table',
key: 'panel-12',
headerActions: [action],
$data: new SceneQueryRunner({
datasource: { uid: 'my-uid' },
queries: [{ query: 'QueryA', refId: 'A' }],
}),
});
const dashboard = new DashboardScene({
uid: 'A',
$variables: new SceneVariableSet({
variables: [variable],
}),
body: DefaultGridLayoutManager.fromVizPanels([panel]),
});
return { action, panel, dashboard, variable };
}