Commit 8473667eb747d6018b9c917c886d8727d37a3f84

Authored by Rafael Manzo
1 parent 3468a54a

Module.Graphic JS covered with unit tests

Signed off by: Daniel Miranda <danielkza2@gmail.com>
Signed off by: Heitor Reis <marcheing@gmail.com>
Showing 1 changed file with 89 additions and 16 deletions   Show diff stats
spec/javascripts/module/graphic_spec.js.coffee
1 1 #= require spec_helper
  2 +#= require Chart
2 3 #= require module/graphic
3 4  
4 5 describe "Module.Graphic", ->
5   - describe "constructor", ->
6   - before ->
7   - @container = 'container404829'
8   - @metric_name = 'Pain'
9   - @module_id = '54405'
  6 + before ->
  7 + @container = 'container404829'
  8 + @metric_name = 'Pain'
  9 + @module_id = '54405'
10 10  
11   - @drawer = sinon.stub()
12   - sinon.stub(window, "$")
13   - $.withArgs("tr#"+@container).returns(@drawer)
  11 + sinon.stub(window, "$")
14 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", ->
15 19 context 'when the drawer is hidden', ->
16 20 before ->
17 21 @drawer.is = sinon.stub().withArgs(':hidden').returns(true)
  22 + @drawer.slideDown = sinon.stub()
  23 + sinon.stub(Module.Graphic.prototype, 'load')
18 24  
19 25 it "should show the drawer and start to load a graphic", ->
20   - @drawer.slideDown = sinon.spy()
21   -
22   - Module.Graphic.prototype.load = sinon.spy()
23   -
24 26 @graphic = new Module.Graphic(@container, @metric_name, @module_id)
25 27  
26 28 assert.isTrue(@drawer.slideDown.calledOnce)
27 29 assert.isTrue(@graphic.load.calledOnce)
28 30  
29   - context 'when the drawer is visible', ->
30   - before ->
  31 + after ->
31 32 @drawer.is = sinon.stub().withArgs(':hidden').returns(false)
  33 + @drawer.slideDown = undefined
  34 + Module.Graphic.prototype.load.restore()
32 35  
  36 + context 'when the drawer is visible', ->
33 37 it 'should hide the drawer', ->
34   - @drawer.slideUp = sinon.spy()
35   -
36 38 @graphic = new Module.Graphic(@container, @metric_name, @module_id)
37 39  
38 40 assert.isTrue(@drawer.slideUp.calledOnce)
  41 +
  42 + after ->
  43 +
  44 + describe 'load', ->
  45 + before ->
  46 + @graphic = new Module.Graphic(@container, @metric_name, @module_id)
  47 +
  48 + it 'should make a POST request', ->
  49 + $.post = sinon.stub().withArgs('/modules/' + @module_id + '/metric_history', {metric_name: @metric_name, container: @container})
  50 +
  51 + @graphic.load()
  52 +
  53 + sinon.assert.calledOnce($.post)
  54 +
  55 + describe 'display', ->
  56 + beforeEach ->
  57 + canvas_context = sinon.stub()
  58 + @canvas = sinon.stub()
  59 + @canvas.getContext = sinon.stub().withArgs('2d').returns(canvas_context)
  60 +
  61 + elements = sinon.stub()
  62 + elements.get = sinon.stub().withArgs(0).returns(@canvas)
  63 + $.withArgs('canvas#'+@container).returns(elements)
  64 +
  65 + @dates = ["2015-06-17", "2015-06-18", "2015-06-19"]
  66 + @values = [1.2, 2.3, 3.4]
  67 +
  68 + opts = {
  69 + bezierCurve: false,
  70 + responsive: true,
  71 + maintainAspectRatio: false
  72 + }
  73 +
  74 + data = {
  75 + labels : @dates,
  76 + datasets : [
  77 + {
  78 + fillColor : "rgba(220,220,220,0.5)",
  79 + strokeColor : "rgba(220,220,220,1)",
  80 + pointColor : "rgba(220,220,220,1)",
  81 + pointStrokeColor : "#000",
  82 + data : @values
  83 + }
  84 + ]
  85 + }
  86 +
  87 + @line = sinon.stub().withArgs(data, opts)
  88 + sinon.stub(window, 'Chart').withArgs(canvas_context).returns({Line: @line})
  89 +
  90 + context 'when the graphic exists', ->
  91 + it 'should render the chart', ->
  92 + Module.Graphic.display(@dates, @values, @container)
  93 + sinon.assert.calledOnce(@line)
  94 +
  95 + context 'when the graphic does not exists', ->
  96 + beforeEach ->
  97 + @chart = sinon.stub()
  98 + @chart.destroy = sinon.stub()
  99 + @canvas.chart = @chart
  100 + @canvas.hasOwnProperty = sinon.stub().withArgs("chart").returns(true)
  101 +
  102 + it 'should destroy the previous chart and render a new one', ->
  103 + Module.Graphic.display(@dates, @values, @container)
  104 + sinon.assert.calledOnce(@chart.destroy)
  105 + sinon.assert.calledOnce(@line)
  106 +
  107 + afterEach ->
  108 + Chart.restore()
  109 +
  110 + after ->
  111 + $.restore()
... ...