1+ import * as r from "./runtime" ;
2+ import S from "s-js" ;
3+
4+ describe ( "r.hydration" , ( ) => {
5+ const container = document . createElement ( "div" ) ,
6+ _tmpl$ = r . template ( `<span><!--#--><!--/--> John</span>` ) ,
7+ _tmpl$2 = r . template ( `<div>First</div>` ) ,
8+ _tmpl$3 = r . template ( `<div>Last</div>` ) ;
9+
10+ it ( "hydrates simple text" , ( ) => {
11+ S . root ( ( ) => {
12+ r . startSSR ( ) ;
13+ const leadingExpr = ( function ( ) {
14+ const _el$ = r . getNextElement ( _tmpl$ ) ,
15+ _el$2 = _el$ . firstChild ,
16+ _el$3 = _el$2 . nextSibling ;
17+ r . insert ( _el$ , "Hi" , _el$3 ) ;
18+ return _el$ ;
19+ } ) ( ) ;
20+ r . insert ( container , leadingExpr ) ;
21+ } ) ;
22+ expect ( container . innerHTML ) . toBe ( `<span _hk="0"><!--#-->Hi<!--/--> John</span>` ) ;
23+ // gather refs
24+ const el1 = container . firstChild ,
25+ el2 = el1 . firstChild ,
26+ el3 = el2 . nextSibling ,
27+ el4 = el3 . nextSibling ;
28+
29+ S . root ( ( ) => {
30+ r . hydration ( ( ) => {
31+ const leadingExpr = ( function ( ) {
32+ const _el$ = r . getNextElement ( _tmpl$ ) ,
33+ _el$2 = _el$ . firstChild ,
34+ [ _el$3 , _co$ ] = r . getNextMarker ( _el$2 . nextSibling ) ;
35+ r . hydration ( ( ) => r . insert ( _el$ , "Hi" , _el$3 , _co$ ) , _el$ ) ;
36+ return _el$ ;
37+ } ) ( ) ;
38+ r . insert ( container , leadingExpr , undefined , [ ...container . childNodes ] ) ;
39+ } , container ) ;
40+ } ) ;
41+ expect ( container . innerHTML ) . toBe ( `<span _hk="0"><!--#-->Hi<!--/--> John</span>` ) ;
42+ expect ( container . firstChild ) . toBe ( el1 ) ;
43+ expect ( el1 . firstChild ) . toBe ( el2 ) ;
44+ expect ( el2 . nextSibling ) . toBe ( el3 ) ;
45+ expect ( el3 . nextSibling ) . toBe ( el4 ) ;
46+ } ) ;
47+
48+ it ( "hydrates with an updated timestamp" , ( ) => {
49+ container . removeChild ( container . firstChild ) ;
50+ const time = Date . now ( ) ;
51+ S . root ( ( ) => {
52+ r . startSSR ( ) ;
53+ const leadingExpr = ( function ( ) {
54+ const _el$ = r . getNextElement ( _tmpl$ ) ,
55+ _el$2 = _el$ . firstChild ,
56+ _el$3 = _el$2 . nextSibling ;
57+ r . insert ( _el$ , time , _el$3 ) ;
58+ return _el$ ;
59+ } ) ( ) ;
60+ r . insert ( container , leadingExpr ) ;
61+ } ) ;
62+ expect ( container . innerHTML ) . toBe ( `<span _hk="0"><!--#-->${ time } <!--/--> John</span>` ) ;
63+ // gather refs
64+ const el1 = container . firstChild ,
65+ el2 = el1 . firstChild ,
66+ el3 = el2 . nextSibling ,
67+ el4 = el3 . nextSibling ;
68+
69+ const updatedTime = Date . now ( ) ;
70+ S . root ( ( ) => {
71+ r . hydration ( ( ) => {
72+ const leadingExpr = ( function ( ) {
73+ const _el$ = r . getNextElement ( _tmpl$ ) ,
74+ _el$2 = _el$ . firstChild ,
75+ [ _el$3 , _co$ ] = r . getNextMarker ( _el$2 . nextSibling ) ;
76+ r . hydration ( ( ) => r . insert ( _el$ , updatedTime , _el$3 , _co$ ) , _el$ ) ;
77+ return _el$ ;
78+ } ) ( ) ;
79+ r . insert ( container , leadingExpr , undefined , [ ...container . childNodes ] ) ;
80+ } , container ) ;
81+ } ) ;
82+ expect ( container . innerHTML ) . toBe ( `<span _hk="0"><!--#-->${ updatedTime } <!--/--> John</span>` ) ;
83+ expect ( container . firstChild ) . toBe ( el1 ) ;
84+ expect ( el1 . firstChild ) . toBe ( el2 ) ;
85+ expect ( el2 . nextSibling ) . toBe ( el3 ) ;
86+ expect ( el3 . nextSibling ) . toBe ( el4 ) ;
87+ } ) ;
88+
89+ it ( "hydrates fragments" , ( ) => {
90+ container . removeChild ( container . firstChild ) ;
91+ r . startSSR ( ) ;
92+ S . root ( ( ) => {
93+ const multiExpression = [ r . getNextElement ( _tmpl$2 ) , 'middle' , r . getNextElement ( _tmpl$3 ) ] ;
94+ r . insert ( container , multiExpression ) ;
95+ } ) ;
96+ expect ( container . innerHTML ) . toBe ( `<div _hk="0">First</div>middle<div _hk="1">Last</div>` ) ;
97+ // gather refs
98+ const el1 = container . firstChild ,
99+ el2 = el1 . nextSibling ,
100+ el3 = el2 . nextSibling ;
101+
102+ S . root ( ( ) => {
103+ r . hydration ( ( ) => {
104+ const multiExpression = [ r . getNextElement ( _tmpl$2 ) , 'middle' , r . getNextElement ( _tmpl$3 ) ] ;
105+ r . insert ( container , multiExpression , undefined , [ ...container . childNodes ] ) ;
106+ } , container ) ;
107+ } ) ;
108+ expect ( container . innerHTML ) . toBe ( `<div _hk="0">First</div>middle<div _hk="1">Last</div>` ) ;
109+ expect ( container . firstChild ) . toBe ( el1 ) ;
110+ expect ( el1 . nextSibling ) . toEqual ( el2 ) ;
111+ expect ( el1 . nextSibling . nextSibling ) . toBe ( el3 ) ;
112+ } ) ;
113+
114+ it ( "hydrates fragments with dynamic" , ( ) => {
115+ while ( container . firstChild ) container . removeChild ( container . firstChild ) ;
116+ r . startSSR ( ) ;
117+ S . root ( ( ) => {
118+ const multiExpression = [ r . getNextElement ( _tmpl$2 ) , ( ) => 'middle' , r . getNextElement ( _tmpl$3 ) ] ;
119+ r . insert ( container , multiExpression ) ;
120+ } ) ;
121+ expect ( container . innerHTML ) . toBe ( `<div _hk="0">First</div>middle<div _hk="1">Last</div>` ) ;
122+ // gather refs
123+ const el1 = container . firstChild ,
124+ el2 = el1 . nextSibling ,
125+ el3 = el2 . nextSibling ;
126+
127+ S . root ( ( ) => {
128+ r . hydration ( ( ) => {
129+ const multiExpression = [ r . getNextElement ( _tmpl$2 ) , ( ) => 'middle' , r . getNextElement ( _tmpl$3 ) ] ;
130+ r . insert ( container , multiExpression , undefined , [ ...container . childNodes ] ) ;
131+ } , container ) ;
132+ } ) ;
133+ expect ( container . innerHTML ) . toBe ( `<div _hk="0">First</div>middle<div _hk="1">Last</div>` ) ;
134+ expect ( container . firstChild ) . toBe ( el1 ) ;
135+ expect ( el1 . nextSibling ) . toEqual ( el2 ) ;
136+ expect ( el1 . nextSibling . nextSibling ) . toBe ( el3 ) ;
137+ } ) ;
138+
139+ it ( "hydrates fragments with dynamic template" , ( ) => {
140+ while ( container . firstChild ) container . removeChild ( container . firstChild ) ;
141+ r . startSSR ( ) ;
142+ S . root ( ( ) => {
143+ const multiExpression = [ r . getNextElement ( _tmpl$2 ) , ( ) => r . getNextElement ( _tmpl$2 ) , r . getNextElement ( _tmpl$3 ) ] ;
144+ r . insert ( container , multiExpression ) ;
145+ } ) ;
146+ expect ( container . innerHTML ) . toBe ( `<div _hk="0">First</div><div _hk="2">First</div><div _hk="1">Last</div>` ) ;
147+ // gather refs
148+ const el1 = container . firstChild ,
149+ el2 = el1 . nextSibling ,
150+ el3 = el2 . nextSibling ;
151+
152+ S . root ( ( ) => {
153+ r . hydration ( ( ) => {
154+ const multiExpression = [ r . getNextElement ( _tmpl$2 ) , ( ) => r . getNextElement ( _tmpl$2 ) , r . getNextElement ( _tmpl$3 ) ] ;
155+ r . insert ( container , multiExpression , undefined , [ ...container . childNodes ] ) ;
156+ } , container ) ;
157+ } ) ;
158+ expect ( container . innerHTML ) . toBe ( `<div _hk="0">First</div><div _hk="2">First</div><div _hk="1">Last</div>` ) ;
159+ expect ( container . firstChild ) . toBe ( el1 ) ;
160+ expect ( el1 . nextSibling ) . toBe ( el2 ) ;
161+ expect ( el1 . nextSibling . nextSibling ) . toBe ( el3 ) ;
162+ } ) ;
163+ } ) ;
0 commit comments