shared_examples "a proposal page" do |path_method|
  let!(:event) { create(:event, review_tags: [ 'intro', 'advanced' ]) }
  let!(:proposal) { create(:proposal, event: event) }
  let!(:reviewer) { create(:organizer, event: event) }

  before { login_user(reviewer) }

  context "a reviewer" do
    context "commenting" do

      before do
        create(:rating, proposal: proposal, person: reviewer)
        visit send(path_method, event, proposal)
      end

      it "can leave a public comment" do
        form = find('#new_public_comment')

        form.fill_in 'public_comment_body', with: 'A new comment'
        form.click_button 'Comment'

        expect(page).to have_css('.comment', 'A new comment')
      end

      it "can leave an internal comment" do
        form = find('#new_internal_comment')

        form.fill_in 'internal_comment_body', with: 'A new comment'
        form.click_button 'Comment'

        expect(page).to have_css('.internal-comments .comment', 'A new comment')
      end
    end

    context "rating and tagging" do
      before { visit send(path_method, event, proposal) }

      it "can rate a proposal", js: true do
        select '5', from: 'rating_score'
        expect(page).to have_css('.text-success', text: 'Average rating:')
        expect(page).to have_css('.text-success', text: '5.0')
      end

      it "can tag a proposal", js: true do
        button_selector = 'button.multiselect'
        button = find(button_selector)
        button.click

        check 'intro'
        check 'advanced'

        # Ideally we could click 'button' again and it would hide the
        # dropdown list of tags. However, if you try to click the button
        # directly capybara complains that the dropdown list is overlapping
        # the element to be clicked. This js snippet manually triggers the
        # click event on the multiselect button.
        page.execute_script("$('#{button_selector}').trigger('click');")

        click_button 'Update'

        expect(page).to have_css('span.label-success', text: 'intro')
        expect(page).to have_css('span.label-success', text: 'advanced')
      end
    end
  end
end
