Skip to content

Commit

Permalink
πŸ”” #840 trigger last if 0 items appended
Browse files Browse the repository at this point in the history
βœ… fix checkLastPage with falsey function
  • Loading branch information
desandro committed Dec 31, 2020
1 parent 8c1b5b8 commit d2a09d1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 28 deletions.
13 changes: 10 additions & 3 deletions js/page-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 36 additions & 13 deletions test/check-last-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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 ) );
} );
15 changes: 15 additions & 0 deletions test/html/page/empty.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />

<title>empty page</title>

</head>
<body>

<h1>empty page</h1>

</body>
</html>
19 changes: 7 additions & 12 deletions test/outlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,15 @@ test( 'outlayer none', withPage, async( t, page ) => {
scrollThreshold: false,
} );

let promises = [
new Promise( ( resolve ) => infScroll.once( 'load', resolve ) ),
new Promise( ( resolve ) => {
infScroll.once( 'append', function( response, path, items ) {
resolve( items );
} );
} ),
];
let promise = new Promise( ( resolve ) => {
infScroll.once( 'load', () => {
serialT.pass('load triggered but not append');
resolve();
} );
} );

infScroll.loadNextPage();
return Promise.all( promises );
} )
.then( ([ , items ]) => {
serialT.is( items.length, 0, 'appended 0 items' );
return promise;
} )
.then( () => serialT.assertions );
} );
Expand Down

0 comments on commit d2a09d1

Please sign in to comment.