modularity-rails 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module ModularityRails
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -13,7 +13,7 @@ loadCS = (url, callback) ->
13
13
  # Helper method to load the modularity library before the tests.
14
14
  describe 'modularity loader', ->
15
15
  it 'loading Modularity library ...', ->
16
- loadCS '/vendor/assets/javascripts/modularity.js.coffee'
16
+ loadCS '/vendor/assets/javascripts/modularity.js.coffee?'+(new Date()).getTime()
17
17
 
18
18
 
19
19
  describe 'modularity', ->
@@ -56,29 +56,35 @@ describe 'modularity', ->
56
56
  module = new Module('testing')
57
57
 
58
58
  it 'shows an alert with the given message if the given condition is false', ->
59
- module.assert false, 'Message'
59
+ Module.assert false, 'Message'
60
60
  expect(alert).toHaveBeenCalledWith("Message")
61
61
 
62
62
  it 'fails when the condition is null', ->
63
- module.assert null, 'Message'
63
+ Module.assert null, 'Message'
64
64
  expect(alert).toHaveBeenCalledWith("Message")
65
65
 
66
66
  it 'fails when the condition is undefined', ->
67
- module.assert undefined, 'Message'
67
+ Module.assert undefined, 'Message'
68
68
  expect(alert).toHaveBeenCalledWith("Message")
69
69
 
70
70
  it 'fails when the condition is an empty array', ->
71
- module.assert [], 'Message'
71
+ Module.assert [], 'Message'
72
72
  expect(alert).toHaveBeenCalledWith("Message")
73
73
 
74
74
  it 'fails when the condition is an empty string', ->
75
- module.assert '', 'Message'
75
+ Module.assert '', 'Message'
76
76
  expect(alert).toHaveBeenCalledWith("Message")
77
77
 
78
78
  it 'passes when the condition is a string', ->
79
- module.assert '123', 'Message'
79
+ Module.assert '123', 'Message'
80
80
  expect(alert).not.toHaveBeenCalled()
81
81
 
82
+ it "returns false if the condition doesn't pass", ->
83
+ expect(Module.assert(false)).toBeFalsy()
84
+
85
+ it "returns true if the condition passes", ->
86
+ expect(Module.assert('123')).toBeTruthy()
87
+
82
88
 
83
89
  describe 'jQuery Integration', ->
84
90
 
@@ -101,3 +107,159 @@ describe 'modularity', ->
101
107
  it 'returns an error if the jQuery object is empty.', ->
102
108
  $('#zonk').module(Module)
103
109
  expect(alert).toHaveBeenCalled()
110
+
111
+
112
+ describe 'event handling', ->
113
+
114
+ # Variables that should be accessible in both the beforeEach block and the tests.
115
+ module = null
116
+ mockContainer = null
117
+
118
+ beforeEach ->
119
+ mockContainer = $('#module_container')
120
+ module = new Module(mockContainer)
121
+ spyOn window, 'alert'
122
+
123
+
124
+ describe 'bind_event', ->
125
+
126
+ beforeEach ->
127
+ spyOn mockContainer, 'bind'
128
+
129
+ it 'binds the given custom jQuery event type and the given callback to the container', ->
130
+ myCallback = ->
131
+ module.bind_event 'myEventType', myCallback
132
+ expect(mockContainer.bind).toHaveBeenCalledWith('myEventType', myCallback)
133
+ expect(window.alert).not.toHaveBeenCalled()
134
+
135
+ it "throws an error if no parameters are given", ->
136
+ module.bind_event()
137
+ expect(window.alert).toHaveBeenCalled()
138
+ expect(mockContainer.bind).not.toHaveBeenCalled()
139
+
140
+ it "throws an error if the given event type doesn't exist", ->
141
+ module.bind_event undefined, ->
142
+ expect(window.alert).toHaveBeenCalled()
143
+ expect(mockContainer.bind).not.toHaveBeenCalled()
144
+
145
+ it 'throws an error if the given event type is not a string', ->
146
+ module.bind_event {}, ->
147
+ expect(window.alert).toHaveBeenCalled()
148
+ expect(mockContainer.bind).not.toHaveBeenCalled()
149
+
150
+ it "throws an error if the given callback doesn't exist", ->
151
+ module.bind_event '123'
152
+ expect(window.alert).toHaveBeenCalled()
153
+ expect(mockContainer.bind).not.toHaveBeenCalled()
154
+
155
+ it 'throws an error if the given callback is not a function', ->
156
+ module.bind_event '123', {}
157
+ expect(window.alert).toHaveBeenCalled()
158
+ expect(mockContainer.bind).not.toHaveBeenCalled()
159
+
160
+
161
+ describe 'fire_event', ->
162
+
163
+ beforeEach ->
164
+ spyOn mockContainer, 'trigger'
165
+
166
+ it 'triggers a custom jQuery event with the given event type on the container object', ->
167
+ module.fire_event 'event type', 'event data'
168
+ expect(mockContainer.trigger).toHaveBeenCalledWith('event type', 'event data')
169
+
170
+ describe 'when no payload is given', ->
171
+ it 'provides an empty object as payload', ->
172
+ module.fire_event 'event type'
173
+ expect(mockContainer.trigger).toHaveBeenCalled()
174
+ expect(mockContainer.trigger.argsForCall[0][1]).toEqual({})
175
+
176
+ it "doesn't change the original payload variable", ->
177
+ data = undefined
178
+ module.fire_event 'event type', data
179
+ expect(data).toBeUndefined
180
+
181
+ it 'provides 0 as payload if 0 is given', ->
182
+ module.fire_event 'event type', 0
183
+ expect(mockContainer.trigger).toHaveBeenCalled()
184
+ expect(mockContainer.trigger.argsForCall[0][1]).toEqual(0)
185
+
186
+ it 'throws an error if the given event type is not a string', ->
187
+ module.fire_event {}
188
+ expect(mockContainer.trigger).not.toHaveBeenCalled()
189
+ expect(window.alert).toHaveBeenCalled()
190
+
191
+
192
+ describe 'global events', ->
193
+
194
+ # Variables that should be accessible in both the beforeEach block and the tests.
195
+ mockGlobalContainer = null
196
+
197
+ beforeEach ->
198
+ mockGlobalContainer = $('#module_container')
199
+ spyOn(Module, 'global_event_container').andReturn(mockGlobalContainer)
200
+
201
+ describe 'bind_global_event', ->
202
+
203
+ beforeEach ->
204
+ spyOn mockGlobalContainer, 'bind'
205
+
206
+ it 'binds the given event type and callback method to the global event container', ->
207
+ callback = ->
208
+ Module.bind_global_event '123', callback
209
+ expect(mockGlobalContainer.bind).toHaveBeenCalledWith('123', callback)
210
+
211
+ it "throws an error if no parameters are given", ->
212
+ Module.bind_global_event()
213
+ expect(window.alert).toHaveBeenCalled()
214
+ expect(mockGlobalContainer.bind).not.toHaveBeenCalled()
215
+
216
+ it "throws an error if the given event type doesn't exist", ->
217
+ Module.bind_global_event undefined, ->
218
+ expect(window.alert).toHaveBeenCalled()
219
+ expect(mockGlobalContainer.bind).not.toHaveBeenCalled()
220
+
221
+ it 'throws an error if the given event type is not a string', ->
222
+ Module.bind_global_event {}, ->
223
+ expect(window.alert).toHaveBeenCalled()
224
+ expect(mockGlobalContainer.bind).not.toHaveBeenCalled()
225
+
226
+ it "throws an error if the given callback doesn't exist", ->
227
+ Module.bind_global_event '123'
228
+ expect(window.alert).toHaveBeenCalled()
229
+ expect(mockGlobalContainer.bind).not.toHaveBeenCalled()
230
+
231
+ it 'throws an error if the given callback is not a function', ->
232
+ Module.bind_global_event '123', {}
233
+ expect(window.alert).toHaveBeenCalled()
234
+ expect(mockGlobalContainer.bind).not.toHaveBeenCalled()
235
+
236
+
237
+ describe 'fire_global_event', ->
238
+
239
+ beforeEach ->
240
+ spyOn mockGlobalContainer, 'trigger'
241
+
242
+ it 'triggers a custom jQuery event with the given event type on the global event container object', ->
243
+ Module.fire_global_event 'event type', 'event data'
244
+ expect(mockGlobalContainer.trigger).toHaveBeenCalledWith('event type', 'event data')
245
+
246
+ describe 'when no payload is given', ->
247
+ it 'provides an empty object as payload', ->
248
+ Module.fire_global_event 'event type'
249
+ expect(mockGlobalContainer.trigger).toHaveBeenCalled()
250
+ expect(mockGlobalContainer.trigger.argsForCall[0][1]).toEqual({})
251
+
252
+ it "doesn't change the original payload variable", ->
253
+ data = undefined
254
+ Module.fire_global_event 'event type', data
255
+ expect(data).toBeUndefined
256
+
257
+ it 'provides 0 as payload if 0 is given', ->
258
+ Module.fire_global_event 'event type', 0
259
+ expect(mockGlobalContainer.trigger).toHaveBeenCalled()
260
+ expect(mockGlobalContainer.trigger.argsForCall[0][1]).toEqual(0)
261
+
262
+ it 'throws an error if the given event type is not a string', ->
263
+ Module.fire_global_event {}
264
+ expect(mockGlobalContainer.trigger).not.toHaveBeenCalled()
265
+ expect(window.alert).toHaveBeenCalled()
@@ -16,38 +16,43 @@ class window.Module
16
16
 
17
17
  # Checks whether the given condition is true.
18
18
  # Shows an alert with the given message if not.
19
- assert: (condition, message) ->
20
- alert(message) unless condition?.length > 0
19
+ @assert: (condition, message) ->
20
+ condition_ok = condition?.length > 0
21
+ alert(message) unless condition_ok
22
+ condition_ok
21
23
 
22
24
  # MODULE EVENTS.
23
25
 
24
26
  # Calls the given function when this widget fires the given local event.
25
27
  bind_event: (event_type, callback) =>
26
- @assert event_type, "Module.bind_event: parameter 'event_type' is empty"
28
+ return unless Module.assert event_type, "Module.bind_event: parameter 'event_type' is empty"
29
+ return alert "Module.bind_event: parameter 'callback' must be a function, #{callback} (#{typeof callback}) given." unless typeof callback == 'function'
27
30
  @container.bind event_type, callback
28
31
 
29
32
  # Fires the given local event with the given data payload.
30
33
  fire_event: (event_type, data) =>
31
- @assert event_type, 'Module.fire_event: You must provide the event type to fire.'
32
- @container.trigger event_type, data or {}
34
+ Module.assert event_type, 'Module.fire_event: You must provide the event type to fire.'
35
+ return alert("Module.fire_event: Event type must be a string, #{event_type} (#{typeof event_type}) given.") unless typeof event_type == 'string'
36
+ @container.trigger event_type, data ?= {}
33
37
 
34
38
 
35
39
  # GLOBAL EVENTS.
36
40
 
37
41
  # Subscribes to the given global event,
38
42
  # i.e. calls the given function when the given global event type happens.
39
- bind_global_event: (event_type, callback) =>
40
- @assert event_type, "Module.bind_global_event: parameter 'event_type' is empty"
43
+ @bind_global_event: (event_type, callback) =>
44
+ return unless @assert event_type, "Module.bind_global_event: parameter 'event_type' is empty"
45
+ return alert "Module.bind_global_event: parameter 'callback' must be a function, #{callback} (#{typeof callback}) given." unless typeof callback == 'function'
41
46
  @global_event_container().bind event_type, callback
42
47
 
43
48
  # Fires the given global event with the given data payload.
44
- fire_global_event: (event_type, data) =>
49
+ @fire_global_event: (event_type, data) =>
45
50
  @assert event_type, 'Module.fire_global_event: You must provide the event type to fire.'
46
- @global_event_container().trigger event_type, data or []
51
+ return alert("Module.fire_global_event: Event type must be a string, #{event_type} (#{typeof event_type}) given.") unless typeof event_type == 'string'
52
+ @global_event_container().trigger event_type, data ?= []
47
53
 
48
54
  # Returns the DOM object that is used to fire global events on.
49
- global_event_container: =>
50
- @global_event_container_cache or= $(window)
55
+ @global_event_container: => @global_event_container_cache or= $(window)
51
56
 
52
57
 
53
58
  # jQuery integration for creating Modules.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modularity-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70185851808700 !ruby/object:Gem::Requirement
16
+ requirement: &70292877992260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70185851808700
24
+ version_requirements: *70292877992260
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: capybara-webkit
27
- requirement: &70185851807920 !ruby/object:Gem::Requirement
27
+ requirement: &70292877991380 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70185851807920
35
+ version_requirements: *70292877991380
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: evergreen
38
- requirement: &70185851802840 !ruby/object:Gem::Requirement
38
+ requirement: &70292877990260 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70185851802840
46
+ version_requirements: *70292877990260
47
47
  description: Description of ModularityRails.
48
48
  email:
49
49
  - kevin.goslar@gmail.com