Skip to content

Commit 09c763c

Browse files
committed
Add test for global options in window.Apex
1 parent 1cbbf63 commit 09c763c

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

lib/apex_charts/renderer.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def initialize(options)
1919

2020
def render
2121
html = ''
22-
html = window_apex if id_number == '1' && !ApexCharts.config.default_options.empty?
22+
html = window_apex if id_number == '1' && !default_options.empty?
2323

2424
chart_rendering = <<~JS
2525
var #{variable} = new ApexCharts(document.querySelector("##{element_id}"), #{substitute_function_object(options.to_json)});
@@ -97,7 +97,7 @@ def style
9797
end
9898

9999
def window_apex
100-
script("window.Apex = #{ApexCharts.config.default_options.to_json}")
100+
script("window.Apex = #{default_options.to_json}")
101101
end
102102

103103
def script(js)
@@ -114,5 +114,12 @@ def indent(content, times=2)
114114
(index.zero? ? '' : ' ' * times) + line
115115
end.join
116116
end
117+
118+
def default_options
119+
@default_options ||=
120+
ApexCharts.config.default_options.reject do |option|
121+
%i[defer module].include?(option)
122+
end
123+
end
117124
end
118125
end

spec/renderer_spec.rb

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,44 @@
3737
expect(parsed.at("script:contains('function(value){return value + \" rabbits\"')")).not_to \
3838
be_nil
3939
end
40-
end
4140

42-
context '.render when defer = true' do
43-
it 'renders div and script elements' do
44-
html = described_class.render(options.merge({defer: true}))
41+
context 'when global options are set' do
42+
let(:default_options) { { tootip: true } }
43+
44+
before do
45+
allow(ApexCharts.config)
46+
.to receive(:default_options)
47+
.and_return(default_options)
48+
end
49+
50+
it 'renders a script with window.Apex initialization ' \
51+
'before rendering first chart' do
52+
html = described_class.render(options.merge(div: {id: 'chart-1'}))
53+
parsed = Nokogiri::HTML.parse(html)
54+
script = parsed.css('script')
55+
56+
expect(script).not_to be_empty
4557

46-
expect(html).to include 'var createChart = function()'
47-
expect(html).to include 'window.addEventListener'
58+
expect(script.text)
59+
.to include("window.Apex = #{default_options.to_json}")
60+
end
4861
end
49-
end
5062

51-
context '.render when module = true' do
52-
it 'renders div and script elements' do
53-
html = described_class.render(options.merge({module: true}))
63+
context 'when defer = true' do
64+
it 'renders div and script elements' do
65+
html = described_class.render(options.merge({defer: true}))
66+
67+
expect(html).to include 'var createChart = function()'
68+
expect(html).to include 'window.addEventListener'
69+
end
70+
end
71+
72+
context 'when module = true' do
73+
it 'renders div and script elements' do
74+
html = described_class.render(options.merge({module: true}))
5475

55-
expect(html).to include 'type="module"'
76+
expect(html).to include 'type="module"'
77+
end
5678
end
5779
end
5880
end

0 commit comments

Comments
 (0)