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

Implement documented predicate methods for has_many #47

Merged
merged 1 commit into from
Aug 25, 2023
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ todos_index.has_no_todo_matching?(text: "Buy milk") # => has_no_css?("ul li span

todos_index.todos.has_count_of?(number) # => has_css?("ul li span[data-role=todo-name]", count: number)
todos_index.has_todos_count?(number) # => has_css?("ul li span[data-role=todo-name]", count: number)

todos_index.todos.has_any_elements? # => has_css?("ul li span[data-role=todo-name]")
todos_index.todos.has_no_elements? # => has_no_css?("ul li span[data-role=todo-name]")
```

The methods defined by PageEz can be passed additional options from Capybara. Refer to documentation for the following methods:
Expand Down
11 changes: 11 additions & 0 deletions lib/page_ez/has_many_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,16 @@ def has_count_of?(count)
**@options.merge(count: count)
)
end

def has_any_elements?
@container.has_css?(
@selector,
**@options
)
end

def has_no_elements?
@container.has_no_css?(@selector, **@options)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def run(target)
target.logged_define_method("has_#{name}_count?") do |count, *args|
send(name, *args).has_count_of?(count)
end

target.logged_define_method("has_#{name}?") do |*args|
send(name, *args).has_any_elements?
end

target.logged_define_method("has_no_#{name}?") do |*args|
send(name, *args).has_no_elements?
end
end
end
end
Expand Down
50 changes: 50 additions & 0 deletions spec/features/capybara_javascript_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,54 @@
expect(test_page.list.items).to have_count_of(4)
end
end

it "allows for checking for any results" do
page = build_page(<<-HTML)
<template id="item">
<li></li>
</template>

<section>
<ul>
</ul>
</section>

<script type="text/javascript">
function generateItem(title, target) {
const template = document.querySelector("template#item");
const item = template.content.cloneNode(true);
item.querySelector("li").textContent = title;

target.appendChild(item);
}

document.addEventListener("DOMContentLoaded", () => {
const target = document.querySelector("section ul");

setTimeout(() => {
generateItem("Item 1", target);
generateItem("Item 2", target);
generateItem("Item 3", target);
}, 4500);
});
</script>
HTML

test_page = Class.new(PageEz::Page) do
has_one :list, "ul" do
has_many :items, "li"
end
end.new(page)

page.visit "/"

with_max_wait_time(seconds: 1) do
expect(test_page.list).not_to have_items
expect(test_page.list).to have_no_items
end

with_max_wait_time(seconds: 2) do
expect(test_page.list).to have_items
end
end
end
43 changes: 43 additions & 0 deletions spec/features/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,47 @@ def name_text
expect(test_page.sections("Section").map(&:text)).to eq(["Section 1", "Section 2"])
expect(test_page.sections("Bogus")).to be_empty
end

it "allows for checking for any or no elements" do
page = build_page(<<-HTML)
<div>
<ul>
<li>1-1</li>
<li>1-2</li>
<li>1-3</li>
</ul>

<ul>
</ul>
</div>
HTML

test_page = Class.new(PageEz::Page) do
has_one :first_list, "ul:nth-of-type(1)" do
has_many :items, "li"
end

has_one :second_list, "ul:nth-of-type(2)" do
has_many :items, "li"
end

has_one :third_list, "ul:nth-of-type(3)" do
has_many :items, "li"
end
end.new(page)

page.visit "/"

expect(test_page.first_list.items).to have_any_elements
expect(test_page.first_list.items).not_to have_no_elements

expect(test_page.second_list.items).not_to have_any_elements
expect(test_page.second_list.items).to have_no_elements

expect(test_page.first_list).to have_items
expect(test_page.first_list).not_to have_no_items

expect(test_page.second_list).not_to have_items
expect(test_page.second_list).to have_no_items
end
end