Skip to content

Commit

Permalink
Replace previous/next page selectors with helper methods in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Aug 29, 2024
1 parent 5c6d894 commit 9368c9e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 45 deletions.
45 changes: 30 additions & 15 deletions test/controllers/diary_entries_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,41 +449,42 @@ def test_index_language
def test_index_paged
# Create several pages worth of diary entries
create_list(:diary_entry, 50)
next_path = diary_entries_path

# Try and get the index
get diary_entries_path
get next_path
assert_response :success
assert_select "article.diary_post", :count => 20
assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
check_no_page_link "Newer Entries"
next_path = check_page_link "Older Entries"

# Try and get the second page
get css_select("li.page-item .page-link").last["href"]
get next_path
assert_response :success
assert_select "article.diary_post", :count => 20
assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
check_page_link "Newer Entries"
next_path = check_page_link "Older Entries"

# Try and get the third page
get css_select("li.page-item .page-link").last["href"]
get next_path
assert_response :success
assert_select "article.diary_post", :count => 10
assert_select "li.page-item.disabled span.page-link", :text => "Older Entries", :count => 1
assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
next_path = check_page_link "Newer Entries"
check_no_page_link "Older Entries"

# Go back to the second page
get css_select("li.page-item .page-link").first["href"]
get next_path
assert_response :success
assert_select "article.diary_post", :count => 20
assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
next_path = check_page_link "Newer Entries"
check_page_link "Older Entries"

# Go back to the first page
get css_select("li.page-item .page-link").first["href"]
get next_path
assert_response :success
assert_select "article.diary_post", :count => 20
assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
check_no_page_link "Newer Entries"
check_page_link "Older Entries"
end

def test_index_invalid_paged
Expand All @@ -497,6 +498,20 @@ def test_index_invalid_paged
end
end

private

def check_no_page_link(name)
assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
end

def check_page_link(name)
assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
return buttons.first.attributes["href"].value
end
end

public

def test_rss
create(:language, :code => "de")
create(:diary_entry, :language_code => "en")
Expand Down
76 changes: 46 additions & 30 deletions test/controllers/traces_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,51 +222,52 @@ def test_index_user
def test_index_paged
# Create several pages worth of traces
create_list(:trace, 50)
next_path = traces_path

# Try and get the index
get traces_path
get next_path
assert_response :success
assert_select "table#trace_list tbody", :count => 1 do
assert_select "tr", :count => 20
end
assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
check_no_page_link "Newer Traces"
next_path = check_page_link "Older Traces"

# Try and get the second page
get css_select("li.page-item a.page-link").last["href"]
get next_path
assert_response :success
assert_select "table#trace_list tbody", :count => 1 do
assert_select "tr", :count => 20
end
assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
check_page_link "Newer Traces"
next_path = check_page_link "Older Traces"

# Try and get the third page
get css_select("li.page-item a.page-link").last["href"]
get next_path
assert_response :success
assert_select "table#trace_list tbody", :count => 1 do
assert_select "tr", :count => 10
end
assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
assert_select "li.page-item.disabled span.page-link", :text => "Older Traces", :count => 2
next_path = check_page_link "Newer Traces"
check_no_page_link "Older Traces"

# Go back to the second page
get css_select("li.page-item a.page-link").first["href"]
get next_path
assert_response :success
assert_select "table#trace_list tbody", :count => 1 do
assert_select "tr", :count => 20
end
assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
next_path = check_page_link "Newer Traces"
check_page_link "Older Traces"

# Go back to the first page
get css_select("li.page-item a.page-link").first["href"]
get next_path
assert_response :success
assert_select "table#trace_list tbody", :count => 1 do
assert_select "tr", :count => 20
end
assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
check_no_page_link "Newer Traces"
check_page_link "Older Traces"
end

# Check a multi-page index of tagged traces
Expand All @@ -275,51 +276,52 @@ def test_index_tagged_paged
create_list(:trace, 100) do |trace, index|
create(:tracetag, :trace => trace, :tag => "London") if index.even?
end
next_path = traces_path :tag => "London"

# Try and get the index
get traces_path(:tag => "London")
get next_path
assert_response :success
assert_select "table#trace_list tbody", :count => 1 do
assert_select "tr", :count => 20
end
assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
check_no_page_link "Newer Traces"
next_path = check_page_link "Older Traces"

# Try and get the second page
get css_select("li.page-item a.page-link").last["href"]
get next_path
assert_response :success
assert_select "table#trace_list tbody", :count => 1 do
assert_select "tr", :count => 20
end
assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
check_page_link "Newer Traces"
next_path = check_page_link "Older Traces"

# Try and get the third page
get css_select("li.page-item a.page-link").last["href"]
get next_path
assert_response :success
assert_select "table#trace_list tbody", :count => 1 do
assert_select "tr", :count => 10
end
assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
assert_select "li.page-item.disabled span.page-link", :text => "Older Traces", :count => 2
next_path = check_page_link "Newer Traces"
check_no_page_link "Older Traces"

# Go back to the second page
get css_select("li.page-item a.page-link").first["href"]
get next_path
assert_response :success
assert_select "table#trace_list tbody", :count => 1 do
assert_select "tr", :count => 20
end
assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
next_path = check_page_link "Newer Traces"
check_page_link "Older Traces"

# Go back to the first page
get css_select("li.page-item a.page-link").first["href"]
get next_path
assert_response :success
assert_select "table#trace_list tbody", :count => 1 do
assert_select "tr", :count => 20
end
assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
check_no_page_link "Newer Traces"
check_page_link "Older Traces"
end

def test_index_invalid_paged
Expand All @@ -333,6 +335,20 @@ def test_index_invalid_paged
end
end

private

def check_no_page_link(name)
assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
end

def check_page_link(name)
assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
return buttons.first.attributes["href"].value
end
end

public

# Check the RSS feed
def test_rss
user = create(:user)
Expand Down

0 comments on commit 9368c9e

Please sign in to comment.