diff options
Diffstat (limited to 'spec')
5 files changed, 275 insertions, 113 deletions
diff --git a/spec/features/issues/create_branch_merge_request_spec.rb b/spec/features/issues/create_branch_merge_request_spec.rb deleted file mode 100644 index edea95c6699..00000000000 --- a/spec/features/issues/create_branch_merge_request_spec.rb +++ /dev/null @@ -1,106 +0,0 @@ -require 'rails_helper' - -feature 'Create Branch/Merge Request Dropdown on issue page', :feature, :js do - let(:user) { create(:user) } - let!(:project) { create(:project, :repository) } - let(:issue) { create(:issue, project: project, title: 'Cherry-Coloured Funk') } - - context 'for team members' do - before do - project.team << [user, :developer] - sign_in(user) - end - - it 'allows creating a merge request from the issue page' do - visit project_issue_path(project, issue) - - perform_enqueued_jobs do - select_dropdown_option('create-mr') - - expect(page).to have_content('WIP: Resolve "Cherry-Coloured Funk"') - expect(current_path).to eq(project_merge_request_path(project, MergeRequest.first)) - - wait_for_requests - end - - visit project_issue_path(project, issue) - - expect(page).to have_content("created branch 1-cherry-coloured-funk") - expect(page).to have_content("mentioned in merge request !1") - end - - it 'allows creating a branch from the issue page' do - visit project_issue_path(project, issue) - - select_dropdown_option('create-branch') - - wait_for_requests - - expect(page).to have_selector('.dropdown-toggle-text ', text: '1-cherry-coloured-funk') - expect(current_path).to eq project_tree_path(project, '1-cherry-coloured-funk') - end - - context "when there is a referenced merge request" do - let!(:note) do - create(:note, :on_issue, :system, project: project, noteable: issue, - note: "mentioned in #{referenced_mr.to_reference}") - end - - let(:referenced_mr) do - create(:merge_request, :simple, source_project: project, target_project: project, - description: "Fixes #{issue.to_reference}", author: user) - end - - before do - referenced_mr.cache_merge_request_closes_issues!(user) - - visit project_issue_path(project, issue) - end - - it 'disables the create branch button' do - expect(page).to have_css('.create-mr-dropdown-wrap .unavailable:not(.hide)') - expect(page).to have_css('.create-mr-dropdown-wrap .available.hide', visible: false) - expect(page).to have_content /1 Related Merge Request/ - end - end - - context 'when merge requests are disabled' do - before do - project.project_feature.update(merge_requests_access_level: 0) - - visit project_issue_path(project, issue) - end - - it 'shows only create branch button' do - expect(page).not_to have_button('Create a merge request') - expect(page).to have_button('Create a branch') - end - end - - context 'when issue is confidential' do - it 'disables the create branch button' do - issue = create(:issue, :confidential, project: project) - - visit project_issue_path(project, issue) - - expect(page).not_to have_css('.create-mr-dropdown-wrap') - end - end - end - - context 'for visitors' do - before do - visit project_issue_path(project, issue) - end - - it 'shows no buttons' do - expect(page).not_to have_selector('.create-mr-dropdown-wrap') - end - end - - def select_dropdown_option(option) - find('.create-mr-dropdown-wrap .dropdown-toggle').click - find("li[data-value='#{option}']").click - find('.js-create-merge-request').click - end -end diff --git a/spec/features/issues/user_creates_branch_and_merge_request_spec.rb b/spec/features/issues/user_creates_branch_and_merge_request_spec.rb new file mode 100644 index 00000000000..539d7e9ff01 --- /dev/null +++ b/spec/features/issues/user_creates_branch_and_merge_request_spec.rb @@ -0,0 +1,248 @@ +require 'rails_helper' + +describe 'User creates branch and merge request on issue page', :js do + let(:user) { create(:user) } + let!(:project) { create(:project, :repository) } + let(:issue) { create(:issue, project: project, title: 'Cherry-Coloured Funk') } + + context 'when signed out' do + before do + visit project_issue_path(project, issue) + end + + it "doesn't show 'Create merge request' button" do + expect(page).not_to have_selector('.create-mr-dropdown-wrap') + end + end + + context 'when signed in' do + before do + project.add_developer(user) + + sign_in(user) + end + + context 'when interacting with the dropdown' do + before do + visit project_issue_path(project, issue) + end + + # In order to improve tests performance, all UI checks are placed in this test. + it 'shows elements' do + button_create_merge_request = find('.js-create-merge-request') + button_toggle_dropdown = find('.create-mr-dropdown-wrap .dropdown-toggle') + + button_toggle_dropdown.click + + dropdown = find('.create-merge-request-dropdown-menu') + + page.within(dropdown) do + button_create_target = find('.js-create-target') + input_branch_name = find('.js-branch-name') + input_source = find('.js-ref') + li_create_branch = find("li[data-value='create-branch']") + li_create_merge_request = find("li[data-value='create-mr']") + + # Test that all elements are presented. + expect(page).to have_content('Create merge request and branch') + expect(page).to have_content('Create branch') + expect(page).to have_content('Branch name') + expect(page).to have_content('Source (branch or tag)') + expect(page).to have_button('Create merge request') + expect(page).to have_selector('.js-branch-name:focus') + + test_selection_mark(li_create_branch, li_create_merge_request, button_create_target, button_create_merge_request) + test_branch_name_checking(input_branch_name) + test_source_checking(input_source) + + # The button inside dropdown should be disabled if any errors occured. + expect(page).to have_button('Create branch', disabled: true) + end + + # The top level button should be disabled if any errors occured. + expect(page).to have_button('Create branch', disabled: true) + end + + context 'when branch name is auto-generated' do + it 'creates a merge request' do + perform_enqueued_jobs do + select_dropdown_option('create-mr') + + expect(page).to have_content('WIP: Resolve "Cherry-Coloured Funk"') + expect(current_path).to eq(project_merge_request_path(project, MergeRequest.first)) + + wait_for_requests + end + + visit project_issue_path(project, issue) + + expect(page).to have_content('created branch 1-cherry-coloured-funk') + expect(page).to have_content('mentioned in merge request !1') + end + + it 'creates a branch' do + select_dropdown_option('create-branch') + + wait_for_requests + + expect(page).to have_selector('.dropdown-toggle-text ', text: '1-cherry-coloured-funk') + expect(current_path).to eq project_tree_path(project, '1-cherry-coloured-funk') + end + end + + context 'when branch name is custom' do + let(:branch_name) { 'custom-branch-name' } + + it 'creates a merge request' do + perform_enqueued_jobs do + select_dropdown_option('create-mr', branch_name) + + expect(page).to have_content('WIP: Resolve "Cherry-Coloured Funk"') + expect(page).to have_content('Request to merge custom-branch-name into') + expect(current_path).to eq(project_merge_request_path(project, MergeRequest.first)) + + wait_for_requests + end + + visit project_issue_path(project, issue) + + expect(page).to have_content('created branch custom-branch-name') + expect(page).to have_content('mentioned in merge request !1') + end + + it 'creates a branch' do + select_dropdown_option('create-branch', branch_name) + + wait_for_requests + + expect(page).to have_selector('.dropdown-toggle-text ', text: branch_name) + expect(current_path).to eq project_tree_path(project, branch_name) + end + end + end + + context "when there is a referenced merge request" do + let!(:note) do + create(:note, :on_issue, :system, project: project, noteable: issue, + note: "mentioned in #{referenced_mr.to_reference}") + end + + let(:referenced_mr) do + create(:merge_request, :simple, source_project: project, target_project: project, + description: "Fixes #{issue.to_reference}", author: user) + end + + before do + referenced_mr.cache_merge_request_closes_issues!(user) + + visit project_issue_path(project, issue) + end + + it 'disables the create branch button' do + expect(page).to have_css('.create-mr-dropdown-wrap .unavailable:not(.hide)') + expect(page).to have_css('.create-mr-dropdown-wrap .available.hide', visible: false) + expect(page).to have_content /1 Related Merge Request/ + end + end + + context 'when merge requests are disabled' do + before do + project.project_feature.update(merge_requests_access_level: 0) + + visit project_issue_path(project, issue) + end + + it 'shows only create branch button' do + expect(page).not_to have_button('Create merge request') + expect(page).to have_button('Create branch') + end + end + + context 'when issue is confidential' do + let(:issue) { create(:issue, :confidential, project: project) } + + it 'disables the create branch button' do + visit project_issue_path(project, issue) + + expect(page).not_to have_css('.create-mr-dropdown-wrap') + end + end + end + + private + + def select_dropdown_option(option, branch_name = nil) + find('.create-mr-dropdown-wrap .dropdown-toggle').click + find("li[data-value='#{option}']").click + + if branch_name + find('.js-branch-name').set(branch_name) + + # Javascript debounces AJAX calls. + # So we have to wait until AJAX requests are started. + # Details are in app/assets/javascripts/create_merge_request_dropdown.js + # this.refDebounce = _.debounce(...) + sleep 0.5 + + wait_for_requests + end + + find('.js-create-merge-request').click + end + + def test_branch_name_checking(input_branch_name) + expect(input_branch_name.value).to eq(issue.to_branch_name) + + input_branch_name.set('new-branch-name') + branch_name_message = find('.js-branch-message') + + expect(branch_name_message).to have_text('Checking branch name availability…') + + wait_for_requests + + expect(branch_name_message).to have_text('Branch name is available') + + input_branch_name.set(project.default_branch) + + expect(branch_name_message).to have_text('Checking branch name availability…') + + wait_for_requests + + expect(branch_name_message).to have_text('Branch is already taken') + end + + def test_selection_mark(li_create_branch, li_create_merge_request, button_create_target, button_create_merge_request) + page.within(li_create_merge_request) do + expect(page).to have_css('i.fa.fa-check') + expect(button_create_target).to have_text('Create merge request') + expect(button_create_merge_request).to have_text('Create merge request') + end + + li_create_branch.click + + page.within(li_create_branch) do + expect(page).to have_css('i.fa.fa-check') + expect(button_create_target).to have_text('Create branch') + expect(button_create_merge_request).to have_text('Create branch') + end + end + + def test_source_checking(input_source) + expect(input_source.value).to eq(project.default_branch) + + input_source.set('mas') # Intentionally entered first 3 letters of `master` to check autocomplete feature later. + source_message = find('.js-ref-message') + + expect(source_message).to have_text('Checking source availability…') + + wait_for_requests + + expect(source_message).to have_text('Source is not available') + + # JavaScript gets refs started with `mas` (entered above) and places the first match. + # User sees `mas` in black color (the part he entered) and the `ter` in gray color (a hint). + # Since hinting is implemented via text selection and rspec/capybara doesn't have matchers for it, + # we just checking the whole source name. + expect(input_source.value).to eq(project.default_branch) + end +end diff --git a/spec/javascripts/droplab/drop_down_spec.js b/spec/javascripts/droplab/drop_down_spec.js index 1ef494a00b8..1225fe2cb66 100644 --- a/spec/javascripts/droplab/drop_down_spec.js +++ b/spec/javascripts/droplab/drop_down_spec.js @@ -279,7 +279,12 @@ describe('DropDown', function () { describe('addEvents', function () { beforeEach(function () { this.list = { addEventListener: () => {} }; - this.dropdown = { list: this.list, clickEvent: () => {}, eventWrapper: {} }; + this.dropdown = { + list: this.list, + clickEvent: () => {}, + closeDropdown: () => {}, + eventWrapper: {}, + }; spyOn(this.list, 'addEventListener'); @@ -288,6 +293,7 @@ describe('DropDown', function () { it('should call .addEventListener', function () { expect(this.list.addEventListener).toHaveBeenCalledWith('click', jasmine.any(Function)); + expect(this.list.addEventListener).toHaveBeenCalledWith('keyup', jasmine.any(Function)); }); }); diff --git a/spec/javascripts/droplab/hook_spec.js b/spec/javascripts/droplab/hook_spec.js index 75bf5f3d611..3d39bd0812b 100644 --- a/spec/javascripts/droplab/hook_spec.js +++ b/spec/javascripts/droplab/hook_spec.js @@ -24,7 +24,7 @@ describe('Hook', function () { }); it('should call DropDown constructor', function () { - expect(dropdownSrc.default).toHaveBeenCalledWith(this.list); + expect(dropdownSrc.default).toHaveBeenCalledWith(this.list, this.config); }); it('should set .type', function () { diff --git a/spec/services/merge_requests/create_from_issue_service_spec.rb b/spec/services/merge_requests/create_from_issue_service_spec.rb index 313f87ae1f6..a7ab389b357 100644 --- a/spec/services/merge_requests/create_from_issue_service_spec.rb +++ b/spec/services/merge_requests/create_from_issue_service_spec.rb @@ -6,8 +6,10 @@ describe MergeRequests::CreateFromIssueService do let(:label_ids) { create_pair(:label, project: project).map(&:id) } let(:milestone_id) { create(:milestone, project: project).id } let(:issue) { create(:issue, project: project, milestone_id: milestone_id) } + let(:custom_source_branch) { 'custom-source-branch' } subject(:service) { described_class.new(project, user, issue_iid: issue.iid) } + subject(:service_with_custom_source_branch) { described_class.new(project, user, issue_iid: issue.iid, branch_name: custom_source_branch) } before do project.add_developer(user) @@ -17,8 +19,8 @@ describe MergeRequests::CreateFromIssueService do it 'returns an error with invalid issue iid' do result = described_class.new(project, user, issue_iid: -1).execute - expect(result[:status]).to eq :error - expect(result[:message]).to eq 'Invalid issue iid' + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('Invalid issue iid') end it 'delegates issue search to IssuesFinder' do @@ -53,6 +55,12 @@ describe MergeRequests::CreateFromIssueService do expect(project.repository.branch_exists?(issue.to_branch_name)).to be_truthy end + it 'creates a branch using passed name' do + service_with_custom_source_branch.execute + + expect(project.repository.branch_exists?(custom_source_branch)).to be_truthy + end + it 'creates a system note' do expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, issue.to_branch_name) @@ -72,19 +80,25 @@ describe MergeRequests::CreateFromIssueService do it 'sets the merge request author to current user' do result = service.execute - expect(result[:merge_request].author).to eq user + expect(result[:merge_request].author).to eq(user) end it 'sets the merge request source branch to the new issue branch' do result = service.execute - expect(result[:merge_request].source_branch).to eq issue.to_branch_name + expect(result[:merge_request].source_branch).to eq(issue.to_branch_name) + end + + it 'sets the merge request source branch to the passed branch name' do + result = service_with_custom_source_branch.execute + + expect(result[:merge_request].source_branch).to eq(custom_source_branch) end it 'sets the merge request target branch to the project default branch' do result = service.execute - expect(result[:merge_request].target_branch).to eq project.default_branch + expect(result[:merge_request].target_branch).to eq(project.default_branch) end end end |