summaryrefslogtreecommitdiff
path: root/spec/features/issues/note_polling_spec.rb
blob: 3cd7ce6dadaf0ad48c5cd8d208ecf05a663ed1aa (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
require 'spec_helper'

describe 'Issue notes polling', :js do
  include NoteInteractionHelpers

  let(:project) { create(:project, :public) }
  let(:issue) { create(:issue, project: project) }

  describe 'creates' do
    before do
      visit project_issue_path(project, issue)
    end

    it 'displays the new comment' do
      note = create(:note, noteable: issue, project: project, note: 'Looks good!')
      wait_for_requests

      expect(page).to have_selector("#note_#{note.id}", text: 'Looks good!')
    end
  end

  describe 'updates' do
    context 'when from own user' do
      let(:user) { create(:user) }
      let(:note_text) { "Hello World" }
      let(:updated_text) { "Bye World" }
      let!(:existing_note) { create(:note, noteable: issue, project: project, author: user, note: note_text) }

      before do
        sign_in(user)
        visit project_issue_path(project, issue)
      end

      it 'displays the updated content' do
        expect(page).to have_selector("#note_#{existing_note.id}", text: note_text)

        update_note(existing_note, updated_text)

        expect(page).to have_selector("#note_#{existing_note.id}", text: updated_text)
      end

      it 'when editing but have not changed anything, and an update comes in, show warning and does not update the note' do
        click_edit_action(existing_note)

        expect(page).to have_field("note[note]", with: note_text)

        update_note(existing_note, updated_text)

        expect(page).not_to have_field("note[note]", with: updated_text)
        expect(page).to have_selector(".alert")
      end

      it 'when editing but you changed some things, an update comes in, and you press cancel, show the updated content' do
        click_edit_action(existing_note)

        expect(page).to have_field("note[note]", with: note_text)

        update_note(existing_note, updated_text)

        find("#note_#{existing_note.id} .note-edit-cancel").click

        expect(page).to have_selector("#note_#{existing_note.id}", text: updated_text)
      end
    end

    context 'when from another user' do
      let(:user1) { create(:user) }
      let(:user2) { create(:user) }
      let(:note_text) { "Hello World" }
      let(:updated_text) { "Bye World" }
      let!(:existing_note) { create(:note, noteable: issue, project: project, author: user1, note: note_text) }

      before do
        sign_in(user2)
        visit project_issue_path(project, issue)
      end

      it 'displays the updated content' do
        expect(page).to have_selector("#note_#{existing_note.id}", text: note_text)

        update_note(existing_note, updated_text)

        expect(page).to have_selector("#note_#{existing_note.id}", text: updated_text)
      end
    end

    context 'system notes' do
      let(:user) { create(:user) }
      let(:note_text) { "Some system note" }
      let!(:system_note) { create(:system_note, noteable: issue, project: project, author: user, note: note_text) }

      before do
        sign_in(user)
        visit project_issue_path(project, issue)
      end

      it 'shows the system note' do
        expect(page).to have_selector("#note_#{system_note.id}", text: note_text)
      end
    end
  end

  def update_note(note, new_text)
    note.update(note: new_text)
    wait_for_requests
  end

  def click_edit_action(note)
    note_element = find("#note_#{note.id}")

    note_element.find('.js-note-edit').click
  end
end