1
1
import { buildPagedList , getOffset } from "./utils" ;
2
2
3
3
describe ( "buildPagedList" , ( ) => {
4
- it . each < {
5
- numPages : number ;
6
- activePage : number ;
7
- expected : ( string | number ) [ ] ;
8
- } > ( [
9
- { numPages : 7 , activePage : 1 , expected : [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] } ,
10
- { numPages : 17 , activePage : 1 , expected : [ 1 , 2 , 3 , 4 , 5 , "right" , 17 ] } ,
11
- {
12
- numPages : 17 ,
13
- activePage : 9 ,
14
- expected : [ 1 , "left" , 8 , 9 , 10 , "right" , 17 ] ,
15
- } ,
16
- {
17
- numPages : 17 ,
18
- activePage : 17 ,
19
- expected : [ 1 , "left" , 13 , 14 , 15 , 16 , 17 ] ,
20
- } ,
21
- ] ) (
22
- `buildPagedList($numPages, $activePage)` ,
23
- ( { numPages, activePage, expected } ) => {
24
- expect ( buildPagedList ( numPages , activePage ) ) . toEqual ( expected ) ;
25
- } ,
26
- ) ;
4
+ it ( "has no placeholder entries when there are seven or fewer pages" , ( ) => {
5
+ for ( let i = 1 ; i <= 7 ; i ++ ) {
6
+ const expectedResult : number [ ] = [ ] ;
7
+ for ( let j = 1 ; j <= i ; j ++ ) {
8
+ expectedResult . push ( j ) ;
9
+ }
10
+
11
+ expect ( buildPagedList ( i , i ) ) . toEqual ( expectedResult ) ;
12
+ }
13
+ } ) ;
14
+
15
+ it ( "has 'right' placeholder for long lists when active page is near beginning" , ( ) => {
16
+ expect ( buildPagedList ( 17 , 1 ) ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 , "right" , 17 ] ) ;
17
+ } ) ;
18
+
19
+ it ( "has 'left' placeholder for long lists when active page is near end" , ( ) => {
20
+ expect ( buildPagedList ( 17 , 17 ) ) . toEqual ( [ 1 , "left" , 13 , 14 , 15 , 16 , 17 ] ) ;
21
+ } ) ;
22
+
23
+ it ( "has both placeholders for long lists when active page is in the middle" , ( ) => {
24
+ expect ( buildPagedList ( 17 , 9 ) ) . toEqual ( [ 1 , "left" , 8 , 9 , 10 , "right" , 17 ] ) ;
25
+ } ) ;
26
+
27
+ it ( "produces an empty array when there are no pages" , ( ) => {
28
+ expect ( buildPagedList ( 0 , 0 ) ) . toEqual ( [ ] ) ;
29
+ } ) ;
30
+
31
+ it ( "makes sure all values are unique (for React rendering keys)" , ( ) => {
32
+ type TestEntry = [ numPages : number , activePage : number ] ;
33
+ const testData : TestEntry [ ] = [
34
+ [ 0 , 0 ] ,
35
+ [ 1 , 1 ] ,
36
+ [ 2 , 2 ] ,
37
+ [ 3 , 3 ] ,
38
+ [ 4 , 4 ] ,
39
+ [ 5 , 5 ] ,
40
+ [ 6 , 6 ] ,
41
+ [ 7 , 7 ] ,
42
+
43
+ [ 10 , 3 ] ,
44
+ [ 7 , 1 ] ,
45
+ [ 17 , 1 ] ,
46
+ [ 17 , 9 ] ,
47
+ ] ;
48
+
49
+ for ( const [ numPages , activePage ] of testData ) {
50
+ const result = buildPagedList ( numPages , activePage ) ;
51
+ const uniqueCount = new Set ( result ) . size ;
52
+
53
+ expect ( uniqueCount ) . toEqual ( result . length ) ;
54
+ }
55
+ } ) ;
27
56
} ) ;
28
57
29
58
describe ( "getOffset" , ( ) => {
@@ -32,9 +61,18 @@ describe("getOffset", () => {
32
61
const limit = 10 ;
33
62
expect ( getOffset ( page , limit ) ) . toEqual ( 0 ) ;
34
63
} ) ;
35
- it ( "returns the limit on page 2" , ( ) => {
36
- const page = 2 ;
37
- const limit = 10 ;
38
- expect ( getOffset ( page , limit ) ) . toEqual ( limit ) ;
64
+
65
+ it ( "Returns the results for page 1 when input is invalid" , ( ) => {
66
+ const inputs = [ 0 , - 1 , - Infinity , NaN , Infinity , 3.6 , 7.4545435 ] ;
67
+
68
+ for ( const input of inputs ) {
69
+ expect ( getOffset ( input , 10 ) ) . toEqual ( 0 ) ;
70
+ }
71
+ } ) ;
72
+
73
+ it ( "Returns offset based on the current page for all pages after 1" , ( ) => {
74
+ expect ( getOffset ( 2 , 10 ) ) . toEqual ( 10 ) ;
75
+ expect ( getOffset ( 3 , 10 ) ) . toEqual ( 20 ) ;
76
+ expect ( getOffset ( 4 , 45 ) ) . toEqual ( 135 ) ;
39
77
} ) ;
40
78
} ) ;
0 commit comments