@@ -113,4 +113,73 @@ describe('sl-tooltip', () => {
113113      expect ( tooltip . matches ( ':popover-open' ) ) . to . be . true ; 
114114    } ) ; 
115115  } ) ; 
116+ 
117+   describe ( 'Tooltip lazy()' ,  ( )  =>  { 
118+     let  el : HTMLElement ; 
119+     let  button : Button ; 
120+     let  tooltip : Tooltip ; 
121+ 
122+     beforeEach ( async  ( )  =>  { 
123+       el  =  await  fixture ( html ` 
124+         < div  style ="display: block; width: 400px; height: 400px; ">  
125+           < sl-button > Button</ sl-button >  
126+         </ div >  
127+       ` ) ; 
128+       button  =  el . querySelector ( 'sl-button' )  as  Button ; 
129+     } ) ; 
130+ 
131+     it ( 'should create a tooltip lazily on pointerover with default aria-describedby' ,  async  ( )  =>  { 
132+       Tooltip . lazy ( button ,  createdTooltip  =>  ( tooltip  =  createdTooltip ) ) ; 
133+ 
134+       button . dispatchEvent ( new  Event ( 'pointerover' ,  {  bubbles : true  } ) ) ; 
135+ 
136+       // Give some time for the tooltip to open 
137+       await  new  Promise ( resolve  =>  setTimeout ( resolve ) ) ; 
138+ 
139+       expect ( tooltip ) . to . exist ; 
140+       expect ( tooltip ! . id ) . to . match ( / s l - t o o l t i p - ( \d + ) / ) ; 
141+       expect ( button ) . to . have . attribute ( 'aria-describedby' ,  tooltip ?. id ) ; 
142+       expect ( button ) . not . to . have . attribute ( 'aria-labelledby' ) ; 
143+       expect ( tooltip ! . matches ( ':popover-open' ) ) . to . be . true ; 
144+     } ) ; 
145+ 
146+     it ( 'should create a tooltip lazily on focusin' ,  async  ( )  =>  { 
147+       Tooltip . lazy ( button ,  createdTooltip  =>  ( tooltip  =  createdTooltip ) ) ; 
148+ 
149+       button . dispatchEvent ( new  Event ( 'focusin' ,  {  bubbles : true  } ) ) ; 
150+ 
151+       // Give some time for the tooltip to open 
152+       await  new  Promise ( resolve  =>  setTimeout ( resolve ) ) ; 
153+ 
154+       expect ( tooltip ) . to . exist ; 
155+       expect ( button ) . to . have . attribute ( 'aria-describedby' ,  tooltip ?. id ) ; 
156+       expect ( tooltip ! . matches ( ':popover-open' ) ) . to . be . true ; 
157+     } ) ; 
158+ 
159+     it ( 'should use aria-labelledby when ariaRelation is label' ,  async  ( )  =>  { 
160+       Tooltip . lazy ( button ,  createdTooltip  =>  ( tooltip  =  createdTooltip ) ,  {  ariaRelation : 'label'  } ) ; 
161+ 
162+       button . dispatchEvent ( new  Event ( 'pointerover' ,  {  bubbles : true  } ) ) ; 
163+ 
164+       // Give some time for the tooltip to open 
165+       await  new  Promise ( resolve  =>  setTimeout ( resolve ) ) ; 
166+ 
167+       expect ( tooltip ) . to . exist ; 
168+       expect ( button ) . to . have . attribute ( 'aria-labelledby' ,  tooltip ?. id ) ; 
169+       expect ( button ) . not . to . have . attribute ( 'aria-describedby' ) ; 
170+     } ) ; 
171+ 
172+     it ( 'should only create the tooltip once' ,  async  ( )  =>  { 
173+       Tooltip . lazy ( button ,  createdTooltip  =>  ( tooltip  =  createdTooltip ) ) ; 
174+ 
175+       button . dispatchEvent ( new  Event ( 'pointerover' ,  {  bubbles : true  } ) ) ; 
176+       button . dispatchEvent ( new  Event ( 'pointerover' ,  {  bubbles : true  } ) ) ;  // second should be ignored 
177+ 
178+       // Give some time for the tooltip to open 
179+       await  new  Promise ( resolve  =>  setTimeout ( resolve ) ) ; 
180+ 
181+       expect ( el . querySelectorAll ( 'sl-tooltip' ) . length ) . to . equal ( 1 ) ; 
182+       expect ( tooltip ) . to . exist ; 
183+     } ) ; 
184+   } ) ; 
116185} ) ; 
0 commit comments