Skip to content

Commit

Permalink
Allow Symbols to be used as selectors
Browse files Browse the repository at this point in the history
This commit allows using a Symbol as a selector. The symbol is converted
to a String using `to_s`.

```ruby
class TodoIndex < PageEz::Page
  has_one :heading, :h1
end
```

```ruby
todo_index = TodoIndex.new

expect(todo_index).to have_heading(text: "Hello")
```
  • Loading branch information
eebs authored and joshuaclayton committed Jul 16, 2023
1 parent 36abf53 commit 6810607
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/page_ez/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.has_one(name, *args, **options, &block)
case [args.length, args.first]
in [2, _] then selector, dynamic_options = args
in [1, Class] then composed_class = args.first
in [1, String] then selector = args.first
in [1, String] | [1, Symbol] then selector = args.first.to_s
end

visitor.process_macro(:has_one, name, selector)
Expand Down
38 changes: 38 additions & 0 deletions spec/features/selectors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "spec_helper"

RSpec.describe "Selectors" do
it "allows string selectors" do
page = build_page(<<-HTML)
<h1>Hello</h1>
HTML

test_page = Class.new(PageEz::Page) do
has_one :heading, "h1"
end.new(page)

page.visit "/"

expect(test_page).to have_heading
end

it "allows symbol selectors" do
page = build_page(<<-HTML)
<h1>Hello</h1>
HTML

test_page = Class.new(PageEz::Page) do
has_one :heading, :h1
end.new(page)

page.visit "/"

expect(test_page).to have_heading
end

def build_page(markup)
AppGenerator
.new
.route("/", markup)
.run
end
end

0 comments on commit 6810607

Please sign in to comment.