|
| 1 | +require 'spec_helper.rb' |
| 2 | + |
| 3 | +describe LazyHighCharts do |
| 4 | + before { Daru::View.plotting_library = :highcharts } |
| 5 | + describe "#init_script" do |
| 6 | + it "generates valid initial script" do |
| 7 | + js = LazyHighCharts.init_script |
| 8 | + expect(js).to match(/BEGIN highstock.js/i) |
| 9 | + expect(js).to match(/Highstock JS/i) |
| 10 | + expect(js).to match(/END highstock.js/i) |
| 11 | + expect(js).to match(/BEGIN highcharts-more.js/i) |
| 12 | + expect(js).to match(/END highcharts-more.js/i) |
| 13 | + expect(js).to match(/BEGIN modules\/exporting.js/i) |
| 14 | + expect(js).to match(/END modules\/exporting.js/i) |
| 15 | + expect(js).to match(/BEGIN highcharts-3d.js/i) |
| 16 | + expect(js).to match(/END highcharts-3d.js/i) |
| 17 | + expect(js).to match(/BEGIN modules\/data.js/i) |
| 18 | + expect(js).to match(/END modules\/data.js/i) |
| 19 | + expect(js).to match(/<script type='text\/javascript'>/i) |
| 20 | + expect(js).to match( |
| 21 | + /var event = document.createEvent\(\"HTMLEvents\"\)/i) |
| 22 | + expect(js).to match( |
| 23 | + /event.initEvent\(\"load_highcharts\", false, false\)/i) |
| 24 | + expect(js).to match(/window.dispatchEvent\(event\)/i) |
| 25 | + expect(js).to match( |
| 26 | + /console.log\(\"Finish loading highchartsjs\"\)/i) |
| 27 | + end |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +describe LazyHighCharts::HighChart do |
| 32 | + before { Daru::View.plotting_library = :highcharts } |
| 33 | + before(:each) do |
| 34 | + @opts = { |
| 35 | + chart: { |
| 36 | + type: 'bar' |
| 37 | + }, |
| 38 | + title: { |
| 39 | + text: 'Bar chart' |
| 40 | + }, |
| 41 | + yAxis: { |
| 42 | + min: 0, |
| 43 | + title: { |
| 44 | + text: 'Total consumption' |
| 45 | + } |
| 46 | + }, |
| 47 | + legend: { |
| 48 | + reversed: true |
| 49 | + } |
| 50 | + } |
| 51 | + @data_vec1 = Daru::Vector.new([5, 3, 4]) |
| 52 | + @data_vec2 = Daru::Vector.new([2, 2, 3]) |
| 53 | + @data_vec3 = Daru::Vector.new([3,4,4]) |
| 54 | + @data_df = Daru::DataFrame.new({John: @data_vec1, Jane: @data_vec2, Joe: @data_vec3}) |
| 55 | + @hc = Daru::View::Plot.new(@data_df, @opts) |
| 56 | + @placeholder = "placeholder" |
| 57 | + end |
| 58 | + |
| 59 | + describe "#to_html" do |
| 60 | + before(:each) do |
| 61 | + @opts = { |
| 62 | + chart_class: 'stock', |
| 63 | + chart: { |
| 64 | + type: 'arearange' |
| 65 | + }, |
| 66 | + rangeSelector: { |
| 67 | + selected: 1 |
| 68 | + }, |
| 69 | + |
| 70 | + title: { |
| 71 | + text: 'AAPL Stock Price' |
| 72 | + } |
| 73 | + } |
| 74 | + @series_dt = [ |
| 75 | + { |
| 76 | + name: 'AAPL Stock Price', |
| 77 | + data: [ |
| 78 | + [1147651200000,67.79], |
| 79 | + [1147737600000,64.98], |
| 80 | + [1147824000000,65.26], |
| 81 | + |
| 82 | + [1149120000000,62.17], |
| 83 | + [1149206400000,61.66], |
| 84 | + [1149465600000,60.00], |
| 85 | + [1149552000000,59.72], |
| 86 | + |
| 87 | + [1157932800000,72.50], |
| 88 | + [1158019200000,72.63], |
| 89 | + [1158105600000,74.20], |
| 90 | + [1158192000000,74.17], |
| 91 | + [1158278400000,74.10], |
| 92 | + [1158537600000,73.89], |
| 93 | + |
| 94 | + [1170288000000,84.74], |
| 95 | + [1170374400000,84.75], |
| 96 | + |
| 97 | + [1174953600000,95.46], |
| 98 | + [1175040000000,93.24], |
| 99 | + [1175126400000,93.75], |
| 100 | + [1175212800000,92.91], |
| 101 | + |
| 102 | + [1180051200000,113.62], |
| 103 | + [1180396800000,114.35], |
| 104 | + [1180483200000,118.77], |
| 105 | + [1180569600000,121.19], |
| 106 | + ], |
| 107 | + marker: { |
| 108 | + enabled: true, |
| 109 | + radius: 3 |
| 110 | + }, |
| 111 | + shadow: true, |
| 112 | + tooltip: { |
| 113 | + valueDecimals: 2 |
| 114 | + } |
| 115 | + } |
| 116 | + ] |
| 117 | + @chart = Daru::View::Plot.new |
| 118 | + @chart.chart.options = @opts; |
| 119 | + @chart.chart.series_data = @series_dt |
| 120 | + end |
| 121 | + it "should plot Highstock when chart_class is set to stock" do |
| 122 | + @hc.options[:chart_class] = "STock"; |
| 123 | + expect(@hc.chart.to_html( |
| 124 | + @placeholder) |
| 125 | + ).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.StockChart/) |
| 126 | + end |
| 127 | + it "should plot HighChart otherwise" do |
| 128 | + expect(@hc.chart.to_html( |
| 129 | + @placeholder) |
| 130 | + ).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.Chart/) |
| 131 | + end |
| 132 | + it "should return a div with an id of high_chart object" do |
| 133 | + expect(@chart.chart.to_html(@placeholder)).to match(/<div id="placeholder">/) |
| 134 | + expect(@hc.chart.to_html(@placeholder)).to match(/<div id="placeholder">/) |
| 135 | + end |
| 136 | + it "should return a script" do |
| 137 | + expect(@chart.chart.to_html(@placeholder)).to match(/script/) |
| 138 | + expect(@hc.chart.to_html(@placeholder)).to match(/script/) |
| 139 | + end |
| 140 | + it "should set variables `chart` `options`" do |
| 141 | + expect(@chart.chart.to_html(@placeholder)).to match(/var\s+options\s+=/) |
| 142 | + expect(@chart.chart.to_html(@placeholder)).to match(/window.chart_placeholder/) |
| 143 | + expect(@hc.chart.to_html(@placeholder)).to match(/var\s+options\s+=/) |
| 144 | + expect(@hc.chart.to_html(@placeholder)).to match(/window.chart_placeholder/) |
| 145 | + end |
| 146 | + it "should take a block setting attributes" do |
| 147 | + expect(@chart.chart.options[:rangeSelector][:selected]).to eq(1) |
| 148 | + expect(@chart.chart.to_html(@placeholder)).to match(/rangeSelector/) |
| 149 | + expect(@chart.chart.to_html( |
| 150 | + @placeholder) |
| 151 | + ).to match(/series\": \[\{ \"name\": \"AAPL Stock Price\"/) |
| 152 | + expect(@chart.chart.to_html( |
| 153 | + @placeholder) |
| 154 | + ).to match(/\"data\": \[ \[ 1147651200000,67.79 \]/) |
| 155 | + expect(@chart.chart.to_html( |
| 156 | + @placeholder) |
| 157 | + ).to match(/\"title\": \{ \"text\": \"AAPL Stock Price\" \}/) |
| 158 | + expect(@chart.chart.to_html( |
| 159 | + @placeholder) |
| 160 | + ).to match(/\"chart\": \{ \"type\": \"arearange\"/) |
| 161 | + expect(@chart.chart.to_html( |
| 162 | + @placeholder) |
| 163 | + ).to match(/\"marker\": \{ \"enabled\": true/) |
| 164 | + expect(@chart.chart.to_html(@placeholder)).to match(/\"shadow\": true/) |
| 165 | + expect(@chart.chart.to_html( |
| 166 | + @placeholder) |
| 167 | + ).to match(/\"tooltip\": \{ \"valueDecimals\": 2/) |
| 168 | + expect(@hc.chart.to_html( |
| 169 | + @placeholder) |
| 170 | + ).to match(/\"chart\": \{ \"type\": \"bar\"/) |
| 171 | + expect(@hc.chart.to_html( |
| 172 | + @placeholder) |
| 173 | + ).to match(/\"data\": \[ \[ 5,2,3 \],\[ 3,2,4 \],\[ 4,3,4 \] \]/) |
| 174 | + expect(@hc.chart.to_html( |
| 175 | + @placeholder) |
| 176 | + ).to match(/\"title\": \{ \"text\": \"Bar chart\" \}/) |
| 177 | + expect(@hc.chart.to_html( |
| 178 | + @placeholder) |
| 179 | + ).to match(/\"legend\": \{ \"reversed\": true \}/) |
| 180 | + expect(@hc.chart.to_html( |
| 181 | + @placeholder) |
| 182 | + ).to match(/\"yAxis\": \{ \"min\": 0/) |
| 183 | + end |
| 184 | + end |
| 185 | + |
| 186 | + describe "#to_html_iruby" do |
| 187 | + it "should plot Highstock when chart_class is set to stock" do |
| 188 | + @hc.options[:chart_class] = "SToCk"; |
| 189 | + expect(@hc.chart.to_html_iruby( |
| 190 | + @placeholder) |
| 191 | + ).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.StockChart/) |
| 192 | + end |
| 193 | + it "should plot HighChart otherwise" do |
| 194 | + expect(@hc.chart.to_html_iruby( |
| 195 | + @placeholder) |
| 196 | + ).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.Chart/) |
| 197 | + end |
| 198 | + end |
| 199 | + |
| 200 | + describe "#extract_chart_class" do |
| 201 | + it "should return StockChart class when chart_class is set to stock" do |
| 202 | + @hc.options[:chart_class] = "SToCk"; |
| 203 | + expect(@hc.chart.extract_chart_class).to eq 'StockChart' |
| 204 | + end |
| 205 | + it "should return Chart class when chart_class is not set" do |
| 206 | + expect(@hc.chart.extract_chart_class).to eq 'Chart' |
| 207 | + end |
| 208 | + it "should raise error when wrong input is given" do |
| 209 | + @hc.options[:chart_class] = "Daru" |
| 210 | + expect{@hc.chart.extract_chart_class}.to raise_error( |
| 211 | + 'chart_class must be selected as either chart, stock or map' |
| 212 | + ) |
| 213 | + end |
| 214 | + end |
| 215 | + |
| 216 | + describe "#chart_hash_must_be_present" do |
| 217 | + it "should check the presence of chart hash in options" do |
| 218 | + @hc.chart.chart_hash_must_be_present |
| 219 | + expect(@hc.options[:chart]).to eq :type=>"bar" |
| 220 | + end |
| 221 | + end |
| 222 | +end |
0 commit comments