This repository has been archived by the owner on Dec 30, 2018. It is now read-only.
/
directive.coffee
116 lines (90 loc) · 3.2 KB
/
directive.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
describe 'angular-masonry', ->
controllerProvider = null
beforeEach module('wu.masonry')
beforeEach module((_$controllerProvider_) ->
controllerProvider = _$controllerProvider_
null
)
beforeEach inject(($rootScope) =>
@scope = $rootScope.$new()
$.fn.masonry.reset()
)
it 'should initialize', inject(($compile) =>
element = angular.element '<masonry></masonry>'
element = $compile(element)(@scope)
)
it 'should call masonry on init', inject(($compile) =>
element = angular.element '<div masonry></div>'
element = $compile(element)(@scope)
expect($.fn.masonry).toHaveBeenCalled()
)
it 'should pass on the column-width attribute', inject(($compile) =>
element = angular.element '<masonry column-width="200"></masonry>'
element = $compile(element)(@scope)
expect($.fn.masonry).toHaveBeenCalledOnce()
call = $.fn.masonry.firstCall
expect(call.args[0].columnWidth).toBe 200
)
it 'should pass on the item-selector attribute', inject(($compile) =>
element = angular.element '<masonry item-selector=".mybrick"></masonry>'
element = $compile(element)(@scope)
expect($.fn.masonry).toHaveBeenCalled()
call = $.fn.masonry.firstCall
expect(call.args[0].itemSelector).toBe '.mybrick'
)
it 'should pass on any options probided', inject(($compile) =>
element = angular.element '<masonry options="{ isOriginLeft: true }"></masonry>'
element = $compile(element)(@scope)
expect($.fn.masonry).toHaveBeenCalled()
call = $.fn.masonry.firstCall
expect(call.args[0].isOriginLeft).toBeTruthy()
)
describe 'MasonryCtrl', =>
beforeEach inject(($controller, $compile) =>
@element = angular.element '<div></div>'
@ctrl = $controller 'MasonryCtrl', {
$scope: @scope
$element: @element
}
)
it 'should not remove after destruction', =>
@ctrl.destroy()
@ctrl.removeBrick()
expect($.fn.masonry).not.toHaveBeenCalled()
it 'should not add after destruction', =>
@ctrl.destroy()
@ctrl.appendBrick()
expect($.fn.masonry).not.toHaveBeenCalled()
describe 'masonry-brick', =>
beforeEach =>
self = this
@appendBrick = sinon.spy()
@removeBrick = sinon.spy()
controllerProvider.register('MasonryCtrl', ->
@appendBrick = self.appendBrick
@removeBrick = self.removeBrick
)
it 'should register an element in the parent controller', inject(($compile) =>
element = angular.element '''
<masonry>
<div class="masonry-brick"></div>
</masonry>
'''
element = $compile(element)(@scope)
expect(@appendBrick).toHaveBeenCalledOnce()
)
it 'should remove an element in the parent controller if destroyed', inject(($compile) =>
@scope.bricks = [1, 2, 3]
element = angular.element '''
<masonry>
<div class="masonry-brick" ng-repeat="brick in bricks"></div>
</masonry>
'''
element = $compile(element)(@scope)
@scope.$digest() # Needed for initial ng-repeat
@scope.$apply(=>
@scope.bricks.splice(0, 1)
)
expect(@appendBrick).toHaveBeenCalledThrice()
expect(@removeBrick).toHaveBeenCalledOnce()
)