New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added highcharts.rb specs #90

Merged
merged 2 commits into from May 23, 2018
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+259 −12
Diff settings

Always

Just for now

Next

Added highcharts.rb specs

  • Loading branch information...
Prakriti-nith committed May 19, 2018
commit 2329a0202f2b0a5660c7a851e830e12c9614fde4
@@ -72,7 +72,7 @@ def generate_html(plot)
path = File.expand_path('../templates/highcharts/static_html.erb', __dir__)
template = File.read(path)
initial_script = init_script
chart_div = generate_body(plots)
chart_div = generate_body(plot)
ERB.new(template).result(binding)
end

@@ -29,18 +29,18 @@ def encapsulate_js_iruby(core_js)
"#{js_start_iruby} #{core_js} #{js_end_iruby}"
# Turbolinks.version < 5
elsif defined?(Turbolinks) && request_is_referrer?
to_s(eventlistener_page_load)
eventlistener_page_load(core_js)
elsif defined?(Turbolinks) && request_turbolinks_5_tureferrer?
to_s(eventlistener_turbolinks_load)
eventlistener_turbolinks_load(core_js)
else
to_s(call_core_js)
call_core_js(core_js)
end

defined?(raw) ? raw(js_output) : js_output
end
# rubocop:enable Metrics/PerceivedComplexity

def eventlistener_page_load
def eventlistener_page_load(core_js)
<<-EOJS
#{js_start_iruby}
var f = function(){
@@ -52,7 +52,7 @@ def eventlistener_page_load
EOJS
end

def eventlistener_turbolinks_load
def eventlistener_turbolinks_load(core_js)
<<-EOJS
#{js_start_iruby}
document.addEventListener("turbolinks:load", function() {
@@ -62,7 +62,7 @@ def eventlistener_turbolinks_load
EOJS
end

def call_core_js
def call_core_js(core_js)
<<-EOJS
#{js_start_iruby}
#{core_js}
Copy path View file
@@ -4,13 +4,63 @@
describe Daru::View::Plot, 'plotting with highcharts' do
before { Daru::View.plotting_library = :highcharts }
before(:each) do
@data = [[1, 15], [2, 30], [4, 40]]

@placeholder = "placeholder"
@html_options = {:class => "stylin"}
@options = {:bars => {:show => true}}

@flot = Daru::View::Plot.new(@data, @options)
@data = [[1, 15], [2, 30], [4, 40]]
@data_vec1 = Daru::Vector.new([3,6,8,9,20])
@data_vec2 = Daru::Vector.new([9,4,1,3,12])
@data_df = Daru::DataFrame.new(rows: @data_vec1, values: @data_vec2)
@options_bar = {
chart: {
type: 'bar',
},
title: {
text: 'Demo Bar Chart'
},
plotOptions: {
bar: {
dataLabels: {
enabled: true
}
}
}
}
@options_line = {
chart: {
type: 'line'
},
title: {
text: 'Demo line Chart'
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: false
}
}
}
@options_column = {
chart: {
type: 'column'
},
title: {
text: 'Demo Column Chart'
}
}
@series_dt = [{
name: 'Tokyo',
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]
}, {
name: 'New York',
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]
}]
@chart_line = Daru::View::Plot.new(@data_vec1, @options_line)
@chart_bar = Daru::View::Plot.new(@data_df, @options_bar)
@chart_column = Daru::View::Plot.new
@chart_column.chart.options = @options_column
@chart_column.chart.series_data = @data
end

describe "initialization" do
@@ -174,4 +224,201 @@
end

end

describe "#init_script" do
it "generates valid initial script" do
js = @chart_bar.init_script
expect(js).to match(/BEGIN highstock.js/i)
expect(js).to match(/Highstock JS/i)
expect(js).to match(/END highstock.js/i)
expect(js).to match(/BEGIN modules\/exporting.js/i)
expect(js).to match(/END modules\/exporting.js/i)
expect(js).to match(/BEGIN highcharts-3d.js/i)
expect(js).to match(/END highcharts-3d.js/i)
expect(js).to match(/BEGIN modules\/data.js/i)
expect(js).to match(/END modules\/data.js/i)
expect(js).to match(/<script type='text\/javascript'>/i)
expect(js).to match(
/var event = document.createEvent\(\"HTMLEvents\"\)/i)
expect(js).to match(
/event.initEvent\(\"load_highcharts\", false, false\)/i)
expect(js).to match(/window.dispatchEvent\(event\)/i)
expect(js).to match(
/console.log\(\"Finish loading highchartsjs\"\)/i)
end
end

# called from #div in plot.rb as @chart_line.div
describe "#generate_body" do
context "should generate valid script of Line Chart" do
it "should generate valid JS of the Line Chart" do
js = @chart_line.adapter.generate_body(@chart_line.chart)
expect(js).to match(/script/)
expect(js).to match(/Highcharts.Chart\(options\)/)
expect(js).to match(/window.chart_/)
end
it "should set the correct options" do
js = @chart_line.adapter.generate_body(@chart_line.chart)
expect(js).to match(/\"chart\": { \"type\": \"line\"/)
expect(js).to match(/\"title\": { \"text\": \"Demo line Chart\" }/)
expect(js).to match(
/\"plotOptions\": { \"line\": { \"dataLabels\": { \"enabled\": true }/
)
end
it "should set correct data" do
js = @chart_line.adapter.generate_body(@chart_line.chart)
expect(js).to match(/series/)
expect(js).to match(/\"type\": null/)
expect(js).to match(/\"name\": null/)
expect(js).to match(/\"data\": \[ 3,6,8,9,20 \]/)
end
end
context "should generate valid script of Bar Chart" do
it "should generate valid JS of the Bar Chart" do
js = @chart_bar.adapter.generate_body(@chart_bar.chart)
expect(js).to match(/script/)
expect(js).to match(/Highcharts.Chart\(options\)/)
expect(js).to match(/window.chart_/)
end
it "should set the correct options" do
js = @chart_bar.adapter.generate_body(@chart_bar.chart)
expect(js).to match(/\"chart\": { \"type\": \"bar\"/)
expect(js).to match(/\"title\": { \"text\": \"Demo Bar Chart\" }/)
expect(js).to match(
/\"plotOptions\": { \"bar\": { \"dataLabels\": { \"enabled\": true }/
)
end
it "should set correct data" do
js = @chart_bar.adapter.generate_body(@chart_bar.chart)
expect(js).to match(/series/)
expect(js).to match(/\"type\": null/)
expect(js).to match(/\"name\": null/)
expect(js).to match(
/\"data\": \[ \[ 3,9 \],\[ 6,4 \],\[ 8,1 \],\[ 9,3 \],\[ 20,12 \] \]/
)
end
end
context "should generate valid script of Column Chart" do
it "should generate valid JS of the Column Chart" do
js = @chart_column.adapter.generate_body(@chart_column.chart)
expect(js).to match(/script/)
expect(js).to match(/Highcharts.Chart\(options\)/)
expect(js).to match(/window.chart_/)
end
it "should set the correct options" do
js = @chart_column.adapter.generate_body(@chart_column.chart)
expect(js).to match(/\"chart\": { \"type\": \"column\"/)
expect(js).to match(/\"title\": { \"text\": \"Demo Column Chart\" }/)
end
it "should set correct data" do
js = @chart_column.adapter.generate_body(@chart_column.chart)
expect(js).to match(/series": \[\[ 1,15 \],\[ 2,30 \],\[ 4,40 \]\]/)
end
end
end

describe "#generate_html" do
it "should generate valid HTML of the Line Chart" do
html = @chart_line.adapter.generate_html(@chart_line.chart)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN highstock.js/i)
expect(html).to match(/END highstock.js/i)
expect(html).to match(/BEGIN modules\/exporting.js/i)
expect(html).to match(/END modules\/exporting.js/i)
expect(html).to match(/BEGIN highcharts-3d.js/i)
expect(html).to match(/END highcharts-3d.js/i)
expect(html).to match(/BEGIN modules\/data.js/i)
expect(html).to match(/END modules\/data.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"line\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo line Chart\" }/)
expect(html).to match(/\"data\": \[ 3,6,8,9,20 \]/)
end
it "should generate valid HTML of the Bar Chart" do
html = @chart_bar.adapter.generate_html(@chart_bar.chart)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN highstock.js/i)
expect(html).to match(/END highstock.js/i)
expect(html).to match(/BEGIN modules\/exporting.js/i)
expect(html).to match(/END modules\/exporting.js/i)
expect(html).to match(/BEGIN highcharts-3d.js/i)
expect(html).to match(/END highcharts-3d.js/i)
expect(html).to match(/BEGIN modules\/data.js/i)
expect(html).to match(/END modules\/data.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"bar\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo Bar Chart\" }/)
expect(html).to match(
/\"data\": \[ \[ 3,9 \],\[ 6,4 \],\[ 8,1 \],\[ 9,3 \],\[ 20,12 \] \]/
)
end
it "should generate valid HTML of the Column Chart" do
html = @chart_column.adapter.generate_html(@chart_column.chart)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN highstock.js/i)
expect(html).to match(/END highstock.js/i)
expect(html).to match(/BEGIN modules\/exporting.js/i)
expect(html).to match(/END modules\/exporting.js/i)
expect(html).to match(/BEGIN highcharts-3d.js/i)
expect(html).to match(/END highcharts-3d.js/i)
expect(html).to match(/BEGIN modules\/data.js/i)
expect(html).to match(/END modules\/data.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"column\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo Column Chart\" }/)
expect(html).to match(/series": \[\[ 1,15 \],\[ 2,30 \],\[ 4,40 \]\]/)
end
end

describe "#export_html_file" do
it "should write valid html code of the Line Chart to the file" do
@chart_line.export_html_file('./plot.html')
path = File.expand_path('../../plot.html', __dir__)
html = File.read(path)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN highstock.js/i)
expect(html).to match(/END highstock.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"line\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo line Chart\" }/)
end
it "should write valid html code of the Bar Chart to the file" do
@chart_bar.export_html_file('./plot.html')
path = File.expand_path('../../plot.html', __dir__)
html = File.read(path)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN highstock.js/i)
expect(html).to match(/END highstock.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"bar\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo Bar Chart\" }/)
expect(html).to match(
/\"data\": \[ \[ 3,9 \],\[ 6,4 \],\[ 8,1 \],\[ 9,3 \],\[ 20,12 \] \]/
)
end
it "should write valid html code of the Column Chart to the file" do
@chart_column.export_html_file('./plot.html')
path = File.expand_path('../../plot.html', __dir__)
html = File.read(path)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN highstock.js/i)
expect(html).to match(/END highstock.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"column\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo Column Chart\" }/)
expect(html).to match(/series": \[\[ 1,15 \],\[ 2,30 \],\[ 4,40 \]\]/)
end
end
end
ProTip! Use n and p to navigate between commits in a pull request.