diff --git a/test/mock_helpers/collection.rb b/test/mock_helpers/collection.rb new file mode 100644 index 000000000..f9b3f4b34 --- /dev/null +++ b/test/mock_helpers/collection.rb @@ -0,0 +1,28 @@ +class MockCollection < Array + + def initialize(arr=Array(1..1000)) + super + @collection = self.clone + end + + def offset(value) + @collection = self[value..-1] + self + end + + def limit(value) + @collection[0, value] + end + + def count(*) + size + end + + class Grouped < MockCollection + + def count(*) + Hash[@collection.map { |value| [value, value + 1] }] + end + + end +end diff --git a/test/mock_helpers/controller.rb b/test/mock_helpers/controller.rb new file mode 100644 index 000000000..20fccfdb7 --- /dev/null +++ b/test/mock_helpers/controller.rb @@ -0,0 +1,27 @@ +# encoding: utf-8 +# frozen_string_literal: true + +require_relative 'collection' + +class MockController + include Pagy::Backend + # we ned to explicitly include this because Pagy::Backend + # does not include it when the test loads this module witout the headers + include Pagy::Helpers + + attr_reader :params + + def initialize(params={a: 'a', page: 3}, url='https://example.com:8080/foo?page=3') + @params = params + @url = url + end + + def request + @request ||= Rack::Request.new(Rack::MockRequest.env_for(@url)) + end + + def response + @response ||= Rack::Response.new + end + +end diff --git a/test/test_helper/elasticsearch_rails.rb b/test/mock_helpers/elasticsearch_rails.rb similarity index 82% rename from test/test_helper/elasticsearch_rails.rb rename to test/mock_helpers/elasticsearch_rails.rb index 06f80818c..1989e0cc0 100644 --- a/test/test_helper/elasticsearch_rails.rb +++ b/test/mock_helpers/elasticsearch_rails.rb @@ -1,6 +1,6 @@ require 'pagy/extras/elasticsearch_rails' -module ElasticsearchRailsTest +module MockElasticsearchRails RESULTS = { 'a' => ('a-1'..'a-1000').to_a, 'b' => ('b-1'..'b-1000').to_a } @@ -34,14 +34,14 @@ def count end end -end -class ElasticsearchRailsModel + class Model - def self.search(*args) - ElasticsearchRailsTest::Response.new(*args) - end + def self.search(*args) + Response.new(*args) + end - extend Pagy::Search + extend Pagy::Search + end end diff --git a/test/test_helper/searchkick.rb b/test/mock_helpers/searchkick.rb similarity index 82% rename from test/test_helper/searchkick.rb rename to test/mock_helpers/searchkick.rb index 83f250372..8ccda5e84 100644 --- a/test/test_helper/searchkick.rb +++ b/test/mock_helpers/searchkick.rb @@ -1,6 +1,6 @@ require 'pagy/extras/searchkick' -module SearchkickTest +module MockSearchkick RESULTS = { 'a' => ('a-1'..'a-1000').to_a, 'b' => ('b-1'..'b-1000').to_a } @@ -28,13 +28,15 @@ def total_count end end -end -class SearchkickModel + class Model - def self.search(*args, &block) - SearchkickTest::Results.new(*args, &block) - end + def self.search(*args, &block) + Results.new(*args, &block) + end - extend Pagy::Search + extend Pagy::Search + end end + + diff --git a/test/mock_helpers/view.rb b/test/mock_helpers/view.rb new file mode 100644 index 000000000..3c615f675 --- /dev/null +++ b/test/mock_helpers/view.rb @@ -0,0 +1,32 @@ +# encoding: utf-8 +# frozen_string_literal: true + +class MockView + include Pagy::Frontend + + def initialize(url='http://example.com:3000/foo?page=2') + @url = url + end + + def request + Rack::Request.new(Rack::MockRequest.env_for(@url)) + end + + class Overridden < MockView + def pagy_get_params(params) + params.except(:a).merge!(k: 'k') + end + end +end + + +class Hash + def except!(*keys) + keys.each { |key| delete(key) } + self + end + + def except(*keys) + dup.except!(*keys) + end +end diff --git a/test/pagy/backend_test.rb b/test/pagy/backend_test.rb index 43906ee94..a051736fc 100644 --- a/test/pagy/backend_test.rb +++ b/test/pagy/backend_test.rb @@ -5,26 +5,26 @@ describe Pagy::Backend do - let(:backend) { TestController.new } + let(:controller) { MockController.new } describe "#pagy" do before do - @collection = TestCollection.new((1..1000).to_a) + @collection = MockCollection.new end it 'paginates with defaults' do - pagy, records = backend.send(:pagy, @collection) + pagy, records = controller.send(:pagy, @collection) pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 pagy.items.must_equal Pagy::VARS[:items] - pagy.page.must_equal backend.params[:page] + pagy.page.must_equal controller.params[:page] records.count.must_equal Pagy::VARS[:items] records.must_equal [41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60] end it 'paginates with vars' do - pagy, records = backend.send(:pagy, @collection, page: 2, items: 10, link_extra: 'X') + pagy, records = controller.send(:pagy, @collection, page: 2, items: 10, link_extra: 'X') pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 pagy.items.must_equal 10 @@ -39,12 +39,12 @@ describe "#pagy_get_vars" do before do - @collection = TestCollection.new((1..1000).to_a) + @collection = MockCollection.new end it 'gets defaults' do vars = {} - merged = backend.send :pagy_get_vars, @collection, vars + merged = controller.send :pagy_get_vars, @collection, vars merged.keys.must_include :count merged.keys.must_include :page merged[:count].must_equal 1000 @@ -53,7 +53,7 @@ it 'gets vars' do vars = {page: 2, items: 10, link_extra: 'X'} - merged = backend.send :pagy_get_vars, @collection, vars + merged = controller.send :pagy_get_vars, @collection, vars merged.keys.must_include :count merged.keys.must_include :page merged.keys.must_include :items @@ -65,9 +65,9 @@ end it 'works with grouped collections' do - @collection = TestGroupedCollection.new((1..1000).to_a) + @collection = MockCollection::Grouped.new((1..1000).to_a) vars = {page: 2, items: 10, link_extra: 'X'} - merged = backend.send :pagy_get_vars, @collection, vars + merged = controller.send :pagy_get_vars, @collection, vars merged.keys.must_include :count merged.keys.must_include :page merged.keys.must_include :items @@ -80,7 +80,7 @@ it 'overrides count and page' do vars = {count: 10, page: 32} - merged = backend.send :pagy_get_vars, @collection, vars + merged = controller.send :pagy_get_vars, @collection, vars merged.keys.must_include :count merged[:count].must_equal 10 merged.keys.must_include :page @@ -92,9 +92,9 @@ describe "#pagy_get_items" do it 'gets items' do - collection = TestCollection.new((1..1000).to_a) + collection = MockCollection.new pagy = Pagy.new count: 1000 - items = backend.send :pagy_get_items, collection, pagy + items = controller.send :pagy_get_items, collection, pagy items.must_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] end diff --git a/test/pagy/countless_test.rb b/test/pagy/countless_test.rb index 1b79da466..bd197d17c 100644 --- a/test/pagy/countless_test.rb +++ b/test/pagy/countless_test.rb @@ -6,13 +6,13 @@ describe Pagy::Countless do - let(:backend) { TestController.new } # page = 3, items = 20 + let(:controller) { MockController.new } # page = 3, items = 20 describe "#finalize" do before do - @empty_collection = TestCollection.new([]) - @collection = TestCollection.new(Array(1..59)) + @empty_collection = MockCollection.new([]) + @collection = MockCollection.new(Array(1..59)) end let(:last_page) { 3 } @@ -22,7 +22,7 @@ end it 'initializes empty collection' do - pagy, _ = backend.send(:pagy_countless, @empty_collection, page: 1) + pagy, _ = controller.send(:pagy_countless, @empty_collection, page: 1) pagy.items.must_equal 20 pagy.pages.must_equal 1 pagy.last.must_equal 1 @@ -33,7 +33,7 @@ end it 'initializes first page' do - pagy, _ = backend.send(:pagy_countless, @collection, page: 1) + pagy, _ = controller.send(:pagy_countless, @collection, page: 1) pagy.must_be_instance_of Pagy::Countless pagy.items.must_equal 20 pagy.last.must_equal 2 @@ -45,7 +45,7 @@ end it 'initializes single full page' do - pagy, _ = backend.send(:pagy_countless, TestCollection.new(Array(1..20)), page: 1) + pagy, _ = controller.send(:pagy_countless, MockCollection.new(Array(1..20)), page: 1) pagy.items.must_equal 20 pagy.pages.must_equal 1 pagy.from.must_equal 1 @@ -55,7 +55,7 @@ end it 'initialize single partial page' do - pagy, _ = backend.send(:pagy_countless, TestCollection.new(Array(1..4)), page: 1) + pagy, _ = controller.send(:pagy_countless, MockCollection.new(Array(1..4)), page: 1) pagy.items.must_equal 4 pagy.pages.must_equal 1 pagy.from.must_equal 1 @@ -66,7 +66,7 @@ end it 'initializes last partial page' do - pagy, _ = backend.send(:pagy_countless, @collection, page: last_page) + pagy, _ = controller.send(:pagy_countless, @collection, page: last_page) pagy.items.must_equal 19 pagy.pages.must_equal last_page pagy.from.must_equal 41 @@ -76,7 +76,7 @@ end it 'handles the :cycle variable' do - pagy, _ = backend.send(:pagy_countless, @collection, page: last_page, cycle: true) + pagy, _ = controller.send(:pagy_countless, @collection, page: last_page, cycle: true) pagy.items.must_equal 19 pagy.pages.must_equal last_page pagy.from.must_equal 41 diff --git a/test/pagy/extras/array_test.rb b/test/pagy/extras/array_test.rb index b26c069ba..a79f51c1a 100644 --- a/test/pagy/extras/array_test.rb +++ b/test/pagy/extras/array_test.rb @@ -6,7 +6,7 @@ describe Pagy::Backend do - let(:backend) { TestController.new } + let(:controller) { MockController.new } describe "#pagy_array" do @@ -15,17 +15,17 @@ end it 'paginates with defaults' do - pagy, items = backend.send(:pagy_array, @collection) + pagy, items = controller.send(:pagy_array, @collection) pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 pagy.items.must_equal Pagy::VARS[:items] - pagy.page.must_equal backend.params[:page] + pagy.page.must_equal controller.params[:page] items.count.must_equal Pagy::VARS[:items] items.must_equal [41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60] end it 'paginates with vars' do - pagy, items = backend.send(:pagy_array, @collection, page: 2, items: 10, link_extra: 'X') + pagy, items = controller.send(:pagy_array, @collection, page: 2, items: 10, link_extra: 'X') pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 pagy.items.must_equal 10 @@ -45,7 +45,7 @@ it 'gets defaults' do vars = {} - merged = backend.send :pagy_array_get_vars, @collection, vars + merged = controller.send :pagy_array_get_vars, @collection, vars merged.keys.must_include :count merged.keys.must_include :page merged[:count].must_equal 1000 @@ -54,7 +54,7 @@ it 'gets vars' do vars = {page: 2, items: 10, link_extra: 'X'} - merged = backend.send :pagy_array_get_vars, @collection, vars + merged = controller.send :pagy_array_get_vars, @collection, vars merged.keys.must_include :count merged.keys.must_include :page merged.keys.must_include :items diff --git a/test/pagy/extras/bootstrap_test.rb b/test/pagy/extras/bootstrap_test.rb index c34dbb270..2abb68ed5 100644 --- a/test/pagy/extras/bootstrap_test.rb +++ b/test/pagy/extras/bootstrap_test.rb @@ -6,26 +6,26 @@ describe Pagy::Frontend do - let(:frontend) { TestView.new } + let(:view) { MockView.new } let(:pagy_test_id) { 'test-id' } describe "#pagy_bootstrap_nav" do it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_bootstrap_nav(pagy).must_equal \ + view.pagy_bootstrap_nav(pagy).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_bootstrap_nav(pagy).must_equal \ + view.pagy_bootstrap_nav(pagy).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_bootstrap_nav(pagy).must_equal \ + view.pagy_bootstrap_nav(pagy).must_equal \ "" end @@ -35,25 +35,25 @@ it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_bootstrap_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bootstrap_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_bootstrap_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bootstrap_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_bootstrap_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bootstrap_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders with :steps' do pagy = Pagy.new(count: 1000, page: 20, steps: {0 => [1,2,2,1], 500 => [2,3,3,2]}) - frontend.pagy_bootstrap_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bootstrap_nav_js(pagy, pagy_test_id).must_equal \ "" end @@ -63,19 +63,19 @@ it 'renders first page' do pagy = Pagy.new(count: 103, page: 1) - frontend.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 103, page: 3) - frontend.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 103, page: 6) - frontend.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end diff --git a/test/pagy/extras/bulma_test.rb b/test/pagy/extras/bulma_test.rb index d742ed4fd..c34417ae8 100644 --- a/test/pagy/extras/bulma_test.rb +++ b/test/pagy/extras/bulma_test.rb @@ -6,26 +6,26 @@ describe Pagy::Frontend do - let(:frontend) { TestView.new } + let(:view) { MockView.new } let(:pagy_test_id) { 'test-id' } describe '#pagy_bulma_nav' do it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_bulma_nav(pagy).must_equal \ + view.pagy_bulma_nav(pagy).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_bulma_nav(pagy).must_equal \ + view.pagy_bulma_nav(pagy).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_bulma_nav(pagy).must_equal \ + view.pagy_bulma_nav(pagy).must_equal \ "" end @@ -35,25 +35,25 @@ it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_bulma_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bulma_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_bulma_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bulma_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_bulma_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bulma_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders with :steps' do pagy = Pagy.new(count: 1000, page: 20, steps: {0 => [1,2,2,1], 500 => [2,3,3,2]}) - frontend.pagy_bulma_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bulma_nav_js(pagy, pagy_test_id).must_equal \ "" end @@ -63,20 +63,20 @@ it 'renders first page' do pagy = Pagy.new(count: 103, page: 1) - frontend.pagy_bulma_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bulma_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 103, page: 3) - html = frontend.pagy_bulma_combo_nav_js(pagy, pagy_test_id) + html = view.pagy_bulma_combo_nav_js(pagy, pagy_test_id) html.must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 103, page: 6) - frontend.pagy_bulma_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bulma_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end diff --git a/test/pagy/extras/countless_test.rb b/test/pagy/extras/countless_test.rb index a06abee1e..29c84a165 100644 --- a/test/pagy/extras/countless_test.rb +++ b/test/pagy/extras/countless_test.rb @@ -6,13 +6,13 @@ describe Pagy::Backend do - let(:backend) { TestController.new } + let(:controller) { MockController.new } let(:last_page) { 1000 / 20 } before do @default_page_param = Pagy::VARS[:page_param] - @collection = TestCollection.new((1..1000).to_a) + @collection = MockCollection.new end after do @@ -22,42 +22,42 @@ describe "#pagy_countless" do it 'shows current and next for first page' do - pagy, _ = backend.send(:pagy_countless, @collection, { size: [1, 4, 4, 1], page: 1 }) + pagy, _ = controller.send(:pagy_countless, @collection, { size: [1, 4, 4, 1], page: 1 }) pagy.series.must_equal ['1', 2] pagy.prev.must_be_nil pagy.next.must_equal 2 end it 'shows start-pages, :gap, before-pages, current and next for intermediate page' do - pagy, _ = backend.send(:pagy_countless, @collection, {page: 25}) + pagy, _ = controller.send(:pagy_countless, @collection, {page: 25}) pagy.series.must_equal [1, :gap, 21, 22, 23, 24, '25', 26] pagy.prev.must_equal 24 pagy.next.must_equal 26 end it 'shows start-pages, :gap, before-pages, current and next for last page' do - pagy, _ = backend.send(:pagy_countless, @collection, {page: last_page}) + pagy, _ = controller.send(:pagy_countless, @collection, {page: last_page}) pagy.series.must_equal [1, :gap, 46, 47, 48, 49, '50'] pagy.prev.must_equal 49 pagy.next.must_be_nil end it 'returns empty series for empty :size variable for first page' do - pagy, _ = backend.send(:pagy_countless, @collection, {size: [], page: 1}) + pagy, _ = controller.send(:pagy_countless, @collection, {size: [], page: 1}) pagy.series.must_equal [] pagy.prev.must_be_nil pagy.next.must_equal 2 end it 'returns empty series for empty :size variable for intermediate page' do - pagy, _ = backend.send(:pagy_countless, @collection, {size: [], page: 25}) + pagy, _ = controller.send(:pagy_countless, @collection, {size: [], page: 25}) pagy.series.must_equal [] pagy.prev.must_equal 24 pagy.next.must_equal 26 end it 'returns empty series for empty :size variable for last page' do - pagy, _ = backend.send(:pagy_countless, @collection, {size: [], page: last_page}) + pagy, _ = controller.send(:pagy_countless, @collection, {size: [], page: last_page}) pagy.series.must_equal [] pagy.prev.must_equal 49 pagy.next.must_be_nil @@ -66,27 +66,27 @@ describe '#pagy_countless_get_vars' do - let(:backend) { TestController.new({a: 'a', page: 3, page_number: 4}) } + let(:controller) { MockController.new({a: 'a', page: 3, page_number: 4}) } it 'sets :page_param from defaults' do Pagy::VARS[:page_param] = :page_number - pagy, items = backend.send(:pagy_countless, @collection) + pagy, paged = controller.send(:pagy_countless, @collection) pagy.page.must_equal 4 - items.must_equal Array(61..80) + paged.must_equal Array(61..80) end it 'sets :page_param from vars' do Pagy::VARS[:page_param] = :page - pagy, items = backend.send(:pagy_countless, @collection, {page_param: :page_number}) + pagy, paged = controller.send(:pagy_countless, @collection, {page_param: :page_number}) pagy.page.must_equal 4 - items.must_equal Array(61..80) + paged.must_equal Array(61..80) end it 'bypasses :page_param with :page variable' do Pagy::VARS[:page_param] = :another_page_number - pagy, items = backend.send(:pagy_countless, @collection, {page_param: :page_number, page: 1}) + pagy, paged = controller.send(:pagy_countless, @collection, {page_param: :page_number, page: 1}) pagy.page.must_equal 1 - items.must_equal Array(1..20) + paged.must_equal Array(1..20) end end diff --git a/test/pagy/extras/elasticsearch_rails_test.rb b/test/pagy/extras/elasticsearch_rails_test.rb index 6200e01c1..2a06a590f 100644 --- a/test/pagy/extras/elasticsearch_rails_test.rb +++ b/test/pagy/extras/elasticsearch_rails_test.rb @@ -2,7 +2,7 @@ # frozen_string_literal: true require_relative '../../test_helper' -require_relative '../../test_helper/elasticsearch_rails' +require_relative '../../mock_helpers/elasticsearch_rails' require 'pagy/extras/overflow' SimpleCov.command_name 'elasticsearch' if ENV['RUN_SIMPLECOV'] @@ -12,19 +12,19 @@ describe '#pagy_search' do it 'extends the class with #pagy_search' do - ElasticsearchRailsModel.must_respond_to :pagy_search + MockElasticsearchRails::Model.must_respond_to :pagy_search end it 'returns class and arguments' do - ElasticsearchRailsModel.pagy_search('a', b:2).must_equal [ElasticsearchRailsModel, 'a', {b: 2}, nil] - args = ElasticsearchRailsModel.pagy_search('a', b:2){|a| a*2} + MockElasticsearchRails::Model.pagy_search('a', b:2).must_equal [MockElasticsearchRails::Model, 'a', {b: 2}, nil] + args = MockElasticsearchRails::Model.pagy_search('a', b:2){|a| a*2} block = args[-1] - args.must_equal [ElasticsearchRailsModel, 'a', {b: 2}, block] + args.must_equal [MockElasticsearchRails::Model, 'a', {b: 2}, block] end it 'adds the caller and arguments' do - ElasticsearchRailsModel.pagy_search('a', b:2).records.must_equal [ElasticsearchRailsModel, 'a', {b: 2}, nil, :records] - ElasticsearchRailsModel.pagy_search('a', b:2).a('b', 2).must_equal [ElasticsearchRailsModel, 'a', {b: 2}, nil, :a, 'b', 2] + MockElasticsearchRails::Model.pagy_search('a', b:2).records.must_equal [MockElasticsearchRails::Model, 'a', {b: 2}, nil, :records] + MockElasticsearchRails::Model.pagy_search('a', b:2).a('b', 2).must_equal [MockElasticsearchRails::Model, 'a', {b: 2}, nil, :a, 'b', 2] end end @@ -33,37 +33,37 @@ describe Pagy::Backend do - let(:backend) { TestController.new } + let(:controller) { MockController.new } describe "#pagy_elasticsearch_rails" do before do - @collection = TestCollection.new((1..1000).to_a) + @collection = MockCollection.new end it 'paginates response with defaults' do - pagy, response = backend.send(:pagy_elasticsearch_rails, ElasticsearchRailsModel.pagy_search('a')) + pagy, response = controller.send(:pagy_elasticsearch_rails, MockElasticsearchRails::Model.pagy_search('a')) records = response.records pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 pagy.items.must_equal Pagy::VARS[:items] - pagy.page.must_equal backend.params[:page] + pagy.page.must_equal controller.params[:page] records.count.must_equal Pagy::VARS[:items] records.must_equal ["R-a-41", "R-a-42", "R-a-43", "R-a-44", "R-a-45", "R-a-46", "R-a-47", "R-a-48", "R-a-49", "R-a-50", "R-a-51", "R-a-52", "R-a-53", "R-a-54", "R-a-55", "R-a-56", "R-a-57", "R-a-58", "R-a-59", "R-a-60"] end it 'paginates records with defaults' do - pagy, records = backend.send(:pagy_elasticsearch_rails, ElasticsearchRailsModel.pagy_search('a').records) + pagy, records = controller.send(:pagy_elasticsearch_rails, MockElasticsearchRails::Model.pagy_search('a').records) pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 pagy.items.must_equal Pagy::VARS[:items] - pagy.page.must_equal backend.params[:page] + pagy.page.must_equal controller.params[:page] records.count.must_equal Pagy::VARS[:items] records.must_equal ["R-a-41", "R-a-42", "R-a-43", "R-a-44", "R-a-45", "R-a-46", "R-a-47", "R-a-48", "R-a-49", "R-a-50", "R-a-51", "R-a-52", "R-a-53", "R-a-54", "R-a-55", "R-a-56", "R-a-57", "R-a-58", "R-a-59", "R-a-60"] end it 'paginates with vars' do - pagy, records = backend.send(:pagy_elasticsearch_rails, ElasticsearchRailsModel.pagy_search('b').records, page: 2, items: 10, link_extra: 'X') + pagy, records = controller.send(:pagy_elasticsearch_rails, MockElasticsearchRails::Model.pagy_search('b').records, page: 2, items: 10, link_extra: 'X') pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 pagy.items.must_equal 10 @@ -74,7 +74,7 @@ end it 'paginates with overflow' do - pagy, records = backend.send(:pagy_elasticsearch_rails, ElasticsearchRailsModel.pagy_search('b').records, page: 200, items: 10, link_extra: 'X', overflow: :last_page) + pagy, records = controller.send(:pagy_elasticsearch_rails, MockElasticsearchRails::Model.pagy_search('b').records, page: 200, items: 10, link_extra: 'X', overflow: :last_page) pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 pagy.items.must_equal 10 @@ -90,7 +90,7 @@ it 'gets defaults' do vars = {} - merged = backend.send :pagy_elasticsearch_rails_get_vars, nil, vars + merged = controller.send :pagy_elasticsearch_rails_get_vars, nil, vars merged.keys.must_include :page merged.keys.must_include :items merged[:page].must_equal 3 @@ -99,7 +99,7 @@ it 'gets vars' do vars = {page: 2, items: 10, link_extra: 'X'} - merged = backend.send :pagy_elasticsearch_rails_get_vars, nil, vars + merged = controller.send :pagy_elasticsearch_rails_get_vars, nil, vars merged.keys.must_include :page merged.keys.must_include :items merged.keys.must_include :link_extra @@ -113,7 +113,7 @@ describe 'Pagy.new_from_elasticsearch_rails' do it 'paginates response with defaults' do - response = ElasticsearchRailsModel.search('a') + response = MockElasticsearchRails::Model.search('a') pagy = Pagy.new_from_elasticsearch_rails(response) pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 @@ -122,7 +122,7 @@ end it 'paginates response with vars' do - response = ElasticsearchRailsModel.search('b', from: 15, size: 15) + response = MockElasticsearchRails::Model.search('b', from: 15, size: 15) pagy = Pagy.new_from_elasticsearch_rails(response, link_extra: 'X') pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 diff --git a/test/pagy/extras/foundation_test.rb b/test/pagy/extras/foundation_test.rb index 75aab9ed1..8f09d3b59 100644 --- a/test/pagy/extras/foundation_test.rb +++ b/test/pagy/extras/foundation_test.rb @@ -6,26 +6,26 @@ describe Pagy::Frontend do - let(:frontend) { TestView.new } + let(:view) { MockView.new } let(:pagy_test_id) { 'test-id' } describe "#pagy_foundation_nav" do it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_foundation_nav(pagy).must_equal \ + view.pagy_foundation_nav(pagy).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_foundation_nav(pagy).must_equal \ + view.pagy_foundation_nav(pagy).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_foundation_nav(pagy).must_equal \ + view.pagy_foundation_nav(pagy).must_equal \ "" end @@ -35,25 +35,25 @@ it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_foundation_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_foundation_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_foundation_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_foundation_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_foundation_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_foundation_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders with :steps' do pagy = Pagy.new(count: 1000, page: 20, steps: {0 => [1,2,2,1], 500 => [2,3,3,2]}) - frontend.pagy_foundation_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_foundation_nav_js(pagy, pagy_test_id).must_equal \ "" end @@ -63,19 +63,19 @@ it 'renders first page' do pagy = Pagy.new(count: 103, page: 1) - frontend.pagy_foundation_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_foundation_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 103, page: 3) - frontend.pagy_foundation_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_foundation_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 103, page: 6) - frontend.pagy_foundation_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_foundation_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end diff --git a/test/pagy/extras/headers_test.rb b/test/pagy/extras/headers_test.rb index 8c83746b6..294984659 100644 --- a/test/pagy/extras/headers_test.rb +++ b/test/pagy/extras/headers_test.rb @@ -10,8 +10,8 @@ describe "#pagy_headers" do before do - @controller = TestController.new - @collection = TestCollection.new((1..1000).to_a) + @controller = MockController.new + @collection = MockCollection.new end it 'returns the full headers hash' do @@ -44,8 +44,8 @@ describe "#pagy_headers_merge" do before do - @controller = TestController.new - @collection = TestCollection.new((1..1000).to_a) + @controller = MockController.new + @collection = MockCollection.new end it 'returns the full headers hash' do diff --git a/test/pagy/extras/i18n_test.rb b/test/pagy/extras/i18n_test.rb index 4179c473c..d99f93a10 100644 --- a/test/pagy/extras/i18n_test.rb +++ b/test/pagy/extras/i18n_test.rb @@ -9,19 +9,19 @@ describe Pagy::Frontend do - let(:frontend) { TestView.new } + let(:view) { MockView.new } describe "#pagy_t with I18n" do it 'pluralizes' do - frontend.pagy_t('pagy.nav.prev').must_equal "‹ Prev" - frontend.pagy_t('pagy.item_name', count: 0).must_equal 'items' - frontend.pagy_t('pagy.item_name', count: 1).must_equal 'item' - frontend.pagy_t('pagy.item_name', count: 10).must_equal 'items' + view.pagy_t('pagy.nav.prev').must_equal "‹ Prev" + view.pagy_t('pagy.item_name', count: 0).must_equal 'items' + view.pagy_t('pagy.item_name', count: 1).must_equal 'item' + view.pagy_t('pagy.item_name', count: 10).must_equal 'items' end it 'handles missing paths' do - frontend.pagy_t('pagy.nav.not_here').must_equal 'translation missing: en.pagy.nav.not_here' + view.pagy_t('pagy.nav.not_here').must_equal 'translation missing: en.pagy.nav.not_here' end end @@ -29,20 +29,20 @@ describe '#pagy_info with I18n' do it 'renders info' do - frontend.pagy_info(Pagy.new count: 0).must_equal "No items found" - frontend.pagy_info(Pagy.new count: 1).must_equal "Displaying 1 item" - frontend.pagy_info(Pagy.new count: 13).must_equal "Displaying 13 items" - frontend.pagy_info(Pagy.new count: 100, page: 3).must_equal "Displaying items 41-60 of 100 in total" + view.pagy_info(Pagy.new count: 0).must_equal "No items found" + view.pagy_info(Pagy.new count: 1).must_equal "Displaying 1 item" + view.pagy_info(Pagy.new count: 13).must_equal "Displaying 13 items" + view.pagy_info(Pagy.new count: 100, page: 3).must_equal "Displaying items 41-60 of 100 in total" end it 'renders with existing i18n path' do ::I18n.locale = 'en' custom_dictionary = File.join(File.dirname(__FILE__), 'i18n.yml') ::I18n.load_path += [custom_dictionary] - frontend.pagy_info(Pagy.new count: 0, i18n_key: 'activerecord.models.product').must_equal "No Products found" - frontend.pagy_info(Pagy.new count: 1, i18n_key: 'activerecord.models.product').must_equal "Displaying 1 Product" - frontend.pagy_info(Pagy.new count: 13, i18n_key: 'activerecord.models.product').must_equal "Displaying 13 Products" - frontend.pagy_info(Pagy.new count: 100, i18n_key: 'activerecord.models.product', page: 3).must_equal "Displaying Products 41-60 of 100 in total" + view.pagy_info(Pagy.new count: 0, i18n_key: 'activerecord.models.product').must_equal "No Products found" + view.pagy_info(Pagy.new count: 1, i18n_key: 'activerecord.models.product').must_equal "Displaying 1 Product" + view.pagy_info(Pagy.new count: 13, i18n_key: 'activerecord.models.product').must_equal "Displaying 13 Products" + view.pagy_info(Pagy.new count: 100, i18n_key: 'activerecord.models.product', page: 3).must_equal "Displaying Products 41-60 of 100 in total" end end diff --git a/test/pagy/extras/items_test.rb b/test/pagy/extras/items_test.rb index 646529a3b..fb526a42c 100644 --- a/test/pagy/extras/items_test.rb +++ b/test/pagy/extras/items_test.rb @@ -3,8 +3,8 @@ require_relative '../../test_helper' require 'pagy/extras/countless' -require_relative '../../test_helper/elasticsearch_rails' -require_relative '../../test_helper/searchkick' +require_relative '../../mock_helpers/elasticsearch_rails' +require_relative '../../mock_helpers/searchkick' require 'pagy/extras/items' SimpleCov.command_name 'items' if ENV['RUN_SIMPLECOV'] @@ -14,19 +14,19 @@ describe "#pagy_get_vars and #pagy_countless_get_vars" do before do - @collection = TestCollection.new((1..1000).to_a) + @collection = MockCollection.new end it 'uses the defaults' do vars = {} - backend = TestController.new + controller = MockController.new [:pagy_get_vars, :pagy_countless_get_vars].each do |method| - merged = backend.send method, @collection, vars + merged = controller.send method, @collection, vars merged.keys.must_include :items merged[:items].must_be_nil end - merged = backend.send :pagy_elasticsearch_rails_get_vars, nil, vars + merged = controller.send :pagy_elasticsearch_rails_get_vars, nil, vars merged.keys.must_include :items merged[:items].must_equal 20 end @@ -34,15 +34,15 @@ it 'uses the vars' do vars = {items: 15} # force items params = {:a=>"a", :page=>3, :items=>12} - backend = TestController.new params - backend.params.must_equal params + controller = MockController.new params + controller.params.must_equal params [:pagy, :pagy_countless].each do |method| - pagy, _ = backend.send method, @collection, vars + pagy, _ = controller.send method, @collection, vars pagy.items.must_equal 15 end - [[:pagy_elasticsearch_rails, ElasticsearchRailsModel], [:pagy_searchkick, SearchkickModel]].each do |meth, mod| - pagy, _ = backend.send meth, mod.pagy_search('a').records, vars + [[:pagy_elasticsearch_rails, MockElasticsearchRails::Model], [:pagy_searchkick, MockSearchkick::Model]].each do |meth, mod| + pagy, _ = controller.send meth, mod.pagy_search('a').records, vars pagy.items.must_equal 15 end end @@ -50,15 +50,15 @@ it 'uses the params' do vars = {} params = {:a=>"a", :page=>3, :items=>12} - backend = TestController.new params - backend.params.must_equal params + controller = MockController.new params + controller.params.must_equal params [:pagy, :pagy_countless].each do |method| - pagy, _ = backend.send method, @collection, vars + pagy, _ = controller.send method, @collection, vars pagy.items.must_equal 12 end - [[:pagy_elasticsearch_rails, ElasticsearchRailsModel], [:pagy_searchkick, SearchkickModel]].each do |meth, mod| - pagy, _ = backend.send meth, mod.pagy_search('a').records, vars + [[:pagy_elasticsearch_rails, MockElasticsearchRails::Model], [:pagy_searchkick, MockSearchkick::Model]].each do |meth, mod| + pagy, _ = controller.send meth, mod.pagy_search('a').records, vars pagy.items.must_equal 12 end end @@ -66,15 +66,15 @@ it 'overrides the params' do vars = {items: 21} params = {:a=>"a", :page=>3, :items=>12} - backend = TestController.new params - backend.params.must_equal params + controller = MockController.new params + controller.params.must_equal params [:pagy, :pagy_countless].each do |method| - pagy, _ = backend.send method, @collection, vars + pagy, _ = controller.send method, @collection, vars pagy.items.must_equal 21 end - [[:pagy_elasticsearch_rails, ElasticsearchRailsModel], [:pagy_searchkick, SearchkickModel]].each do |meth, mod| - pagy, _ = backend.send meth, mod.pagy_search('a').records, vars + [[:pagy_elasticsearch_rails, MockElasticsearchRails::Model], [:pagy_searchkick, MockSearchkick::Model]].each do |meth, mod| + pagy, _ = controller.send meth, mod.pagy_search('a').records, vars pagy.items.must_equal 21 end end @@ -82,15 +82,15 @@ it 'uses the max_items default' do vars = {} params = {:a=>"a", :page=>3, :items=>120} - backend = TestController.new params - backend.params.must_equal params + controller = MockController.new params + controller.params.must_equal params [:pagy, :pagy_countless].each do |method| - pagy, _ = backend.send method, @collection, vars + pagy, _ = controller.send method, @collection, vars pagy.items.must_equal 100 end - [[:pagy_elasticsearch_rails, ElasticsearchRailsModel], [:pagy_searchkick, SearchkickModel]].each do |meth, mod| - pagy, _ = backend.send meth, mod.pagy_search('a').records, vars + [[:pagy_elasticsearch_rails, MockElasticsearchRails::Model], [:pagy_searchkick, MockSearchkick::Model]].each do |meth, mod| + pagy, _ = controller.send meth, mod.pagy_search('a').records, vars pagy.items.must_equal 100 end end @@ -98,15 +98,15 @@ it 'doesn\'t limit the items from vars' do vars = {max_items: nil} params = {:a=>"a", :items=>1000} - backend = TestController.new params - backend.params.must_equal params + controller = MockController.new params + controller.params.must_equal params [:pagy, :pagy_countless].each do |method| - pagy, _ = backend.send method, @collection, vars + pagy, _ = controller.send method, @collection, vars pagy.items.must_equal 1000 end - [[:pagy_elasticsearch_rails, ElasticsearchRailsModel], [:pagy_searchkick, SearchkickModel]].each do |meth, mod| - pagy, _ = backend.send meth, mod.pagy_search('a').records, vars + [[:pagy_elasticsearch_rails, MockElasticsearchRails::Model], [:pagy_searchkick, MockSearchkick::Model]].each do |meth, mod| + pagy, _ = controller.send meth, mod.pagy_search('a').records, vars pagy.items.must_equal 1000 end end @@ -114,16 +114,16 @@ it 'doesn\'t limit the items from default' do vars = {} params = {:a=>"a", :items=>1000} - backend = TestController.new params - backend.params.must_equal params + controller = MockController.new params + controller.params.must_equal params Pagy::VARS[:max_items] = nil [:pagy, :pagy_countless].each do |method| - pagy, _ = backend.send method, @collection, vars + pagy, _ = controller.send method, @collection, vars pagy.items.must_equal 1000 end - [[:pagy_elasticsearch_rails, ElasticsearchRailsModel], [:pagy_searchkick, SearchkickModel]].each do |meth, mod| - pagy, _ = backend.send meth, mod.pagy_search('a').records, vars + [[:pagy_elasticsearch_rails, MockElasticsearchRails::Model], [:pagy_searchkick, MockSearchkick::Model]].each do |meth, mod| + pagy, _ = controller.send meth, mod.pagy_search('a').records, vars pagy.items.must_equal 1000 end Pagy::VARS[:max_items] = 100 # reset default @@ -132,15 +132,15 @@ it 'uses items_param from vars' do vars = {items_param: :custom} params = {:a=>"a", :page=>3, :items_param=>:custom, :custom=>14} - backend = TestController.new params - backend.params.must_equal params + controller = MockController.new params + controller.params.must_equal params [:pagy, :pagy_countless].each do |method| - pagy, _ = backend.send method, @collection, vars + pagy, _ = controller.send method, @collection, vars pagy.items.must_equal 14 end - [[:pagy_elasticsearch_rails, ElasticsearchRailsModel], [:pagy_searchkick, SearchkickModel]].each do |meth, mod| - pagy, _ = backend.send meth, mod.pagy_search('a').records, vars + [[:pagy_elasticsearch_rails, MockElasticsearchRails::Model], [:pagy_searchkick, MockSearchkick::Model]].each do |meth, mod| + pagy, _ = controller.send meth, mod.pagy_search('a').records, vars pagy.items.must_equal 14 end end @@ -148,16 +148,16 @@ it 'uses items_param from default' do vars = {} params = {:a=>"a", :page=>3, :custom=>15} - backend = TestController.new params - backend.params.must_equal params + controller = MockController.new params + controller.params.must_equal params Pagy::VARS[:items_param] = :custom [:pagy, :pagy_countless].each do |method| - pagy, _ = backend.send method, @collection, vars + pagy, _ = controller.send method, @collection, vars pagy.items.must_equal 15 end - [[:pagy_elasticsearch_rails, ElasticsearchRailsModel], [:pagy_searchkick, SearchkickModel]].each do |meth, mod| - pagy, _ = backend.send meth, mod.pagy_search('a').records, vars + [[:pagy_elasticsearch_rails, MockElasticsearchRails::Model], [:pagy_searchkick, MockSearchkick::Model]].each do |meth, mod| + pagy, _ = controller.send meth, mod.pagy_search('a').records, vars pagy.items.must_equal 15 end Pagy::VARS[:items_param] = :items # reset default @@ -169,33 +169,33 @@ describe Pagy::Frontend do - let(:frontend) { TestView.new } + let(:view) { MockView.new } describe '#pagy_url_for' do it 'renders basic url' do pagy = Pagy.new count: 1000, page: 3 - frontend.pagy_url_for(5, pagy).must_equal '/foo?page=5&items=20' + view.pagy_url_for(5, pagy).must_equal '/foo?page=5&items=20' end it 'renders basic url and items var' do pagy = Pagy.new count: 1000, page: 3, items: 50 - frontend.pagy_url_for(5, pagy).must_equal '/foo?page=5&items=50' + view.pagy_url_for(5, pagy).must_equal '/foo?page=5&items=50' end it 'renders url with items_params' do pagy = Pagy.new count: 1000, page: 3, items_param: :custom - frontend.pagy_url_for(5, pagy).must_equal '/foo?page=5&custom=20' + view.pagy_url_for(5, pagy).must_equal '/foo?page=5&custom=20' end it 'renders url with anchor' do pagy = Pagy.new count: 1000, page: 3, anchor: '#anchor' - frontend.pagy_url_for(6, pagy).must_equal '/foo?page=6&items=20#anchor' + view.pagy_url_for(6, pagy).must_equal '/foo?page=6&items=20#anchor' end it 'renders url with params and anchor' do pagy = Pagy.new count: 1000, page: 3, params: {a: 3, b: 4}, anchor: '#anchor', items: 40 - frontend.pagy_url_for(5, pagy).must_equal "/foo?page=5&a=3&b=4&items=40#anchor" + view.pagy_url_for(5, pagy).must_equal "/foo?page=5&a=3&b=4&items=40#anchor" end end @@ -204,9 +204,7 @@ it 'renders items selector' do @pagy = Pagy.new count: 1000, page: 3 - html = frontend.pagy_items_selector_js(@pagy, 'test-id') - - html.must_equal \ + view.pagy_items_selector_js(@pagy, 'test-id').must_equal \ "Show items per page" end diff --git a/test/pagy/extras/materialize_test.rb b/test/pagy/extras/materialize_test.rb index 4df5768d5..a02eec1e9 100644 --- a/test/pagy/extras/materialize_test.rb +++ b/test/pagy/extras/materialize_test.rb @@ -6,27 +6,27 @@ describe Pagy::Frontend do - let(:frontend) { TestView.new } + let(:view) { MockView.new } let(:pagy_test_id) { 'test-id' } describe "#pagy_materialize_nav" do it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_materialize_nav(pagy).must_equal \ + view.pagy_materialize_nav(pagy).must_equal \ "
" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_materialize_nav(pagy).must_equal \ + view.pagy_materialize_nav(pagy).must_equal \ " " end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_materialize_nav(pagy).must_equal \ + view.pagy_materialize_nav(pagy).must_equal \ " " end @@ -36,25 +36,25 @@ it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_materialize_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_materialize_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_materialize_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_materialize_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_materialize_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_materialize_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders with :steps' do pagy = Pagy.new(count: 1000, page: 20, steps: {0 => [1,2,2,1], 500 => [2,3,3,2]}) - frontend.pagy_materialize_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_materialize_nav_js(pagy, pagy_test_id).must_equal \ "" end @@ -62,25 +62,21 @@ describe "#pagy_materialize_combo_nav_js" do - before do - @array = (1..103).to_a.extend(Pagy::Array::PageMethod) - end - it 'renders first page' do pagy = Pagy.new(count: 103, page: 1) - frontend.pagy_materialize_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_materialize_combo_nav_js(pagy, pagy_test_id).must_equal \ "