From 09c763c5fe81f3df7dac4e6e319e666e0ea061d0 Mon Sep 17 00:00:00 2001 From: Adrian Setyadi Date: Sun, 20 Oct 2024 16:03:24 +0700 Subject: [PATCH] Add test for global options in window.Apex --- lib/apex_charts/renderer.rb | 11 ++++++++-- spec/renderer_spec.rb | 44 +++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/apex_charts/renderer.rb b/lib/apex_charts/renderer.rb index 2acbcc6..dfcb1c3 100644 --- a/lib/apex_charts/renderer.rb +++ b/lib/apex_charts/renderer.rb @@ -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)}); @@ -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) @@ -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 diff --git a/spec/renderer_spec.rb b/spec/renderer_spec.rb index adf099e..c050055 100644 --- a/spec/renderer_spec.rb +++ b/spec/renderer_spec.rb @@ -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