@@ -66,14 +66,14 @@ function expectPages(fixture: ComponentFixture<any>, pagesDef: string[]): void {
6666 expect ( pages [ i ] . classList . contains ( 'disabled' ) ) . toBeTruthy ( ) ;
6767 expect ( normalizeText ( pages [ i ] . textContent ) ) . toEqual ( normalizeText ( pageDef ) ) ;
6868 if ( normalizeText ( pages [ i ] . textContent ) !== '...' ) {
69- expect ( pages [ i ] . querySelector ( 'a' ) . getAttribute ( 'tabindex' ) ) . toEqual ( '-1' ) ;
69+ expect ( ( pages [ i ] . querySelector ( 'a' ) || pages [ i ] . querySelector ( 'button' ) ) . getAttribute ( 'tabindex' ) ) . toEqual ( '-1' ) ;
7070 }
7171 } else {
7272 expect ( pages [ i ] . classList . contains ( 'active' ) ) . toBeFalsy ( ) ;
7373 expect ( pages [ i ] . classList . contains ( 'disabled' ) ) . toBeFalsy ( ) ;
7474 expect ( normalizeText ( pages [ i ] . textContent ) ) . toEqual ( normalizeText ( pageDef ) ) ;
7575 if ( normalizeText ( pages [ i ] . textContent ) !== '...' ) {
76- expect ( pages [ i ] . querySelector ( 'a' ) . hasAttribute ( 'tabindex' ) ) . toBeFalsy ( ) ;
76+ expect ( ( pages [ i ] . querySelector ( 'a' ) || pages [ i ] . querySelector ( 'button' ) ) . hasAttribute ( 'tabindex' ) ) . toBeFalsy ( ) ;
7777 }
7878 }
7979 }
@@ -291,11 +291,15 @@ describe('Pagination component', () => {
291291
292292 it ( 'should call the updateRoute method on the paginationService with the correct params' , fakeAsync ( ( ) => {
293293 testComp . collectionSize = 60 ;
294+ testFixture . detectChanges ( ) ;
294295
295296 changePage ( testFixture , 3 ) ;
296297 tick ( ) ;
297298 expect ( paginationService . updateRoute ) . toHaveBeenCalledWith ( 'test' , Object . assign ( { page : 3 } ) , { } , false ) ;
298299
300+ currentPagination . next ( Object . assign ( new PaginationComponentOptions ( ) , pagination , { currentPage : 3 } ) ) ;
301+ testFixture . detectChanges ( ) ;
302+
299303 changePage ( testFixture , 0 ) ;
300304 tick ( ) ;
301305 expect ( paginationService . updateRoute ) . toHaveBeenCalledWith ( 'test' , Object . assign ( { page : 2 } ) , { } , false ) ;
@@ -482,6 +486,58 @@ describe('Pagination component', () => {
482486 expect ( input ) . toBeDefined ( ) ;
483487 expect ( input . nativeElement . disabled ) . toBeFalse ( ) ;
484488 } ) ;
489+
490+ describe ( 'selectPage' , ( ) => {
491+ // collectionSize (200) / pageSize (10) => 20 pages
492+ let paginationServiceStub : any ;
493+
494+ beforeEach ( ( ) => {
495+ paginationServiceStub = TestBed . inject ( PaginationService ) ;
496+ spyOn ( paginationServiceStub , 'updateRoute' ) . and . callThrough ( ) ;
497+ spyOn ( component . pageChange , 'emit' ) ;
498+ spyOn ( component . paginationChange , 'emit' ) ;
499+ } ) ;
500+
501+ it ( 'should navigate to the requested page when it is within range' , ( ) => {
502+ component . selectPage ( '7' ) ;
503+ expect ( paginationServiceStub . updateRoute ) . toHaveBeenCalledWith ( 'test' , { page : 7 } , { } , false ) ;
504+ expect ( component . pageChange . emit ) . toHaveBeenCalledWith ( 7 ) ;
505+ } ) ;
506+
507+ it ( 'should clamp a page number that is too high to the last page' , ( ) => {
508+ component . selectPage ( '999' ) ;
509+ expect ( paginationServiceStub . updateRoute ) . toHaveBeenCalledWith ( 'test' , { page : 20 } , { } , false ) ;
510+ expect ( component . pageChange . emit ) . toHaveBeenCalledWith ( 20 ) ;
511+ } ) ;
512+
513+ it ( 'should clamp a page number below 1 to the first page' , ( ) => {
514+ component . selectPage ( '0' ) ;
515+ expect ( paginationServiceStub . updateRoute ) . toHaveBeenCalledWith ( 'test' , { page : 1 } , { } , false ) ;
516+ expect ( component . pageChange . emit ) . toHaveBeenCalledWith ( 1 ) ;
517+ } ) ;
518+
519+ it ( 'should ignore non-numeric input' , ( ) => {
520+ component . selectPage ( 'abc' ) ;
521+ expect ( paginationServiceStub . updateRoute ) . not . toHaveBeenCalled ( ) ;
522+ expect ( component . pageChange . emit ) . not . toHaveBeenCalled ( ) ;
523+ } ) ;
524+
525+ it ( 'should emit a paginationChange event so the results are reloaded' , ( ) => {
526+ component . selectPage ( '3' ) ;
527+ expect ( component . paginationChange . emit ) . toHaveBeenCalled ( ) ;
528+ } ) ;
529+
530+ it ( 'should not emit a paginationChange event for invalid input' , ( ) => {
531+ component . selectPage ( 'abc' ) ;
532+ expect ( component . paginationChange . emit ) . not . toHaveBeenCalled ( ) ;
533+ } ) ;
534+
535+ it ( 'should close the popover when one is provided' , ( ) => {
536+ const popover = jasmine . createSpyObj ( 'NgbPopover' , [ 'close' ] ) ;
537+ component . selectPage ( '3' , popover ) ;
538+ expect ( popover . close ) . toHaveBeenCalled ( ) ;
539+ } ) ;
540+ } ) ;
485541 } ) ;
486542
487543} ) ;
0 commit comments