11import React from 'react' ;
22
33import '@testing-library/jest-dom/extend-expect' ;
4- import { render , screen } from '../../util/TestSetup' ;
4+ import { render , screen , fireEvent } from '../../util/TestSetup' ;
55
66import { Pagination } from './Pagination' ;
77
@@ -11,9 +11,21 @@ const fourButtonsPagination = {
1111 number : 0 ,
1212} ;
1313
14+ const fourButtonsSelectedAtLastPagination = {
15+ numberOfElements : 11 ,
16+ totalPages : 2 ,
17+ number : 1 ,
18+ } ;
19+
20+ const sixButtonsPagination = {
21+ numberOfElements : 61 ,
22+ totalPages : 7 ,
23+ number : 3 ,
24+ } ;
25+
1426describe ( 'Pagination.js Test Suite' , ( ) => {
1527 beforeEach ( ( ) => {
16- render ( < Pagination numberOfElements = { fourButtonsPagination . numberOfElements } totalPages = { fourButtonsPagination . totalPages } selectedPage = { fourButtonsPagination . number } /> ) ;
28+ render ( < Pagination numberOfElements = { sixButtonsPagination . numberOfElements } totalPages = { sixButtonsPagination . totalPages } selectedPage = { sixButtonsPagination . number } /> ) ;
1729 } ) ;
1830
1931 test ( 'should match snapshot' , ( ) => {
@@ -22,33 +34,108 @@ describe('Pagination.js Test Suite', () => {
2234 expect ( pagination ) . toMatchSnapshot ( ) ;
2335 } ) ;
2436
25- test ( 'should have 4 buttons (< 1 2 >) ' , ( ) => {
37+ test ( 'should have 6 buttons (< 2 3 4 5 6 >) with 61 records and a size of 10 ' , ( ) => {
2638 const pagination = screen . getByTestId ( 'pagination-wrapper' ) ;
27- const leftarrow = screen . getByTestId ( 'leftarrow-pagination-wrapper' ) ;
39+ const leftArrow = screen . getByTestId ( 'leftarrow-pagination-wrapper' ) ;
40+ const morePreviousPage = screen . getByTestId ( 'morePreviousPage-pagination-wrapper' ) ;
41+ const previousPage = screen . getByTestId ( 'previousPage-pagination-wrapper' ) ;
2842 const selectedPage = screen . getByTestId ( 'selectedPage-pagination-wrapper' ) ;
2943 const nextPage = screen . getByTestId ( 'nextPage-pagination-wrapper' ) ;
30- const afterarrow = screen . getByTestId ( 'afterarrow-pagination-wrapper' ) ;
44+ const moreNextPage = screen . getByTestId ( 'moreNextPage-pagination-wrapper' ) ;
45+ const afterArrow = screen . getByTestId ( 'afterarrow-pagination-wrapper' ) ;
3146
3247 expect ( pagination ) . toBeInTheDocument ( ) ;
33- expect ( leftarrow ) . toHaveTextContent ( '<' ) ;
34- expect ( selectedPage ) . toHaveTextContent ( '1' ) ;
35- expect ( nextPage ) . toHaveTextContent ( '2' ) ;
36- expect ( afterarrow ) . toHaveTextContent ( '>' ) ;
48+ expect ( leftArrow ) . toHaveTextContent ( '<' ) ;
49+ expect ( morePreviousPage ) . toHaveTextContent ( '2' ) ;
50+ expect ( previousPage ) . toHaveTextContent ( '3' ) ;
51+ expect ( selectedPage ) . toHaveTextContent ( '4' ) ;
52+ expect ( nextPage ) . toHaveTextContent ( '5' ) ;
53+ expect ( moreNextPage ) . toHaveTextContent ( '6' ) ;
54+ expect ( afterArrow ) . toHaveTextContent ( '>' ) ;
3755 } ) ;
3856
39- test ( 'should have button < disabled ' , ( ) => {
57+ test ( 'should have button 4 selected ' , ( ) => {
4058 const pagination = screen . getByTestId ( 'pagination-wrapper' ) ;
41- const leftarrow = screen . getByTestId ( 'leftarrow -pagination-wrapper' ) ;
59+ const selectedPage = screen . getByTestId ( 'selectedPage -pagination-wrapper' ) ;
4260
4361 expect ( pagination ) . toBeInTheDocument ( ) ;
44- expect ( leftarrow ) . toHaveProperty ( 'disabled ' , true ) ;
62+ expect ( selectedPage ) . toHaveProperty ( 'design ' , 'Emphasized' ) ;
4563 } ) ;
4664
47- test ( 'should have button 1 selected' , ( ) => {
48- const pagination = screen . getByTestId ( 'pagination-wrapper' ) ;
49- const selectedPage = screen . getByTestId ( 'selectedPage-pagination-wrapper' ) ;
65+ test ( 'should increment selected page when nextPage is selected' , async ( ) => {
66+ let selectedPageCount = 3 ;
67+ let setSelectedPageCount = ( params ) => {
68+ selectedPageCount = params ;
69+ } ;
70+ render (
71+ < Pagination
72+ numberOfElements = { sixButtonsPagination . numberOfElements }
73+ totalPages = { sixButtonsPagination . totalPages }
74+ selectedPage = { sixButtonsPagination . number }
75+ setPage = { ( params ) => setSelectedPageCount ( params ) }
76+ /> ,
77+ ) ;
78+ const pagination = screen . getAllByTestId ( 'pagination-wrapper' ) [ 1 ] ;
79+ const afterArrow = screen . getAllByTestId ( 'afterarrow-pagination-wrapper' ) [ 1 ] ;
5080
5181 expect ( pagination ) . toBeInTheDocument ( ) ;
52- expect ( selectedPage ) . toHaveProperty ( 'design' , 'Emphasized' ) ;
82+ expect ( selectedPageCount ) . toBe ( 3 ) ;
83+ fireEvent . click ( afterArrow ) ;
84+ expect ( selectedPageCount ) . toBe ( 4 ) ;
85+ } ) ;
86+
87+ test ( 'should decrease selected page when nextPage is selected' , async ( ) => {
88+ let selectedPageCount = 3 ;
89+ let setSelectedPageCount = ( params ) => {
90+ selectedPageCount = params ;
91+ } ;
92+ render (
93+ < Pagination
94+ numberOfElements = { sixButtonsPagination . numberOfElements }
95+ totalPages = { sixButtonsPagination . totalPages }
96+ selectedPage = { sixButtonsPagination . number }
97+ setPage = { ( params ) => setSelectedPageCount ( params ) }
98+ /> ,
99+ ) ;
100+ const pagination = screen . getAllByTestId ( 'pagination-wrapper' ) [ 1 ] ;
101+ const leftArrow = screen . getAllByTestId ( 'leftarrow-pagination-wrapper' ) [ 1 ] ;
102+
103+ expect ( pagination ) . toBeInTheDocument ( ) ;
104+ expect ( selectedPageCount ) . toBe ( 3 ) ;
105+ fireEvent . click ( leftArrow ) ;
106+ expect ( selectedPageCount ) . toBe ( 2 ) ;
107+ } ) ;
108+
109+ test ( 'should have button < disabled when first page is selected in a example with 2 pages' , ( ) => {
110+ render ( < Pagination numberOfElements = { fourButtonsPagination . numberOfElements } totalPages = { fourButtonsPagination . totalPages } selectedPage = { fourButtonsPagination . number } /> ) ;
111+
112+ const pagination = screen . getAllByTestId ( 'pagination-wrapper' ) [ 1 ] ;
113+ const leftarrow = screen . getAllByTestId ( 'leftarrow-pagination-wrapper' ) [ 1 ] ;
114+
115+ expect ( pagination ) . toBeInTheDocument ( ) ;
116+ expect ( leftarrow ) . toHaveProperty ( 'disabled' , true ) ;
117+ } ) ;
118+
119+ test ( 'should have button > disabled when last page is selected in a example with 2 pages' , ( ) => {
120+ render (
121+ < Pagination
122+ numberOfElements = { fourButtonsSelectedAtLastPagination . numberOfElements }
123+ totalPages = { fourButtonsSelectedAtLastPagination . totalPages }
124+ selectedPage = { fourButtonsSelectedAtLastPagination . number }
125+ /> ,
126+ ) ;
127+
128+ const afterarrow = screen . getAllByTestId ( 'afterarrow-pagination-wrapper' ) [ 1 ] ;
129+
130+ expect ( afterarrow ) . toBeInTheDocument ( ) ;
131+ expect ( afterarrow ) . toHaveProperty ( 'disabled' , true ) ;
132+ } ) ;
133+
134+ test ( 'should render nothing (return undefined) when no pagination data is passed' , ( ) => {
135+ render ( < Pagination numberOfElements = { null } totalPages = { null } selectedPage = { null } /> ) ;
136+
137+ const pagination = screen . getAllByTestId ( 'pagination-wrapper' ) [ 1 ] ;
138+
139+ expect ( pagination ) . toBeUndefined ( ) ;
53140 } ) ;
54141} ) ;
0 commit comments