import { h, render, lmount, nextTick, type WDom } from 'lithent'; import { createLContext } from '@/index'; // Type for multiple states type AppContext = { user: string; count: number; }; const testContext = createLContext(); const { Provider, contextState, useContext } = testContext; // Test 1: Basic lcontext usage with auto useRenew const Component = lmount(() => { const userState = contextState('Alice'); const countState = contextState(0); return () => (
); }); // Child component using useContext without renew parameter const ChildComponent = lmount(() => { const ctx = useContext(testContext); // No renew parameter! Auto useRenew return () => (

User: {ctx.user?.value ?? 'N/A'}

Count: {ctx.count?.value ?? 'N/A'}

); }); // Test 2: Selective subscription const ChildWithSelectiveSubscription = lmount(() => { const ctx = useContext(testContext, ['user']); // Only subscribe to user return () => (

User: {ctx.user?.value ?? 'N/A'}

); }); const ComponentWithSelectiveChild = lmount(() => { const userState = contextState('Alice'); const countState = contextState(0); return () => (
); }); // Test 3: Children passed as props const ProviderWrapper = lmount((_props, children: WDom[]) => { const userState = contextState('Charlie'); const countState = contextState(100); return () => ( {children} ); }); const ChildViaProps = lmount(() => { const ctx = useContext(testContext); return () => (

User: {ctx.user?.value ?? 'N/A'}

Count: {ctx.count?.value ?? 'N/A'}

); }); const ComponentWithPropsChildren = lmount(() => { return () => (
); }); const testWrap = document.getElementById('root'); // Render for manual testing if (testWrap) { render(


, testWrap ); } if (import.meta.vitest) { const { it, expect } = import.meta.vitest; it('lcontext should work with auto useRenew', async () => { const container = document.createElement('div'); const TestComp = lmount(() => { const userState = contextState('Alice'); const countState = contextState(0); const Child = lmount(() => { const ctx = useContext(testContext); return () => (
{ctx.user?.value ?? 'N/A'} {ctx.count?.value ?? 'N/A'}
); }); return () => (
); }); render(, container); await nextTick(); expect(container.querySelector('.user')?.textContent).toBe('Alice'); expect(container.querySelector('.count')?.textContent).toBe('0'); const toggleBtn = container.querySelector('.toggle') as HTMLButtonElement; toggleBtn?.click(); await nextTick(); expect(container.querySelector('.user')?.textContent).toBe('Bob'); }); it('lcontext selective subscription should work', async () => { const container = document.createElement('div'); const TestComp = lmount(() => { const userState = contextState('Alice'); const countState = contextState(0); const Child = lmount(() => { const ctx = useContext(testContext, ['user']); // Only user return () => (
{ctx.user?.value ?? 'N/A'}
); }); return () => (
); }); render(, container); await nextTick(); expect(container.querySelector('.user')?.textContent).toBe('Alice'); // Change user (subscribed) - should update const changeUserBtn = container.querySelector( '.change-user' ) as HTMLButtonElement; changeUserBtn?.click(); await nextTick(); expect(container.querySelector('.user')?.textContent).toBe('Bob'); }); it('lcontext should work with children passed as props', async () => { const container = document.createElement('div'); const TestProvider = lmount((_props, children: WDom[]) => { const userState = contextState('Start'); const countState = contextState(50); return () => ( {children} ); }); const Child = lmount(() => { const ctx = useContext(testContext); return () => (
{ctx.user?.value ?? 'N/A'} {ctx.count?.value ?? 'N/A'}
); }); const TestComp = lmount(() => { return () => ( ); }); render(, container); await nextTick(); expect(container.querySelector('.user')?.textContent).toBe('Start'); expect(container.querySelector('.count')?.textContent).toBe('50'); const updateBtn = container.querySelector('.update') as HTMLButtonElement; updateBtn?.click(); await nextTick(); expect(container.querySelector('.user')?.textContent).toBe('Updated'); }); }