grafana/public/app/features/scenes/core/SceneObjectBase.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

69 lines
1.7 KiB
TypeScript

import { SceneObjectBase } from './SceneObjectBase';
import { SceneLayoutChild, SceneObject, SceneObjectStatePlain } from './types';
interface TestSceneState extends SceneObjectStatePlain {
name?: string;
nested?: SceneObject<TestSceneState>;
children?: SceneLayoutChild[];
actions?: SceneObject[];
}
class TestScene extends SceneObjectBase<TestSceneState> {}
describe('SceneObject', () => {
it('Can clone', () => {
const scene = new TestScene({
nested: new TestScene({
name: 'nested',
}),
children: [
new TestScene({
name: 'layout child',
}),
],
});
scene.state.nested?.activate();
const clone = scene.clone();
expect(clone).not.toBe(scene);
expect(clone.state.nested).not.toBe(scene.state.nested);
expect(clone.state.nested?.isActive).toBe(undefined);
expect(clone.state.children![0]).not.toBe(scene.state.children![0]);
});
it('SceneObject should have parent when added to container', () => {
const scene = new TestScene({
nested: new TestScene({
name: 'nested',
}),
children: [
new TestScene({
name: 'layout child',
}),
],
actions: [
new TestScene({
name: 'layout child',
}),
],
});
expect(scene.parent).toBe(undefined);
expect(scene.state.nested?.parent).toBe(scene);
expect(scene.state.children![0].parent).toBe(scene);
expect(scene.state.actions![0].parent).toBe(scene);
});
it('Can clone with state change', () => {
const scene = new TestScene({
nested: new TestScene({
name: 'nested',
}),
});
const clone = scene.clone({ name: 'new name' });
expect(clone.state.name).toBe('new name');
});
});