Skip to content

Commit d2a09d1

Browse files
committed
πŸ”” #840 trigger last if 0 items appended
βœ… fix checkLastPage with falsey function
1 parent 8c1b5b8 commit d2a09d1

File tree

4 files changed

+68
-28
lines changed

4 files changed

+68
-28
lines changed

β€Žjs/page-load.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,25 @@ proto.onPageLoad = function( body, path, response ) {
9191
};
9292

9393
proto.appendNextPage = function( body, path, response ) {
94-
let { append, responseBody } = this.options;
94+
let { append, responseBody, domParseResponse } = this.options;
9595
// do not append json
96-
let isDocument = responseBody == 'text';
96+
let isDocument = responseBody == 'text' && domParseResponse;
9797
if ( !isDocument || !append ) return { body, response };
9898

9999
let items = body.querySelectorAll( append );
100+
let promiseValue = { body, response, items };
101+
// last page hit if no items. #840
102+
if ( !items || !items.length ) {
103+
this.lastPageReached( body, path );
104+
return promiseValue;
105+
}
106+
100107
let fragment = getItemsFragment( items );
101108
let appendReady = () => {
102109
this.appendItems( items, fragment );
103110
this.isLoading = false;
104111
this.dispatchEvent( 'append', null, [ body, path, items ] );
105-
return { body, response, items };
112+
return promiseValue;
106113
};
107114

108115
// TODO add hook for option to trigger appendReady

β€Žtest/check-last-page.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,9 @@ function getPageAssertions() {
3636
serialT.fail('last event should not trigger when not last page');
3737
}
3838

39-
await ( function() {
40-
let promise = new Promise( function( resolve ) {
41-
infScroll.once( 'append', function() {
42-
infScroll.off( 'last', onLast );
43-
resolve();
44-
} );
45-
} );
46-
// load page 2
47-
infScroll.loadNextPage();
48-
return promise;
49-
} )();
39+
await infScroll.loadNextPage().then( function() {
40+
infScroll.off( 'last', onLast );
41+
} );
5042

5143
let promise = new Promise( function( resolve ) {
5244
infScroll.once( 'last', function() {
@@ -88,13 +80,15 @@ test( 'checkLastPage: ".selector-string"', withPage, async function( t, page ) {
8880
assertions.forEach( ({ method, args }) => t[ method ]( ...args ) );
8981
} );
9082

91-
test( 'checkLastPage with path: function() {}', withPage, async function( t, page ) {
83+
test( 'checkLastPage with empty page', withPage, async function( t, page ) {
9284
await page.evaluate( function() {
9385
window.infScroll = new InfiniteScroll( '.container', {
9486
// provide only page/2.html, then falsy
9587
path: function() {
96-
if ( this.pageIndex < 3 ) {
88+
if ( this.pageIndex < 2 ) {
9789
return `page/${this.pageIndex + 1}.html`;
90+
} else {
91+
return 'page/empty.html';
9892
}
9993
},
10094
// checkLastPage: true, // true by default
@@ -105,3 +99,32 @@ test( 'checkLastPage with path: function() {}', withPage, async function( t, pag
10599
let assertions = await page.evaluate( getPageAssertions() );
106100
assertions.forEach( ({ method, args }) => t[ method ]( ...args ) );
107101
} );
102+
103+
test( 'checkLastPage with path: function() {}', withPage, async function( t, page ) {
104+
let assertions = await page.evaluate( function() {
105+
let infScroll = new InfiniteScroll( '.container', {
106+
// provide only page/2.html, then falsy
107+
path: function() {
108+
if ( this.pageIndex < 2 ) {
109+
return `page/${this.pageIndex + 1}.html`;
110+
}
111+
},
112+
// checkLastPage: true, // true by default
113+
append: '.post',
114+
} );
115+
116+
// function returning falsey will trigger last right after pageLoad
117+
let promise = new Promise( function( resolve ) {
118+
infScroll.once( 'last', function() {
119+
serialT.is( infScroll.pageIndex, 2 );
120+
resolve( serialT.assertions );
121+
} );
122+
} );
123+
124+
// load page 2
125+
infScroll.loadNextPage();
126+
return promise;
127+
} );
128+
129+
assertions.forEach( ({ method, args }) => t[ method ]( ...args ) );
130+
} );

β€Žtest/html/page/empty.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width" />
6+
7+
<title>empty page</title>
8+
9+
</head>
10+
<body>
11+
12+
<h1>empty page</h1>
13+
14+
</body>
15+
</html>

β€Žtest/outlayer.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,15 @@ test( 'outlayer none', withPage, async( t, page ) => {
113113
scrollThreshold: false,
114114
} );
115115

116-
let promises = [
117-
new Promise( ( resolve ) => infScroll.once( 'load', resolve ) ),
118-
new Promise( ( resolve ) => {
119-
infScroll.once( 'append', function( response, path, items ) {
120-
resolve( items );
121-
} );
122-
} ),
123-
];
116+
let promise = new Promise( ( resolve ) => {
117+
infScroll.once( 'load', () => {
118+
serialT.pass('load triggered but not append');
119+
resolve();
120+
} );
121+
} );
124122

125123
infScroll.loadNextPage();
126-
return Promise.all( promises );
127-
} )
128-
.then( ([ , items ]) => {
129-
serialT.is( items.length, 0, 'appended 0 items' );
124+
return promise;
130125
} )
131126
.then( () => serialT.assertions );
132127
} );

0 commit comments

Comments
Β (0)