form_spec.rb 9.3 KB
Newer Older
1 2
require 'rails_helper'

3
describe 'New/edit issue', :feature, :js do
4
  include GitlabRoutingHelper
5
  include ActionView::Helpers::JavaScriptHelper
C
Clement Ho 已提交
6
  include FormHelper
7

8 9
  let!(:project)   { create(:project) }
  let!(:user)      { create(:user)}
10
  let!(:user2)     { create(:user)}
11 12 13
  let!(:milestone) { create(:milestone, project: project) }
  let!(:label)     { create(:label, project: project) }
  let!(:label2)    { create(:label, project: project) }
14
  let!(:issue)     { create(:issue, project: project, assignees: [user], milestone: milestone) }
15 16 17

  before do
    project.team << [user, :master]
18
    project.team << [user2, :master]
19 20 21 22 23 24 25 26
    login_as(user)
  end

  context 'new issue' do
    before do
      visit new_namespace_project_issue_path(project.namespace, project)
    end

C
Clement Ho 已提交
27
    describe 'shorten users API pagination limit (CE)' do
C
Clement Ho 已提交
28
      before do
C
Clement Ho 已提交
29 30 31 32 33 34
        # Using `allow_any_instance_of`/`and_wrap_original`, `original` would
        # somehow refer to the very block we defined to _wrap_ that method, instead of
        # the original method, resulting in infinite recurison when called.
        # This is likely a bug with helper modules included into dynamically generated view classes.
        # To work around this, we have to hold on to and call to the original implementation manually.
        original_issue_dropdown_options = FormHelper.instance_method(:issue_dropdown_options)
C
Clement Ho 已提交
35
        allow_any_instance_of(FormHelper).to receive(:issue_dropdown_options).and_wrap_original do |original, *args|
C
Clement Ho 已提交
36 37
          options = original_issue_dropdown_options.bind(original.receiver).call(*args)
          options[:data][:per_page] = 2
C
Clement Ho 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

          options
        end

        visit new_namespace_project_issue_path(project.namespace, project)

        click_button 'Unassigned'

        wait_for_requests
      end

      it 'should display selected users even if they are not part of the original API call' do
        find('.dropdown-input-field').native.send_keys user2.name

        page.within '.dropdown-menu-user' do
          expect(page).to have_content user2.name
          click_link user2.name
        end

C
Clement Ho 已提交
57
        find('.js-assignee-search').click
C
Clement Ho 已提交
58 59 60 61 62 63 64 65 66
        find('.js-dropdown-input-clear').click

        page.within '.dropdown-menu-user' do
          expect(page).to have_content user.name
          expect(find('.dropdown-menu-user a.is-active').first(:xpath, '..')['data-user-id']).to eq(user2.id.to_s)
        end
      end
    end

C
Clement Ho 已提交
67
    describe 'single assignee (CE)' do
C
Clement Ho 已提交
68
      before do
C
Clement Ho 已提交
69
        click_button 'Unassigned'
C
Clement Ho 已提交
70

71
        wait_for_requests
72 73 74 75 76 77 78
      end

      it 'unselects other assignees when unassigned is selected' do
        page.within '.dropdown-menu-user' do
          click_link user2.name
        end

C
Clement Ho 已提交
79 80
        click_button user2.name

81 82 83 84 85 86 87 88 89 90 91 92 93
        page.within '.dropdown-menu-user' do
          click_link 'Unassigned'
        end

        expect(find('input[name="issue[assignee_ids][]"]', visible: false).value).to match('0')
      end

      it 'toggles assign to me when current user is selected and unselected' do
        page.within '.dropdown-menu-user' do
          click_link user.name
        end

        expect(find('a', text: 'Assign to me', visible: false)).not_to be_visible
C
Clement Ho 已提交
94

C
Clement Ho 已提交
95 96
        click_button user.name

C
Clement Ho 已提交
97 98 99 100 101 102 103 104
        page.within('.dropdown-menu-user') do
          click_link user.name
        end

        expect(page.find('.dropdown-menu-user', visible: false)).not_to be_visible
      end
    end

P
Phil Hughes 已提交
105
    it 'allows user to create new issue' do
106 107 108
      fill_in 'issue_title', with: 'title'
      fill_in 'issue_description', with: 'title'

109
      expect(find('a', text: 'Assign to me')).to be_visible
110
      click_button 'Unassigned'
111

112
      wait_for_requests
113

114
      page.within '.dropdown-menu-user' do
115
        click_link user2.name
116
      end
117
      expect(find('input[name="issue[assignee_ids][]"]', visible: false).value).to match(user2.id.to_s)
118 119 120 121 122 123
      page.within '.js-assignee-search' do
        expect(page).to have_content user2.name
      end
      expect(find('a', text: 'Assign to me')).to be_visible

      click_link 'Assign to me'
124 125
      assignee_ids = page.all('input[name="issue[assignee_ids][]"]', visible: false)

C
Clement Ho 已提交
126
      expect(assignee_ids[0].value).to match(user.id.to_s)
127

128
      page.within '.js-assignee-search' do
C
Clement Ho 已提交
129
        expect(page).to have_content user.name
130
      end
131
      expect(find('a', text: 'Assign to me', visible: false)).not_to be_visible
132 133

      click_button 'Milestone'
134 135 136 137 138
      page.within '.issue-milestone' do
        click_link milestone.title
      end
      expect(find('input[name="issue[milestone_id]"]', visible: false).value).to match(milestone.id.to_s)
      page.within '.js-milestone-select' do
139 140 141 142
        expect(page).to have_content milestone.title
      end

      click_button 'Labels'
143 144 145 146 147 148
      page.within '.dropdown-menu-labels' do
        click_link label.title
        click_link label2.title
      end
      page.within '.js-label-select' do
        expect(page).to have_content label.title
149
      end
150 151
      expect(page.all('input[name="issue[label_ids][]"]', visible: false)[1].value).to match(label.id.to_s)
      expect(page.all('input[name="issue[label_ids][]"]', visible: false)[2].value).to match(label2.id.to_s)
152 153 154

      click_button 'Submit issue'

155 156
      page.within '.issuable-sidebar' do
        page.within '.assignee' do
C
Clement Ho 已提交
157
          expect(page).to have_content "Assignee"
158 159 160 161 162 163 164 165 166 167
        end

        page.within '.milestone' do
          expect(page).to have_content milestone.title
        end

        page.within '.labels' do
          expect(page).to have_content label.title
          expect(page).to have_content label2.title
        end
168
      end
169 170 171 172 173 174 175 176

      page.within '.issuable-meta' do
        issue = Issue.find_by(title: 'title')

        expect(page).to have_text("Issue #{issue.to_reference}")
        # compare paths because the host differ in test
        expect(find_link(issue.to_reference)[:href]).to end_with(issue_path(issue))
      end
177
    end
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

    it 'correctly updates the dropdown toggle when removing a label' do
      click_button 'Labels'

      page.within '.dropdown-menu-labels' do
        click_link label.title
      end

      expect(find('.js-label-select')).to have_content(label.title)

      page.within '.dropdown-menu-labels' do
        click_link label.title
      end

      expect(find('.js-label-select')).to have_content('Labels')
    end
194 195

    it 'correctly updates the selected user when changing assignee' do
C
Clement Ho 已提交
196
      click_button 'Unassigned'
C
Clement Ho 已提交
197

198
      wait_for_requests
199

200 201 202 203
      page.within '.dropdown-menu-user' do
        click_link user.name
      end

C
Clement Ho 已提交
204 205
      expect(find('.js-assignee-search')).to have_content(user.name)
      click_button user.name
206 207 208 209 210

      page.within '.dropdown-menu-user' do
        click_link user2.name
      end

C
Clement Ho 已提交
211
      expect(find('.js-assignee-search')).to have_content(user2.name)
212
    end
213 214 215 216 217 218 219
  end

  context 'edit issue' do
    before do
      visit edit_namespace_project_issue_path(project.namespace, project, issue)
    end

P
Phil Hughes 已提交
220
    it 'allows user to update issue' do
221
      expect(find('input[name="issue[assignee_ids][]"]', visible: false).value).to match(user.id.to_s)
222
      expect(find('input[name="issue[milestone_id]"]', visible: false).value).to match(milestone.id.to_s)
223
      expect(find('a', text: 'Assign to me', visible: false)).not_to be_visible
224

225 226 227
      page.within '.js-user-search' do
        expect(page).to have_content user.name
      end
228

229 230
      page.within '.js-milestone-select' do
        expect(page).to have_content milestone.title
231 232
      end

233 234 235 236 237 238 239 240 241 242 243
      click_button 'Labels'
      page.within '.dropdown-menu-labels' do
        click_link label.title
        click_link label2.title
      end
      page.within '.js-label-select' do
        expect(page).to have_content label.title
      end
      expect(page.all('input[name="issue[label_ids][]"]', visible: false)[1].value).to match(label.id.to_s)
      expect(page.all('input[name="issue[label_ids][]"]', visible: false)[2].value).to match(label2.id.to_s)

244 245
      click_button 'Save changes'

246 247 248 249 250 251 252 253 254 255 256 257 258
      page.within '.issuable-sidebar' do
        page.within '.assignee' do
          expect(page).to have_content user.name
        end

        page.within '.milestone' do
          expect(page).to have_content milestone.title
        end

        page.within '.labels' do
          expect(page).to have_content label.title
          expect(page).to have_content label2.title
        end
259 260 261
      end
    end
  end
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  describe 'sub-group project' do
    let(:group) { create(:group) }
    let(:nested_group_1) { create(:group, parent: group) }
    let(:sub_group_project) { create(:empty_project, group: nested_group_1) }

    before do
      sub_group_project.add_master(user)

      visit new_namespace_project_issue_path(sub_group_project.namespace, sub_group_project)
    end

    it 'creates new label from dropdown' do
      click_button 'Labels'

      click_link 'Create new label'

      page.within '.dropdown-new-label' do
        fill_in 'new_label_name', with: 'test label'
        first('.suggest-colors-dropdown a').click

        click_button 'Create'

        wait_for_requests
      end

      page.within '.dropdown-menu-labels' do
        expect(page).to have_link 'test label'
      end
    end
  end

294 295 296 297 298 299 300 301 302
  def before_for_selector(selector)
    js = <<-JS.strip_heredoc
      (function(selector) {
        var el = document.querySelector(selector);
        return window.getComputedStyle(el, '::before').getPropertyValue('content');
      })("#{escape_javascript(selector)}")
    JS
    page.evaluate_script(js)
  end
C
Clement Ho 已提交
303
end