Commit abdd2edc69ec5c81804e4addd917a8b6e9be245d

Authored by Diego Camarinha
2 parents 76666012 3aa3e248

Merge pull request #234 from mezuro/js_tests

Js tests
Gemfile
... ... @@ -103,7 +103,7 @@ group :development, :test do
103 103 gem 'capistrano-rvm', "~>0.1.0"
104 104  
105 105 # JavaScript unit tests
106   - gem "konacha", "~> 3.3.0"
  106 + gem "konacha"
107 107  
108 108 # Better error interface
109 109 gem 'better_errors'
... ... @@ -112,7 +112,7 @@ group :development, :test do
112 112 # Localization assistance
113 113 gem 'i18n_generators'
114 114  
115   - gem 'sprockets', '~>2.12.3' # spckets 3.0.3 breaks konacha
  115 + gem 'sprockets'
116 116  
117 117 # Mocks and stubs for javascript tests
118 118 gem 'sinon-rails'
... ...
Gemfile.lock
... ... @@ -139,7 +139,6 @@ GEM
139 139 globalid (0.3.5)
140 140 activesupport (>= 4.1.0)
141 141 google-analytics-rails (0.0.6)
142   - hike (1.2.3)
143 142 http-cookie (1.0.2)
144 143 domain_name (~> 0.5)
145 144 http_accept_language (2.0.5)
... ... @@ -160,12 +159,13 @@ GEM
160 159 kalibro_client (0.3.0)
161 160 activesupport (>= 2.2.1)
162 161 faraday_middleware (~> 0.9.0)
163   - konacha (3.3.0)
  162 + konacha (3.5.1)
164 163 actionpack (>= 3.1, < 5)
165 164 capybara
166 165 colorize
167 166 railties (>= 3.1, < 5)
168   - sprockets
  167 + sprockets (>= 2, < 4)
  168 + tilt
169 169 less (2.6.0)
170 170 commonjs (~> 0.2.7)
171 171 less-rails (2.7.0)
... ... @@ -188,12 +188,12 @@ GEM
188 188 ntlm-http (~> 0.1, >= 0.1.1)
189 189 webrobots (>= 0.0.9, < 0.2)
190 190 metaclass (0.0.4)
191   - mime-types (2.5)
  191 + mime-types (2.6.1)
192 192 mini_portile (0.6.2)
193 193 minitest (5.7.0)
194 194 mocha (1.1.0)
195 195 metaclass (~> 0.0.1)
196   - multi_json (1.11.0)
  196 + multi_json (1.11.1)
197 197 multi_test (0.1.2)
198 198 multipart-post (2.0.0)
199 199 net-http-digest_auth (1.4)
... ... @@ -211,7 +211,7 @@ GEM
211 211 cliver (~> 0.3.1)
212 212 multi_json (~> 1.0)
213 213 websocket-driver (>= 0.2.0)
214   - rack (1.6.1)
  214 + rack (1.6.4)
215 215 rack-test (0.6.3)
216 216 rack (>= 1.0)
217 217 rails (4.2.1)
... ... @@ -280,11 +280,8 @@ GEM
280 280 sinon-rails (1.15.0)
281 281 railties (>= 3.1)
282 282 spring (1.3.6)
283   - sprockets (2.12.3)
284   - hike (~> 1.2)
285   - multi_json (~> 1.0)
  283 + sprockets (3.2.0)
286 284 rack (~> 1.0)
287   - tilt (~> 1.1, != 1.3.0)
288 285 sprockets-rails (2.3.0)
289 286 actionpack (>= 3.0)
290 287 activesupport (>= 3.0)
... ... @@ -352,7 +349,7 @@ DEPENDENCIES
352 349 jquery-rails
353 350 jquery-ui-rails (~> 5.0.0)
354 351 kalibro_client (~> 0.3.0)
355   - konacha (~> 3.3.0)
  352 + konacha
356 353 mocha
357 354 pg (~> 0.18.1)
358 355 poltergeist (~> 1.6.0)
... ... @@ -365,7 +362,7 @@ DEPENDENCIES
365 362 simplecov
366 363 sinon-rails
367 364 spring
368   - sprockets (~> 2.12.3)
  365 + sprockets
369 366 sqlite3
370 367 therubyracer
371 368 turbolinks
... ...
spec/javascripts/module/graphic_spec.js.coffee 0 → 100644
... ... @@ -0,0 +1,109 @@
  1 +#= require spec_helper
  2 +#= require Chart
  3 +#= require module/graphic
  4 +
  5 +describe "Module.Graphic", ->
  6 + before ->
  7 + @container = 'container404829'
  8 + @metric_name = 'Pain'
  9 + @module_id = '54405'
  10 +
  11 + sinon.stub(window, "$")
  12 +
  13 + @drawer = sinon.stub()
  14 + @drawer.is = sinon.stub().withArgs(':hidden').returns(false)
  15 + @drawer.slideUp = sinon.stub()
  16 + $.withArgs("tr#"+@container).returns(@drawer)
  17 +
  18 + describe "constructor", ->
  19 + context 'when the drawer is hidden', ->
  20 + before ->
  21 + @drawer.is = sinon.stub().withArgs(':hidden').returns(true)
  22 + @drawer.slideDown = sinon.stub()
  23 + sinon.stub(Module.Graphic.prototype, 'load')
  24 +
  25 + it "should show the drawer and start to load a graphic", ->
  26 + @graphic = new Module.Graphic(@container, @metric_name, @module_id)
  27 +
  28 + sinon.assert.calledOnce(@drawer.slideDown)
  29 + sinon.assert.calledOnce(@graphic.load)
  30 +
  31 + after ->
  32 + @drawer.is = sinon.stub().withArgs(':hidden').returns(false)
  33 + @drawer.slideDown = undefined
  34 + Module.Graphic.prototype.load.restore()
  35 +
  36 + context 'when the drawer is visible', ->
  37 + it 'should hide the drawer', ->
  38 + @graphic = new Module.Graphic(@container, @metric_name, @module_id)
  39 +
  40 + sinon.assert.calledOnce(@drawer.slideUp)
  41 +
  42 + describe 'load', ->
  43 + before ->
  44 + @graphic = new Module.Graphic(@container, @metric_name, @module_id)
  45 +
  46 + it 'should make a POST request', ->
  47 + $.post = sinon.stub().withArgs('/modules/' + @module_id + '/metric_history', {metric_name: @metric_name, container: @container})
  48 +
  49 + @graphic.load()
  50 +
  51 + sinon.assert.calledOnce($.post, '/modules/' + @module_id + '/metric_history', {metric_name: @metric_name, container: @container})
  52 +
  53 + describe 'display', ->
  54 + beforeEach ->
  55 + canvas_context = sinon.stub()
  56 + @canvas = sinon.stub()
  57 + @canvas.getContext = sinon.stub().withArgs('2d').returns(canvas_context)
  58 +
  59 + elements = sinon.stub()
  60 + elements.get = sinon.stub().withArgs(0).returns(@canvas)
  61 + $.withArgs('canvas#'+@container).returns(elements)
  62 +
  63 + @dates = ["2015-06-17", "2015-06-18", "2015-06-19"]
  64 + @values = [1.2, 2.3, 3.4]
  65 +
  66 + opts = {
  67 + bezierCurve: false,
  68 + responsive: true,
  69 + maintainAspectRatio: false
  70 + }
  71 +
  72 + data = {
  73 + labels : @dates,
  74 + datasets : [
  75 + {
  76 + fillColor : "rgba(220,220,220,0.5)",
  77 + strokeColor : "rgba(220,220,220,1)",
  78 + pointColor : "rgba(220,220,220,1)",
  79 + pointStrokeColor : "#000",
  80 + data : @values
  81 + }
  82 + ]
  83 + }
  84 +
  85 + @line = sinon.stub().withArgs(data, opts)
  86 + sinon.stub(window, 'Chart').withArgs(canvas_context).returns({Line: @line})
  87 +
  88 + context 'when the graphic exists', ->
  89 + it 'should render the chart', ->
  90 + Module.Graphic.display(@dates, @values, @container)
  91 + sinon.assert.calledOnce(@line)
  92 +
  93 + context 'when the graphic does not exists', ->
  94 + beforeEach ->
  95 + @chart = sinon.stub()
  96 + @chart.destroy = sinon.stub()
  97 + @canvas.chart = @chart
  98 + @canvas.hasOwnProperty = sinon.stub().withArgs("chart").returns(true)
  99 +
  100 + it 'should destroy the previous chart and render a new one', ->
  101 + Module.Graphic.display(@dates, @values, @container)
  102 + sinon.assert.calledOnce(@chart.destroy)
  103 + sinon.assert.calledOnce(@line)
  104 +
  105 + afterEach ->
  106 + Chart.restore()
  107 +
  108 + after ->
  109 + $.restore()
... ...
spec/javascripts/repository/branch_spec.js.coffee
1   -#= require jquery
2   -#= require repository
  1 +#= require spec_helper
3 2 #= require repository/branch
4   -#= require sinon
5 3  
6   -describe "Branch#constructor", ->
7   - it "should construct a branch", ->
8   - subject = new Repository.Branch()
9   - subject.names.should.deep.equal({})
10   - assert.isNull(subject.request)
11   -
12   -describe "toggle", ->
13   - before ->
  4 +describe "Repository.Branch", ->
  5 + beforeEach ->
14 6 @subject = new Repository.Branch()
15 7  
16   - @combo_box = sinon.stub()
17   - $ = sinon.stub(window, "$")
18   - $.withArgs("#branches").returns(@combo_box)
  8 + describe "constructor", ->
  9 + it "should construct a branch", ->
  10 + @subject.names.should.deep.equal({})
  11 + assert.isNull(@subject.request)
19 12  
20   - context "scm_type = SVN", ->
  13 + describe "#toggle", ->
21 14 before ->
22   - @combo_box.hide = sinon.spy()
23   - $.withArgs("#repository_scm_type").returns({val: -> "SVN"})
  15 + @combo_box = sinon.stub()
  16 + sinon.stub(window, "$")
  17 + $.withArgs("#branches").returns(@combo_box)
24 18  
25   - it "should hide the branches combo box", ->
26   - @subject.toggle()
27   - assert.isTrue(@combo_box.hide.calledOnce)
  19 + after ->
  20 + window.$.restore()
28 21  
29   - context "scm_type != SVN", ->
30   - before ->
31   - @combo_box.show = sinon.spy()
32   - $.withArgs("#repository_address").returns({val: -> "https://github.com/mezuro/prezento.git"})
  22 + context "scm_type = SVN", ->
  23 + beforeEach ->
  24 + @combo_box.hide = sinon.spy()
  25 + $.withArgs("#repository_scm_type").returns({val: -> "SVN"})
  26 +
  27 + it "should hide the branches combo box", ->
  28 + @subject.toggle()
  29 + sinon.assert.calledOnce(@combo_box.hide)
  30 +
  31 + context "scm_type != SVN", ->
  32 + beforeEach ->
  33 + @combo_box.show = sinon.spy()
  34 + $.withArgs("#repository_address").returns({val: -> "https://github.com/mezuro/prezento.git"})
  35 + $.withArgs("#repository_scm_type").returns({val: -> "GIT"})
  36 + sinon.stub(@subject, "fetch").withArgs("https://github.com/mezuro/prezento.git")
  37 +
  38 + it "should show the branches combo box", ->
  39 + @subject.toggle()
  40 + sinon.assert.calledOnce(@combo_box.show)
  41 +
  42 + describe "#cancel_request", ->
  43 + context 'request is not null', ->
  44 + beforeEach ->
  45 + @request = sinon.stub()
  46 + @request.abort = sinon.spy()
  47 + @subject.request = @request
  48 +
  49 + it 'should abort the request', ->
  50 + @subject.cancel_request()
  51 + sinon.assert.calledOnce(@request.abort)
  52 + assert.isNull(@subject.request)
  53 +
  54 + describe "#fill_options", ->
  55 + beforeEach ->
  56 + @el = jQuery("<select></select>")
  57 +
  58 + context 'with branches that contain master', ->
  59 + it 'should place master first', ->
  60 + @subject.fill_options(['dev', 'stable', 'master'], @el)
  61 +
  62 + option_values = []
  63 + for option in @el.get(0).options
  64 + option_values.push(option.value)
  65 +
  66 + assert.deepEqual(option_values, ['master', 'dev', 'stable'])
  67 +
  68 + context "with branches that don't contain master", ->
  69 + it "shouldn't change anything", ->
  70 + @subject.fill_options(['dev', 'stable', 'branch_do_daniel'], @el)
  71 +
  72 + option_values = []
  73 + for option in @el.get(0).options
  74 + option_values.push(option.value)
  75 +
  76 + assert.deepEqual(option_values, ['dev', 'stable', 'branch_do_daniel'])
  77 +
  78 + describe '#fetch', ->
  79 + beforeEach ->
  80 + @subject.names = {}
  81 + @address = 'https://github.com/mezuro/kalibro_processor.git'
  82 + @subject.cancel_request = sinon.stub()
  83 + sinon.stub(@subject, "fill_options")
  84 +
  85 + @select = {empty: sinon.stub()}
  86 + # $ is a global jQuery variable. That is why we don't need to declare it as @$
  87 + $ = sinon.stub(window, "$")
  88 + $.withArgs("#repository_branch").returns(@select)
33 89 $.withArgs("#repository_scm_type").returns({val: -> "GIT"})
34   - sinon.stub(@subject, "fetch").withArgs("https://github.com/mezuro/prezento.git")
35 90  
36   - it "should show the branches combo box", ->
37   - @subject.toggle()
38   - assert.isTrue(@combo_box.show.calledOnce)
  91 + afterEach ->
  92 + @subject.fill_options.restore()
  93 + $.restore()
  94 +
  95 + context 'with valid address', ->
  96 + context 'with an address that is not cached', ->
  97 + beforeEach ->
  98 + $.get = sinon.stub().withArgs('/repository_branches', {'url': @address, 'scm_type': 'GIT'}).returns().yields({
  99 + 'branches': ['stable', 'dev', 'master']
  100 + })
  101 +
  102 + it 'should fetch the branches and fill the options', ->
  103 + @subject.fetch(@address)
  104 + sinon.assert.calledWith(@select.empty)
  105 + sinon.assert.calledWith(@subject.fill_options, ['stable', 'dev', 'master'], @select)
  106 +
  107 + context 'with an address that is cached', ->
  108 + beforeEach ->
  109 + @subject.names[@address] = ['stable', 'dev', 'master']
  110 + $.get = sinon.stub()
  111 +
  112 + it 'should not request the branches but fill the select with the cached values', ->
  113 + @subject.fetch(@address)
  114 + sinon.assert.calledWith(@select.empty)
  115 + sinon.assert.calledWith(@subject.fill_options, ['stable', 'dev', 'master'], @select)
  116 + sinon.assert.notCalled($.get)
  117 +
  118 + context 'with an invalid address', ->
  119 + beforeEach ->
  120 + $.get = sinon.stub().withArgs('/repository_branches', {'url': @address, 'scm_type': 'GIT'}).returns().yields({
  121 + 'errors': ['InvalidUrl']
  122 + })
  123 +
  124 + it 'should not fill the options', ->
  125 + @subject.fetch(@address)
  126 + sinon.assert.calledWith(@select.empty)
  127 + sinon.assert.notCalled(@subject.fill_options)
... ...
spec/javascripts/spec_helper.js.coffee 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +#= require jquery
  2 +#= require sinon
  3 +
  4 +#= require modules
  5 +#= require repository
0 6 \ No newline at end of file
... ...