diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index a6f1fb7de..671d4e097 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -18,11 +18,8 @@ def link_to_specific_page(scope, page, total_entries, **options) Rails.logger.warn "Specific page path could not be generated for page: #{page.to_i}" nil end - rescue ArgumentError => e - Rails.logger.error "Error in link_to_specific_page: #{e.message}" - nil - rescue StandardError => e - Rails.logger.error "Unexpected error in link_to_specific_page: #{e.message}" + rescue => e + Rails.logger.error "#{e.message}" nil end end @@ -34,7 +31,7 @@ def path_to_specific_page(scope, page_integer, total_entries, options = {}) limit = scope.instance_variable_get(:@limit) total_pages = (total_entries.to_f / limit).ceil - Rails.logger.info "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page_integer}" + Rails.logger.debug "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page_integer}" # Validate inputs raise ArgumentError, 'Page number must be a positive integer' unless page_integer.positive? @@ -42,9 +39,9 @@ def path_to_specific_page(scope, page_integer, total_entries, options = {}) # Generate URL using Kaminari's Page helper Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page_integer)).url rescue ArgumentError => e - Rails.logger.info "Error in path_to_specific_page: #{e.message}" + Rails.logger.error "Error in path_to_specific_page: #{e.message}" nil - rescue StandardError => e + rescue => e Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}\n#{e.backtrace.join("\n")}" nil end diff --git a/spec/lib/kanamari/helpers/helper_methods_spec.rb b/spec/lib/kanamari/helpers/helper_methods_spec.rb new file mode 100644 index 000000000..3d1805930 --- /dev/null +++ b/spec/lib/kanamari/helpers/helper_methods_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe Kaminari::Helpers::HelperMethods do + let(:dummy_class) { Class.new { extend Kaminari::Helpers::HelperMethods } } + let(:scope) { double('FacetPaginator', instance_variable_get: 20) } + let(:valid_page) { 1 } + let(:total_entries) { 100 } + let(:options) { { some_option: 'value' } } + + describe '#link_to_specific_page' do + before do + # Stub Kaminari::Helpers::Page.new to return a mock URL + allow(Kaminari::Helpers::Page).to receive(:new).and_return(double(url: '/some_path')) + end + + it 'generates a valid link for correct input' do + result = dummy_class.path_to_specific_page(scope, valid_page, total_entries, options) + expect(Rails.logger).to_not receive(:error) + # Expect the method to return the mocked URL + expect(result).to eq('/some_path') + end + + + it 'logs and returns nil for invalid page input' do + invalid_page = -1 + expect(Rails.logger).to receive(:error).with(/Page number must be a positive integer/) + expect(dummy_class.link_to_specific_page(scope, invalid_page, total_entries, **options)) + .to be_nil + end + + it 'logs and returns nil if page exceeds total pages' do + invalid_page = 999 + expect(Rails.logger).to receive(:error).with(/Page number exceeds total pages/) + expect(dummy_class.link_to_specific_page(scope, invalid_page, total_entries, **options)) + .to be_nil + end + + it 'logs and returns nil if an unexpected error occurs' do + allow(Kaminari::Helpers::Page).to receive(:new).and_raise(StandardError, 'Simulated Kaminari Error') + expect(Rails.logger).to receive(:error).with(/Unexpected error in path_to_specific_page/) + expect(dummy_class.link_to_specific_page(scope, valid_page, total_entries, **options)) + .to be_nil + end + end +end