From 090f1d6e6ff6786e4ead3373eb4daf98543cc5d5 Mon Sep 17 00:00:00 2001 From: pezholio Date: Wed, 18 Dec 2024 11:38:34 +0000 Subject: [PATCH] Use new methods in PaginatedTimeline This calls the new methods on each entry, simplifying the code, and allowing us to stub the response, rather than relying on the objects set up in `seed_document_event_history` --- app/models/document/paginated_timeline.rb | 24 +----- .../document/paginated_timeline_test.rb | 81 ++++++++++++++----- 2 files changed, 66 insertions(+), 39 deletions(-) diff --git a/app/models/document/paginated_timeline.rb b/app/models/document/paginated_timeline.rb index c4ee3862cab..7993d2dd67a 100644 --- a/app/models/document/paginated_timeline.rb +++ b/app/models/document/paginated_timeline.rb @@ -35,33 +35,15 @@ def query end def entries_on_newer_editions(edition) - @entries_on_newer_editions ||= entries.select do |entry| - if entry.is_a?(EditorialRemark) - entry.edition_id > edition.id - else - entry.item_id > edition.id - end - end + entries.select { |e| e.is_for_newer_edition?(edition) } end def entries_on_current_edition(edition) - @entries_on_current_edition ||= entries.select do |entry| - if entry.is_a?(EditorialRemark) - entry.edition_id == edition.id - else - entry.item_id == edition.id - end - end + entries.select { |e| e.is_for_current_edition?(edition) } end def entries_on_previous_editions(edition) - @entries_on_previous_editions ||= entries.select do |entry| - if entry.is_a?(EditorialRemark) - entry.edition_id < edition.id - else - entry.item_id < edition.id - end - end + entries.select { |e| e.is_for_older_edition?(edition) } end def total_count diff --git a/test/unit/app/models/document/paginated_timeline_test.rb b/test/unit/app/models/document/paginated_timeline_test.rb index 628a710c66b..b8074b1d665 100644 --- a/test/unit/app/models/document/paginated_timeline_test.rb +++ b/test/unit/app/models/document/paginated_timeline_test.rb @@ -263,30 +263,75 @@ class PaginatedTimelineTest < ActiveSupport::TestCase end end - describe "#entries_on_newer_editions" do - it "returns entries on newer editions than the one passed in" do - timeline = Document::PaginatedTimeline.new(document: @document, page: 1) - expected_entries = timeline.entries.slice(1, 2) + describe "filtering entries" do + let(:entries_for_newer_editions) do + 2.times.map do + stub("entry", is_for_newer_edition?: true, is_for_current_edition?: false, is_for_older_edition?: false) + end + end - assert_equal expected_entries, timeline.entries_on_newer_editions(@first_edition) + let(:entries_for_current_edition) do + 4.times.map do + stub("entry", is_for_newer_edition?: false, is_for_current_edition?: true, is_for_older_edition?: false) + end end - end - - describe "#entries_on_current_edition" do - it "returns entries for the edition passed in" do - timeline = Document::PaginatedTimeline.new(document: @document, page: 1) - expected_entries = timeline.entries - timeline.entries.slice(1, 2) - assert_equal expected_entries, timeline.entries_on_current_edition(@first_edition) + let(:entries_for_older_editions) do + 3.times.map do + stub("entry", is_for_newer_edition?: false, is_for_current_edition?: false, is_for_older_edition?: true) + end end - end - describe "#entries_on_previous_editions" do - it "returns entries on previous editions" do - timeline = Document::PaginatedTimeline.new(document: @document, page: 1) - expected_entries = timeline.entries - timeline.entries.slice(1, 2) + let(:all_entries) do + [*entries_for_newer_editions, *entries_for_current_edition, *entries_for_older_editions] + end + + let(:timeline) { Document::PaginatedTimeline.new(document: @document, page: 1) } + + before do + timeline.stubs(:entries).returns(all_entries) + end + + describe "#entries_on_newer_editions" do + it "returns entries on newer editions than the one passed in" do + assert_equal entries_for_newer_editions, timeline.entries_on_newer_editions(@first_edition) + end + + it "calls the entries with the expected edition" do + all_entries.each do |entry| + entry.expects(:is_for_newer_edition?).with(@first_edition) + end + + timeline.entries_on_newer_editions(@first_edition) + end + end + + describe "#entries_on_current_edition" do + it "returns entries for the edition passed in" do + assert_equal entries_for_current_edition, timeline.entries_on_current_edition(@first_edition) + end + + it "calls the entries with the expected edition" do + all_entries.each do |entry| + entry.expects(:is_for_current_edition?).with(@first_edition) + end - assert_equal expected_entries, timeline.entries_on_previous_editions(@newest_edition) + timeline.entries_on_current_edition(@first_edition) + end + end + + describe "#entries_on_previous_editions" do + it "returns entries on previous editions" do + assert_equal entries_for_older_editions, timeline.entries_on_previous_editions(@newest_edition) + end + + it "calls the entries with the expected edition" do + all_entries.each do |entry| + entry.expects(:is_for_older_edition?).with(@first_edition) + end + + timeline.entries_on_previous_editions(@first_edition) + end end end