From d23fb5fec37f462110abc140d3db610d8c83a717 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Sun, 13 Sep 2026 18:46:37 +0200 Subject: [PATCH] test: cover PaginationHelpersTest, teardown and ParamsNestedTest --- test/test_pagination_helpers.rb | 74 +++++++++++++++++++++++++++++++ test/test_params_nested.rb | 77 +++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 test/test_pagination_helpers.rb create mode 100644 test/test_params_nested.rb diff --git a/test/test_pagination_helpers.rb b/test/test_pagination_helpers.rb new file mode 100644 index 0000000..6ac4a68 --- /dev/null +++ b/test/test_pagination_helpers.rb @@ -0,0 +1,74 @@ +require 'minitest/autorun' +require 'prism' +require 'tmpdir' +require 'fileutils' + +class PaginationHelpersTest < Minitest::Test + def setup + @dir = Dir.mktmpdir('prism-pagination-helpers') + @store = Prism::Orm::Store.new(File.join(@dir, 'test.pstore')) + @store.create_table(:widgets, %w[id name position]) + widget_class = Class.new(Prism::Orm::Model) { table_name :widgets } + widget_class.store = @store + Object.const_set(:PaginatedWidget, widget_class) + 25.times { |i| PaginatedWidget.create!(name: "widget-#{i}", position: i) } + + @context = Prism::Template::Context.new({}) + end + + def teardown + FileUtils.remove_entry(@dir) + Object.send(:remove_const, :PaginatedWidget) if Object.const_defined?(:PaginatedWidget) + end + + def path_for + ->(page) { "/widgets?page=#{page}" } + end + + def test_returns_blank_when_everything_fits_on_one_page + relation = PaginatedWidget.page(1) + assert_equal '', @context.paginate(relation, path: path_for) + end + + def test_renders_a_link_per_page_within_the_window + relation = PaginatedWidget.order(:position).page(1).per(5) + html = @context.paginate(relation, path: path_for) + assert_includes html, 'href="/widgets?page=2"' + assert_includes html, '>2<' + end + + def test_current_page_link_carries_the_current_class + relation = PaginatedWidget.order(:position).page(3).per(5) + html = @context.paginate(relation, path: path_for) + assert_includes html, 'href="/widgets?page=3" class="page-link current"' + end + + def test_first_page_disables_the_previous_link + relation = PaginatedWidget.order(:position).page(1).per(5) + html = @context.paginate(relation, path: path_for) + assert_includes html, 'Previous' + end + + def test_last_page_disables_the_next_link + relation = PaginatedWidget.order(:position).page(5).per(5) + html = @context.paginate(relation, path: path_for) + assert_includes html, 'Next' + end + + def test_far_apart_pages_are_separated_by_a_gap_marker + relation = PaginatedWidget.order(:position).page(1).per(1) + html = @context.paginate(relation, path: path_for) + assert_includes html, '...' + end + + def test_renders_through_a_real_erb_template + views_dir = File.join(@dir, 'views', 'widgets') + FileUtils.mkdir_p(views_dir) + File.write(File.join(views_dir, 'index.erb'), '<%== paginate(widgets, path: ->(n) { "/widgets?page=#{n}" }) %>') + + view = Prism::View.new(File.join(@dir, 'views')) + output = view.render('widgets/index', locals: { widgets: PaginatedWidget.order(:position).page(1).per(5) }) + assert_includes output, '