11import { cloneDeep } from 'lodash' ;
22
33import { config } from '@grafana/runtime' ;
4- import { behaviors , sceneGraph , SceneQueryRunner } from '@grafana/scenes' ;
5- import { DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0/dashboard.gen' ;
4+ import {
5+ behaviors ,
6+ ConstantVariable ,
7+ CustomVariable ,
8+ DataSourceVariable ,
9+ IntervalVariable ,
10+ QueryVariable ,
11+ TextBoxVariable ,
12+ sceneGraph ,
13+ GroupByVariable ,
14+ AdHocFiltersVariable ,
15+ } from '@grafana/scenes' ;
16+ import {
17+ AdhocVariableKind ,
18+ ConstantVariableKind ,
19+ CustomVariableKind ,
20+ DashboardV2Spec ,
21+ DatasourceVariableKind ,
22+ GroupByVariableKind ,
23+ IntervalVariableKind ,
24+ QueryVariableKind ,
25+ TextVariableKind ,
26+ } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0/dashboard.gen' ;
627import { handyTestingSchema } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0/examples' ;
728import { DashboardWithAccessInfo } from 'app/features/dashboard/api/dashboard_api' ;
829import { MIXED_DATASOURCE_NAME } from 'app/plugins/datasource/mixed/MixedDataSource' ;
930
31+ import { DashboardDataLayerSet } from '../scene/DashboardDataLayerSet' ;
1032import { DashboardLayoutManager } from '../scene/types' ;
1133import { dashboardSceneGraph } from '../utils/dashboardSceneGraph' ;
1234import { getQueryRunnerFor } from '../utils/utils' ;
35+ import { validateVariable , validateVizPanel } from '../v2schema/test-helpers' ;
1336
1437import { transformSaveModelSchemaV2ToScene } from './transformSaveModelSchemaV2ToScene' ;
1538import { transformCursorSynctoEnum } from './transformToV2TypesUtils' ;
@@ -85,14 +108,96 @@ describe('transformSaveModelSchemaV2ToScene', () => {
85108 expect ( dashboardControls . state . refreshPicker . state . intervals ) . toEqual ( time . autoRefreshIntervals ) ;
86109 expect ( dashboardControls . state . hideTimeControls ) . toBe ( time . hideTimepicker ) ;
87110
88- // TODO: Variables
89- // expect(scene.state?.$variables?.state.variables).toHaveLength(dash.variables.length);
90- // expect(scene.state?.$variables?.getByName(dash.variables[0].spec.name)).toBeInstanceOf(QueryVariable);
91- // expect(scene.state?.$variables?.getByName(dash.variables[1].spec.name)).toBeInstanceOf(TextBoxVariable); ...
111+ // Variables
112+ const variables = scene . state ?. $variables ;
113+ expect ( variables ?. state . variables ) . toHaveLength ( dash . variables . length ) ;
114+ validateVariable ( {
115+ sceneVariable : variables ?. state . variables [ 0 ] ,
116+ variableKind : dash . variables [ 0 ] as QueryVariableKind ,
117+ scene : scene ,
118+ dashSpec : dash ,
119+ sceneVariableClass : QueryVariable ,
120+ index : 0 ,
121+ } ) ;
122+ validateVariable ( {
123+ sceneVariable : variables ?. state . variables [ 1 ] ,
124+ variableKind : dash . variables [ 1 ] as CustomVariableKind ,
125+ scene : scene ,
126+ dashSpec : dash ,
127+ sceneVariableClass : CustomVariable ,
128+ index : 1 ,
129+ } ) ;
130+ validateVariable ( {
131+ sceneVariable : variables ?. state . variables [ 2 ] ,
132+ variableKind : dash . variables [ 2 ] as DatasourceVariableKind ,
133+ scene : scene ,
134+ dashSpec : dash ,
135+ sceneVariableClass : DataSourceVariable ,
136+ index : 2 ,
137+ } ) ;
138+ validateVariable ( {
139+ sceneVariable : variables ?. state . variables [ 3 ] ,
140+ variableKind : dash . variables [ 3 ] as ConstantVariableKind ,
141+ scene : scene ,
142+ dashSpec : dash ,
143+ sceneVariableClass : ConstantVariable ,
144+ index : 3 ,
145+ } ) ;
146+ validateVariable ( {
147+ sceneVariable : variables ?. state . variables [ 4 ] ,
148+ variableKind : dash . variables [ 4 ] as IntervalVariableKind ,
149+ scene : scene ,
150+ dashSpec : dash ,
151+ sceneVariableClass : IntervalVariable ,
152+ index : 4 ,
153+ } ) ;
154+ validateVariable ( {
155+ sceneVariable : variables ?. state . variables [ 5 ] ,
156+ variableKind : dash . variables [ 5 ] as TextVariableKind ,
157+ scene : scene ,
158+ dashSpec : dash ,
159+ sceneVariableClass : TextBoxVariable ,
160+ index : 5 ,
161+ } ) ;
162+ validateVariable ( {
163+ sceneVariable : variables ?. state . variables [ 6 ] ,
164+ variableKind : dash . variables [ 6 ] as GroupByVariableKind ,
165+ scene : scene ,
166+ dashSpec : dash ,
167+ sceneVariableClass : GroupByVariable ,
168+ index : 6 ,
169+ } ) ;
170+ validateVariable ( {
171+ sceneVariable : variables ?. state . variables [ 7 ] ,
172+ variableKind : dash . variables [ 7 ] as AdhocVariableKind ,
173+ scene : scene ,
174+ dashSpec : dash ,
175+ sceneVariableClass : AdHocFiltersVariable ,
176+ index : 7 ,
177+ } ) ;
92178
93- // TODO: Annotations
94- // expect(scene.state.annotations).toHaveLength(dash.annotations.length);
95- // expect(scene.state.annotations[0].text).toBe(dash.annotations[0].text); ...
179+ // Annotations
180+ expect ( scene . state . $data ) . toBeInstanceOf ( DashboardDataLayerSet ) ;
181+ const dataLayers = scene . state . $data as DashboardDataLayerSet ;
182+ expect ( dataLayers . state . annotationLayers ) . toHaveLength ( dash . annotations . length ) ;
183+ expect ( dataLayers . state . annotationLayers [ 0 ] . state . name ) . toBe ( dash . annotations [ 0 ] . spec . name ) ;
184+ expect ( dataLayers . state . annotationLayers [ 0 ] . state . isEnabled ) . toBe ( dash . annotations [ 0 ] . spec . enable ) ;
185+ expect ( dataLayers . state . annotationLayers [ 0 ] . state . isHidden ) . toBe ( dash . annotations [ 0 ] . spec . hide ) ;
186+
187+ // Enabled
188+ expect ( dataLayers . state . annotationLayers [ 1 ] . state . name ) . toBe ( dash . annotations [ 1 ] . spec . name ) ;
189+ expect ( dataLayers . state . annotationLayers [ 1 ] . state . isEnabled ) . toBe ( dash . annotations [ 1 ] . spec . enable ) ;
190+ expect ( dataLayers . state . annotationLayers [ 1 ] . state . isHidden ) . toBe ( dash . annotations [ 1 ] . spec . hide ) ;
191+
192+ // Disabled
193+ expect ( dataLayers . state . annotationLayers [ 2 ] . state . name ) . toBe ( dash . annotations [ 2 ] . spec . name ) ;
194+ expect ( dataLayers . state . annotationLayers [ 2 ] . state . isEnabled ) . toBe ( dash . annotations [ 2 ] . spec . enable ) ;
195+ expect ( dataLayers . state . annotationLayers [ 2 ] . state . isHidden ) . toBe ( dash . annotations [ 2 ] . spec . hide ) ;
196+
197+ // Hidden
198+ expect ( dataLayers . state . annotationLayers [ 3 ] . state . name ) . toBe ( dash . annotations [ 3 ] . spec . name ) ;
199+ expect ( dataLayers . state . annotationLayers [ 3 ] . state . isEnabled ) . toBe ( dash . annotations [ 3 ] . spec . enable ) ;
200+ expect ( dataLayers . state . annotationLayers [ 3 ] . state . isHidden ) . toBe ( dash . annotations [ 3 ] . spec . hide ) ;
96201
97202 // To be implemented
98203 // expect(timePicker.state.ranges).toEqual(dash.timeSettings.quickRanges);
@@ -101,31 +206,7 @@ describe('transformSaveModelSchemaV2ToScene', () => {
101206 const vizPanels = ( scene . state . body as DashboardLayoutManager ) . getVizPanels ( ) ;
102207 expect ( vizPanels ) . toHaveLength ( 1 ) ;
103208 const vizPanel = vizPanels [ 0 ] ;
104- expect ( vizPanel . state . title ) . toBe ( dash . elements [ 'test-panel-uid' ] . spec . title ) ;
105- expect ( vizPanel . state . description ) . toBe ( dash . elements [ 'test-panel-uid' ] . spec . description ) ;
106- expect ( vizPanel . state . pluginId ) . toBe ( dash . elements [ 'test-panel-uid' ] . spec . vizConfig . kind ) ;
107- expect ( vizPanel . state . pluginVersion ) . toBe ( dash . elements [ 'test-panel-uid' ] . spec . vizConfig . spec . pluginVersion ) ;
108- expect ( vizPanel . state . options ) . toEqual ( dash . elements [ 'test-panel-uid' ] . spec . vizConfig . spec . options ) ;
109- expect ( vizPanel . state . fieldConfig ) . toEqual ( dash . elements [ 'test-panel-uid' ] . spec . vizConfig . spec . fieldConfig ) ;
110-
111- // FIXME: There is an error of data being undefined
112- // expect(vizPanel.state.$data).toBeInstanceOf(SceneDataTransformer);
113- // const dataTransformer = vizPanel.state.$data as SceneDataTransformer;
114- // expect(dataTransformer.state.transformations).toEqual([{ id: 'transform1', options: {} }]);
115-
116- // expect(dataTransformer.state.$data).toBeInstanceOf(SceneQueryRunner);
117- const queryRunner = getQueryRunnerFor ( vizPanel ) ;
118- expect ( queryRunner ) . toBeInstanceOf ( SceneQueryRunner ) ;
119- expect ( queryRunner ?. state . datasource ) . toBeUndefined ( ) ;
120- // expect(queryRunner.state.queries).toEqual([{ query: 'test-query', datasource: { uid: 'datasource1', type: 'prometheus' } }]);
121- // expect(queryRunner.state.maxDataPoints).toBe(100);
122- // expect(queryRunner.state.cacheTimeout).toBe('1m');
123- // expect(queryRunner.state.queryCachingTTL).toBe(60);
124- // expect(queryRunner.state.minInterval).toBe('1m');
125- // expect(queryRunner.state.dataLayerFilter?.panelId).toBe(1);
126-
127- // FIXME: Fix the key incompatibility since panel is not numeric anymore
128- // expect(vizPanel.state.key).toBe(dash.elements['test-panel-uid'].spec.uid);
209+ validateVizPanel ( vizPanel , dash ) ;
129210
130211 // FIXME: Tests for layout
131212 } ) ;
0 commit comments