import {createRef, createSignal, range} from '@revideo/core'; import {describe, expect, it} from 'vitest'; import {useScene2D} from '../../scenes'; import {Node} from '../Node'; import {mockScene2D} from './mockScene2D'; describe('children', () => { mockScene2D(); it('Append children', () => { const view = useScene2D().getView(); const parent = createRef(); const childA = createRef(); const childB = createRef(); view.add( , ); expect(childA().parent()).toBe(parent()); expect(childB().parent()).toBe(parent()); expect(parent().children()).toEqual([childA(), childB()]); }); it('Clear children', () => { const view = useScene2D().getView(); const parent = createRef(); const childA = createRef(); const childB = createRef(); view.add( , ); parent().removeChildren(); expect(childA().parent()).toBe(null); expect(childB().parent()).toBe(null); expect(parent().children()).toEqual([]); }); it('Replace children', () => { const view = useScene2D().getView(); const parent = createRef(); const childA = ; const childB = ; view.add({childA}); parent().children(childB); expect(childA.parent()).toBe(null); expect(childB.parent()).toBe(parent()); expect(parent().children()).toEqual([childB]); }); it('Take a child from another node', () => { const view = useScene2D().getView(); const parentA = createRef(); const parentB = createRef(); const child = createRef(); view.add( <> , ); expect(parentA().children()).toEqual([child()]); expect(parentB().children()).toEqual([]); expect(child().parent()).toBe(parentA()); parentB().children(child()); expect(parentA().children()).toEqual([]); expect(parentB().children()).toEqual([child()]); expect(child().parent()).toBe(parentB()); }); it('Append children conditionally', () => { const view = useScene2D().getView(); const parent = createRef(); const childA = ; const childB = ; const predicate = createSignal(false); view.add({() => (predicate() ? childA : childB)}); expect(parent().children()).toEqual([childB]); expect(childA.parent()).toBe(null); expect(childB.parent()).toBe(parent()); predicate(true); expect(parent().children()).toEqual([childA]); expect(childA.parent()).toBe(parent()); expect(childB.parent()).toBe(null); }); it('Spawn reactive children', () => { const view = useScene2D().getView(); const parent = createRef(); const count = createSignal(3); view.add( {() => range(count()).map(() => )}, ); expect(parent().children().length).toBe(3); count(5); expect(parent().children().length).toBe(5); }); it('Replace a spawner', () => { const view = useScene2D().getView(); const parent = createRef(); const childA = ; const childB = ; view.add({() => childA}); expect(parent().children()).toEqual([childA]); expect(childA.parent()).toBe(parent()); parent().children(childB); expect(childA.parent()).toBe(null); expect(childB.parent()).toBe(parent()); expect(parent().children()).toEqual([childB]); }); });