Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set xpath flag if/when overwriting selector with previous_selector #278

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/cable_ready/operation_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module CableReady
class OperationBuilder
include Identifiable
attr_reader :identifier, :previous_selector
attr_reader :identifier, :previous_selector, :previous_xpath

def self.finalizer_for(identifier)
proc {
Expand Down Expand Up @@ -43,9 +43,13 @@ def add_operation_method(name)
options.each { |key, value| options[key] = value.send("to_#{key}".to_sym) if value.respond_to?("to_#{key}".to_sym) }
end
options["selector"] = selector if selector && options.exclude?("selector")
options["selector"] = previous_selector if previous_selector && options.exclude?("selector")
if previous_selector && options.exclude?("selector")
options["selector"] = previous_selector
options["xpath"] = previous_xpath if previous_xpath
hopsoft marked this conversation as resolved.
Show resolved Hide resolved
end
if options.include?("selector")
@previous_selector = options["selector"]
@previous_xpath = options["xpath"]
options["selector"] = identifiable?(previous_selector) ? dom_id(previous_selector) : previous_selector
end
options["operation"] = name.to_s.camelize(:lower)
Expand Down
13 changes: 13 additions & 0 deletions test/lib/cable_ready/operation_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ class CableReady::OperationBuilderTest < ActiveSupport::TestCase
assert_equal(operations, @operation_builder.operations_payload)
end

test "should set xpath correctly if re-using previous xpath selector in next operation" do
@operation_builder
.add_css_class(name: "test", selector: "/html/div[0]", xpath: true)
.inner_html(html: "<span>Narrow is the XPATH</span>")

operations = [
{"operation" => "addCssClass", "name" => "test", "selector" => "/html/div[0]", "xpath" => true},
{"operation" => "innerHtml", "html" => "<span>Narrow is the XPATH</span>", "selector" => "/html/div[0]", "xpath" => true}
]

assert_equal(operations, @operation_builder.operations_payload)
end

test "should clear previous_selector after calling reset!" do
@operation_builder.add_operation_method("inner_html")
@operation_builder.inner_html(selector: "#smelly", html: "<span>I rock</span>")
Expand Down