From 47a783fb1d3906a29bbb251813a090ff4a7b1be5 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 17 Apr 2024 18:53:05 +0300 Subject: [PATCH] Replace previous/next page selectors with helper methods in tests --- .../diary_entries_controller_test.rb | 41 +++++++---- test/controllers/traces_controller_test.rb | 72 +++++++++++-------- 2 files changed, 68 insertions(+), 45 deletions(-) diff --git a/test/controllers/diary_entries_controller_test.rb b/test/controllers/diary_entries_controller_test.rb index 35ce238bb..d80886fa3 100644 --- a/test/controllers/diary_entries_controller_test.rb +++ b/test/controllers/diary_entries_controller_test.rb @@ -471,41 +471,42 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 @@ -995,4 +996,14 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}" end end + + 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 end diff --git a/test/controllers/traces_controller_test.rb b/test/controllers/traces_controller_test.rb index 1cf209f2b..bccd0ef9c 100644 --- a/test/controllers/traces_controller_test.rb +++ b/test/controllers/traces_controller_test.rb @@ -197,51 +197,52 @@ class TracesControllerTest < ActionDispatch::IntegrationTest 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 @@ -250,51 +251,52 @@ class TracesControllerTest < ActionDispatch::IntegrationTest 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 @@ -583,6 +585,16 @@ class TracesControllerTest < ActionDispatch::IntegrationTest end end + 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 + def check_trace_show(trace) assert_response :success assert_template "show" -- 2.39.5