Skip to content

Commit 23ed46f

Browse files
authored
Merge pull request #90 from Prakriti-nith/highcharts_spec
Added highcharts.rb specs
2 parents 20acde5 + f8c07b6 commit 23ed46f

File tree

2 files changed

+264
-16
lines changed

2 files changed

+264
-16
lines changed

lib/daru/view/adapters/highcharts.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def generate_html(plot)
7272
path = File.expand_path('../templates/highcharts/static_html.erb', __dir__)
7373
template = File.read(path)
7474
initial_script = init_script
75-
chart_div = generate_body(plots)
75+
chart_div = generate_body(plot)
7676
ERB.new(template).result(binding)
7777
end
7878

spec/adapters/highcharts_spec.rb

+263-15
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,63 @@
44
describe Daru::View::Plot, 'plotting with highcharts' do
55
before { Daru::View.plotting_library = :highcharts }
66
before(:each) do
7-
@data = [[1, 15], [2, 30], [4, 40]]
8-
97
@placeholder = "placeholder"
108
@html_options = {:class => "stylin"}
11-
@options = {:bars => {:show => true}}
12-
13-
@flot = Daru::View::Plot.new(@data, @options)
9+
@data = [[1, 15], [2, 30], [4, 40]]
10+
@data_vec1 = Daru::Vector.new([3,6,8,9,20])
11+
@data_vec2 = Daru::Vector.new([9,4,1,3,12])
12+
@data_df = Daru::DataFrame.new(rows: @data_vec1, values: @data_vec2)
13+
@options_bar = {
14+
chart: {
15+
type: 'bar',
16+
},
17+
title: {
18+
text: 'Demo Bar Chart'
19+
},
20+
plotOptions: {
21+
bar: {
22+
dataLabels: {
23+
enabled: true
24+
}
25+
}
26+
}
27+
}
28+
@options_line = {
29+
chart: {
30+
type: 'line'
31+
},
32+
title: {
33+
text: 'Demo line Chart'
34+
},
35+
plotOptions: {
36+
line: {
37+
dataLabels: {
38+
enabled: true
39+
},
40+
enableMouseTracking: false
41+
}
42+
}
43+
}
44+
@options_column = {
45+
chart: {
46+
type: 'column'
47+
},
48+
title: {
49+
text: 'Demo Column Chart'
50+
}
51+
}
52+
@series_dt = [{
53+
name: 'Tokyo',
54+
data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
55+
}, {
56+
name: 'New York',
57+
data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]
58+
}]
59+
@chart_line = Daru::View::Plot.new(@data_vec1, @options_line)
60+
@chart_bar = Daru::View::Plot.new(@data_df, @options_bar)
61+
@chart_column = Daru::View::Plot.new
62+
@chart_column.chart.options = @options_column
63+
@chart_column.chart.series_data = @data
1464
end
1565

1666
describe "initialization" do
@@ -41,16 +91,17 @@
4191

4292
it "should set options by default" do
4393
expect(Daru::View::Plot.new.chart.options).to eq({
44-
:title => {:text => nil},
45-
:legend => {:layout => "vertical", :style => {}},
46-
:xAxis => {},
47-
:yAxis => {:title => {:text => nil}, :labels => {}},
48-
:tooltip => {:enabled => true},
49-
:credits => {:enabled => false},
50-
:plotOptions => {:areaspline => {}},
51-
:chart => {:defaultSeriesType => "line", :renderTo => nil},
52-
:subtitle => {}})
53-
end
94+
:title => {:text => nil},
95+
:legend => {:layout => "vertical", :style => {}},
96+
:xAxis => {},
97+
:yAxis => {:title => {:text => nil}, :labels => {}},
98+
:tooltip => {:enabled => true},
99+
:credits => {:enabled => false},
100+
:plotOptions => {:areaspline => {}},
101+
:chart => {:defaultSeriesType => "line", :renderTo => nil},
102+
:subtitle => {}
103+
})
104+
end
54105

55106
it "should set data empty by default" do
56107
expect(Daru::View::Plot.new.chart.series_data).to eq(
@@ -174,4 +225,201 @@
174225
end
175226

176227
end
228+
229+
describe "#init_script" do
230+
it "generates valid initial script" do
231+
js = @chart_bar.init_script
232+
expect(js).to match(/BEGIN highstock.js/i)
233+
expect(js).to match(/Highstock JS/i)
234+
expect(js).to match(/END highstock.js/i)
235+
expect(js).to match(/BEGIN modules\/exporting.js/i)
236+
expect(js).to match(/END modules\/exporting.js/i)
237+
expect(js).to match(/BEGIN highcharts-3d.js/i)
238+
expect(js).to match(/END highcharts-3d.js/i)
239+
expect(js).to match(/BEGIN modules\/data.js/i)
240+
expect(js).to match(/END modules\/data.js/i)
241+
expect(js).to match(/<script type='text\/javascript'>/i)
242+
expect(js).to match(
243+
/var event = document.createEvent\(\"HTMLEvents\"\)/i)
244+
expect(js).to match(
245+
/event.initEvent\(\"load_highcharts\", false, false\)/i)
246+
expect(js).to match(/window.dispatchEvent\(event\)/i)
247+
expect(js).to match(
248+
/console.log\(\"Finish loading highchartsjs\"\)/i)
249+
end
250+
end
251+
252+
# called from #div in plot.rb as @chart_line.div
253+
describe "#generate_body" do
254+
context "should generate valid script of Line Chart" do
255+
it "should generate valid JS of the Line Chart" do
256+
js = @chart_line.adapter.generate_body(@chart_line.chart)
257+
expect(js).to match(/script/)
258+
expect(js).to match(/Highcharts.Chart\(options\)/)
259+
expect(js).to match(/window.chart_/)
260+
end
261+
it "should set the correct options" do
262+
js = @chart_line.adapter.generate_body(@chart_line.chart)
263+
expect(js).to match(/\"chart\": { \"type\": \"line\"/)
264+
expect(js).to match(/\"title\": { \"text\": \"Demo line Chart\" }/)
265+
expect(js).to match(
266+
/\"plotOptions\": { \"line\": { \"dataLabels\": { \"enabled\": true }/
267+
)
268+
end
269+
it "should set correct data" do
270+
js = @chart_line.adapter.generate_body(@chart_line.chart)
271+
expect(js).to match(/series/)
272+
expect(js).to match(/\"type\": null/)
273+
expect(js).to match(/\"name\": null/)
274+
expect(js).to match(/\"data\": \[ 3,6,8,9,20 \]/)
275+
end
276+
end
277+
context "should generate valid script of Bar Chart" do
278+
it "should generate valid JS of the Bar Chart" do
279+
js = @chart_bar.adapter.generate_body(@chart_bar.chart)
280+
expect(js).to match(/script/)
281+
expect(js).to match(/Highcharts.Chart\(options\)/)
282+
expect(js).to match(/window.chart_/)
283+
end
284+
it "should set the correct options" do
285+
js = @chart_bar.adapter.generate_body(@chart_bar.chart)
286+
expect(js).to match(/\"chart\": { \"type\": \"bar\"/)
287+
expect(js).to match(/\"title\": { \"text\": \"Demo Bar Chart\" }/)
288+
expect(js).to match(
289+
/\"plotOptions\": { \"bar\": { \"dataLabels\": { \"enabled\": true }/
290+
)
291+
end
292+
it "should set correct data" do
293+
js = @chart_bar.adapter.generate_body(@chart_bar.chart)
294+
expect(js).to match(/series/)
295+
expect(js).to match(/\"type\": null/)
296+
expect(js).to match(/\"name\": null/)
297+
expect(js).to match(
298+
/\"data\": \[ \[ 3,9 \],\[ 6,4 \],\[ 8,1 \],\[ 9,3 \],\[ 20,12 \] \]/
299+
)
300+
end
301+
end
302+
context "should generate valid script of Column Chart" do
303+
it "should generate valid JS of the Column Chart" do
304+
js = @chart_column.adapter.generate_body(@chart_column.chart)
305+
expect(js).to match(/script/)
306+
expect(js).to match(/Highcharts.Chart\(options\)/)
307+
expect(js).to match(/window.chart_/)
308+
end
309+
it "should set the correct options" do
310+
js = @chart_column.adapter.generate_body(@chart_column.chart)
311+
expect(js).to match(/\"chart\": { \"type\": \"column\"/)
312+
expect(js).to match(/\"title\": { \"text\": \"Demo Column Chart\" }/)
313+
end
314+
it "should set correct data" do
315+
js = @chart_column.adapter.generate_body(@chart_column.chart)
316+
expect(js).to match(/series": \[\[ 1,15 \],\[ 2,30 \],\[ 4,40 \]\]/)
317+
end
318+
end
319+
end
320+
321+
describe "#generate_html" do
322+
it "should generate valid HTML of the Line Chart" do
323+
html = @chart_line.adapter.generate_html(@chart_line.chart)
324+
expect(html).to match(/html/)
325+
expect(html).to match(/Chart/)
326+
expect(html).to match(/BEGIN highstock.js/i)
327+
expect(html).to match(/END highstock.js/i)
328+
expect(html).to match(/BEGIN modules\/exporting.js/i)
329+
expect(html).to match(/END modules\/exporting.js/i)
330+
expect(html).to match(/BEGIN highcharts-3d.js/i)
331+
expect(html).to match(/END highcharts-3d.js/i)
332+
expect(html).to match(/BEGIN modules\/data.js/i)
333+
expect(html).to match(/END modules\/data.js/i)
334+
expect(html).to match(/script/)
335+
expect(html).to match(/Highcharts.Chart\(options\)/)
336+
expect(html).to match(/\"chart\": { \"type\": \"line\"/)
337+
expect(html).to match(/\"title\": { \"text\": \"Demo line Chart\" }/)
338+
expect(html).to match(/\"data\": \[ 3,6,8,9,20 \]/)
339+
end
340+
it "should generate valid HTML of the Bar Chart" do
341+
html = @chart_bar.adapter.generate_html(@chart_bar.chart)
342+
expect(html).to match(/html/)
343+
expect(html).to match(/Chart/)
344+
expect(html).to match(/BEGIN highstock.js/i)
345+
expect(html).to match(/END highstock.js/i)
346+
expect(html).to match(/BEGIN modules\/exporting.js/i)
347+
expect(html).to match(/END modules\/exporting.js/i)
348+
expect(html).to match(/BEGIN highcharts-3d.js/i)
349+
expect(html).to match(/END highcharts-3d.js/i)
350+
expect(html).to match(/BEGIN modules\/data.js/i)
351+
expect(html).to match(/END modules\/data.js/i)
352+
expect(html).to match(/script/)
353+
expect(html).to match(/Highcharts.Chart\(options\)/)
354+
expect(html).to match(/\"chart\": { \"type\": \"bar\"/)
355+
expect(html).to match(/\"title\": { \"text\": \"Demo Bar Chart\" }/)
356+
expect(html).to match(
357+
/\"data\": \[ \[ 3,9 \],\[ 6,4 \],\[ 8,1 \],\[ 9,3 \],\[ 20,12 \] \]/
358+
)
359+
end
360+
it "should generate valid HTML of the Column Chart" do
361+
html = @chart_column.adapter.generate_html(@chart_column.chart)
362+
expect(html).to match(/html/)
363+
expect(html).to match(/Chart/)
364+
expect(html).to match(/BEGIN highstock.js/i)
365+
expect(html).to match(/END highstock.js/i)
366+
expect(html).to match(/BEGIN modules\/exporting.js/i)
367+
expect(html).to match(/END modules\/exporting.js/i)
368+
expect(html).to match(/BEGIN highcharts-3d.js/i)
369+
expect(html).to match(/END highcharts-3d.js/i)
370+
expect(html).to match(/BEGIN modules\/data.js/i)
371+
expect(html).to match(/END modules\/data.js/i)
372+
expect(html).to match(/script/)
373+
expect(html).to match(/Highcharts.Chart\(options\)/)
374+
expect(html).to match(/\"chart\": { \"type\": \"column\"/)
375+
expect(html).to match(/\"title\": { \"text\": \"Demo Column Chart\" }/)
376+
expect(html).to match(/series": \[\[ 1,15 \],\[ 2,30 \],\[ 4,40 \]\]/)
377+
end
378+
end
379+
380+
describe "#export_html_file" do
381+
it "should write valid html code of the Line Chart to the file" do
382+
@chart_line.export_html_file('./plot.html')
383+
path = File.expand_path('../../plot.html', __dir__)
384+
html = File.read(path)
385+
expect(html).to match(/html/)
386+
expect(html).to match(/Chart/)
387+
expect(html).to match(/BEGIN highstock.js/i)
388+
expect(html).to match(/END highstock.js/i)
389+
expect(html).to match(/script/)
390+
expect(html).to match(/Highcharts.Chart\(options\)/)
391+
expect(html).to match(/\"chart\": { \"type\": \"line\"/)
392+
expect(html).to match(/\"title\": { \"text\": \"Demo line Chart\" }/)
393+
end
394+
it "should write valid html code of the Bar Chart to the file" do
395+
@chart_bar.export_html_file('./plot.html')
396+
path = File.expand_path('../../plot.html', __dir__)
397+
html = File.read(path)
398+
expect(html).to match(/html/)
399+
expect(html).to match(/Chart/)
400+
expect(html).to match(/BEGIN highstock.js/i)
401+
expect(html).to match(/END highstock.js/i)
402+
expect(html).to match(/script/)
403+
expect(html).to match(/Highcharts.Chart\(options\)/)
404+
expect(html).to match(/\"chart\": { \"type\": \"bar\"/)
405+
expect(html).to match(/\"title\": { \"text\": \"Demo Bar Chart\" }/)
406+
expect(html).to match(
407+
/\"data\": \[ \[ 3,9 \],\[ 6,4 \],\[ 8,1 \],\[ 9,3 \],\[ 20,12 \] \]/
408+
)
409+
end
410+
it "should write valid html code of the Column Chart to the file" do
411+
@chart_column.export_html_file('./plot.html')
412+
path = File.expand_path('../../plot.html', __dir__)
413+
html = File.read(path)
414+
expect(html).to match(/html/)
415+
expect(html).to match(/Chart/)
416+
expect(html).to match(/BEGIN highstock.js/i)
417+
expect(html).to match(/END highstock.js/i)
418+
expect(html).to match(/script/)
419+
expect(html).to match(/Highcharts.Chart\(options\)/)
420+
expect(html).to match(/\"chart\": { \"type\": \"column\"/)
421+
expect(html).to match(/\"title\": { \"text\": \"Demo Column Chart\" }/)
422+
expect(html).to match(/series": \[\[ 1,15 \],\[ 2,30 \],\[ 4,40 \]\]/)
423+
end
424+
end
177425
end

0 commit comments

Comments
 (0)