File: parallel_hunk_component_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (97 lines) | stat: -rw-r--r-- 3,135 bytes parent folder | download
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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe RapidDiffs::Viewers::Text::ParallelHunkComponent, type: :component, feature_category: :code_review_workflow do
  let_it_be(:diff_file) { build(:diff_file) }
  let(:lines) { diff_file.diff_lines_with_match_tail }
  let(:old_line) { lines.find { |line| line.type == 'old' } }
  let(:new_line) { lines.find { |line| line.type == 'new' } }
  let(:hunk) do
    Gitlab::Diff::ViewerHunk.new(
      header: lines.first,
      lines: lines.drop(1)
    )
  end

  it "renders header" do
    render_component
    expect(page).to have_text(hunk.header_text, count: 2)
  end

  it "renders lines" do
    render_component
    page_text = page.native.inner_html
    hunk.lines.each do |line|
      expect(page_text).to include(line.text_content)
    end
  end

  it "renders line id" do
    old_line_id = old_line.id(diff_file.file_hash, :old)
    new_line_id = new_line.id(diff_file.file_hash, :new)
    render_component
    expect(page).to have_selector("##{old_line_id}")
    expect(page).to have_selector("##{new_line_id}")
  end

  it "renders line link" do
    old_line_id = old_line.id(diff_file.file_hash, :old)
    new_line_id = new_line.id(diff_file.file_hash, :new)
    render_component
    expect(page).to have_selector("a[href='##{old_line_id}']")
    expect(page).to have_selector("a[href='##{new_line_id}']")
  end

  it "renders legacy line id" do
    line_id = diff_file.line_code(old_line)
    render_component
    expect(page).to have_selector("[data-legacy-id='#{line_id}']")
  end

  it "renders expand up" do
    diff_hunk = Gitlab::Diff::ViewerHunk.new(
      header: Gitlab::Diff::Line.new("", 'match', 1, 0, 0),
      lines: lines.drop(1)
    )
    render_component(diff_hunk)
    expect(page).to have_selector('button svg use[href$="#expand-up"]')
  end

  it "renders expand down" do
    diff_hunk = Gitlab::Diff::ViewerHunk.new(
      header: Gitlab::Diff::Line.new("", 'match', 100, 0, 0),
      lines: []
    )
    render_component(diff_hunk)
    expect(page).to have_selector('button svg use[href$="#expand-down"]')
  end

  it "renders both expand up and down" do
    diff_hunk = Gitlab::Diff::ViewerHunk.new(
      header: Gitlab::Diff::Line.new("", 'match', 1, 0, 0),
      lines: lines.drop(1),
      prev: Gitlab::Diff::ViewerHunk.new(lines: [])
    )
    render_component(diff_hunk)
    expect(page).to have_selector('button svg use[href$="#expand-up"]')
    expect(page).to have_selector('button svg use[href$="#expand-down"]')
  end

  it "renders expand both" do
    last_prev_line = lines.first
    diff_hunk = Gitlab::Diff::ViewerHunk.new(
      header: lines.first,
      lines: lines.drop(1),
      prev: Gitlab::Diff::ViewerHunk.new(lines: [last_prev_line])
    )
    allow(diff_hunk.lines.first).to receive(:old_pos).and_return(5)
    allow(last_prev_line).to receive(:old_pos).and_return(2)
    render_component(diff_hunk)
    expect(page).to have_selector('button svg use[href$="#expand"]')
  end

  def render_component(diff_hunk = hunk)
    render_inline(described_class.new(diff_file: diff_file, diff_hunk: diff_hunk))
  end
end