grafana/public/app/features/scenes/variables/SceneVariableSet.test.ts
Torkel Ödegaard 8d92417a16
Scenes: Improve typing of scene state to avoid type guards and casting (#52422)
* Trying to get rid of type guard but failing

* Improve typing of scene object state

* Fixed wrongly renamed event

* Tweaks
2022-07-19 17:46:49 +02:00

44 lines
1.4 KiB
TypeScript

import { SceneObjectBase } from '../core/SceneObjectBase';
import { SceneObjectStatePlain } from '../core/types';
import { sceneTemplateInterpolator, SceneVariableManager, TextBoxSceneVariable } from './SceneVariableSet';
interface TestSceneState extends SceneObjectStatePlain {
nested?: TestScene;
}
class TestScene extends SceneObjectBase<TestSceneState> {}
describe('SceneObject with variables', () => {
it('Should be interpolate and use closest variable', () => {
const scene = new TestScene({
$variables: new SceneVariableManager({
variables: [
new TextBoxSceneVariable({
name: 'test',
current: { value: 'hello' },
}),
new TextBoxSceneVariable({
name: 'atRootOnly',
current: { value: 'RootValue' },
}),
],
}),
nested: new TestScene({
$variables: new SceneVariableManager({
variables: [
new TextBoxSceneVariable({
name: 'test',
current: { value: 'nestedValue' },
}),
],
}),
}),
});
expect(sceneTemplateInterpolator('${test}', scene)).toBe('hello');
expect(sceneTemplateInterpolator('${test}', scene.state.nested!)).toBe('nestedValue');
expect(sceneTemplateInterpolator('${atRootOnly}', scene.state.nested!)).toBe('RootValue');
});
});