11/*global angular,inject,describe,it,jasmine,expect,beforeEach,module*/
2+ const directive = require ( '../../../../ng-admin/Crud/list/maDatagridInfinitePagination' ) ;
3+
24describe ( 'directive: ma-datagrid-infinite-pagination' , function ( ) {
3- var directive = require ( '../../../../ng-admin/Crud/list/maDatagridInfinitePagination' ) ,
4- $compile ,
5- scope ,
6- $window ,
7- $document ,
8- element ,
9- bodyHeightMock ,
10- pageSize = 2000 ,
11- directiveUsage = '<ma-datagrid-infinite-pagination next-page="nextPage" total-items="{{ totalItems }}" per-page="{{ itemsPerPage }}"></ma-datagrid-infinite-pagination>' ;
12-
13- function initializeBodyHeightMock ( ) {
14- if ( ! angular . element ( $document [ 0 ] . querySelector ( '#mock' ) ) . length ) {
5+ let $compile ;
6+ let $scope ;
7+ let $window ;
8+ let $document ;
9+ let element ;
10+ let bodyHeightMock ;
11+ let handler ;
12+ let pageSize = 2000 ;
13+ let directiveUsage = `<ma-datagrid-infinite-pagination
14+ next-page="nextPage"
15+ total-items="{{ totalItems }}"
16+ per-page="{{ itemsPerPage }}"
17+ ></ma-datagrid-infinite-pagination>` ;
18+
19+ function waitForProcessing ( scope , callback ) {
20+ const interval = setInterval ( ( ) => {
21+ if ( ! scope . processing ) {
22+ clearInterval ( interval ) ;
23+ callback ( null , true ) ;
24+ }
25+ } , 100 ) ;
26+ }
27+
28+ function initializeBodyHeightMock ( ) {
29+ if ( ! angular . element ( $document [ 0 ] . querySelector ( '#mock' ) ) . length ) {
1530 bodyHeightMock = angular . element ( `<div id="mock" style="height:${ pageSize } px"></div>` ) [ 0 ] ;
1631 angular . element ( $document [ 0 ] . body ) . append ( bodyHeightMock ) ;
17- } else {
32+ } else {
1833 simulateLoadOnBodyHeight ( 1 ) ;
1934 }
2035 }
2136
22- function simulateLoadOnBodyHeight ( page ) {
37+ function simulateLoadOnBodyHeight ( page ) {
2338 angular . element ( $document [ 0 ] . querySelector ( '#mock' ) ) . css ( 'height' , ( pageSize * page ) + 'px' ) ;
2439 }
2540
26- function simulateScrollToPage ( page ) {
27- $window . scrollY = pageSize * ( page - 1 ) + 1500 ;
28- angular . element ( $window ) . triggerHandler ( 'scroll' ) ;
41+ function simulateScrollToPage ( page , scope , callback ) {
42+ const scrollSize = pageSize * ( page - 1 ) + 1500 ;
43+ $window . scrollY = scrollSize ;
44+ handler ( { deltaY : scrollSize } ) ;
45+
46+ if ( scope && callback ) {
47+ waitForProcessing ( scope , callback ) ;
48+ }
2949 }
3050
31- function initializeScope ( ) {
32- scope . nextPage = jasmine . createSpy ( 'nextPage' ) . and . callFake ( function ( page ) {
51+ function initializeScope ( scope ) {
52+ scope . nextPage = jasmine . createSpy ( 'nextPage' ) . and . callFake ( ( ) => ( page ) => {
3353 simulateLoadOnBodyHeight ( page ) ;
3454 } ) ;
3555 scope . totalItems = 100 ;
3656 scope . itemsPerPage = 10 ;
3757 }
3858
39- function initializeElement ( ) {
40- element = $compile ( directiveUsage ) ( scope ) ;
41- scope . $digest ( ) ;
59+ function initializeElement ( ) {
60+ initializeScope ( $scope ) ;
61+ element = $compile ( directiveUsage ) ( $scope ) ;
62+ $scope . $digest ( ) ;
4263 }
4364
4465 angular . module ( 'testapp_DatagridInfinitePagination' , [ ] )
@@ -48,45 +69,82 @@ describe('directive: ma-datagrid-infinite-pagination', function () {
4869
4970 beforeEach ( inject ( function ( _$compile_ , _$rootScope_ , _$window_ , _$document_ ) {
5071 $compile = _$compile_ ;
51- scope = _$rootScope_ . $new ( ) ;
72+ $ scope = _$rootScope_ . $new ( ) ;
5273 $window = _$window_ ;
5374 $window . innerHeight = 759 ;
75+ spyOn ( $window , 'addEventListener' ) . and . callFake ( ( evt , callback ) => {
76+ handler = callback ;
77+ } ) ;
5478 $document = _$document_ ;
5579 initializeBodyHeightMock ( ) ;
56- initializeScope ( ) ;
5780 initializeElement ( ) ;
5881 } ) ) ;
5982
60- it ( 'should trigger next-page when scrolling' , function ( ) {
61- simulateScrollToPage ( 2 ) ;
62- expect ( scope . nextPage ) . toHaveBeenCalled ( ) ;
63- } ) ;
83+ it ( 'should trigger next-page when scrolling' , function ( done ) {
84+ const isolatedScope = element . isolateScope ( ) ;
85+ initializeScope ( isolatedScope ) ;
6486
65- it ( 'should trigger next-page twice when scrolling twice' , function ( ) {
66- simulateScrollToPage ( 2 ) ;
67- simulateScrollToPage ( 3 ) ;
68- expect ( scope . nextPage . calls . count ( ) ) . toEqual ( 2 ) ;
87+ waitForProcessing ( isolatedScope , ( ) => {
88+ simulateScrollToPage ( 2 , isolatedScope , ( ) => {
89+ expect ( isolatedScope . nextPage ) . toHaveBeenCalled ( ) ;
90+ done ( ) ;
91+ } ) ;
92+ } ) ;
6993 } ) ;
7094
71- it ( 'should trigger next-page with right page number' , function ( ) {
72- simulateScrollToPage ( 2 ) ;
73- simulateScrollToPage ( 3 ) ;
74- expect ( scope . nextPage . calls . argsFor ( 0 ) ) . toEqual ( [ 2 ] ) ;
75- expect ( scope . nextPage . calls . argsFor ( 1 ) ) . toEqual ( [ 3 ] ) ;
95+ it ( 'should trigger next-page twice when scrolling twice' , function ( done ) {
96+ const isolatedScope = element . isolateScope ( ) ;
97+ initializeScope ( isolatedScope ) ;
98+
99+ waitForProcessing ( isolatedScope , ( ) => {
100+ simulateScrollToPage ( 2 , isolatedScope , ( ) => {
101+ simulateScrollToPage ( 3 , isolatedScope , ( ) => {
102+ expect ( isolatedScope . nextPage . calls . count ( ) ) . toEqual ( 3 ) ;
103+ done ( ) ;
104+ } ) ;
105+ } ) ;
106+ } ) ;
76107 } ) ;
77108
78- it ( 'should not trigger next-page if not scrolling' , function ( ) {
79- expect ( scope . nextPage ) . not . toHaveBeenCalled ( ) ;
109+ it ( 'should trigger next-page with right page number' , function ( done ) {
110+ const isolatedScope = element . isolateScope ( ) ;
111+ initializeScope ( isolatedScope ) ;
112+
113+ const argsForCall = [ ] ;
114+
115+ isolatedScope . nextPage = jasmine . createSpy ( 'nextPage' ) . and . callFake ( ( ) => ( page ) => {
116+ simulateLoadOnBodyHeight ( page ) ;
117+ argsForCall . push ( page ) ;
118+ } ) ;
119+
120+ waitForProcessing ( isolatedScope , ( ) => {
121+ simulateScrollToPage ( 2 , isolatedScope , ( ) => {
122+ simulateScrollToPage ( 3 , isolatedScope , ( ) => {
123+ expect ( argsForCall [ 0 ] ) . toEqual ( 2 ) ;
124+ expect ( argsForCall [ 1 ] ) . toEqual ( 3 ) ;
125+ done ( ) ;
126+ } ) ;
127+ } ) ;
128+ } ) ;
80129 } ) ;
81130
82- it ( 'should not trigger next-page when scrolling up' , function ( ) {
83- simulateScrollToPage ( 2 ) ;
84- simulateScrollToPage ( 3 ) ;
85- simulateScrollToPage ( 2 ) ;
86- expect ( scope . nextPage . calls . count ( ) ) . toEqual ( 2 ) ;
131+ it ( 'should not trigger next-page when scrolling up' , function ( done ) {
132+ const isolatedScope = element . isolateScope ( ) ;
133+ initializeScope ( isolatedScope ) ;
134+
135+ waitForProcessing ( isolatedScope , ( ) => {
136+ simulateScrollToPage ( 2 , isolatedScope , ( ) => {
137+ simulateScrollToPage ( 3 , isolatedScope , ( ) => {
138+ simulateScrollToPage ( 2 , isolatedScope , ( ) => {
139+ expect ( isolatedScope . nextPage . calls . count ( ) ) . toEqual ( 3 ) ;
140+ done ( ) ;
141+ } ) ;
142+ } ) ;
143+ } ) ;
144+ } ) ;
87145 } ) ;
88146
89- afterEach ( function ( ) {
90- scope . $destroy ( ) ;
147+ afterEach ( function ( ) {
148+ $ scope. $destroy ( ) ;
91149 } ) ;
92150} ) ;
0 commit comments