summaryrefslogtreecommitdiff
path: root/spec/components/diffs/overflow_warning_component_spec.rb
blob: ee4014ee492e2d337cc1b2c22762dc048e2c70b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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