Skip to content

Commit

Permalink
Fall back to element name with no selector
Browse files Browse the repository at this point in the history
In cases where a page is utilizing semantic elements, it is convenient
to be able to omit the selector and use the semantic element name as the
selector.

```ruby
class PageWithSidebar < PageEz::Page
  has_one :sidebar, "aside"
  has_one :main do
    has_one :header do
      has_one :heading, "h1"
    end
    has_one :content, "section"
  end
end
```
  • Loading branch information
eebs authored and joshuaclayton committed Jul 16, 2023
1 parent 6810607 commit 7b9569a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/page_ez/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def self.has_one(name, *args, **options, &block)
in [2, _] then selector, dynamic_options = args
in [1, Class] then composed_class = args.first
in [1, String] | [1, Symbol] then selector = args.first.to_s
in [0, _] then selector = name.to_s
end

visitor.process_macro(:has_one, name, selector)
Expand Down
14 changes: 14 additions & 0 deletions spec/features/selectors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@
expect(test_page).to have_heading
end

it "uses the element name as a selector when one isn't given" do
page = build_page(<<-HTML)
<heading>Hello</heading>
HTML

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

page.visit "/"

expect(test_page).to have_heading
end

def build_page(markup)
AppGenerator
.new
Expand Down

0 comments on commit 7b9569a

Please sign in to comment.