2018-06-02 22:48:23 +03:00
var should = require ( 'should' ) ,
2019-06-19 12:30:28 +03:00
hbs = require ( '../../../frontend/services/themes/engine' ) ,
2017-03-21 11:24:11 +03:00
configUtils = require ( '../../utils/configUtils' ) ,
path = require ( 'path' ) ,
2014-10-10 18:54:07 +04:00
2019-06-19 12:30:28 +03:00
helpers = require ( '../../../frontend/helpers' ) ;
2014-10-10 18:54:07 +04:00
describe ( '{{pagination}} helper' , function ( ) {
before ( function ( done ) {
2018-08-30 19:17:27 +03:00
hbs . express4 ( { partialsDir : [ configUtils . config . get ( 'paths' ) . helperTemplates ] } ) ;
2015-03-10 18:52:00 +03:00
2014-10-10 18:54:07 +04:00
hbs . cachePartials ( function ( ) {
done ( ) ;
} ) ;
2017-03-23 22:00:58 +03:00
// The pagination partial expects this helper
// @TODO: change to register with Ghost's own registration tools
hbs . registerHelper ( 'page_url' , helpers . page _url ) ;
2014-10-10 18:54:07 +04:00
} ) ;
var paginationRegex = /class="pagination"/ ,
newerRegex = /class="newer-posts"/ ,
olderRegex = /class="older-posts"/ ,
pageRegex = /class="page-number"/ ;
it ( 'should throw if pagination data is incorrect' , function ( ) {
var runHelper = function ( data ) {
2019-08-19 14:41:09 +03:00
return function ( ) {
helpers . pagination . call ( data ) ;
} ;
} , expectedMessage = 'The {{pagination}} helper was used outside of a paginated context. See https://ghost.org/docs/api/handlebars-themes/helpers/pagination/.' ;
2014-10-10 18:54:07 +04:00
2017-11-28 15:15:05 +03:00
runHelper ( 'not an object' ) . should . throwError ( expectedMessage ) ;
2017-03-21 11:24:11 +03:00
runHelper ( function ( ) {
2017-11-28 15:15:05 +03:00
} ) . should . throwError ( expectedMessage ) ;
2014-10-10 18:54:07 +04:00
} ) ;
it ( 'can render single page with no pagination necessary' , function ( ) {
var rendered = helpers . pagination . call ( {
pagination : { page : 1 , prev : null , next : null , limit : 15 , total : 8 , pages : 1 } ,
tag : { slug : 'slug' }
} ) ;
should . exist ( rendered ) ;
// strip out carriage returns and compare.
rendered . string . should . match ( paginationRegex ) ;
rendered . string . should . match ( pageRegex ) ;
rendered . string . should . match ( /Page 1 of 1/ ) ;
rendered . string . should . not . match ( newerRegex ) ;
rendered . string . should . not . match ( olderRegex ) ;
} ) ;
it ( 'can render first page of many with older posts link' , function ( ) {
var rendered = helpers . pagination . call ( {
pagination : { page : 1 , prev : null , next : 2 , limit : 15 , total : 8 , pages : 3 }
} ) ;
should . exist ( rendered ) ;
rendered . string . should . match ( paginationRegex ) ;
rendered . string . should . match ( pageRegex ) ;
rendered . string . should . match ( olderRegex ) ;
rendered . string . should . match ( /Page 1 of 3/ ) ;
rendered . string . should . not . match ( newerRegex ) ;
} ) ;
it ( 'can render middle pages of many with older and newer posts link' , function ( ) {
var rendered = helpers . pagination . call ( {
pagination : { page : 2 , prev : 1 , next : 3 , limit : 15 , total : 8 , pages : 3 }
} ) ;
should . exist ( rendered ) ;
rendered . string . should . match ( paginationRegex ) ;
rendered . string . should . match ( pageRegex ) ;
rendered . string . should . match ( olderRegex ) ;
rendered . string . should . match ( newerRegex ) ;
rendered . string . should . match ( /Page 2 of 3/ ) ;
} ) ;
it ( 'can render last page of many with newer posts link' , function ( ) {
var rendered = helpers . pagination . call ( {
pagination : { page : 3 , prev : 2 , next : null , limit : 15 , total : 8 , pages : 3 }
} ) ;
should . exist ( rendered ) ;
rendered . string . should . match ( paginationRegex ) ;
rendered . string . should . match ( pageRegex ) ;
rendered . string . should . match ( newerRegex ) ;
rendered . string . should . match ( /Page 3 of 3/ ) ;
rendered . string . should . not . match ( olderRegex ) ;
} ) ;
it ( 'validates values' , function ( ) {
var runErrorTest = function ( data ) {
return function ( ) {
helpers . pagination . call ( data ) ;
} ;
} ;
runErrorTest ( { pagination : { page : 3 , prev : true , next : null , limit : 15 , total : 8 , pages : 3 } } )
. should . throwError ( 'Invalid value, Next/Prev must be a number' ) ;
runErrorTest ( { pagination : { page : 3 , prev : 2 , next : true , limit : 15 , total : 8 , pages : 3 } } )
. should . throwError ( 'Invalid value, Next/Prev must be a number' ) ;
runErrorTest ( { pagination : { limit : 15 , total : 8 , pages : 3 } } )
. should . throwError ( 'All values must be defined for page, pages, limit and total' ) ;
runErrorTest ( { pagination : { page : 3 , total : 8 , pages : 3 } } )
. should . throwError ( 'All values must be defined for page, pages, limit and total' ) ;
runErrorTest ( { pagination : { page : 3 , limit : 15 , pages : 3 } } )
. should . throwError ( 'All values must be defined for page, pages, limit and total' ) ;
runErrorTest ( { pagination : { page : 3 , limit : 15 , total : 8 } } )
. should . throwError ( 'All values must be defined for page, pages, limit and total' ) ;
runErrorTest ( { pagination : { page : null , prev : null , next : null , limit : 15 , total : 8 , pages : 3 } } )
. should . throwError ( 'Invalid value, check page, pages, limit and total are numbers' ) ;
runErrorTest ( { pagination : { page : 1 , prev : null , next : null , limit : null , total : 8 , pages : 3 } } )
. should . throwError ( 'Invalid value, check page, pages, limit and total are numbers' ) ;
runErrorTest ( { pagination : { page : 1 , prev : null , next : null , limit : 15 , total : null , pages : 3 } } )
. should . throwError ( 'Invalid value, check page, pages, limit and total are numbers' ) ;
runErrorTest ( { pagination : { page : 1 , prev : null , next : null , limit : 15 , total : 8 , pages : null } } )
. should . throwError ( 'Invalid value, check page, pages, limit and total are numbers' ) ;
} ) ;
} ) ;
2015-03-28 19:00:57 +03:00
describe ( '{{pagination}} helper with custom template' , function ( ) {
before ( function ( done ) {
2018-08-30 19:17:27 +03:00
hbs . express4 ( { partialsDir : [ path . resolve ( configUtils . config . get ( 'paths' ) . corePath , 'test/unit/helpers/test_tpl' ) ] } ) ;
2015-03-28 19:00:57 +03:00
hbs . cachePartials ( function ( ) {
done ( ) ;
} ) ;
} ) ;
2019-09-10 12:37:04 +03:00
it ( 'can render single page with @site.title' , function ( ) {
2015-03-28 19:00:57 +03:00
var rendered = helpers . pagination . call ( {
pagination : { page : 1 , prev : null , next : null , limit : 15 , total : 8 , pages : 1 } ,
tag : { slug : 'slug' }
} , {
data : {
2019-09-10 12:37:04 +03:00
site : {
2015-03-28 19:00:57 +03:00
title : 'Chaos is a ladder.'
}
}
} ) ;
should . exist ( rendered ) ;
// strip out carriage returns and compare.
rendered . string . should . match ( /Page 1 of 1/ ) ;
rendered . string . should . containEql ( 'Chaos is a ladder' ) ;
2019-03-09 22:35:19 +03:00
rendered . string . should . not . containEql ( 'isHeader is set' ) ;
} ) ;
it ( 'can pass attributes through' , function ( ) {
var rendered = helpers . pagination . call ( {
pagination : { page : 1 , prev : null , next : null , limit : 15 , total : 8 , pages : 1 } ,
tag : { slug : 'slug' }
} , {
hash : { isHeader : true } ,
data : {
2019-09-10 12:37:04 +03:00
site : { }
2019-03-09 22:35:19 +03:00
}
} ) ;
should . exist ( rendered ) ;
// strip out carriage returns and compare.
rendered . string . should . match ( /Page 1 of 1/ ) ;
rendered . string . should . not . containEql ( 'Chaos is a ladder' ) ;
rendered . string . should . containEql ( 'isHeader is set' ) ;
2015-03-28 19:00:57 +03:00
} ) ;
} ) ;