BigW Consortium Gitlab

note_polling_spec.rb 4.87 KB
Newer Older
1 2
require 'spec_helper'

3
feature 'Issue notes polling', :feature, :js do
4 5
  include NoteInteractionHelpers

6 7
  let(:project) { create(:empty_project, :public) }
  let(:issue) { create(:issue, project: project) }
8

9 10 11 12 13 14 15 16 17 18 19
  describe 'creates' do
    before do
      visit namespace_project_issue_path(project.namespace, project, issue)
    end

    it 'displays the new comment' do
      note = create(:note, noteable: issue, project: project, note: 'Looks good!')
      page.execute_script('notes.refresh();')

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

22
  describe 'updates' do
23 24 25 26 27
    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) }
28

29 30 31 32
      before do
        login_as(user)
        visit namespace_project_issue_path(project.namespace, project, issue)
      end
33

34 35
      it 'has .original-note-content to compare against' do
        expect(page).to have_selector("#note_#{existing_note.id}", text: note_text)
36
        expect(page).to have_selector("#note_#{existing_note.id} .original-note-content", count: 1, visible: false)
37

38
        update_note(existing_note, updated_text)
39

40
        expect(page).to have_selector("#note_#{existing_note.id}", text: updated_text)
41
        expect(page).to have_selector("#note_#{existing_note.id} .original-note-content", count: 1, visible: false)
42
      end
43

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

47
        update_note(existing_note, updated_text)
48

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

52
      it 'when editing but have not changed anything, and an update comes in, show the updated content in the textarea' do
53
        click_edit_action(existing_note)
54

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

57
        update_note(existing_note, updated_text)
58

59 60
        expect(page).to have_field("note[note]", with: updated_text)
      end
61

62
      it 'when editing but you changed some things, and an update comes in, show a warning' do
63
        click_edit_action(existing_note)
64

65 66 67 68
        expect(page).to have_field("note[note]", with: note_text)

        find("#note_#{existing_note.id} .js-note-text").set('something random')
        update_note(existing_note, updated_text)
69

70 71 72 73
        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
74
        click_edit_action(existing_note)
75 76 77 78 79 80 81 82 83 84 85 86

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

        find("#note_#{existing_note.id} .js-note-text").set('something random')

        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
87

88 89 90 91 92 93
    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) }
94

95 96 97 98
      before do
        login_as(user2)
        visit namespace_project_issue_path(project.namespace, project, issue)
      end
99

100 101
      it 'has .original-note-content to compare against' do
        expect(page).to have_selector("#note_#{existing_note.id}", text: note_text)
102
        expect(page).to have_selector("#note_#{existing_note.id} .original-note-content", count: 1, visible: false)
103

104
        update_note(existing_note, updated_text)
105

106
        expect(page).to have_selector("#note_#{existing_note.id}", text: updated_text)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        expect(page).to have_selector("#note_#{existing_note.id} .original-note-content", count: 1, visible: false)
      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
        login_as(user)
        visit namespace_project_issue_path(project.namespace, project, issue)
      end

      it 'has .original-note-content to compare against' do
        expect(page).to have_selector("#note_#{system_note.id}", text: note_text)
        expect(page).to have_selector("#note_#{system_note.id} .original-note-content", count: 1, visible: false)
124
      end
125 126 127 128 129 130
    end
  end

  def update_note(note, new_text)
    note.update(note: new_text)
    page.execute_script('notes.refresh();')
131
  end
132 133 134 135 136 137 138 139

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

    open_more_actions_dropdown(note)

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