@@ -10,6 +10,11 @@ const responses = {
1010 }
1111 } )
1212 } ,
13+ '/slow-hello' : function ( ) {
14+ return new Promise ( resolve => {
15+ setTimeout ( resolve , 100 )
16+ } ) . then ( responses [ '/hello' ] )
17+ } ,
1318 '/one-two' : function ( ) {
1419 return new Response ( '<p id="one">one</p><p id="two">two</p>' , {
1520 status : 200 ,
@@ -372,7 +377,7 @@ suite('include-fragment-element', function() {
372377 } )
373378 } )
374379
375- test . only ( 'fires replaced event' , function ( ) {
380+ test ( 'fires replaced event' , function ( ) {
376381 const elem = document . createElement ( 'include-fragment' )
377382 document . body . appendChild ( elem )
378383
@@ -386,7 +391,7 @@ suite('include-fragment-element', function() {
386391 } )
387392 } )
388393
389- test . only ( 'fires events for include-fragment node replacement operations for fragment manipulation' , function ( ) {
394+ test ( 'fires events for include-fragment node replacement operations for fragment manipulation' , function ( ) {
390395 const elem = document . createElement ( 'include-fragment' )
391396 document . body . appendChild ( elem )
392397
@@ -404,7 +409,7 @@ suite('include-fragment-element', function() {
404409 } )
405410 } )
406411
407- test . only ( 'does not replace node if event was canceled ' , function ( ) {
412+ test ( 'does not replace node if event was canceled ' , function ( ) {
408413 const elem = document . createElement ( 'include-fragment' )
409414 document . body . appendChild ( elem )
410415
@@ -420,4 +425,112 @@ suite('include-fragment-element', function() {
420425 assert ( document . querySelector ( 'include-fragment' ) , 'Node should not be replaced' )
421426 } )
422427 } )
428+
429+ test ( 'sets loading to "eager" by default' , function ( ) {
430+ const div = document . createElement ( 'div' )
431+ div . innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
432+ document . body . appendChild ( div )
433+
434+ assert ( div . firstChild . loading , 'eager' )
435+ } )
436+
437+ test ( 'loading will return "eager" even if set to junk value' , function ( ) {
438+ const div = document . createElement ( 'div' )
439+ div . innerHTML = '<include-fragment loading="junk" src="/hello">loading</include-fragment>'
440+ document . body . appendChild ( div )
441+
442+ assert ( div . firstChild . loading , 'eager' )
443+ } )
444+
445+ test ( 'loading=lazy loads if already visible on page' , function ( ) {
446+ const div = document . createElement ( 'div' )
447+ div . innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
448+ document . body . appendChild ( div )
449+
450+ return when ( div . firstChild , 'include-fragment-replaced' ) . then ( ( ) => {
451+ assert . equal ( document . querySelector ( 'include-fragment' ) , null )
452+ assert . equal ( document . querySelector ( '#replaced' ) . textContent , 'hello' )
453+ } )
454+ } )
455+
456+ test ( 'loading=lazy does not load if not visible on page' , function ( ) {
457+ const div = document . createElement ( 'div' )
458+ div . innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
459+ div . hidden = true
460+ document . body . appendChild ( div )
461+ return Promise . race ( [
462+ when ( div . firstChild , 'load' ) . then ( ( ) => {
463+ throw new Error ( '<include-fragment loading=lazy> loaded too early' )
464+ } ) ,
465+ new Promise ( resolve => setTimeout ( resolve , 100 ) )
466+ ] )
467+ } )
468+
469+ test ( 'loading=lazy loads as soon as element visible on page' , function ( ) {
470+ const div = document . createElement ( 'div' )
471+ div . innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
472+ div . hidden = true
473+ let failed = false
474+ document . body . appendChild ( div )
475+ const fail = ( ) => failed = true
476+ div . firstChild . addEventListener ( 'load' , fail )
477+
478+ setTimeout ( function ( ) {
479+ div . hidden = false
480+ div . firstChild . removeEventListener ( 'load' , fail )
481+ } , 100 )
482+
483+ return when ( div . firstChild , 'load' ) . then ( ( ) => {
484+ assert . ok ( ! failed , "Load occured too early" )
485+ } )
486+ } )
487+
488+ test ( 'loading=lazy does not observably change during load cycle' , function ( ) {
489+ const div = document . createElement ( 'div' )
490+ div . innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
491+ const elem = div . firstChild
492+ document . body . appendChild ( div )
493+
494+ return when ( elem , 'loadstart' ) . then ( ( ) => {
495+ assert . equal ( elem . loading , 'lazy' , "loading mode changed observably" )
496+ } )
497+ } )
498+
499+ test ( 'loading=lazy can be switched to eager to load' , function ( ) {
500+ const div = document . createElement ( 'div' )
501+ div . innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
502+ div . hidden = true
503+ let failed = false
504+ document . body . appendChild ( div )
505+ const fail = ( ) => failed = true
506+ div . firstChild . addEventListener ( 'load' , fail )
507+
508+ setTimeout ( function ( ) {
509+ div . firstChild . loading = 'eager'
510+ div . firstChild . removeEventListener ( 'load' , fail )
511+ } , 100 )
512+
513+ return when ( div . firstChild , 'load' ) . then ( ( ) => {
514+ assert . ok ( ! failed , "Load occured too early" )
515+ } )
516+ } )
517+
518+ test ( 'loading=lazy wont load twice even if load is manually called' , function ( ) {
519+ const div = document . createElement ( 'div' )
520+ div . innerHTML = '<include-fragment loading="lazy" src="/slow-hello">loading</include-fragment>'
521+ div . hidden = true
522+ document . body . appendChild ( div )
523+ let count = 0
524+ div . firstChild . addEventListener ( 'loadstart' , ( ) => count += 1 )
525+ const load = div . firstChild . load ( )
526+ setTimeout ( ( ) => {
527+ div . hidden = false
528+ } , 0 )
529+
530+ return load
531+ . then ( ( ) => when ( div . firstChild , 'loadend' ) )
532+ . then ( ( ) => {
533+ assert . equal ( count , 1 , "Load occured too many times" )
534+ } )
535+ } )
423536} )
0 commit comments