-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.coffee
742 lines (474 loc) · 14.5 KB
/
test.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
describe 'angular-sticky-table-header', ->
options =
cloneClassName: 'sticky-clone'
stuckClassName: 'sticky-stuck'
interval: 10
window._ =
debounce: (fn) -> fn
throttle: (fn) -> fn
$window =
scrollX: 0
scrollY: 0
on: ->
off: ->
beforeEach (module 'turn/stickyTableHeader'), ($provide) ->
$provide.value 'options', options
$provide.value '$window', $window
null
beforeEach ->
inject ($compile, $rootScope) =>
@scope = do $rootScope.$new
angular.extend @scope,
columnCollection: ['foo', 'bar', 'baz']
rowCollection: (Array.apply null, Array 200).map -> ['moo', 'woo', 'zoo']
@element = angular.element """
<div sticky-table-header rows="rowCollection">
<table class="table">
<thead>
<tr>
<th ng-repeat="th in columnCollection">{{th}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="tr in rowCollection">
<td ng-repeat="td in tr">{{td}}</td>
</tr>
</tbody>
</table>
</div>
"""
($compile @element) @scope
do @scope.$digest
@scope = do @element.scope
$window =
scrollY: 0
on: ->
off: ->
#########################################
describe '#createClone', ->
it 'should clone the first <tr> it finds and append it to the <thead>', ->
expect (@element.find 'thead tr').length
.toBe 1
do @scope.createClone
expect (@element.find 'thead tr').length
.toBe 2
it 'should clone the <tr>\'s contents', ->
do @scope.createClone
expect ($((@element.find 'thead tr')[1]).find 'th').length
.toBe @scope.$parent.columnCollection.length
it 'should clone the <tr>\'s events', ->
mock =
fn: ->
spyOn mock, 'fn'
$ (@element.find 'thead tr')[0]
.find 'th'
.on 'click', mock.fn
clone = do @scope.createClone
$ (@element.find 'thead tr')[1]
.find 'th'
.click()
expect mock.fn
.toHaveBeenCalled()
it 'should mirror the original <tr>\'s className', ->
@element.find 'thead tr'
.addClass 'test'
do @scope.createClone
expect $((@element.find 'thead tr')[1]).hasClass 'test'
.toBe true
it 'should assign the clone the className defined in options.cloneClassName', ->
do @scope.createClone
expect $((@element.find 'thead tr')[1]).hasClass options.cloneClassName
.toBe true
describe '#resetClone', ->
it 'should call #removeClones, #createClone, and #sizeClone', inject ($timeout) ->
spyOn @scope, 'removeClones'
spyOn @scope, 'createClone'
spyOn @scope, 'sizeClone'
do @scope.resetClone
do $timeout.flush
expect @scope.removeClones
.toHaveBeenCalled()
expect @scope.createClone
.toHaveBeenCalled()
expect @scope.sizeClone
.toHaveBeenCalled()
it 'should set scope.clone to the value returned by #createClone', ->
@scope.clone = null
@scope.removeClones = ->
@scope.createClone = -> 42
@scope.sizeClone = ->
do @scope.resetClone
expect @scope.clone
.toBe 42
describe '#removeClones', ->
it 'should set scope.stuck to false', ->
do @scope.createClone
do @scope.removeClones
expect @scope.stuck
.toBe false
it 'should remove all <tr> clones', ->
do @scope.createClone
do @scope.createClone
do @scope.createClone
expect (@element.find '.' + options.cloneClassName).length
.toBe 3
do @scope.removeClones
expect (@element.find '.' + options.cloneClassName).length
.toBe 0
# TODO: add tests for #setClonedCellWidths
describe '#setCloneGutter', ->
it 'should set the <th> clone\'s width CSS equal to scope.offset', ->
@scope.clone =
css: ->
@scope.offset =
width: 2
spyOn @scope.clone, 'css'
do @scope.setCloneGutter
expect @scope.clone.css
.toHaveBeenCalledWith @scope.offset
describe '#setOffset', ->
table = null
beforeEach ->
table = (@element.find 'table').get 0
spyOn table, 'getBoundingClientRect'
.andReturn
width: 'bar'
top: 'zoo'
spyOn (do $).__proto__, 'offset'
.andReturn
width: 'woo'
top: 'moo'
@scope.offset = null
do @scope.setOffset
it 'should call getBoundingClientRect on the first <tr>', ->
do expect table.getBoundingClientRect
.toHaveBeenCalledWith
it 'should call $.offset on the first <tr>', ->
do expect (do $).__proto__.offset
.toHaveBeenCalledWith
it 'should set scope.offset equal to the width returned by getBoundingClientRect and the top returned by $.offset', ->
expect @scope.offset
.toEqual
width: 'bar'
top: 'moo'
describe '#setStuck', ->
it 'should set scope.stuck equal to the boolean passed into it', ->
@scope.stuck = null
@scope.setStuck true
expect @scope.stuck
.toBe true
it 'should coerce non-boolean values into booleans', ->
@scope.setStuck true
expect @scope.stuck
.toBe true
@scope.setStuck 'foo'
expect @scope.stuck
.toBe true
@scope.setStuck 42
expect @scope.stuck
.toBe true
@scope.setStuck null
expect @scope.stuck
.toBe false
@scope.setStuck 0
expect @scope.stuck
.toBe false
@scope.setStuck false
expect @scope.stuck
.toBe false
it 'should call #toggleClone', ->
spyOn @scope, 'toggleClone'
@scope.setStuck true
expect @scope.toggleClone
.toHaveBeenCalledWith true
@scope.setStuck false
expect @scope.toggleClone
.toHaveBeenCalledWith false
describe '#toggleClone', ->
it 'should toggle options.stuckClassName on the clone', ->
@scope.clone =
addClass: ->
removeClass: ->
spyOn @scope.clone, 'addClass'
spyOn @scope.clone, 'removeClass'
@scope.toggleClone true
expect @scope.clone.addClass
.toHaveBeenCalledWith options.stuckClassName
@scope.toggleClone false
expect @scope.clone.removeClass
.toHaveBeenCalledWith options.stuckClassName
it 'should coerce non-boolean values into booleans', ->
@scope.clone =
addClass: ->
removeClass: ->
spyOn @scope.clone, 'addClass'
spyOn @scope.clone, 'removeClass'
@scope.toggleClone 1
expect @scope.clone.addClass
.toHaveBeenCalledWith options.stuckClassName
@scope.toggleClone 0
expect @scope.clone.removeClass
.toHaveBeenCalledWith options.stuckClassName
describe '#sizeClone', ->
it 'should call #setOffset, #setClonedCellWidths, #setClonedCellWidths, and #checkScroll with no arguments', ->
@scope.clone = true
@scope.setClonedCellWidths = ->
@scope.setCloneGutter = ->
@scope.setOffset = ->
@scope.checkScroll = ->
spyOn @scope, 'setClonedCellWidths'
spyOn @scope, 'setCloneGutter'
spyOn @scope, 'setOffset'
spyOn @scope, 'checkScroll'
do @scope.sizeClone
expect @scope.setOffset
.toHaveBeenCalledWith
expect @scope.setClonedCellWidths
.toHaveBeenCalledWith
expect @scope.setCloneGutter
.toHaveBeenCalledWith
expect @scope.checkScroll
.toHaveBeenCalledWith
describe '#checkScroll', ->
beforeEach ->
spyOn @scope, 'setStuck'
spyOn @scope, 'setClonedCellWidths'
.andCallFake ->
[
{ elementScrollY: 0, windowScrollY: 0 }
{ elementScrollY: 0, windowScrollY: 1 }
{ elementScrollY: 1, windowScrollY: 0 }
{ elementScrollY: 1, windowScrollY: 1 }
{ elementScrollY: 2, windowScrollY: -1 }
{ elementScrollY: -1, windowScrollY: 2 }
].forEach (data) ->
it 'should call #setStuck with true and #setClonedCellWidths with no arguments when scope.stuck is false and scrollY is >= offset.top', ->
spyOn @element, 'scrollTop'
.andReturn data.elementScrollY
@scope.clone = css: ->
@scope.stuck = false
@scope.offset = top: 0
$window.scrollY = data.windowScrollY
do @scope.checkScroll
expect @scope.setStuck
.toHaveBeenCalledWith true
do expect @scope.setClonedCellWidths
.toHaveBeenCalled
[
{ elementScrollY: 0, windowScrollY: 0 }
{ elementScrollY: 0, windowScrollY: -1 }
{ elementScrollY: -1, windowScrollY: 0 }
].forEach (data) ->
it 'should call #setStuck with false when scope.stuck is true and scrollY is < offset.top', ->
spyOn @element, 'scrollTop'
.andReturn data.elementScrollY
@scope.clone = css: ->
@scope.stuck = true
@scope.offset = top: 1
$window.scrollY = data.windowScrollY
do @scope.checkScroll
expect @scope.setStuck
.toHaveBeenCalledWith false
it 'should not call #setStuck otherwise', ->
# stuck = true, scrollY >= offset
@scope.clone = css: ->
@scope.stuck = true
@scope.offset = top: 0
$window.scrollY = 1
do @scope.checkScroll
# stuck = false, scrollY < offset
@scope.stuck = false
@scope.offset = top: 1
$window.scrollY = 0
do @scope.checkScroll
do expect @scope.setStuck
.not.toHaveBeenCalled
it 'should set top', ->
@scope.clone = css: ->
spyOn @scope.clone, 'css'
do @scope.checkScroll
expect @scope.clone.css
.toHaveBeenCalledWith 'top', jasmine.any(Number)
describe '#rowsChanged', ->
it 'should call #sizeClone with no arguments after a $timeout', inject ($timeout) ->
spyOn @scope, 'sizeClone'
do @scope.rowsChanged
do $timeout.flush
expect @scope.sizeClone
.toHaveBeenCalledWith
describe '#on', ->
it 'should call #observeTr and #addEvents with no arguments', ->
spyOn @scope, 'observeTr'
spyOn @scope, 'addEvents'
do @scope.on
expect @scope.observeTr
.toHaveBeenCalledWith
expect @scope.addEvents
.toHaveBeenCalledWith
describe '#off', ->
it 'should call #mutationObserver, #removeEvents, and #removeClones with no arguments', ->
@scope.mutationObserver = ->
spyOn @scope, 'mutationObserver'
spyOn @scope, 'removeEvents'
spyOn @scope, 'removeClones'
do @scope.off
expect @scope.mutationObserver
.toHaveBeenCalledWith
expect @scope.removeEvents
.toHaveBeenCalledWith
expect @scope.removeClones
.toHaveBeenCalledWith
describe '#addEvents', ->
it 'should store resize and scroll events on scope.windowEvents', ->
@scope.windowEvents = null
do @scope.addEvents
expect @scope.windowEvents.resize
.toBe @scope.sizeClone
expect @scope.windowEvents.scroll
.toBe @scope.checkScroll
it 'should store the scroll event on scope.elementEvents', ->
@scope.elementEvents = null
do @scope.addEvents
expect @scope.elementEvents.scroll
.toBe @scope.checkScroll
it 'should bind windowEvents to the $window', inject ($window) ->
spyOn (do $).__proto__, 'on'
do spyOn angular, 'element'
.andCallThrough
do @scope.addEvents
expect angular.element
.toHaveBeenCalledWith $window
expect (do $).__proto__.on
.toHaveBeenCalledWith @scope.windowEvents
it 'should bind elementEvents to the element', ->
spyOn (do $).__proto__, 'on'
do @scope.addEvents
expect (do $).__proto__.on
.toHaveBeenCalledWith @scope.elementEvents
describe '#removeEvents', ->
it 'should not reset scope.windowEvents or unbind events from the $window if windowEvents.resize, windowEvents.scroll, or elementEvents.scroll are falsey', ->
spyOn (do $).__proto__, 'on'
[
{
elementEvents: scroll: null
windowEvents: resize: null, scroll: null
}
{
elementEvents: scroll: true
windowEvents: resize: null, scroll: null
}
{
elementEvents: scroll: null
windowEvents: resize: true, scroll: null
}
{
elementEvents: scroll: null
windowEvents: resize: null, scroll: true
}
{
elementEvents: scroll: true
windowEvents: resize: true, scroll: null
}
{
elementEvents: scroll: true
windowEvents: resize: null, scroll: true
}
{
elementEvents: scroll: null
windowEvents: resize: true, scroll: true
}
].forEach (vars) =>
# set mock vars
@scope.elementEvents = vars.elementEvents
@scope.windowEvents = vars.windowEvents
do @scope.removeEvents
do expect (do $).__proto__.on
.not.toHaveBeenCalled
expect @scope.elementEvents
.toEqual vars.elementEvents
expect @scope.windowEvents
.toEqual vars.windowEvents
it 'should unbind events from the element', ->
events =
scroll: true
@scope.elementEvents = angular.copy events
spyOn (do $).__proto__, 'off'
do @scope.removeEvents
expect (do $).__proto__.off
.toHaveBeenCalledWith events
it 'should unbind events from the $window', inject ($window) ->
events =
resize: true
scroll: true
@scope.windowEvents = angular.copy events
spyOn (do $).__proto__, 'off'
do spyOn angular, 'element'
.andCallThrough
do @scope.removeEvents
expect angular.element
.toHaveBeenCalledWith $window
expect (do $).__proto__.off
.toHaveBeenCalledWith events
it 'should set scope.elementEvents to an empty object', ->
@scope.elementEvents =
scroll: true
do @scope.removeEvents
expect @scope.elementEvents
.toEqual {}
it 'should set scope.windowEvents to an empty object', ->
@scope.windowEvents =
resize: true
scroll: true
do @scope.removeEvents
expect @scope.windowEvents
.toEqual {}
describe '#changeDisabled', ->
it 'shouldn\'t call anything if the 1st argument is identical to the 2nd argument', ->
spyOn @scope, 'on'
spyOn @scope, 'off'
spyOn @scope, 'resetClone'
@scope.changeDisabled true, true
do expect @scope.on
.not.toHaveBeenCalled
do expect @scope.off
.not.toHaveBeenCalled
do expect @scope.resetClone
.not.toHaveBeenCalled
it 'should call #off with no arguments if the 1st argument is truthy', ->
spyOn @scope, 'off'
@scope.changeDisabled true
expect @scope.off
.toHaveBeenCalledWith
it 'should call #on and #resetClone with no arguments, and #toggleClone with false if the 1st argument is truthy', ->
spyOn @scope, 'on'
spyOn @scope, 'resetClone'
spyOn @scope, 'toggleClone'
@scope.changeDisabled false
expect @scope.on
.toHaveBeenCalledWith
expect @scope.resetClone
.toHaveBeenCalledWith
expect @scope.toggleClone
.toHaveBeenCalledWith false
describe '$destroy', ->
it 'should call #off with no arguments', ->
@scope.mutationObserver = ->
spyOn @scope, 'off'
do @scope.$destroy
expect @scope.off
.toHaveBeenCalledWith
describe '$watches', ->
it 'should call #changeDisabled when scope.disabled changes', inject ($timeout) ->
spyOn @scope, 'changeDisabled'
@element.attr 'disabled', 'foo'
do @scope.$apply
$timeout ->
expect @scope.changeDisabled
.toHaveBeenCalled()
it 'should call #rowsChanged when scope.rows changes', inject ($timeout) ->
spyOn @scope, 'rowsChanged'
@element.attr 'rows', 'foo'
do @scope.$apply
$timeout ->
expect @scope.rowsChanged
.toHaveBeenCalled()