graphic_spec.js.coffee
3.35 KB
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
#= require spec_helper
#= require Chart
#= require module/graphic
describe "Module.Graphic", ->
before ->
@container = 'container404829'
@metric_name = 'Pain'
@module_id = '54405'
sinon.stub(window, "$")
@drawer = sinon.stub()
@drawer.is = sinon.stub().withArgs(':hidden').returns(false)
@drawer.slideUp = sinon.stub()
$.withArgs("tr#"+@container).returns(@drawer)
describe "constructor", ->
context 'when the drawer is hidden', ->
before ->
@drawer.is = sinon.stub().withArgs(':hidden').returns(true)
@drawer.slideDown = sinon.stub()
sinon.stub(Module.Graphic.prototype, 'load')
it "should show the drawer and start to load a graphic", ->
@graphic = new Module.Graphic(@container, @metric_name, @module_id)
sinon.assert.calledOnce(@drawer.slideDown)
sinon.assert.calledOnce(@graphic.load)
after ->
@drawer.is = sinon.stub().withArgs(':hidden').returns(false)
@drawer.slideDown = undefined
Module.Graphic.prototype.load.restore()
context 'when the drawer is visible', ->
it 'should hide the drawer', ->
@graphic = new Module.Graphic(@container, @metric_name, @module_id)
sinon.assert.calledOnce(@drawer.slideUp)
describe 'load', ->
before ->
@graphic = new Module.Graphic(@container, @metric_name, @module_id)
it 'should make a POST request', ->
$.post = sinon.stub().withArgs('/modules/' + @module_id + '/metric_history', {metric_name: @metric_name, container: @container})
@graphic.load()
sinon.assert.calledOnce($.post, '/modules/' + @module_id + '/metric_history', {metric_name: @metric_name, container: @container})
describe 'display', ->
beforeEach ->
canvas_context = sinon.stub()
@canvas = sinon.stub()
@canvas.getContext = sinon.stub().withArgs('2d').returns(canvas_context)
elements = sinon.stub()
elements.get = sinon.stub().withArgs(0).returns(@canvas)
$.withArgs('canvas#'+@container).returns(elements)
@dates = ["2015-06-17", "2015-06-18", "2015-06-19"]
@values = [1.2, 2.3, 3.4]
opts = {
bezierCurve: false,
responsive: true,
maintainAspectRatio: false
}
data = {
labels : @dates,
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#000",
data : @values
}
]
}
@line = sinon.stub().withArgs(data, opts)
sinon.stub(window, 'Chart').withArgs(canvas_context).returns({Line: @line})
context 'when the graphic exists', ->
it 'should render the chart', ->
Module.Graphic.display(@dates, @values, @container)
sinon.assert.calledOnce(@line)
context 'when the graphic does not exists', ->
beforeEach ->
@chart = sinon.stub()
@chart.destroy = sinon.stub()
@canvas.chart = @chart
@canvas.hasOwnProperty = sinon.stub().withArgs("chart").returns(true)
it 'should destroy the previous chart and render a new one', ->
Module.Graphic.display(@dates, @values, @container)
sinon.assert.calledOnce(@chart.destroy)
sinon.assert.calledOnce(@line)
afterEach ->
Chart.restore()
after ->
$.restore()