import { Parser } from '@rekajs/parser'; import * as t from '@rekajs/types'; import { invariant } from '@rekajs/utils'; import { Frame, FrameOpts } from '../frame'; import { RekaOpts } from '../interfaces'; import { Reka } from '../reka'; const createFrame = ( program: string, opts?: Partial<{ state: RekaOpts; frame: FrameOpts }> ) => { const reka = Reka.create(opts?.state); reka.load( t.state({ program: Parser.parseProgram(program), extensions: {}, }) ); return reka.createFrame({ id: 'main-frame', ...(opts?.frame ?? {}), component: { name: 'App', props: {}, ...(opts?.frame?.component ?? {}), }, }); }; describe('evaluator', () => { describe('view', () => { it('should be able to compute a view', async () => { const frame = await createFrame(` component App() { val counter = 0; } => (
) `); invariant(frame.view?.children[0] instanceof t.RekaComponentView); expect(frame.view.children[0].render.length).toEqual(1); invariant(frame.view.children[0].render[0] instanceof t.TagView); expect(frame.view.children[0].render[0].tag).toEqual('div'); invariant( frame.view.children[0].render[0].children[0] instanceof t.TagView ); expect(frame.view.children[0].render[0].children[0]).toMatchObject({ type: 'TagView', tag: 'text', props: { value: 0 }, }); }); it('should be able to compute multiple views from @each directive', async () => { const frame = await createFrame(` component App() { val items = [1,2,3]; } => ( ) `); expect( t.assert(frame.view?.children[0], t.RekaComponentView, (view) => { return t.assert(view.render[0], t.TagView, (view) => { return view.children .slice(1) .every( (c, i) => c instanceof t.TagView && c.props['value'] === `${i}:${i + 1}` ); }); }) ).toEqual(true); }); it('should be able to conditionally render view from @if directive', async () => { const frame = await createFrame(` component App() { val items = [1,2,3]; } => (
) `); expect( t.assert(frame.view?.children[0], t.RekaComponentView, (view) => { return t.assert(view.render[0], t.TagView, (view) => { return view.children.map((child) => t.assert(child, t.TagView, (c) => c.props['value']) ); }); }) ).toEqual(['Hi']); }); it('should be able to render another Reka component', async () => { const frame = await createFrame(` component App() { val color="red"; } => (