@@ -37,6 +37,104 @@ describe('position elements', function () {
37
37
} ) ;
38
38
} ) ;
39
39
40
+ describe ( 'offset' , function ( ) {
41
+ var $document ;
42
+
43
+ beforeEach ( inject ( function ( _$document_ ) {
44
+ $document = _$document_ ;
45
+ } ) ) ;
46
+
47
+ it ( 'returns getBoundingClientRect by default' , function ( ) {
48
+ var el = angular . element ( '<div>Foo</div>' ) ;
49
+
50
+ /* getBoundingClientRect values will be based on the testing Chrome window
51
+ so that makes this tests very brittle if we don't mock */
52
+ spyOn ( el [ 0 ] , 'getBoundingClientRect' ) . and . returnValue ( {
53
+ width : 100 ,
54
+ height : 100 ,
55
+ top : 2 ,
56
+ left : 2
57
+ } ) ;
58
+ $document . find ( 'body' ) . append ( el ) ;
59
+
60
+ var offset = $position . offset ( el ) ;
61
+
62
+ expect ( offset ) . toEqual ( {
63
+ width : 100 ,
64
+ height : 100 ,
65
+ top : 2 ,
66
+ left : 2
67
+ } ) ;
68
+
69
+ el . remove ( ) ;
70
+ } ) ;
71
+ } ) ;
72
+
73
+ describe ( 'position' , function ( ) {
74
+ var $document , el ;
75
+
76
+ beforeEach ( inject ( function ( _$document_ ) {
77
+ $document = _$document_ ;
78
+ } ) ) ;
79
+
80
+ afterEach ( function ( ) {
81
+ el . remove ( ) ;
82
+ } ) ;
83
+
84
+ it ( 'gets position with document as the relative parent' , function ( ) {
85
+ el = angular . element ( '<div>Foo</div>' ) ;
86
+
87
+ spyOn ( el [ 0 ] , 'getBoundingClientRect' ) . and . returnValue ( {
88
+ width : 100 ,
89
+ height : 100 ,
90
+ top : 2 ,
91
+ left : 2
92
+ } ) ;
93
+
94
+ $document . find ( 'body' ) . append ( el ) ;
95
+
96
+ var position = $position . position ( el ) ;
97
+
98
+ expect ( position ) . toEqual ( {
99
+ width : 100 ,
100
+ height : 100 ,
101
+ top : 2 ,
102
+ left : 2
103
+ } ) ;
104
+ } ) ;
105
+
106
+ it ( 'gets position with another DOM as the relative parent' , function ( ) {
107
+ el = angular . element ( '<div id="outer" style="position:relative;"><div id="inner">Foo</div></div>' ) ;
108
+
109
+ $document . find ( 'body' ) . append ( el ) ;
110
+
111
+ var outerEl = angular . element ( document . getElementById ( 'outer' ) ) ;
112
+ var innerEl = angular . element ( document . getElementById ( 'inner' ) ) ;
113
+
114
+ spyOn ( outerEl [ 0 ] , 'getBoundingClientRect' ) . and . returnValue ( {
115
+ width : 100 ,
116
+ height : 100 ,
117
+ top : 2 ,
118
+ left : 2
119
+ } ) ;
120
+ spyOn ( innerEl [ 0 ] , 'getBoundingClientRect' ) . and . returnValue ( {
121
+ width : 20 ,
122
+ height : 20 ,
123
+ top : 5 ,
124
+ left : 5
125
+ } ) ;
126
+
127
+ var position = $position . position ( innerEl ) ;
128
+
129
+ expect ( position ) . toEqual ( {
130
+ width : 20 ,
131
+ height : 20 ,
132
+ top : 3 ,
133
+ left : 3
134
+ } ) ;
135
+ } ) ;
136
+ } ) ;
137
+
40
138
describe ( 'append-to-body: false' , function ( ) {
41
139
beforeEach ( function ( ) {
42
140
//mock position info normally queried from the DOM
@@ -103,4 +201,71 @@ describe('position elements', function () {
103
201
expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'right-bottom' ) ) . toBePositionedAt ( 120 , 120 ) ;
104
202
} ) ;
105
203
} ) ;
204
+
205
+ describe ( 'append-to-body: true' , function ( ) {
206
+ beforeEach ( function ( ) {
207
+ //mock offset info normally queried from the DOM
208
+ $position . offset = function ( ) {
209
+ return {
210
+ width : 20 ,
211
+ height : 20 ,
212
+ top : 100 ,
213
+ left : 100
214
+ } ;
215
+ } ;
216
+ } ) ;
217
+
218
+ it ( 'should position element on top-center by default' , function ( ) {
219
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'other' , true ) ) . toBePositionedAt ( 90 , 105 ) ;
220
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'top' , true ) ) . toBePositionedAt ( 90 , 105 ) ;
221
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'top-center' , true ) ) . toBePositionedAt ( 90 , 105 ) ;
222
+ } ) ;
223
+
224
+ it ( 'should position on top-left' , function ( ) {
225
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'top-left' , true ) ) . toBePositionedAt ( 90 , 100 ) ;
226
+ } ) ;
227
+
228
+ it ( 'should position on top-right' , function ( ) {
229
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'top-right' , true ) ) . toBePositionedAt ( 90 , 120 ) ;
230
+ } ) ;
231
+
232
+ it ( 'should position elements on bottom-center when "bottom" specified' , function ( ) {
233
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'bottom' , true ) ) . toBePositionedAt ( 120 , 105 ) ;
234
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'bottom-center' , true ) ) . toBePositionedAt ( 120 , 105 ) ;
235
+ } ) ;
236
+
237
+ it ( 'should position elements on bottom-left' , function ( ) {
238
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'bottom-left' , true ) ) . toBePositionedAt ( 120 , 100 ) ;
239
+ } ) ;
240
+
241
+ it ( 'should position elements on bottom-right' , function ( ) {
242
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'bottom-right' , true ) ) . toBePositionedAt ( 120 , 120 ) ;
243
+ } ) ;
244
+
245
+ it ( 'should position elements on left-center when "left" specified' , function ( ) {
246
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'left' , true ) ) . toBePositionedAt ( 105 , 90 ) ;
247
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'left-center' , true ) ) . toBePositionedAt ( 105 , 90 ) ;
248
+ } ) ;
249
+
250
+ it ( 'should position elements on left-top when "left-top" specified' , function ( ) {
251
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'left-top' , true ) ) . toBePositionedAt ( 100 , 90 ) ;
252
+ } ) ;
253
+
254
+ it ( 'should position elements on left-bottom when "left-bottom" specified' , function ( ) {
255
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'left-bottom' , true ) ) . toBePositionedAt ( 120 , 90 ) ;
256
+ } ) ;
257
+
258
+ it ( 'should position elements on right-center when "right" specified' , function ( ) {
259
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'right' , true ) ) . toBePositionedAt ( 105 , 120 ) ;
260
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'right-center' , true ) ) . toBePositionedAt ( 105 , 120 ) ;
261
+ } ) ;
262
+
263
+ it ( 'should position elements on right-top when "right-top" specified' , function ( ) {
264
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'right-top' , true ) ) . toBePositionedAt ( 100 , 120 ) ;
265
+ } ) ;
266
+
267
+ it ( 'should position elements on right-top when "right-top" specified' , function ( ) {
268
+ expect ( $position . positionElements ( { } , new TargetElMock ( 10 , 10 ) , 'right-bottom' , true ) ) . toBePositionedAt ( 120 , 120 ) ;
269
+ } ) ;
270
+ } ) ;
106
271
} ) ;
0 commit comments