import { VNode , vNodeInstantiate , vNodeRender , createVElement , createVText } from "../../lib/vnode" ; import { syncVNodes } from "../../lib/vnode" ; import { scheduleMacrotask } from "../../lib/scheduler" ; function injectVNode ( parent , node , nextRef ? ) { vNodeInstantiate ( node , undefined ) ; parent . insertBefore ( node . ref ! , nextRef ! ) ; vNodeRender ( node , undefined ) ; } function gen ( item , keys ) { if ( typeof item === "number" ) { return keys ? createVText ( item . toString ( ) ) . key ( item . toString ( ) ) : createVText ( item . toString ( ) ) ; } else if ( Array . isArray ( item ) ) { let result = [ ] ; for ( let i = 0 ; i < item . length ; i ++ ) { result . push ( gen ( item [ i ] , keys ) as VNode ) ; } return result ; } else { let e = createVElement ( "div" ) . key ( item . key ) ; if ( keys ) { e . trackByKeyChildren ( gen ( item . children , keys ) as VNode [ ] ) ; } else { e . children ( gen ( item . children , keys ) as VNode [ ] ) ; } return e ; } } function checkInnerHtmlEquals ( ax , bx , cx , keys ) { const a = createVElement ( "div" ) ; const b = createVElement ( "div" ) ; const c = createVElement ( "div" ) ; if ( keys ) { a . trackByKeyChildren ( ax ) ; b . trackByKeyChildren ( bx ) ; c . trackByKeyChildren ( cx ) ; } else { a . children ( ax ) . disableChildrenShapeError ( ) ; b . children ( bx ) . disableChildrenShapeError ( ) ; c . children ( cx ) . disableChildrenShapeError ( ) ; } const aDiv = document . createElement ( "div" ) ; const bDiv = document . createElement ( "div" ) ; injectVNode ( aDiv , a , undefined ) ; injectVNode ( bDiv , b , undefined ) ; syncVNodes ( a , c , undefined ) ; if ( aDiv . innerHTML !== bDiv . innerHTML ) { throw Error ( `html doesn't match: ${ aDiv . innerHTML } => ${ bDiv . innerHTML }` ) ; } } function getRandomInt ( min , max ) { return Math . floor ( Math . random ( ) * ( max - min ) ) + min ; } function generateRandomSeq ( length ) { const result = [ ] as number [ ] ; const length2 = length << 1 ; const used = new Set ( ) ; for ( let i = 0 ; i < length ; i ++ ) { let id = getRandomInt ( 0 , length2 ) ; while ( used . has ( id ) ) { id = getRandomInt ( 0 , length2 ) ; } used . add ( id ) ; result . push ( id ) ; } return result ; } function createTestCase ( a , b ) { checkInnerHtmlEquals ( gen ( a , true ) as VNode [ ] , gen ( b , true ) as VNode [ ] , gen ( b , true ) as VNode [ ] , true ) ; } document . addEventListener ( "DOMContentLoaded" , ( ) => { let j = 0 ; document . body . innerText = j . toString ( ) ; function runTests ( ) { for ( let i = 0 ; i < 1000 ; i ++ ) { const aLength = getRandomInt ( 0 , 32 ) ; const bLength = getRandomInt ( 0 , 32 ) ; const a = generateRandomSeq ( aLength ) ; const b = generateRandomSeq ( bLength ) ; createTestCase ( a , b ) ; } document . body . innerText = ( ++ j ) . toString ( ) ; scheduleMacrotask ( runTests ) ; } runTests ( ) ; } ) ;