@@ -10,6 +10,8 @@ describe('gridster directive', function() {
1010 var startCount ;
1111 var resizeCount ;
1212 var stopCount ;
13+ var rootScope ;
14+ var broadcastOnRootScope ;
1315
1416 var dragHelper = function ( el , dx , dy ) {
1517 el . simulate ( 'mouseover' ) . simulate ( 'drag' , {
@@ -20,6 +22,9 @@ describe('gridster directive', function() {
2022 } ;
2123
2224 beforeEach ( inject ( function ( $rootScope , $compile ) {
25+ rootScope = $rootScope ;
26+ broadcastOnRootScope = spyOn ( rootScope , '$broadcast' ) . and . callThrough ( ) ;
27+
2328 $scope = $rootScope . $new ( ) ;
2429 startCount = resizeCount = stopCount = 0 ;
2530
@@ -89,31 +94,49 @@ describe('gridster directive', function() {
8994
9095 it ( 'should initialize resizable' , function ( ) {
9196 var $widget = $el . find ( 'li:first-child' ) ;
92- setTimeout ( function ( ) {
93- expect ( $widget . find ( '.handle-n' ) . length ) . toBe ( 1 ) ;
94- } ) ;
97+
98+ expect ( $widget . find ( '.handle-n' ) . length ) . toBe ( 1 ) ;
9599 } ) ;
96100
97101 it ( 'should update widget dimensions on resize & trigger custom resize events' , function ( ) {
102+ var $widget = $el . find ( 'li:first-child' ) ;
103+ var handle = $widget . find ( '.handle-e' ) ;
98104
99- setTimeout ( function ( ) {
100- var $widget = $el . find ( 'li:first-child' ) ;
101- var handle = $widget . find ( '.handle-e' ) ;
105+ expect ( $widget . width ( ) ) . toBe ( 155 ) ;
106+ expect ( $scope . dashboard . widgets [ 0 ] . sizeX ) . toBe ( 1 ) ;
107+ expect ( startCount ) . toBe ( 0 ) ;
108+ expect ( resizeCount ) . toBe ( 0 ) ;
109+ expect ( stopCount ) . toBe ( 0 ) ;
102110
103- expect ( $widget . width ( ) ) . toBe ( 155 ) ;
104- expect ( $scope . dashboard . widgets [ 0 ] . sizeX ) . toBe ( 1 ) ;
105- expect ( startCount ) . toBe ( 0 ) ;
106- expect ( resizeCount ) . toBe ( 0 ) ;
107- expect ( stopCount ) . toBe ( 0 ) ;
111+ dragHelper ( handle , 50 ) ; // should resize to next width step
108112
109- dragHelper ( handle , 50 ) ; // should resize to next width step
113+ expect ( $widget . width ( ) ) . toBe ( 320 ) ;
114+ expect ( $scope . dashboard . widgets [ 0 ] . sizeX ) . toBe ( 2 ) ;
115+ expect ( startCount ) . toBe ( 1 ) ;
116+ expect ( resizeCount ) . toBe ( 1 ) ;
117+ expect ( stopCount ) . toBe ( 1 ) ;
118+ } ) ;
110119
111- expect ( $widget . width ( ) ) . toBe ( 320 ) ;
112- expect ( $scope . dashboard . widgets [ 0 ] . sizeX ) . toBe ( 2 ) ;
113- expect ( startCount ) . toBe ( 1 ) ;
114- expect ( resizeCount ) . toBe ( 1 ) ;
115- expect ( stopCount ) . toBe ( 1 ) ;
116- } ) ;
120+ it ( 'should broadcast "size changed" event on resize' , function ( ) {
121+ // arrange
122+ var eHandle = $el . find ( 'li:first-child' ) . find ( '.handle-e' ) ;
123+ var sHandle = $el . find ( 'li:first-child' ) . find ( '.handle-s' ) ;
124+ broadcastOnRootScope . calls . reset ( ) ;
125+
126+ // act
127+ dragHelper ( eHandle , 50 ) ;
128+
129+ // assert
130+ expect ( broadcastOnRootScope ) . toHaveBeenCalledWith ( 'gridster-item-size-changed' , 2 , 1 ) ;
131+
132+ // arrange
133+ broadcastOnRootScope . calls . reset ( ) ;
134+
135+ // act
136+ dragHelper ( sHandle , 0 , 50 ) ;
137+
138+ // assert
139+ expect ( broadcastOnRootScope ) . toHaveBeenCalledWith ( 'gridster-item-size-changed' , 2 , 2 ) ;
117140 } ) ;
118141
119142} ) ;
0 commit comments