Skip to content

Commit

Permalink
SceneQueryRunner: Add tests for scopes (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
tskarhed authored Feb 26, 2025
1 parent 6fbea37 commit ea428ac
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
71 changes: 70 additions & 1 deletion packages/scenes/src/querying/SceneQueryRunner.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { map, Observable, of } from 'rxjs';
import { map, Observable, of, BehaviorSubject } from 'rxjs';

import {
DataQueryRequest,
Expand Down Expand Up @@ -39,6 +39,8 @@ import { ExtraQueryDescriptor, ExtraQueryProvider } from './ExtraQueryProvider';
import { SafeSerializableSceneObject } from '../utils/SafeSerializableSceneObject';
import { SceneQueryStateControllerState } from '../behaviors/types';
import { config } from '@grafana/runtime';
import { SceneScopesBridge } from '../core/SceneScopesBridge';
import { sceneGraph } from '../core/sceneGraph';

const getDataSourceMock = jest.fn().mockReturnValue({
uid: 'test-uid',
Expand Down Expand Up @@ -2567,6 +2569,73 @@ describe.each(['11.1.2', '11.1.1'])('SceneQueryRunner', (v) => {
expect(clone['_results']['_buffer']).not.toEqual([]);
});
});
describe('scopes', () => {
let getScopesBridgeSpy = jest.spyOn(sceneGraph, 'getScopesBridge');

afterEach(() => {
getScopesBridgeSpy.mockReturnValue(undefined);
});

it('should run queries with scopes when scopesBridge is provided', async () => {
const queryRunner = new SceneQueryRunner({
queries: [{ refId: 'A' }],
$timeRange: new SceneTimeRange(),
});

const scopes = new SceneScopesBridge({});
const mockState = {
value: [
{
metadata: { name: 'Scope 1' },
spec: {
title: 'Scope 1',
type: 'test',
description: 'Test scope',
category: 'test',
filters: [],
},
},
],
drawerOpened: false,
enabled: true,
loading: false,
readOnly: false,
};

scopes.updateContext({
state: mockState,
stateObservable: new BehaviorSubject(mockState),
changeScopes: () => {},
setReadOnly: () => {},
setEnabled: () => {},
});

getScopesBridgeSpy.mockReturnValue(scopes);

queryRunner.activate();
await new Promise((r) => setTimeout(r, 1));

expect(sentRequest?.scopes).toBeDefined();
expect(sentRequest?.scopes?.[0]).toMatchObject({
metadata: { name: 'Scope 1' },
spec: {
title: 'Scope 1',
type: 'test',
},
});
});
});
it('should not run queries with scopes when scopesBridge is not provided and the feature is disabled', async () => {
const queryRunner = new SceneQueryRunner({
queries: [{ refId: 'A' }],
$timeRange: new SceneTimeRange(),
});

queryRunner.activate();
await new Promise((r) => setTimeout(r, 1));

expect(sentRequest?.scopes).toBeUndefined();
});
});

class CustomDataSource extends RuntimeDataSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ exports[`SceneQueryRunner when running query should build DataQueryRequest objec
"from": "now-6h",
"to": "now",
},
"requestId": "SQR178",
"requestId": "SQR180",
"scopes": undefined,
"startTime": 1689063488000,
"targets": [
Expand Down

0 comments on commit ea428ac

Please sign in to comment.