@@ -21,4 +21,76 @@ describe('utils', function () {
2121 assert . strictEqual ( utils . toNative ( true ) , true )
2222 } )
2323 } )
24+
25+ describe ( 'getPage' , function ( ) {
26+ var array = [ 1 , 2 , 3 , 4 , 5 ]
27+ var perPage = 2
28+
29+ it ( 'should return first page' , function ( ) {
30+ assert . deepEqual (
31+ utils . getPage ( array , 1 , perPage ) ,
32+ {
33+ items : [ 1 , 2 ] ,
34+ current : 1 ,
35+ first : 1 ,
36+ next : 2 ,
37+ last : 3
38+ }
39+ )
40+ } )
41+
42+ it ( 'should return second page' , function ( ) {
43+ assert . deepEqual (
44+ utils . getPage ( array , 2 , perPage ) ,
45+ {
46+ items : [ 3 , 4 ] ,
47+ current : 2 ,
48+ first : 1 ,
49+ prev : 1 ,
50+ next : 3 ,
51+ last : 3
52+ }
53+ )
54+ } )
55+
56+ it ( 'should return third page (last)' , function ( ) {
57+ assert . deepEqual (
58+ utils . getPage ( array , 3 , perPage ) ,
59+ {
60+ items : [ 5 ] ,
61+ current : 3 ,
62+ first : 1 ,
63+ prev : 2 ,
64+ last : 3
65+ }
66+ )
67+ } )
68+
69+ it ( 'should return an empty array if page is greater than the last page' , function ( ) {
70+ assert . deepEqual (
71+ utils . getPage ( array , 99 , perPage ) ,
72+ {
73+ items : [ ]
74+ }
75+ )
76+ } )
77+
78+ it ( 'should return the array if perPage is greater than the array size' , function ( ) {
79+ assert . deepEqual (
80+ utils . getPage ( array , 1 , 99 ) ,
81+ {
82+ items : array
83+ }
84+ )
85+ } )
86+
87+ it ( 'should return an empty array if the array is empty' , function ( ) {
88+ assert . deepEqual (
89+ utils . getPage ( [ ] , 1 , 1 ) ,
90+ {
91+ items : [ ]
92+ }
93+ )
94+ } )
95+ } )
2496} )
0 commit comments