@@ -9,6 +9,13 @@ import wait from 'ember-test-helpers/wait';
9
9
10
10
import { setupTableStickyPolyfill } from 'ember-table/-private/sticky/table-sticky-polyfill' ;
11
11
12
+ const HEADER_PIXEL_EPSILON = 10 ;
13
+ const FOOTER_PIXEL_EPSILON = 10 ;
14
+
15
+ function isNearTo ( value , expected , epsilon = 0.01 ) {
16
+ return Math . abs ( value - expected ) <= epsilon ;
17
+ }
18
+
12
19
const standardTemplate = hbs `
13
20
<div style="height: 500px;">
14
21
<div class="ember-table">
@@ -45,14 +52,14 @@ const standardTemplate = hbs`
45
52
</div>
46
53
` ;
47
54
48
- function constructMatrix ( n , m ) {
55
+ function constructMatrix ( n , m , prefix = '' ) {
49
56
let rows = emberA ( ) ;
50
57
51
58
for ( let i = 0 ; i < n ; i ++ ) {
52
59
let cols = emberA ( ) ;
53
60
54
61
for ( let j = 0 ; j < m ; j ++ ) {
55
- cols . pushObject ( m ) ;
62
+ cols . pushObject ( ` ${ m } ${ prefix } ` ) ;
56
63
}
57
64
58
65
rows . pushObject ( cols ) ;
@@ -70,7 +77,7 @@ function verifyHeader(assert) {
70
77
let cellRect = cell . getBoundingClientRect ( ) ;
71
78
let containerRect = find ( '.ember-table' ) . getBoundingClientRect ( ) ;
72
79
73
- assert . ok ( Math . abs ( cellRect . top - containerRect . top - expectedOffset ) < 10 ) ;
80
+ assert . ok ( Math . abs ( cellRect . top - containerRect . top - expectedOffset ) < HEADER_PIXEL_EPSILON ) ;
74
81
}
75
82
} ) ;
76
83
}
@@ -86,16 +93,18 @@ function verifyFooter(assert) {
86
93
let cellRect = cell . getBoundingClientRect ( ) ;
87
94
let containerRect = find ( '.ember-table' ) . getBoundingClientRect ( ) ;
88
95
89
- assert . ok ( Math . abs ( containerRect . bottom - cellRect . bottom - expectedOffset ) < 10 ) ;
96
+ assert . ok (
97
+ Math . abs ( containerRect . bottom - cellRect . bottom - expectedOffset ) < FOOTER_PIXEL_EPSILON
98
+ ) ;
90
99
}
91
100
} ) ;
92
101
}
93
102
94
103
componentModule ( 'Unit | Private | TableStickyPolyfill' , function ( ) {
95
104
test ( 'it works' , async function ( assert ) {
96
- this . set ( 'headerRows' , constructMatrix ( 3 , 3 ) ) ;
97
- this . set ( 'bodyRows' , constructMatrix ( 20 , 3 ) ) ;
98
- this . set ( 'footerRows' , constructMatrix ( 3 , 3 ) ) ;
105
+ this . set ( 'headerRows' , constructMatrix ( 3 , 3 , 'thead' ) ) ;
106
+ this . set ( 'bodyRows' , constructMatrix ( 20 , 3 , 'tbody' ) ) ;
107
+ this . set ( 'footerRows' , constructMatrix ( 3 , 3 , 'tfoot' ) ) ;
99
108
100
109
await this . render ( standardTemplate ) ;
101
110
@@ -175,4 +184,113 @@ componentModule('Unit | Private | TableStickyPolyfill', function() {
175
184
verifyHeader ( assert ) ;
176
185
verifyFooter ( assert ) ;
177
186
} ) ;
187
+
188
+ test ( 'maxStickyProportion: when the footer is > 50% of the height' , async function ( assert ) {
189
+ let maxStickyProportion = 0.5 ;
190
+ this . set ( 'headerRows' , constructMatrix ( 3 , 3 , 'header' ) ) ;
191
+ this . set ( 'bodyRows' , constructMatrix ( 30 , 3 , 'body' ) ) ;
192
+ this . set ( 'footerRows' , constructMatrix ( 30 , 3 , 'footer' ) ) ;
193
+
194
+ await this . render ( standardTemplate ) ;
195
+
196
+ setupTableStickyPolyfill ( find ( 'thead' ) ) ;
197
+ setupTableStickyPolyfill ( find ( 'tfoot' ) ) ;
198
+
199
+ await wait ( ) ;
200
+
201
+ let firstCell = find ( 'tfoot tr:first-child td:first-child' ) ;
202
+ let lastCell = find ( 'tfoot tr:last-child td:first-child' ) ;
203
+ let container = find ( '.ember-table' ) ;
204
+
205
+ let firstCellRect = firstCell . getBoundingClientRect ( ) ;
206
+ let lastCellRect = lastCell . getBoundingClientRect ( ) ;
207
+ let containerRect = container . getBoundingClientRect ( ) ;
208
+
209
+ assert . ok (
210
+ find ( 'tfoot' ) . getBoundingClientRect ( ) . height > maxStickyProportion * containerRect . height ,
211
+ 'precond - footer is > 50% of the table height'
212
+ ) ;
213
+
214
+ assert . ok (
215
+ isNearTo ( ( firstCellRect . top - containerRect . top ) / containerRect . height , maxStickyProportion ) ,
216
+ 'the top of the first footer cell is close to 50% of the way up the table'
217
+ ) ;
218
+
219
+ assert . ok (
220
+ isNearTo (
221
+ ( containerRect . bottom - firstCellRect . top ) / containerRect . height ,
222
+ maxStickyProportion
223
+ ) ,
224
+ 'the top of the first footer cell is close to 50% of the way down the table'
225
+ ) ;
226
+
227
+ assert . ok ( lastCellRect . top > containerRect . bottom , 'last footer cell is out of view' ) ;
228
+
229
+ await scrollTo ( '.ember-table' , 0 , container . scrollHeight ) ;
230
+
231
+ // Recompute dimensions
232
+ lastCellRect = lastCell . getBoundingClientRect ( ) ;
233
+ containerRect = container . getBoundingClientRect ( ) ;
234
+
235
+ assert . equal (
236
+ lastCellRect . bottom ,
237
+ containerRect . bottom ,
238
+ 'after scroll, last footer cell is at bottom of table'
239
+ ) ;
240
+ } ) ;
241
+
242
+ test ( 'maxStickyProportion: when the header > 50% of the height' , async function ( assert ) {
243
+ let maxStickyProportion = 0.5 ;
244
+ this . set ( 'headerRows' , constructMatrix ( 30 , 3 , 'header' ) ) ;
245
+ this . set ( 'bodyRows' , constructMatrix ( 30 , 3 , 'body' ) ) ;
246
+ this . set ( 'footerRows' , constructMatrix ( 3 , 3 , 'footer' ) ) ;
247
+
248
+ await this . render ( standardTemplate ) ;
249
+
250
+ setupTableStickyPolyfill ( find ( 'thead' ) ) ;
251
+ setupTableStickyPolyfill ( find ( 'tfoot' ) ) ;
252
+
253
+ await wait ( ) ;
254
+
255
+ let firstCell = find ( 'thead tr:first-child th:first-child' ) ;
256
+ let lastCell = find ( 'thead tr:last-child th:first-child' ) ;
257
+ let container = find ( '.ember-table' ) ;
258
+
259
+ let firstCellRect = firstCell . getBoundingClientRect ( ) ;
260
+ let lastCellRect = lastCell . getBoundingClientRect ( ) ;
261
+ let containerRect = container . getBoundingClientRect ( ) ;
262
+
263
+ assert . ok (
264
+ find ( 'thead' ) . getBoundingClientRect ( ) . height > maxStickyProportion * containerRect . height ,
265
+ 'precond - header is > 50% of the table height'
266
+ ) ;
267
+
268
+ assert . equal (
269
+ firstCellRect . top ,
270
+ containerRect . top ,
271
+ 'top of first header cell is at top of table'
272
+ ) ;
273
+ assert . ok ( lastCellRect . top > containerRect . bottom , 'last header cell is out of view' ) ;
274
+
275
+ await scrollTo ( '.ember-table' , 0 , container . scrollHeight ) ;
276
+
277
+ // recompute dimensions
278
+ lastCellRect = lastCell . getBoundingClientRect ( ) ;
279
+ containerRect = container . getBoundingClientRect ( ) ;
280
+
281
+ assert . ok (
282
+ isNearTo (
283
+ ( lastCellRect . bottom - containerRect . top ) / containerRect . height ,
284
+ maxStickyProportion
285
+ ) ,
286
+ 'the bottom of the last header cell is close to 50% of the way up the table'
287
+ ) ;
288
+ assert . ok (
289
+ isNearTo (
290
+ ( containerRect . bottom - lastCellRect . bottom ) / containerRect . height ,
291
+ maxStickyProportion
292
+ ) ,
293
+ 'the bottom of the last header cell is close to 50% of the way down the table'
294
+ ) ;
295
+ } ) ;
178
296
} ) ;
0 commit comments