diff --git a/js/page-load.js b/js/page-load.js index 7b8d3dc..b101942 100644 --- a/js/page-load.js +++ b/js/page-load.js @@ -91,18 +91,25 @@ proto.onPageLoad = function( body, path, response ) { }; proto.appendNextPage = function( body, path, response ) { - let { append, responseBody } = this.options; + let { append, responseBody, domParseResponse } = this.options; // do not append json - let isDocument = responseBody == 'text'; + let isDocument = responseBody == 'text' && domParseResponse; if ( !isDocument || !append ) return { body, response }; let items = body.querySelectorAll( append ); + let promiseValue = { body, response, items }; + // last page hit if no items. #840 + if ( !items || !items.length ) { + this.lastPageReached( body, path ); + return promiseValue; + } + let fragment = getItemsFragment( items ); let appendReady = () => { this.appendItems( items, fragment ); this.isLoading = false; this.dispatchEvent( 'append', null, [ body, path, items ] ); - return { body, response, items }; + return promiseValue; }; // TODO add hook for option to trigger appendReady diff --git a/test/check-last-page.js b/test/check-last-page.js index fa05520..9b5f1f2 100644 --- a/test/check-last-page.js +++ b/test/check-last-page.js @@ -36,17 +36,9 @@ function getPageAssertions() { serialT.fail('last event should not trigger when not last page'); } - await ( function() { - let promise = new Promise( function( resolve ) { - infScroll.once( 'append', function() { - infScroll.off( 'last', onLast ); - resolve(); - } ); - } ); - // load page 2 - infScroll.loadNextPage(); - return promise; - } )(); + await infScroll.loadNextPage().then( function() { + infScroll.off( 'last', onLast ); + } ); let promise = new Promise( function( resolve ) { infScroll.once( 'last', function() { @@ -88,13 +80,15 @@ test( 'checkLastPage: ".selector-string"', withPage, async function( t, page ) { assertions.forEach( ({ method, args }) => t[ method ]( ...args ) ); } ); -test( 'checkLastPage with path: function() {}', withPage, async function( t, page ) { +test( 'checkLastPage with empty page', withPage, async function( t, page ) { await page.evaluate( function() { window.infScroll = new InfiniteScroll( '.container', { // provide only page/2.html, then falsy path: function() { - if ( this.pageIndex < 3 ) { + if ( this.pageIndex < 2 ) { return `page/${this.pageIndex + 1}.html`; + } else { + return 'page/empty.html'; } }, // checkLastPage: true, // true by default @@ -105,3 +99,32 @@ test( 'checkLastPage with path: function() {}', withPage, async function( t, pag let assertions = await page.evaluate( getPageAssertions() ); assertions.forEach( ({ method, args }) => t[ method ]( ...args ) ); } ); + +test( 'checkLastPage with path: function() {}', withPage, async function( t, page ) { + let assertions = await page.evaluate( function() { + let infScroll = new InfiniteScroll( '.container', { + // provide only page/2.html, then falsy + path: function() { + if ( this.pageIndex < 2 ) { + return `page/${this.pageIndex + 1}.html`; + } + }, + // checkLastPage: true, // true by default + append: '.post', + } ); + + // function returning falsey will trigger last right after pageLoad + let promise = new Promise( function( resolve ) { + infScroll.once( 'last', function() { + serialT.is( infScroll.pageIndex, 2 ); + resolve( serialT.assertions ); + } ); + } ); + + // load page 2 + infScroll.loadNextPage(); + return promise; + } ); + + assertions.forEach( ({ method, args }) => t[ method ]( ...args ) ); +} ); diff --git a/test/html/page/empty.html b/test/html/page/empty.html new file mode 100644 index 0000000..fe03eb0 --- /dev/null +++ b/test/html/page/empty.html @@ -0,0 +1,15 @@ + + +
+ + + +