summaryrefslogtreecommitdiff
path: root/spec/features/projects/commit/comments/user_adds_comment_spec.rb
blob: 18917bec6d00f35031bc179c3281b41273e6b4b6 (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
require "spec_helper"

describe "User adds a comment on a commit", :js do
  include Spec::Support::Helpers::Features::NotesHelpers
  include RepoHelpers

  let(:comment_text) { "XML attached" }
  let(:another_comment_text) { "SVG attached" }
  let(:project) { create(:project, :repository) }
  let(:user) { create(:user) }

  before do
    sign_in(user)
    project.add_developer(user)
  end

  context "inline view" do
    before do
      visit(project_commit_path(project, sample_commit.id))
    end

    it "adds a comment" do
      EMOJI = ":+1:".freeze

      page.within(".js-main-target-form") do
        expect(page).not_to have_link("Cancel")

        fill_in("note[note]", with: "#{comment_text} #{EMOJI}")

        # Check on `Preview` tab
        click_link("Preview")

        expect(find(".js-md-preview")).to have_content(comment_text).and have_css("gl-emoji")
        expect(page).not_to have_css(".js-note-text")

        # Check on the `Write` tab
        click_link("Write")

        expect(page).to have_field("note[note]", with: "#{comment_text} #{EMOJI}")

        # Submit comment from the `Preview` tab to get rid of a separate `it` block
        # which would specially tests if everything gets cleared from the note form.
        click_link("Preview")
        click_button("Comment")
      end

      wait_for_requests

      page.within(".note") do
        expect(page).to have_content(comment_text).and have_css("gl-emoji")
      end

      page.within(".js-main-target-form") do
        expect(page).to have_field("note[note]", with: "").and have_no_css(".js-md-preview")
      end
    end

    context "when commenting on diff" do
      it "adds a comment" do
        page.within(".diff-file:nth-of-type(1)") do
          # Open a form for a comment and check UI elements are visible and acting as expecting.
          click_diff_line(sample_commit.line_code)

          expect(page).to have_css(".js-temp-notes-holder form.new-note")
                    .and have_css(".js-close-discussion-note-form", text: "Cancel")

          # The `Cancel` button closes the current form. The page should not have any open forms after that.
          find(".js-close-discussion-note-form").click

          expect(page).not_to have_css("form.new_note")

          # Try to open the same form twice. There should be only one form opened.
          click_diff_line(sample_commit.line_code)
          click_diff_line(sample_commit.line_code)

          expect(page).to have_css("form.new-note", count: 1)

          # Fill in a form.
          page.within("form[data-line-code='#{sample_commit.line_code}']") do
            fill_in("note[note]", with: "#{comment_text} :smile:")
          end

          # Open another form and check we have two forms now (because the first one is filled in).
          click_diff_line(sample_commit.del_line_code)

          expect(page).to have_field("note[note]", with: "#{comment_text} :smile:")
                    .and have_field("note[note]", with: "")

          # Test Preview feature for both forms.
          page.within("form[data-line-code='#{sample_commit.line_code}']") do
            click_link("Preview")
          end

          page.within("form[data-line-code='#{sample_commit.del_line_code}']") do
            fill_in("note[note]", with: another_comment_text)

            click_link("Preview")
          end

          expect(page).to have_css(".js-md-preview", visible: true, count: 2)
                    .and have_content(comment_text)
                    .and have_content(another_comment_text)
                    .and have_xpath("//gl-emoji[@data-name='smile']")

          # Test UI elements, then submit.
          page.within("form[data-line-code='#{sample_commit.line_code}']") do
            expect(find(".js-note-text", visible: false).text).to eq("")
            expect(page).to have_css('.js-md-write-button')

            click_button("Comment")
          end

          expect(page).to have_button("Reply...").and have_no_css("form.new_note")
        end

        # A comment should be added and visible.
        page.within(".diff-file:nth-of-type(1) .note") do
          expect(page).to have_content(comment_text).and have_xpath("//gl-emoji[@data-name='smile']")
        end
      end
    end
  end

  context "side-by-side view" do
    before do
      visit(project_commit_path(project, sample_commit.id, view: "parallel"))
    end

    it "adds a comment" do
      NEW_COMMENT = "New comment".freeze
      OLD_COMMENT = "Old comment".freeze

      # Left side.
      click_parallel_diff_line(sample_commit.del_line_code)

      page.within(".diff-file:nth-of-type(1) form[data-line-code='#{sample_commit.del_line_code}']") do
        fill_in("note[note]", with: OLD_COMMENT)
        click_button("Comment")
      end

      page.within(".diff-file:nth-of-type(1) .notes_content.parallel.old") do
        expect(page).to have_content(OLD_COMMENT)
      end

      # Right side.
      click_parallel_diff_line(sample_commit.line_code)

      page.within(".diff-file:nth-of-type(1) form[data-line-code='#{sample_commit.line_code}']") do
        fill_in("note[note]", with: NEW_COMMENT)
        click_button("Comment")
      end

      wait_for_requests

      expect(all(".diff-file:nth-of-type(1) .notes_content.parallel.new")[1].text).to have_content(NEW_COMMENT)
    end
  end

  private

  def click_diff_line(line)
    find(".line_holder[id='#{line}'] td:nth-of-type(1)").hover
    find(".line_holder[id='#{line}'] button").click
  end

  def click_parallel_diff_line(line)
    find(".line_holder.parallel td[id='#{line}']").find(:xpath, 'preceding-sibling::*[1][self::td]').hover
    find(".line_holder.parallel button[data-line-code='#{line}']").click
  end
end