// example.jsx import type { Renew } from '@/index'; import { h, Fragment, render, mount, ref, portal, nextTick } from '@/index'; const testChangeRef = ref void)>(null); const Renew = mount((renew, _props) => { let count1 = 0; let count2 = 0; let count3 = 0; let count4 = 0; const el = ref(null); const change = () => { count1 += 1; count2 += 2; count3 += 3; count4 -= 1; renew(); }; testChangeRef.value = change; return () => (
  • count1: {count1}
  • count2: {count2}
  • count3: {count3}
  • count4: {count4}
  • {portal( , document.getElementById('bottom') as HTMLElement )}
    ); }); const PortalExam = mount<{ count1: number; count2: number; count3: number; count4: number; }>((_, _p) => { return ({ count1, count2, count3, count4 }) => (
    1: {count1}
    2: {count2}
    3: {count3}
    4: {count4}
    ); }); const testWrap = document.getElementById('root') || document.createElement('div'); const portalElement = document.createElement('div'); portalElement.id = 'bottom'; portalElement.style.cssText = 'border: 1px solid red; position: absolute; bottom: 0; width: 300px; height: 300px;'; if (document.body) { document.body.appendChild(portalElement); } render(, testWrap); if (import.meta.vitest) { const { it, expect } = import.meta.vitest; it('The portal is well exposed in its initial state.', () => { expect(testWrap.outerHTML).toBe( '
  • count1: 0
  • count2: 0
  • count3: 0
  • count4: 0
  • ' ); expect(portalElement.outerHTML).toBe( '
    1: 0
    2: 0
    3: 0
    4: 0
    ' ); }); it('The updated status is also reflected well in the portal.', () => { if (testChangeRef.value) { testChangeRef.value(); testChangeRef.value(); testChangeRef.value(); } nextTick().then(() => { expect(testWrap.outerHTML).toBe( '
  • count1: 3
  • count2: 6
  • count3: 9
  • count4: -3
  • ' ); expect(portalElement.outerHTML).toBe( '
    1: 3
    2: 6
    3: 9
    4: -3
    ' ); }); }); }