From 405b4d25353ef41388fe258a10a9399a68b44b7c Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Mon, 13 May 2019 17:25:11 +0700 Subject: [PATCH] reorganization and renaming of test helpers --- test/mock_helpers/collection.rb | 28 ++++ test/mock_helpers/controller.rb | 27 +++ .../elasticsearch_rails.rb | 14 +- .../searchkick.rb | 16 +- test/mock_helpers/view.rb | 32 ++++ test/pagy/backend_test.rb | 26 +-- test/pagy/countless_test.rb | 18 +- test/pagy/extras/array_test.rb | 12 +- test/pagy/extras/bootstrap_test.rb | 22 +-- test/pagy/extras/bulma_test.rb | 22 +-- test/pagy/extras/countless_test.rb | 30 ++-- test/pagy/extras/elasticsearch_rails_test.rb | 38 ++--- test/pagy/extras/foundation_test.rb | 22 +-- test/pagy/extras/headers_test.rb | 8 +- test/pagy/extras/i18n_test.rb | 28 ++-- test/pagy/extras/items_test.rb | 108 ++++++------ test/pagy/extras/materialize_test.rb | 26 ++- test/pagy/extras/navs_test.rb | 16 +- test/pagy/extras/searchkick_test.rb | 38 ++--- test/pagy/extras/semantic_test.rb | 22 +-- test/pagy/extras/shared_combo_test.rb | 12 +- test/pagy/extras/shared_test.rb | 10 +- test/pagy/extras/support_test.rb | 66 ++++---- test/pagy/extras/trim_test.rb | 44 ++--- test/pagy/frontend_test.rb | 156 ++++++------------ test/test_helper.rb | 5 +- test/test_helper/array.rb | 20 --- test/test_helper/backend.rb | 54 ------ test/test_helper/frontend.rb | 37 ----- 29 files changed, 440 insertions(+), 517 deletions(-) create mode 100644 test/mock_helpers/collection.rb create mode 100644 test/mock_helpers/controller.rb rename test/{test_helper => mock_helpers}/elasticsearch_rails.rb (82%) rename test/{test_helper => mock_helpers}/searchkick.rb (82%) create mode 100644 test/mock_helpers/view.rb delete mode 100644 test/test_helper/array.rb delete mode 100644 test/test_helper/backend.rb delete mode 100644 test/test_helper/frontend.rb 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 \ "
" end it 'renders intermediate page' do pagy = Pagy.new(count: 103, page: 3) - frontend.pagy_materialize_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_materialize_combo_nav_js(pagy, pagy_test_id).must_equal \ "
" end it 'renders last page' do pagy = Pagy.new(count: 103, page: 6) - frontend.pagy_materialize_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_materialize_combo_nav_js(pagy, pagy_test_id).must_equal \ "
" end diff --git a/test/pagy/extras/navs_test.rb b/test/pagy/extras/navs_test.rb index df7257789..b2f4f9173 100644 --- a/test/pagy/extras/navs_test.rb +++ b/test/pagy/extras/navs_test.rb @@ -6,32 +6,32 @@ describe Pagy::Frontend do - let(:frontend) { TestView.new } + let(:view) { MockView.new } let(:pagy_test_id) { 'test-id' } describe "#pagy_nav_js" do it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_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_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_nav_js(pagy, pagy_test_id).must_equal \ "" end @@ -41,19 +41,19 @@ it 'renders first page' do pagy = Pagy.new(count: 103, page: 1) - frontend.pagy_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 103, page: 3) - frontend.pagy_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 103, page: 6) - frontend.pagy_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_combo_nav_js(pagy, pagy_test_id).must_equal \ "" end diff --git a/test/pagy/extras/searchkick_test.rb b/test/pagy/extras/searchkick_test.rb index 9995f45f4..de6dc2f92 100644 --- a/test/pagy/extras/searchkick_test.rb +++ b/test/pagy/extras/searchkick_test.rb @@ -2,7 +2,7 @@ # frozen_string_literal: true require_relative '../../test_helper' -require_relative '../../test_helper/searchkick' +require_relative '../../mock_helpers/searchkick' 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 - SearchkickModel.must_respond_to :pagy_search + MockSearchkick::Model.must_respond_to :pagy_search end it 'returns class and arguments' do - SearchkickModel.pagy_search('a', b:2).must_equal [SearchkickModel, 'a', {b: 2}, nil] - args = SearchkickModel.pagy_search('a', b:2){|a| a*2} + MockSearchkick::Model.pagy_search('a', b:2).must_equal [MockSearchkick::Model, 'a', {b: 2}, nil] + args = MockSearchkick::Model.pagy_search('a', b:2){|a| a*2} block = args[-1] - args.must_equal [SearchkickModel, 'a', {b: 2}, block] + args.must_equal [MockSearchkick::Model, 'a', {b: 2}, block] end it 'adds the caller and arguments' do - SearchkickModel.pagy_search('a', b:2).results.must_equal [SearchkickModel, 'a', {b: 2}, nil, :results] - SearchkickModel.pagy_search('a', b:2).a('b', 2).must_equal [SearchkickModel, 'a', {b: 2}, nil, :a, 'b', 2] + MockSearchkick::Model.pagy_search('a', b:2).results.must_equal [MockSearchkick::Model, 'a', {b: 2}, nil, :results] + MockSearchkick::Model.pagy_search('a', b:2).a('b', 2).must_equal [MockSearchkick::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_searchkick" 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_searchkick, SearchkickModel.pagy_search('a'){'B-'}) + pagy, response = controller.send(:pagy_searchkick, MockSearchkick::Model.pagy_search('a'){'B-'}) results = response.results 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] results.count.must_equal Pagy::VARS[:items] results.must_equal ["R-B-a-41", "R-B-a-42", "R-B-a-43", "R-B-a-44", "R-B-a-45", "R-B-a-46", "R-B-a-47", "R-B-a-48", "R-B-a-49", "R-B-a-50", "R-B-a-51", "R-B-a-52", "R-B-a-53", "R-B-a-54", "R-B-a-55", "R-B-a-56", "R-B-a-57", "R-B-a-58", "R-B-a-59", "R-B-a-60"] end it 'paginates results with defaults' do - pagy, results = backend.send(:pagy_searchkick, SearchkickModel.pagy_search('a').results) + pagy, results = controller.send(:pagy_searchkick, MockSearchkick::Model.pagy_search('a').results) 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] results.count.must_equal Pagy::VARS[:items] results.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, results = backend.send(:pagy_searchkick, SearchkickModel.pagy_search('b').results, page: 2, items: 10, link_extra: 'X') + pagy, results = controller.send(:pagy_searchkick, MockSearchkick::Model.pagy_search('b').results, 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, results = backend.send(:pagy_searchkick, SearchkickModel.pagy_search('b').results, page: 200, items: 10, link_extra: 'X', overflow: :last_page) + pagy, results = controller.send(:pagy_searchkick, MockSearchkick::Model.pagy_search('b').results, 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_searchkick_get_vars, nil, vars + merged = controller.send :pagy_searchkick_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_searchkick_get_vars, nil, vars + merged = controller.send :pagy_searchkick_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_searchkick' do it 'paginates results with defaults' do - results = SearchkickModel.search('a') + results = MockSearchkick::Model.search('a') pagy = Pagy.new_from_searchkick(results) pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 @@ -122,7 +122,7 @@ end it 'paginates results with vars' do - results = SearchkickModel.search('b', page: 2, per_page: 15) + results = MockSearchkick::Model.search('b', page: 2, per_page: 15) pagy = Pagy.new_from_searchkick(results, link_extra: 'X') pagy.must_be_instance_of Pagy pagy.count.must_equal 1000 diff --git a/test/pagy/extras/semantic_test.rb b/test/pagy/extras/semantic_test.rb index 0047e627a..7920c3ca8 100644 --- a/test/pagy/extras/semantic_test.rb +++ b/test/pagy/extras/semantic_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_semantic_nav" do it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_semantic_nav(pagy).must_equal \ + view.pagy_semantic_nav(pagy).must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_semantic_nav(pagy).must_equal \ + view.pagy_semantic_nav(pagy).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_semantic_nav(pagy).must_equal \ + view.pagy_semantic_nav(pagy).must_equal \ "" end @@ -35,25 +35,25 @@ it 'renders first page' do pagy = Pagy.new(count: 1000, page: 1) - frontend.pagy_semantic_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_semantic_nav_js(pagy, pagy_test_id).must_equal \ "
" end it 'renders intermediate page' do pagy = Pagy.new(count: 1000, page: 20) - frontend.pagy_semantic_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_semantic_nav_js(pagy, pagy_test_id).must_equal \ "
" end it 'renders last page' do pagy = Pagy.new(count: 1000, page: 50) - frontend.pagy_semantic_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_semantic_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_semantic_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_semantic_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_semantic_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_semantic_combo_nav_js(pagy, pagy_test_id).must_equal \ "
Page of 6
" end it 'renders intermediate page' do pagy = Pagy.new(count: 103, page: 3) - frontend.pagy_semantic_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_semantic_combo_nav_js(pagy, pagy_test_id).must_equal \ "
Page of 6
" end it 'renders last page' do pagy = Pagy.new(count: 103, page: 6) - frontend.pagy_semantic_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_semantic_combo_nav_js(pagy, pagy_test_id).must_equal \ "
Page of 6
" end diff --git a/test/pagy/extras/shared_combo_test.rb b/test/pagy/extras/shared_combo_test.rb index 78883a795..065b2e824 100644 --- a/test/pagy/extras/shared_combo_test.rb +++ b/test/pagy/extras/shared_combo_test.rb @@ -10,34 +10,34 @@ # add tests for oj and pagy_id describe Pagy::Frontend do - let(:frontend) { TestSimpleView.new } + let(:view) { MockView.new('http://example.com:3000/foo?') } describe "#pagy_marked_link" do it 'should return the marked link' do pagy = Pagy.new(count: 100, page: 4) - frontend.instance_eval do + view.instance_eval do pagy_marked_link(pagy_link_proc(pagy)).must_equal( "" ) end pagy = Pagy.new(count: 100, page: 4, page_param: 'p') - frontend.instance_eval do + view.instance_eval do pagy_marked_link(pagy_link_proc(pagy)).must_equal( "" ) end pagy = Pagy.new(count: 100, page: 4, items_param: 'i') - frontend.instance_eval do + view.instance_eval do pagy_marked_link(pagy_link_proc(pagy)).must_equal( "" ) end pagy = Pagy.new(count: 100, page: 4, page_param: 'p', items_param: 'i') - frontend.instance_eval do + view.instance_eval do pagy_marked_link(pagy_link_proc(pagy)).must_equal( "" ) @@ -52,7 +52,7 @@ it 'renders items selector with trim' do pagy = Pagy.new count: 1000, page: 3 - frontend.pagy_items_selector_js(pagy, 'test-id').must_equal \ + view.pagy_items_selector_js(pagy, 'test-id').must_equal \ "Show items per page" end diff --git a/test/pagy/extras/shared_test.rb b/test/pagy/extras/shared_test.rb index f388f697a..b9a68e4b2 100644 --- a/test/pagy/extras/shared_test.rb +++ b/test/pagy/extras/shared_test.rb @@ -12,12 +12,12 @@ # add tests for oj and pagy_id describe Pagy::Frontend do - let(:frontend) { TestSimpleView.new } + let(:view) { MockView.new('http://example.com:3000/foo?') } describe "#pagy_json_tag" do it 'should use oj' do - frontend.pagy_json_tag(:test_function, 'some-id', 'some-string', 123, true).must_equal \ + view.pagy_json_tag(:test_function, 'some-id', 'some-string', 123, true).must_equal \ "" end @@ -40,11 +40,11 @@ it 'should return only the "standard" link' do pagy = Pagy.new(count: 100, page: 4) - frontend.instance_eval do + view.instance_eval do pagy_marked_link(pagy_link_proc(pagy)).must_equal("") end pagy = Pagy.new(count: 100, page: 4, page_param: 'p') - frontend.instance_eval do + view.instance_eval do pagy_marked_link(pagy_link_proc(pagy)).must_equal("") end end @@ -53,7 +53,7 @@ # we need an intermediate call to get the right caller def call_pagy_id - frontend.pagy_id + view.pagy_id end end diff --git a/test/pagy/extras/support_test.rb b/test/pagy/extras/support_test.rb index 18e603b63..ba083d40e 100644 --- a/test/pagy/extras/support_test.rb +++ b/test/pagy/extras/support_test.rb @@ -9,36 +9,36 @@ describe Pagy::Frontend do - let(:frontend) { TestView.new } + let(:view) { MockView.new } describe "#pagy_prev_url" do it 'returns the prev url for page 1' do pagy = Pagy.new count: 1000, page: 1 pagy_countless = Pagy::Countless.new(page: 1).finalize(21) - frontend.pagy_prev_url(pagy).must_be_nil - frontend.pagy_prev_url(pagy_countless).must_be_nil + view.pagy_prev_url(pagy).must_be_nil + view.pagy_prev_url(pagy_countless).must_be_nil end it 'returns the prev url for page 3' do pagy = Pagy.new count: 1000, page: 3 pagy_countless = Pagy::Countless.new(page: 3).finalize(21) - frontend.pagy_prev_url(pagy).must_equal '/foo?page=2' - frontend.pagy_prev_url(pagy_countless).must_equal '/foo?page=2' + view.pagy_prev_url(pagy).must_equal '/foo?page=2' + view.pagy_prev_url(pagy_countless).must_equal '/foo?page=2' end it 'returns the prev url for page 6' do pagy = Pagy.new count: 1000, page: 6 pagy_countless = Pagy::Countless.new(page: 6).finalize(21) - frontend.pagy_prev_url(pagy).must_equal '/foo?page=5' - frontend.pagy_prev_url(pagy_countless).must_equal '/foo?page=5' + view.pagy_prev_url(pagy).must_equal '/foo?page=5' + view.pagy_prev_url(pagy_countless).must_equal '/foo?page=5' end it 'returns the prev url for last page' do pagy = Pagy.new count: 1000, page: 50 pagy_countless = Pagy::Countless.new(page: 50).finalize(20) - frontend.pagy_prev_url(pagy).must_equal '/foo?page=49' - frontend.pagy_prev_url(pagy_countless).must_equal '/foo?page=49' + view.pagy_prev_url(pagy).must_equal '/foo?page=49' + view.pagy_prev_url(pagy_countless).must_equal '/foo?page=49' end end @@ -48,29 +48,29 @@ it 'returns the next url for page 1' do pagy = Pagy.new count: 1000, page: 1 pagy_countless = Pagy::Countless.new(page: 1).finalize(21) - frontend.pagy_next_url(pagy).must_equal '/foo?page=2' - frontend.pagy_next_url(pagy_countless).must_equal '/foo?page=2' + view.pagy_next_url(pagy).must_equal '/foo?page=2' + view.pagy_next_url(pagy_countless).must_equal '/foo?page=2' end it 'returns the next url for page 3' do pagy = Pagy.new count: 1000, page: 3 pagy_countless = Pagy::Countless.new(page: 3).finalize(21) - frontend.pagy_next_url(pagy).must_equal '/foo?page=4' - frontend.pagy_next_url(pagy_countless).must_equal '/foo?page=4' + view.pagy_next_url(pagy).must_equal '/foo?page=4' + view.pagy_next_url(pagy_countless).must_equal '/foo?page=4' end it 'returns the url next for page 6' do pagy = Pagy.new count: 1000, page: 6 pagy_countless = Pagy::Countless.new(page: 6).finalize(21) - frontend.pagy_next_url(pagy).must_equal '/foo?page=7' - frontend.pagy_next_url(pagy_countless).must_equal '/foo?page=7' + view.pagy_next_url(pagy).must_equal '/foo?page=7' + view.pagy_next_url(pagy_countless).must_equal '/foo?page=7' end it 'returns the url next for last page' do pagy = Pagy.new count: 1000, page: 50 pagy_countless = Pagy::Countless.new(page: 50).finalize(20) - frontend.pagy_next_url(pagy).must_be_nil - frontend.pagy_next_url(pagy_countless).must_be_nil + view.pagy_next_url(pagy).must_be_nil + view.pagy_next_url(pagy_countless).must_be_nil end end @@ -80,29 +80,29 @@ it 'renders the prev link for page 1' do pagy = Pagy.new count: 1000, page: 1 pagy_countless = Pagy::Countless.new(page: 1).finalize(21) - frontend.pagy_prev_link(pagy).must_equal "‹ Prev" - frontend.pagy_prev_link(pagy_countless).must_equal "‹ Prev" + view.pagy_prev_link(pagy).must_equal "‹ Prev" + view.pagy_prev_link(pagy_countless).must_equal "‹ Prev" end it 'renders the prev link for page 3' do pagy = Pagy.new count: 1000, page: 3 pagy_countless = Pagy::Countless.new(page: 3).finalize(21) - frontend.pagy_prev_link(pagy).must_equal "‹ Prev" - frontend.pagy_prev_link(pagy_countless).must_equal "‹ Prev" + view.pagy_prev_link(pagy).must_equal "‹ Prev" + view.pagy_prev_link(pagy_countless).must_equal "‹ Prev" end it 'renders the prev link for page 6' do pagy = Pagy.new count: 1000, page: 6 pagy_countless = Pagy::Countless.new(page: 6).finalize(21) - frontend.pagy_prev_link(pagy).must_equal "‹ Prev" - frontend.pagy_prev_link(pagy_countless).must_equal "‹ Prev" + view.pagy_prev_link(pagy).must_equal "‹ Prev" + view.pagy_prev_link(pagy_countless).must_equal "‹ Prev" end it 'renders the prev link for last page' do pagy = Pagy.new count: 1000, page: 50 pagy_countless = Pagy::Countless.new(page: 50).finalize(20) - frontend.pagy_prev_link(pagy).must_equal "‹ Prev" - frontend.pagy_prev_link(pagy_countless).must_equal "‹ Prev" + view.pagy_prev_link(pagy).must_equal "‹ Prev" + view.pagy_prev_link(pagy_countless).must_equal "‹ Prev" end end @@ -112,29 +112,29 @@ it 'renders the next link for page 1' do pagy = Pagy.new count: 1000, page: 1 pagy_countless = Pagy::Countless.new(page: 1).finalize(21) - frontend.pagy_next_link(pagy).must_equal "Next ›" - frontend.pagy_next_link(pagy_countless).must_equal "Next ›" + view.pagy_next_link(pagy).must_equal "Next ›" + view.pagy_next_link(pagy_countless).must_equal "Next ›" end it 'renders the next link for page 3' do pagy = Pagy.new count: 1000, page: 3 pagy_countless = Pagy::Countless.new(page: 3).finalize(21) - frontend.pagy_next_link(pagy).must_equal "Next ›" - frontend.pagy_next_link(pagy_countless).must_equal "Next ›" + view.pagy_next_link(pagy).must_equal "Next ›" + view.pagy_next_link(pagy_countless).must_equal "Next ›" end it 'renders the next link for page 6' do pagy = Pagy.new count: 1000, page: 6 pagy_countless = Pagy::Countless.new(page: 6).finalize(21) - frontend.pagy_next_link(pagy).must_equal "Next ›" - frontend.pagy_next_link(pagy_countless).must_equal "Next ›" + view.pagy_next_link(pagy).must_equal "Next ›" + view.pagy_next_link(pagy_countless).must_equal "Next ›" end it 'renders the next link for last page' do pagy = Pagy.new count: 1000, page: 50 pagy_countless = Pagy::Countless.new(page: 50).finalize(20) - frontend.pagy_next_link(pagy).must_equal "Next ›" - frontend.pagy_next_link(pagy_countless).must_equal "Next ›" + view.pagy_next_link(pagy).must_equal "Next ›" + view.pagy_next_link(pagy_countless).must_equal "Next ›" end end diff --git a/test/pagy/extras/trim_test.rb b/test/pagy/extras/trim_test.rb index 3b90da911..b17d2804b 100644 --- a/test/pagy/extras/trim_test.rb +++ b/test/pagy/extras/trim_test.rb @@ -14,18 +14,18 @@ describe Pagy::Frontend do - let(:frontend) { TestView.new } + let(:view) { MockView.new } let(:pagy_test_id) { 'test-id' } describe "#pagy_link_proc" do it 'returns trimmed link' do pagy = Pagy.new(count: 1000) - link = frontend.pagy_link_proc(pagy) + link = view.pagy_link_proc(pagy) link.call(1).must_equal("1") link.call(10).must_equal("10") pagy = Pagy.new(count: 1000, params: {a:3,b:4}) - link = frontend.pagy_link_proc(pagy) + link = view.pagy_link_proc(pagy) link.call(1).must_equal("1") link.call(10).must_equal("10") end @@ -36,21 +36,21 @@ it 'renders first page' do pagy = Pagy.new(count: 103, page: 1) - html = frontend.pagy_nav(pagy) + html = view.pagy_nav(pagy) html.must_equal \ "" end it 'renders intermediate page' do pagy = Pagy.new(count: 103, page: 3) - html = frontend.pagy_nav(pagy) + html = view.pagy_nav(pagy) html.must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 103, page: 6) - html = frontend.pagy_nav(pagy) + html = view.pagy_nav(pagy) html.must_equal \ "" end @@ -61,29 +61,29 @@ it 'renders first page' do pagy = Pagy.new(count: 103, page: 1) - frontend.pagy_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_nav_js(pagy, pagy_test_id).must_equal \ "" - frontend.pagy_bootstrap_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bootstrap_nav_js(pagy, pagy_test_id).must_equal \ "" - frontend.pagy_bulma_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bulma_nav_js(pagy, pagy_test_id).must_equal \ "" - frontend.pagy_foundation_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_foundation_nav_js(pagy, pagy_test_id).must_equal \ "" - frontend.pagy_materialize_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_materialize_nav_js(pagy, pagy_test_id).must_equal \ "
" - frontend.pagy_semantic_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_semantic_nav_js(pagy, pagy_test_id).must_equal \ "
" end it 'renders intermediate page' do pagy = Pagy.new(count: 103, page: 3) - frontend.pagy_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_nav_js(pagy, pagy_test_id).must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 103, page: 6) - frontend.pagy_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_nav_js(pagy, pagy_test_id).must_equal \ "" end @@ -93,30 +93,30 @@ it 'renders first page' do pagy = Pagy.new(count: 103, page: 1) - frontend.pagy_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_combo_nav_js(pagy, pagy_test_id).must_equal \ "" - frontend.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bootstrap_combo_nav_js(pagy, pagy_test_id).must_equal \ "" - frontend.pagy_bulma_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_bulma_combo_nav_js(pagy, pagy_test_id).must_equal \ "" - frontend.pagy_foundation_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_foundation_combo_nav_js(pagy, pagy_test_id).must_equal \ "" - frontend.pagy_materialize_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_materialize_combo_nav_js(pagy, pagy_test_id).must_equal \ "
" - frontend.pagy_semantic_combo_nav_js(pagy, pagy_test_id).must_equal \ + view.pagy_semantic_combo_nav_js(pagy, pagy_test_id).must_equal \ "
Page of 6
" end it 'renders intermediate page' do pagy = Pagy.new(count: 103, page: 3) - html = frontend.pagy_combo_nav_js(pagy, pagy_test_id) + html = view.pagy_combo_nav_js(pagy, pagy_test_id) html.must_equal \ "" end it 'renders last page' do pagy = Pagy.new(count: 103, page: 6) - html = frontend.pagy_combo_nav_js(pagy, pagy_test_id) + html = view.pagy_combo_nav_js(pagy, pagy_test_id) html.must_equal \ "" end diff --git a/test/pagy/frontend_test.rb b/test/pagy/frontend_test.rb index 25a0fc51d..d92b14920 100644 --- a/test/pagy/frontend_test.rb +++ b/test/pagy/frontend_test.rb @@ -5,85 +5,38 @@ describe Pagy::Frontend do - let(:frontend) { TestView.new } + let(:view) { MockView.new } describe "#pagy_nav" do - before do - @array = (1..103).to_a.extend(Pagy::Array::PageMethod) - end - it 'renders page 1' do - pagy, _ = @array.pagy(1) - frontend.pagy_nav(pagy).must_equal \ - '' + pagy = Pagy.new count: 103, page: 1 + view.pagy_nav(pagy).must_equal \ + "" end it 'renders page 3' do - pagy, _ = @array.pagy(3) - frontend.pagy_nav(pagy).must_equal \ - '' + pagy = Pagy.new count: 103, page: 3 + view.pagy_nav(pagy).must_equal \ + "" end it 'renders page 6' do - pagy, _ = @array.pagy(6) - frontend.pagy_nav(pagy).must_equal \ - '' + pagy = Pagy.new count: 103, page: 6 + view.pagy_nav(pagy).must_equal \ + "" end it 'renders page 10' do - @array = (1..1000).to_a.extend(Pagy::Array::PageMethod) - pagy, _ = @array.pagy(10) - frontend.pagy_nav(pagy).must_equal \ - '' + pagy = Pagy.new count: 1000, page: 10 + view.pagy_nav(pagy).must_equal \ + "" end it 'renders with link_extras' do - pagy, _ = @array.pagy(1, link_extra: "X") - frontend.pagy_nav(pagy).must_include '?page=2" X rel' + pagy = Pagy.new count: 103, page: 1, link_extra: "X" + view.pagy_nav(pagy).must_include '?page=2" X rel' end end @@ -91,9 +44,8 @@ describe "#pagy_link_proc" do it 'renders with extras' do - @array = (1..103).to_a.extend(Pagy::Array::PageMethod) - pagy, _ = @array.pagy(1) - frontend.pagy_link_proc(pagy, "X").call(1).must_equal '1' + pagy = Pagy.new count: 103, page: 1 + view.pagy_link_proc(pagy, "X").call(1).must_equal '1' end end @@ -101,23 +53,23 @@ describe "#pagy_t" 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 'interpolates' do - frontend.pagy_t('pagy.info.no_items', count: 0).must_equal "No %{item_name} found" - frontend.pagy_t('pagy.info.single_page', count: 1).must_equal "Displaying 1 %{item_name}" - frontend.pagy_t('pagy.info.single_page', count: 10).must_equal "Displaying 10 %{item_name}" - frontend.pagy_t('pagy.info.multiple_pages', count: 10).must_equal "Displaying %{item_name} %{from}-%{to} of 10 in total" - frontend.pagy_t('pagy.info.multiple_pages', item_name: 'Products', count: 300, from: 101, to: 125).must_equal "Displaying Products 101-125 of 300 in total" + view.pagy_t('pagy.info.no_items', count: 0).must_equal "No %{item_name} found" + view.pagy_t('pagy.info.single_page', count: 1).must_equal "Displaying 1 %{item_name}" + view.pagy_t('pagy.info.single_page', count: 10).must_equal "Displaying 10 %{item_name}" + view.pagy_t('pagy.info.multiple_pages', count: 10).must_equal "Displaying %{item_name} %{from}-%{to} of 10 in total" + view.pagy_t('pagy.info.multiple_pages', item_name: 'Products', count: 300, from: 101, to: 125).must_equal "Displaying Products 101-125 of 300 in total" end it 'handles missing paths' do - frontend.pagy_t('pagy.nav.not_here').must_equal '[translation missing: "pagy.nav.not_here"]' - frontend.pagy_t('pagy.nav.gap.not_here').must_equal '[translation missing: "pagy.nav.gap.not_here"]' + view.pagy_t('pagy.nav.not_here').must_equal '[translation missing: "pagy.nav.not_here"]' + view.pagy_t('pagy.nav.gap.not_here').must_equal '[translation missing: "pagy.nav.gap.not_here"]' end end @@ -141,16 +93,16 @@ it 'switches :locale according to @pagy_locale' do Pagy::I18n.load({locale: 'de'}, {locale: 'en'}, {locale: 'nl'}) - frontend.instance_variable_set(:'@pagy_locale', 'nl') - frontend.pagy_t('pagy.item_name', count: 1).must_equal "stuk" - frontend.instance_variable_set(:'@pagy_locale', 'en') - frontend.pagy_t('pagy.item_name', count: 1).must_equal "item" - frontend.instance_variable_set(:'@pagy_locale', nil) - frontend.pagy_t('pagy.item_name', count: 1).must_equal "Eintrag" - frontend.instance_variable_set(:'@pagy_locale', 'unknown') - frontend.pagy_t('pagy.item_name', count: 1).must_equal "Eintrag" # silently serves the first loaded locale + view.instance_variable_set(:'@pagy_locale', 'nl') + view.pagy_t('pagy.item_name', count: 1).must_equal "stuk" + view.instance_variable_set(:'@pagy_locale', 'en') + view.pagy_t('pagy.item_name', count: 1).must_equal "item" + view.instance_variable_set(:'@pagy_locale', nil) + view.pagy_t('pagy.item_name', count: 1).must_equal "Eintrag" + view.instance_variable_set(:'@pagy_locale', 'unknown') + view.pagy_t('pagy.item_name', count: 1).must_equal "Eintrag" # silently serves the first loaded locale Pagy::I18n.load(locale: 'en') # reset for other tests - frontend.instance_variable_set(:'@pagy_locale', nil) # reset for other tests + view.instance_variable_set(:'@pagy_locale', nil) # reset for other tests end end @@ -158,19 +110,19 @@ describe "#pagy_info" do it 'renders without i18n path' 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 Pagy::I18n['en'][0]['pagy.info.product.one'] = lambda{|_| 'Product'} Pagy::I18n['en'][0]['pagy.info.product.other'] = lambda{|_| 'Products'} - frontend.pagy_info(Pagy.new count: 0, i18n_key: 'pagy.info.product').must_equal "No Products found" - frontend.pagy_info(Pagy.new count: 1, i18n_key: 'pagy.info.product').must_equal "Displaying 1 Product" - frontend.pagy_info(Pagy.new count: 13, i18n_key: 'pagy.info.product').must_equal "Displaying 13 Products" - frontend.pagy_info(Pagy.new count: 100, i18n_key: 'pagy.info.product', page: 3).must_equal "Displaying Products 41-60 of 100 in total" + view.pagy_info(Pagy.new count: 0, i18n_key: 'pagy.info.product').must_equal "No Products found" + view.pagy_info(Pagy.new count: 1, i18n_key: 'pagy.info.product').must_equal "Displaying 1 Product" + view.pagy_info(Pagy.new count: 13, i18n_key: 'pagy.info.product').must_equal "Displaying 13 Products" + view.pagy_info(Pagy.new count: 100, i18n_key: 'pagy.info.product', page: 3).must_equal "Displaying Products 41-60 of 100 in total" Pagy::I18n.load(locale: 'en') # reset for other tests end end @@ -179,26 +131,26 @@ it 'renders basic url' do pagy = Pagy.new count: 1000, page: 3 - frontend.pagy_url_for(5, pagy). must_equal '/foo?page=5' - frontend.pagy_url_for(5, pagy, :url). must_equal 'http://example.com:3000/foo?page=5' + view.pagy_url_for(5, pagy). must_equal '/foo?page=5' + view.pagy_url_for(5, pagy, :url). must_equal 'http://example.com:3000/foo?page=5' end it 'renders url with params' do pagy = Pagy.new count: 1000, page: 3, params: {a: 3, b: 4} - frontend.pagy_url_for(5, pagy).must_equal "/foo?page=5&a=3&b=4" - frontend.pagy_url_for(5, pagy, :url).must_equal "http://example.com:3000/foo?page=5&a=3&b=4" + view.pagy_url_for(5, pagy).must_equal "/foo?page=5&a=3&b=4" + view.pagy_url_for(5, pagy, :url).must_equal "http://example.com:3000/foo?page=5&a=3&b=4" 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#anchor' - frontend.pagy_url_for(6, pagy, :url).must_equal 'http://example.com:3000/foo?page=6#anchor' + view.pagy_url_for(6, pagy).must_equal '/foo?page=6#anchor' + view.pagy_url_for(6, pagy, :url).must_equal 'http://example.com:3000/foo?page=6#anchor' end it 'renders url with params and anchor' do pagy = Pagy.new count: 1000, page: 3, params: {a: 3, b: 4}, anchor: '#anchor' - frontend.pagy_url_for(5, pagy).must_equal "/foo?page=5&a=3&b=4#anchor" - frontend.pagy_url_for(5, pagy, :url).must_equal "http://example.com:3000/foo?page=5&a=3&b=4#anchor" + view.pagy_url_for(5, pagy).must_equal "/foo?page=5&a=3&b=4#anchor" + view.pagy_url_for(5, pagy, :url).must_equal "http://example.com:3000/foo?page=5&a=3&b=4#anchor" end end @@ -206,7 +158,7 @@ describe '#pagy_get_params' do it 'overrides params' do - overridden = TestViewOverride.new + overridden = MockView::Overridden.new pagy = Pagy.new count: 1000, page: 3, params: {a: 3, b: 4}, anchor: '#anchor' overridden.pagy_url_for(5, pagy).must_equal "/foo?page=5&b=4&k=k#anchor" end diff --git a/test/test_helper.rb b/test/test_helper.rb index 053415929..beb0d7ec4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -15,7 +15,6 @@ require 'pagy' require 'pagy/countless' require 'rack' -require_relative 'test_helper/array' -require_relative 'test_helper/frontend' -require_relative 'test_helper/backend' +require_relative 'mock_helpers/view' +require_relative 'mock_helpers/controller' require "minitest/autorun" diff --git a/test/test_helper/array.rb b/test/test_helper/array.rb deleted file mode 100644 index d73e34c53..000000000 --- a/test/test_helper/array.rb +++ /dev/null @@ -1,20 +0,0 @@ -# encoding: utf-8 -# frozen_string_literal: true - -class Pagy - module Array - # extend any array object to get the page method (mostly useful for testing) - module PageMethod - # extended_array = (1..100).to_a.extend(Pagy::Array::PageMethods) - # pagy, paged = extended_array.page(4) - def pagy(page, vars={}) - vars = VARS.merge(vars) # use merge instead of **vars for ruby versions < 2.* - .merge(count: count, page: page) # add count and page - pagy = Pagy.new(vars) # create the pagy object - paged = self[pagy.offset, pagy.items] # paged using #offset and #items from the pagy object - return pagy, paged # return the pagy object and the page of items - end - - end - end -end diff --git a/test/test_helper/backend.rb b/test/test_helper/backend.rb deleted file mode 100644 index b2ad31916..000000000 --- a/test/test_helper/backend.rb +++ /dev/null @@ -1,54 +0,0 @@ -# encoding: utf-8 -# frozen_string_literal: true - -class TestController - 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}) - @params = params - end - - def request - @request ||= Rack::Request.new(Rack::MockRequest.env_for('https://example.com:8080/foo?page=1')) - end - - def response - @response ||= Rack::Response.new - end - -end - -class TestCollection < Array - - def initialize(*args) - 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 - -end - -class TestGroupedCollection < TestCollection - - def count(*) - Hash[@collection.map { |value| [value, value + 1] }] - end - -end diff --git a/test/test_helper/frontend.rb b/test/test_helper/frontend.rb deleted file mode 100644 index 1ba4e7332..000000000 --- a/test/test_helper/frontend.rb +++ /dev/null @@ -1,37 +0,0 @@ -# encoding: utf-8 -# frozen_string_literal: true - -class TestView - include Pagy::Frontend - - def request - Rack::Request.new(Rack::MockRequest.env_for('http://example.com:3000/foo?page=2')) - end -end - -class TestSimpleView - include Pagy::Frontend - - def request - Rack::Request.new(Rack::MockRequest.env_for('http://example.com:3000/foo?')) - end -end - - -class TestViewOverride < TestView - def pagy_get_params(params) - params.except(:a).merge!(k: 'k') - end -end - -class Hash - def except!(*keys) - keys.each { |key| delete(key) } - self - end - - def except(*keys) - dup.except!(*keys) - end -end -