@@ -38,10 +38,10 @@ import { FindListOptions } from '../../core/data/find-list-options.model';
3838import { PaginationService } from '../../core/pagination/pagination.service' ;
3939import { HostWindowService } from '../host-window.service' ;
4040import { MockActivatedRoute } from '../mocks/active-router.mock' ;
41- import { HostWindowServiceMock } from '../mocks/host-window-service.mock' ;
4241import { RouterMock } from '../mocks/router.mock' ;
4342import { TranslateLoaderMock } from '../mocks/translate-loader.mock' ;
4443import { RSSComponent } from '../rss-feed/rss.component' ;
44+ import { HostWindowServiceStub } from '../testing/host-window-service.stub' ;
4545import { createTestComponent } from '../testing/utils.test' ;
4646import { EnumKeysPipe } from '../utils/enum-keys-pipe' ;
4747import { PaginationComponent } from './pagination.component' ;
@@ -100,7 +100,10 @@ function changePage(fixture: ComponentFixture<any>, idx: number): void {
100100 const de = fixture . debugElement . query ( By . css ( '.pagination' ) ) ;
101101 const buttons = de . nativeElement . querySelectorAll ( 'li' ) ;
102102
103- buttons [ idx ] . querySelector ( 'a' ) . click ( ) ;
103+ const clickableElement = buttons [ idx ] . querySelector ( 'a' ) || buttons [ idx ] . querySelector ( 'button' ) ;
104+ if ( clickableElement ) {
105+ clickableElement . click ( ) ;
106+ }
104107 fixture . detectChanges ( ) ;
105108}
106109
@@ -115,7 +118,7 @@ describe('Pagination component', () => {
115118 let testFixture : ComponentFixture < TestComponent > ;
116119 let de : DebugElement ;
117120 let html ;
118- let hostWindowServiceStub : HostWindowServiceMock ;
121+ let hostWindowServiceStub : HostWindowServiceStub ;
119122
120123 let activatedRouteStub : MockActivatedRoute ;
121124 let routerStub : RouterMock ;
@@ -128,6 +131,7 @@ describe('Pagination component', () => {
128131 const pagination = new PaginationComponentOptions ( ) ;
129132 pagination . currentPage = 1 ;
130133 pagination . pageSize = 10 ;
134+ pagination . maxSize = 10 ;
131135
132136 const sort = new SortOptions ( 'score' , SortDirection . DESC ) ;
133137 const findlistOptions = Object . assign ( new FindListOptions ( ) , { currentPage : 1 , elementsPerPage : 10 } ) ;
@@ -139,7 +143,7 @@ describe('Pagination component', () => {
139143 beforeEach ( waitForAsync ( ( ) => {
140144 activatedRouteStub = new MockActivatedRoute ( ) ;
141145 routerStub = new RouterMock ( ) ;
142- hostWindowServiceStub = new HostWindowServiceMock ( _initialState . width ) ;
146+ hostWindowServiceStub = new HostWindowServiceStub ( _initialState . width ) ;
143147
144148 currentPagination = new BehaviorSubject < PaginationComponentOptions > ( pagination ) ;
145149 currentSort = new BehaviorSubject < SortOptions > ( sort ) ;
@@ -199,6 +203,7 @@ describe('Pagination component', () => {
199203 [paginationOptions]='paginationOptions'
200204 [sortOptions]='sortOptions'
201205 [collectionSize]='collectionSize'
206+ [enablePaginationInput]="false"
202207 (pageChange)='pageChanged($event)'
203208 (pageSizeChange)='pageSizeChanged($event)'
204209 >
@@ -209,6 +214,9 @@ describe('Pagination component', () => {
209214 </ds-pagination>` ;
210215 testFixture = createTestComponent ( html , TestComponent ) as ComponentFixture < TestComponent > ;
211216 testComp = testFixture . componentInstance ;
217+ testComp . paginationOptions . maxSize = 10 ;
218+
219+ testFixture . detectChanges ( ) ;
212220 } ) ;
213221
214222 it ( 'should create Pagination Component' , inject ( [ PaginationComponent ] , ( app : PaginationComponent ) => {
@@ -330,6 +338,7 @@ describe('Pagination component', () => {
330338 (pageChange)='pageChanged($event)'
331339 (pageSizeChange)='pageSizeChanged($event)'
332340 [showPaginator]='false'
341+ [enablePaginationInput]="false"
333342 [objects]='objects'
334343 (prev)="goPrev()"
335344 (next)="goNext()"
@@ -391,6 +400,95 @@ describe('Pagination component', () => {
391400 } ) ;
392401 } ) ;
393402
403+ describe ( 'Pagination input field' , ( ) => {
404+ let fixture : ComponentFixture < PaginationComponent > ;
405+ let component : PaginationComponent ;
406+
407+ beforeEach ( waitForAsync ( ( ) => {
408+ TestBed . configureTestingModule ( {
409+ imports : [
410+ CommonModule ,
411+ NgbModule ,
412+ PaginationComponent ,
413+ EnumKeysPipe ,
414+ RouterTestingModule ,
415+ TranslateModule . forRoot ( {
416+ loader : { provide : TranslateLoader , useClass : TranslateLoaderMock } ,
417+ } ) ,
418+ StoreModule . forRoot ( { } , { } ) ,
419+ ] ,
420+ providers : [
421+ { provide : HostWindowService , useValue : hostWindowServiceStub } ,
422+ { provide : PaginationService , useValue : {
423+ getCurrentPagination : ( ) => new BehaviorSubject ( { currentPage : 5 , pageSize : 10 } ) ,
424+ getCurrentSort : ( ) => new BehaviorSubject ( { direction : SortDirection . ASC , field : 'name' } ) ,
425+ updateRoute : ( ) => {
426+ //
427+ } ,
428+ } } ,
429+ ] ,
430+ schemas : [ CUSTOM_ELEMENTS_SCHEMA ] ,
431+ } ) . compileComponents ( ) ;
432+ } ) ) ;
433+
434+ beforeEach ( ( ) => {
435+ fixture = TestBed . createComponent ( PaginationComponent ) ;
436+ component = fixture . componentInstance ;
437+ component . enablePaginationInput = true ;
438+ component . collectionSize = 100 ;
439+ component . paginationOptions = {
440+ id : 'test' ,
441+ currentPage : 5 ,
442+ pageSize : 10 ,
443+ pageSizeOptions : [ 10 , 20 , 50 ] ,
444+ directionLinks : true ,
445+ boundaryLinks : true ,
446+ ellipses : true ,
447+ maxSize : 10 ,
448+ rotate : false ,
449+ size : 'lg' ,
450+ disabled : false ,
451+ } ;
452+ fixture . detectChanges ( ) ;
453+ } ) ;
454+
455+ it ( 'should render the input field on the active page' , ( ) => {
456+ fixture . detectChanges ( ) ;
457+ const input = fixture . debugElement . nativeElement . querySelector ( 'input#paginationInput' ) ;
458+ expect ( input ) . toBeTruthy ( ) ;
459+ expect ( input . type ) . toBe ( 'text' ) ;
460+ } ) ;
461+
462+ it ( 'should set the input width to match the number of digits of the last page plus 3 (in ch)' , ( ) => {
463+ fixture . detectChanges ( ) ;
464+ const input = fixture . debugElement . nativeElement . querySelector ( 'input#paginationInput' ) ;
465+ // 10 pages, so 2 digits + 3 = 5ch
466+ expect ( input . style . width ) . toContain ( 'ch' ) ;
467+ expect ( parseInt ( input . style . width , 10 ) ) . toBeGreaterThanOrEqual ( 5 ) ;
468+ } ) ;
469+
470+ it ( 'should call selectPage when clicking the search button' , ( ) => {
471+ spyOn ( component , 'selectPage' ) ;
472+ fixture . detectChanges ( ) ;
473+ const button = fixture . debugElement . nativeElement . querySelector ( 'button.search-button' ) ;
474+ const input = fixture . debugElement . nativeElement . querySelector ( 'input#paginationInput' ) ;
475+ input . value = '3' ;
476+ button . click ( ) ;
477+ expect ( component . selectPage ) . toHaveBeenCalledWith ( '3' ) ;
478+ } ) ;
479+
480+ it ( 'should call selectPage when pressing enter in the input' , ( ) => {
481+ spyOn ( component , 'selectPage' ) ;
482+ fixture . detectChanges ( ) ;
483+ const input = fixture . debugElement . nativeElement . querySelector ( 'input#paginationInput' ) ;
484+ input . value = '7' ;
485+ const event = new KeyboardEvent ( 'keyup' , { key : 'Enter' } ) ;
486+ input . dispatchEvent ( event ) ;
487+ fixture . detectChanges ( ) ;
488+ expect ( component . selectPage ) . toHaveBeenCalledWith ( '7' ) ;
489+ } ) ;
490+ } ) ;
491+
394492} ) ;
395493
396494// declare a test component
0 commit comments