Commit 5ae34eed authored by Jacques Erasmus's avatar Jacques Erasmus

Merge branch 'diffs-overflow-warning-component' into 'master'

Migrate diffs overflow warning to component

See merge request gitlab-org/gitlab!83970
parents c337f226 17d02a40
= render Pajamas::AlertComponent.new(title: _('Too many changes to show.'),
variant: :warning,
alert_class: 'gl-mb-5') do
.gl-alert-body
= message
.gl-alert-actions
= diff_link
= patch_link
# frozen_string_literal: true
module Diffs
class OverflowWarningComponent < BaseComponent
# Skipping coverage because of https://gitlab.com/gitlab-org/gitlab/-/issues/357381
#
# This is fully tested by the output in the view part of this component,
# but undercoverage doesn't understand the relationship between the two parts.
#
# :nocov:
def initialize(diffs:, diff_files:, project:, commit: nil, merge_request: nil)
@diffs = diffs
@diff_files = diff_files
@project = project
@commit = commit
@merge_request = merge_request
end
def message
html_escape(message_text) % {
display_size: @diff_files.size,
real_size: @diffs.real_size,
strong_open: '<strong>'.html_safe,
strong_close: '</strong>'.html_safe
}
end
def diff_link
text = _("Plain diff")
if commit?
link_to text, project_commit_path(@project, @commit, format: :diff), class: button_classes
elsif merge_request?
link_to text, merge_request_path(@merge_request, format: :diff), class: button_classes
end
end
def patch_link
text = _("Email patch")
if commit?
link_to text, project_commit_path(@project, @commit, format: :patch), class: button_classes
elsif merge_request?
link_to text, merge_request_path(@merge_request, format: :patch), class: button_classes
end
end
private
def commit?
current_controller?(:commit) &&
@commit.present?
end
def merge_request?
current_controller?("projects/merge_requests/diffs") &&
@merge_request.present? &&
@merge_request.persisted?
end
def message_text
_(
"To preserve performance only %{strong_open}%{display_size} " \
"of %{real_size}%{strong_close} files are displayed."
)
end
def button_classes
"btn gl-alert-action btn-default gl-button btn-default-secondary"
end
# :nocov:
end
end
......@@ -28,7 +28,7 @@
= render Diffs::StatsComponent.new(diff_files: diff_files)
- if render_overflow_warning?(diffs)
= render 'projects/diffs/warning', diff_files: diffs
= render Diffs::OverflowWarningComponent.new(diffs: diffs, diff_files: diff_files, project: @project, commit: @commit, merge_request: @merge_request)
.files{ data: { can_create_note: can_create_note } }
- if load_diff_files_async
......
= render Pajamas::AlertComponent.new(title: _('Too many changes to show.'),
variant: :warning,
alert_class: 'gl-mb-5') do
.gl-alert-body
= html_escape(_("To preserve performance only %{strong_open}%{display_size} of %{real_size}%{strong_close} files are displayed.")) % { display_size: diff_files.size, real_size: diff_files.real_size, strong_open: '<strong>'.html_safe, strong_close: '</strong>'.html_safe }
.gl-alert-actions
- if current_controller?(:commit)
= link_to _("Plain diff"), project_commit_path(@project, @commit, format: :diff), class: "btn gl-alert-action btn-default gl-button btn-default-secondary"
= link_to _("Email patch"), project_commit_path(@project, @commit, format: :patch), class: "btn gl-alert-action btn-default gl-button btn-default-secondary"
- elsif current_controller?('projects/merge_requests/diffs') && @merge_request&.persisted?
= link_to _("Plain diff"), merge_request_path(@merge_request, format: :diff), class: "btn gl-alert-action btn-default gl-button btn-default-secondary"
= link_to _("Email patch"), merge_request_path(@merge_request, format: :patch), class: "btn gl-alert-action btn-default gl-button btn-default-secondary"
# frozen_string_literal: true
require "spec_helper"
RSpec.describe Diffs::OverflowWarningComponent, type: :component do
include RepoHelpers
subject(:component) do
described_class.new(
diffs: diffs,
diff_files: diff_files,
project: project,
commit: commit,
merge_request: merge_request
)
end
let_it_be(:project) { create(:project, :repository) }
let_it_be(:repository) { project.repository }
let_it_be(:commit) { project.commit(sample_commit.id) }
let_it_be(:diffs) { commit.raw_diffs }
let_it_be(:diff) { diffs.first }
let_it_be(:diff_refs) { commit.diff_refs }
let_it_be(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: diff_refs, repository: repository) }
let_it_be(:diff_files) { [diff_file] }
let_it_be(:merge_request) { create(:merge_request, source_project: project) }
let(:expected_button_classes) do
"btn gl-alert-action btn-default gl-button btn-default-secondary"
end
describe "rendered component" do
subject { rendered_component }
context "on a commit page" do
before do
with_controller_class Projects::CommitController do
render_inline component
end
end
it { is_expected.to include(component.message) }
it "links to the diff" do
expect(component.diff_link).to eq(
ActionController::Base.helpers.link_to(
_("Plain diff"),
project_commit_path(project, commit, format: :diff),
class: expected_button_classes
)
)
is_expected.to include(component.diff_link)
end
it "links to the patch" do
expect(component.patch_link).to eq(
ActionController::Base.helpers.link_to(
_("Email patch"),
project_commit_path(project, commit, format: :patch),
class: expected_button_classes
)
)
is_expected.to include(component.patch_link)
end
end
context "on a merge request page and the merge request is persisted" do
before do
with_controller_class Projects::MergeRequests::DiffsController do
render_inline component
end
end
it { is_expected.to include(component.message) }
it "links to the diff" do
expect(component.diff_link).to eq(
ActionController::Base.helpers.link_to(
_("Plain diff"),
merge_request_path(merge_request, format: :diff),
class: expected_button_classes
)
)
is_expected.to include(component.diff_link)
end
it "links to the patch" do
expect(component.patch_link).to eq(
ActionController::Base.helpers.link_to(
_("Email patch"),
merge_request_path(merge_request, format: :patch),
class: expected_button_classes
)
)
is_expected.to include(component.patch_link)
end
end
context "both conditions fail" do
before do
allow(component).to receive(:commit?).and_return(false)
allow(component).to receive(:merge_request?).and_return(false)
render_inline component
end
it { is_expected.to include(component.message) }
it { is_expected.not_to include(expected_button_classes) }
it { is_expected.not_to include("Plain diff") }
it { is_expected.not_to include("Email patch") }
end
end
describe "#message" do
subject { component.message }
it { is_expected.to be_a(String) }
it "is HTML-safe" do
expect(subject.html_safe?).to be_truthy
end
end
describe "#diff_link" do
subject { component.diff_link }
before do
allow(component).to receive(:link_to).and_return("foo")
render_inline component
end
it "is a string when on a commit page" do
allow(component).to receive(:commit?).and_return(true)
is_expected.to eq("foo")
end
it "is a string when on a merge request page" do
allow(component).to receive(:commit?).and_return(false)
allow(component).to receive(:merge_request?).and_return(true)
is_expected.to eq("foo")
end
it "is nil in other situations" do
allow(component).to receive(:commit?).and_return(false)
allow(component).to receive(:merge_request?).and_return(false)
is_expected.to be_nil
end
end
describe "#patch_link" do
subject { component.patch_link }
before do
allow(component).to receive(:link_to).and_return("foo")
render_inline component
end
it "is a string when on a commit page" do
allow(component).to receive(:commit?).and_return(true)
is_expected.to eq("foo")
end
it "is a string when on a merge request page" do
allow(component).to receive(:commit?).and_return(false)
allow(component).to receive(:merge_request?).and_return(true)
is_expected.to eq("foo")
end
it "is nil in other situations" do
allow(component).to receive(:commit?).and_return(false)
allow(component).to receive(:merge_request?).and_return(false)
is_expected.to be_nil
end
end
end
......@@ -191,6 +191,7 @@ RSpec.configure do |config|
config.include MigrationsHelpers, :migration
config.include RedisHelpers
config.include Rails.application.routes.url_helpers, type: :routing
config.include Rails.application.routes.url_helpers, type: :component
config.include PolicyHelpers, type: :policy
config.include ExpectRequestWithStatus, type: :request
config.include IdempotentWorkerHelper, type: :worker
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment