grafana/public/app/features/scenes/core/SceneObjectBase.test.ts
Torkel Ödegaard 849134b5dd
Scene: Small refactorings and name changes (#51866)
* Rename onMount and onUnmount and some other small refactorings

* More refactorings fixing typescript issues
2022-07-07 16:49:05 +02:00

69 lines
1.7 KiB
TypeScript

import { SceneObjectBase } from './SceneObjectBase';
import { SceneObject, SceneObjectList, SceneObjectState } from './types';
interface TestSceneState extends SceneObjectState {
name?: string;
nested?: SceneObject<TestSceneState>;
children?: SceneObjectList;
actions?: SceneObjectList;
}
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');
});
});