Skip to content

Commit

Permalink
Handle parsing page count from last few index pages
Browse files Browse the repository at this point in the history
  • Loading branch information
kremio committed Oct 26, 2018
1 parent 7337a84 commit fb114f0
Show file tree
Hide file tree
Showing 3 changed files with 441 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/index.js
Expand Up @@ -26,10 +26,19 @@ module.exports = async ( url ) => {
.get()

//The number of pages
let pageCount
const lastLink = $(".wp-pagenavi a.last").first()
if( lastLink.length != 0 ){
pageCount = lastLink.attr('href')
.match(/([0-9]+)$/)[1]
}else{
pageCount = $(".wp-pagenavi a.page").last().text()
}
/*
const pageCount = $(".wp-pagenavi a.last").first()
.attr('href')
.match(/([0-9]+)$/)[1]

*/
return {
reportsURLs,
pageCount: Number(pageCount)
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/index.test.js
Expand Up @@ -37,3 +37,14 @@ test( 'Base test', async () => {
const result = await scrapeIndex( 'https://domain.tld/path/to/page.html' )
expect( result ).toEqual( expectedResult )
})

test( 'Page without link to last page with classname "last"', async () => {
const sampleIndex = fs.readFileSync('./tests/samples/indexNoLast.html', 'utf8')
request.mockImplementation((...args) => {
const cb = args.pop()
cb( null, {statusCode: 200}, sampleIndex )
})

const result = await scrapeIndex( 'https://domain.tld/path/to/page.html' )
expect( result.pageCount ).toEqual( 36 )
})

0 comments on commit fb114f0

Please sign in to comment.