Add specs for PolymorphicAssociation formatter

This commit is contained in:
Klaus Zanders
2025-07-04 15:10:56 +02:00
parent cd19180e64
commit fda4a14d1b
3 changed files with 112 additions and 2 deletions
@@ -1,3 +1,5 @@
# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -27,6 +29,5 @@
#++
FactoryBot.define do
factory :journal_time_entry_journal, class: "Journal::TimeEntryJournal" do
end
factory :journal_time_entry_journal, class: "Journal::TimeEntryJournal"
end
+16
View File
@@ -68,21 +68,37 @@ FactoryBot.define do
factory :message_journal, class: "Journal" do
journable_type { "Message" }
data { build(:journal_message_journal) }
callback(:after_stub) do |journal, options|
journal.journable ||= options.journable || build_stubbed(:message)
end
end
factory :news_journal, class: "Journal" do
journable_type { "News" }
data { build(:journal_message_journal) }
callback(:after_stub) do |journal, options|
journal.journable ||= options.journable || build_stubbed(:news)
end
end
factory :project_journal, class: "Journal" do
journable factory: :project
data { build(:journal_project_journal) }
callback(:after_stub) do |journal, options|
journal.journable ||= options.journable || build_stubbed(:project)
end
end
factory :time_entry_journal, class: "Journal" do
journable_type { "TimeEntry" }
data { association(:journal_time_entry_journal) }
callback(:after_stub) do |journal, options|
journal.journable ||= options.journable || build_stubbed(:time_entry)
end
end
end
end
@@ -0,0 +1,93 @@
# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
require "spec_helper"
RSpec.describe JournalFormatter::PolymorphicAssociation do
describe ".render" do
let(:journal) { build_stubbed(:time_entry_journal) }
let(:instance) { described_class.new(journal) }
let(:old_wp) { create(:work_package) }
let(:new_wp) { create(:work_package) }
let(:old_value) { old_wp&.to_gid }
let(:new_value) { new_wp&.to_gid }
context "when setting new value" do
let(:old_wp) { nil }
it "renders the new value" do
expect(instance.render("entity_gid", [old_value, new_value]))
.to eq(I18n.t(:text_journal_set_to,
label: "<strong>Logged for</strong>",
value: "<i>#{new_wp.subject}</i>"))
expect(instance.render("entity_gid", [old_value, new_value], html: false))
.to eq(I18n.t(:text_journal_set_to,
label: "Logged for",
value: new_wp.subject))
end
end
context "when changing value" do
it "renders the change from old to new value" do
expect(instance.render("entity_gid", [old_value, new_value]))
.to eq(I18n.t(:text_journal_changed_plain,
label: "<strong>Logged for</strong>",
linebreak: nil,
old: "<i>#{old_wp.subject}</i>",
new: "<i>#{new_wp.subject}</i>"))
expect(instance.render("entity_gid", [old_value, new_value], html: false))
.to eq(I18n.t(:text_journal_changed_plain,
label: "Logged for",
linebreak: nil,
old: old_wp.subject,
new: new_wp.subject))
end
end
context "when removing the value" do
let(:new_value) { nil }
it "renders as removed" do
expect(instance.render("entity_gid", [old_value, new_value]))
.to eq(I18n.t(:text_journal_deleted,
label: "<strong>Logged for</strong>",
old: "<strike><i>#{old_wp.subject}</i></strike>"))
expect(instance.render("entity_gid", [old_value, new_value], html: false))
.to eq(I18n.t(:text_journal_deleted,
label: "Logged for",
old: old_wp.subject))
end
end
end
end