Commit e9c0e204 authored by nmilojevic1's avatar nmilojevic1

Refactor reference filter spec

- Replace changed_nodes with new_nodes
parent 486ad968
...@@ -26,7 +26,7 @@ module Banzai ...@@ -26,7 +26,7 @@ module Banzai
super super
if update_nodes_enabled? if update_nodes_enabled?
@changed_nodes = {} @new_nodes = {}
@nodes = self.result[:reference_filter_nodes] @nodes = self.result[:reference_filter_nodes]
end end
end end
...@@ -166,31 +166,31 @@ module Banzai ...@@ -166,31 +166,31 @@ module Banzai
def replace_text_with_html(node, index, html) def replace_text_with_html(node, index, html)
if update_nodes_enabled? if update_nodes_enabled?
replace_and_update_changed_nodes(node, index, html) replace_and_update_new_nodes(node, index, html)
else else
node.replace(html) node.replace(html)
end end
end end
def replace_and_update_changed_nodes(node, index, html) def replace_and_update_new_nodes(node, index, html)
previous_node = node.previous previous_node = node.previous
next_node = node.next next_node = node.next
parent_node = node.parent parent_node = node.parent
# Unfortunately node.replace(html) returns re-parented nodes, not the actual replaced nodes in the doc # Unfortunately node.replace(html) returns re-parented nodes, not the actual replaced nodes in the doc
# We need to find the actual nodes in the doc that were replaced # We need to find the actual nodes in the doc that were replaced
node.replace(html) node.replace(html)
@changed_nodes[index] = [] @new_nodes[index] = []
# We find first replaced node. If previous_node is nil, we take first parent child # We replaced node with new nodes, so we find first new node. If previous_node is nil, we take first parent child
replaced_node = previous_node ? previous_node.next : parent_node&.children&.first new_node = previous_node ? previous_node.next : parent_node&.children&.first
# We iterate from first to last replaced node and store replaced nodes in @changed_nodes # We iterate from first to last replaced node and store replaced nodes in @new_nodes
while replaced_node && replaced_node != next_node while new_node && new_node != next_node
@changed_nodes[index] << replaced_node.xpath(query) @new_nodes[index] << new_node.xpath(query)
replaced_node = replaced_node.next new_node = new_node.next
end end
@changed_nodes[index].flatten! @new_nodes[index].flatten!
end end
def only_path? def only_path?
...@@ -198,14 +198,14 @@ module Banzai ...@@ -198,14 +198,14 @@ module Banzai
end end
def with_update_nodes def with_update_nodes
@changed_nodes = {} @new_nodes = {}
yield.tap { update_nodes! } yield.tap { update_nodes! }
end end
# Once Filter completes replacing nodes, we update nodes with @changed_nodes # Once Filter completes replacing nodes, we update nodes with @new_nodes
def update_nodes! def update_nodes!
@changed_nodes.sort_by { |index, _changed_nodes| -index }.each do |index, changed_nodes| @new_nodes.sort_by { |index, _new_nodes| -index }.each do |index, new_nodes|
nodes[index, 1] = changed_nodes nodes[index, 1] = new_nodes
end end
result[:reference_filter_nodes] = nodes result[:reference_filter_nodes] = nodes
end end
......
...@@ -59,10 +59,10 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -59,10 +59,10 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end end
end end
RSpec.shared_context 'changed nodes' do RSpec.shared_context 'new nodes' do
let(:nodes) { [{ value: "1" }, { value: "2" }, { value: "3" }] } let(:nodes) { [{ value: "1" }, { value: "2" }, { value: "3" }] }
let(:expected_nodes) { [{ value: "1.1" }, { value: "1.2" }, { value: "1.3" }, { value: "2.1" }, { value: "2.2" }, { value: "2.3" }, { value: "3.1" }, { value: "3.2" }, { value: "3.3" }] } let(:expected_nodes) { [{ value: "1.1" }, { value: "1.2" }, { value: "1.3" }, { value: "2.1" }, { value: "2.2" }, { value: "2.3" }, { value: "3.1" }, { value: "3.2" }, { value: "3.3" }] }
let(:changed_nodes) do let(:new_nodes) do
{ {
0 => [{ value: "1.1" }, { value: "1.2" }, { value: "1.3" }], 0 => [{ value: "1.1" }, { value: "1.2" }, { value: "1.3" }],
2 => [{ value: "3.1" }, { value: "3.2" }, { value: "3.3" }], 2 => [{ value: "3.1" }, { value: "3.2" }, { value: "3.3" }],
...@@ -95,20 +95,20 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -95,20 +95,20 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
expect(document.css('a').length).to eq 1 expect(document.css('a').length).to eq 1
end end
it 'calls replace_and_update_changed_nodes' do it 'calls replace_and_update_new_nodes' do
expect(filter).to receive(:replace_and_update_changed_nodes).with(filter.nodes[index], index, html) expect(filter).to receive(:replace_and_update_new_nodes).with(filter.nodes[index], index, html)
filter.send(method_name, *args) do filter.send(method_name, *args) do
html html
end end
end end
it 'stores filtered replaced nodes' do it 'stores filtered new nodes' do
filter.send(method_name, *args) do filter.send(method_name, *args) do
html html
end end
expect(filter.instance_variable_get(:@changed_nodes)).to eq({ index => [filter.each_node.to_a[index]] }) expect(filter.instance_variable_get(:@new_nodes)).to eq({ index => [filter.each_node.to_a[index]] })
end end
context "with update_nodes_for_banzai_reference_filter feature flag disabled" do context "with update_nodes_for_banzai_reference_filter feature flag disabled" do
...@@ -116,8 +116,8 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -116,8 +116,8 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
stub_feature_flags(update_nodes_for_banzai_reference_filter: false) stub_feature_flags(update_nodes_for_banzai_reference_filter: false)
end end
it 'does not call replace_and_update_changed_nodes' do it 'does not call replace_and_update_new_nodes' do
expect(filter).not_to receive(:replace_and_update_changed_nodes).with(filter.nodes[index], index, html) expect(filter).not_to receive(:replace_and_update_new_nodes).with(filter.nodes[index], index, html)
filter.send(method_name, *args) do filter.send(method_name, *args) do
html html
...@@ -158,8 +158,9 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -158,8 +158,9 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end end
end end
RSpec.shared_examples 'replaces text when pattern matches' do describe '#replace_text_when_pattern_matches' do
include_context 'document nodes' include_context 'document nodes'
let(:node) { Nokogiri::HTML.fragment('text @reference') }
let(:ref_pattern) { %r{(?<!\w)@(?<user>[a-zA-Z0-9_\-\.]*)}x } let(:ref_pattern) { %r{(?<!\w)@(?<user>[a-zA-Z0-9_\-\.]*)}x }
...@@ -177,16 +178,18 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -177,16 +178,18 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end end
end end
RSpec.shared_examples 'replaces link node when text' do describe '#replace_link_node_with_text' do
include_context 'document nodes' include_context 'document nodes'
let(:node) { Nokogiri::HTML.fragment('<a>end text</a>') }
it_behaves_like 'replaces document node', :replace_link_node_with_text do it_behaves_like 'replaces document node', :replace_link_node_with_text do
let(:existing_content) { node.text } let(:existing_content) { node.text }
end end
end end
RSpec.shared_examples 'replaces link node with href' do describe '#replace_link_node_with_href' do
include_context 'document nodes' include_context 'document nodes'
let(:node) { Nokogiri::HTML.fragment('<a href="link">end text</a>') }
let(:href_link) { CGI.unescape(node.attr('href').to_s) } let(:href_link) { CGI.unescape(node.attr('href').to_s) }
it_behaves_like 'replaces document node', :replace_link_node_with_href do it_behaves_like 'replaces document node', :replace_link_node_with_href do
...@@ -194,27 +197,9 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -194,27 +197,9 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end end
end end
describe '#replace_text_when_pattern_matches' do
it_behaves_like 'replaces text when pattern matches' do
let(:node) { Nokogiri::HTML.fragment('text @reference') }
end
end
describe '#replace_link_node_with_text' do
it_behaves_like 'replaces link node when text' do
let(:node) { Nokogiri::HTML.fragment('<a>end text</a>') }
end
end
describe '#replace_link_node_with_href' do
it_behaves_like 'replaces link node with href' do
let(:node) { Nokogiri::HTML.fragment('<a href="link">end text</a>') }
end
end
describe "#call_and_update_nodes" do describe "#call_and_update_nodes" do
context "with update_nodes_for_banzai_reference_filter feature flag enabled" do context "with update_nodes_for_banzai_reference_filter feature flag enabled" do
include_context 'changed nodes' include_context 'new nodes'
let(:document) { Nokogiri::HTML.fragment('<a href="foo">foo</a>') } let(:document) { Nokogiri::HTML.fragment('<a href="foo">foo</a>') }
let(:filter) { described_class.new(document, project: project) } let(:filter) { described_class.new(document, project: project) }
...@@ -222,10 +207,10 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -222,10 +207,10 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
stub_feature_flags(update_nodes_for_banzai_reference_filter: true) stub_feature_flags(update_nodes_for_banzai_reference_filter: true)
end end
it "replaces all nodes", :aggregate_failures do it "updates all new nodes", :aggregate_failures do
filter.instance_variable_set('@nodes', nodes) filter.instance_variable_set('@nodes', nodes)
expect(filter).to receive(:call) { filter.instance_variable_set('@changed_nodes', changed_nodes) } expect(filter).to receive(:call) { filter.instance_variable_set('@new_nodes', new_nodes) }
expect(filter).to receive(:with_update_nodes).and_call_original expect(filter).to receive(:with_update_nodes).and_call_original
expect(filter).to receive(:update_nodes!).and_call_original expect(filter).to receive(:update_nodes!).and_call_original
...@@ -236,7 +221,7 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -236,7 +221,7 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end end
context "with update_nodes_for_banzai_reference_filter feature flag disabled" do context "with update_nodes_for_banzai_reference_filter feature flag disabled" do
include_context 'changed nodes' include_context 'new nodes'
before do before do
stub_feature_flags(update_nodes_for_banzai_reference_filter: false) stub_feature_flags(update_nodes_for_banzai_reference_filter: false)
...@@ -247,7 +232,7 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -247,7 +232,7 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
filter = described_class.new(document, project: project) filter = described_class.new(document, project: project)
filter.instance_variable_set('@nodes', nodes) filter.instance_variable_set('@nodes', nodes)
expect(filter).to receive(:call) { filter.instance_variable_set('@changed_nodes', changed_nodes) } expect(filter).to receive(:call) { filter.instance_variable_set('@new_nodes', new_nodes) }
expect(filter).not_to receive(:with_update_nodes) expect(filter).not_to receive(:with_update_nodes)
expect(filter).not_to receive(:update_nodes!) expect(filter).not_to receive(:update_nodes!)
...@@ -260,7 +245,7 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -260,7 +245,7 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end end
describe ".call" do describe ".call" do
include_context 'changed nodes' include_context 'new nodes'
let(:document) { Nokogiri::HTML.fragment('<a href="foo">foo</a>') } let(:document) { Nokogiri::HTML.fragment('<a href="foo">foo</a>') }
...@@ -270,11 +255,11 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -270,11 +255,11 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
stub_feature_flags(update_nodes_for_banzai_reference_filter: true) stub_feature_flags(update_nodes_for_banzai_reference_filter: true)
end end
it "replaces all nodes", :aggregate_failures do it "updates all nodes", :aggregate_failures do
expect_next_instance_of(described_class) do |filter| expect_next_instance_of(described_class) do |filter|
expect(filter).to receive(:call_and_update_nodes).and_call_original expect(filter).to receive(:call_and_update_nodes).and_call_original
expect(filter).to receive(:with_update_nodes).and_call_original expect(filter).to receive(:with_update_nodes).and_call_original
expect(filter).to receive(:call) { filter.instance_variable_set('@changed_nodes', changed_nodes) } expect(filter).to receive(:call) { filter.instance_variable_set('@new_nodes', new_nodes) }
expect(filter).to receive(:update_nodes!).and_call_original expect(filter).to receive(:update_nodes!).and_call_original
end end
...@@ -290,11 +275,11 @@ RSpec.describe Banzai::Filter::ReferenceFilter do ...@@ -290,11 +275,11 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
stub_feature_flags(update_nodes_for_banzai_reference_filter: false) stub_feature_flags(update_nodes_for_banzai_reference_filter: false)
end end
it "replace all nodes", :aggregate_failures do it "updates all nodes", :aggregate_failures do
expect_next_instance_of(described_class) do |filter| expect_next_instance_of(described_class) do |filter|
expect(filter).to receive(:call_and_update_nodes).and_call_original expect(filter).to receive(:call_and_update_nodes).and_call_original
expect(filter).not_to receive(:with_update_nodes) expect(filter).not_to receive(:with_update_nodes)
expect(filter).to receive(:call) { filter.instance_variable_set('@changed_nodes', changed_nodes) } expect(filter).to receive(:call) { filter.instance_variable_set('@new_nodes', new_nodes) }
expect(filter).not_to receive(:update_nodes!) expect(filter).not_to receive(:update_nodes!)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment