Skip to content

Commit

Permalink
Add test for global options in window.Apex
Browse files Browse the repository at this point in the history
  • Loading branch information
styd committed Oct 20, 2024
1 parent 1cbbf63 commit 09c763c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
11 changes: 9 additions & 2 deletions lib/apex_charts/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(options)

def render
html = ''
html = window_apex if id_number == '1' && !ApexCharts.config.default_options.empty?
html = window_apex if id_number == '1' && !default_options.empty?

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

def window_apex
script("window.Apex = #{ApexCharts.config.default_options.to_json}")
script("window.Apex = #{default_options.to_json}")
end

def script(js)
Expand All @@ -114,5 +114,12 @@ def indent(content, times=2)
(index.zero? ? '' : ' ' * times) + line
end.join
end

def default_options
@default_options ||=
ApexCharts.config.default_options.reject do |option|
%i[defer module].include?(option)
end
end
end
end
44 changes: 33 additions & 11 deletions spec/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,44 @@
expect(parsed.at("script:contains('function(value){return value + \" rabbits\"')")).not_to \
be_nil
end
end

context '.render when defer = true' do
it 'renders div and script elements' do
html = described_class.render(options.merge({defer: true}))
context 'when global options are set' do
let(:default_options) { { tootip: true } }

before do
allow(ApexCharts.config)
.to receive(:default_options)
.and_return(default_options)
end

it 'renders a script with window.Apex initialization ' \
'before rendering first chart' do
html = described_class.render(options.merge(div: {id: 'chart-1'}))
parsed = Nokogiri::HTML.parse(html)
script = parsed.css('script')

expect(script).not_to be_empty

expect(html).to include 'var createChart = function()'
expect(html).to include 'window.addEventListener'
expect(script.text)
.to include("window.Apex = #{default_options.to_json}")
end
end
end

context '.render when module = true' do
it 'renders div and script elements' do
html = described_class.render(options.merge({module: true}))
context 'when defer = true' do
it 'renders div and script elements' do
html = described_class.render(options.merge({defer: true}))

expect(html).to include 'var createChart = function()'
expect(html).to include 'window.addEventListener'
end
end

context 'when module = true' do
it 'renders div and script elements' do
html = described_class.render(options.merge({module: true}))

expect(html).to include 'type="module"'
expect(html).to include 'type="module"'
end
end
end
end

0 comments on commit 09c763c

Please sign in to comment.