Commit c3d9d3ca authored by Jarka Kadlecova's avatar Jarka Kadlecova

Refactor spec code

parent b440e0fa
......@@ -7,12 +7,35 @@ describe EpicIssues::CreateService do
let(:project) { create(:project, group: group) }
let(:issue) { create :issue, project: project }
let(:user) { create :user }
let(:reference) { issue.to_reference(full: true) }
let(:params) do
{}
let(:valid_reference) { issue.to_reference(full: true) }
def assign_issue(references)
params = { issue_references: references }
described_class.new(epic, user, params).execute
end
shared_examples 'returns success' do
it 'creates relationships' do
expect { subject }.to change(EpicIssue, :count).from(0).to(1)
expect(EpicIssue.find_by!(issue_id: issue.id)).to have_attributes(epic: epic)
end
it 'returns success status' do
expect(subject).to eq(status: :success)
end
end
subject { described_class.new(epic, user, params).execute }
shared_examples 'returns an error' do
it 'returns an error' do
expect(subject).to eq(message: 'No Issue found for given params', status: :error, http_status: 404)
end
it 'no relationship is created' do
expect { subject }.not_to change { EpicIssue.count }
end
end
context 'when user has permissions to link the issue' do
before do
......@@ -20,83 +43,39 @@ describe EpicIssues::CreateService do
end
context 'when the reference list is empty' do
let(:params) do
{ issue_references: [] }
end
it 'returns error' do
is_expected.to eq(message: 'No Issue found for given params', status: :error, http_status: 404)
it 'returns an error' do
expect(assign_issue([])).to eq(message: 'No Issue found for given params', status: :error, http_status: 404)
end
end
context 'when there is an issue to relate' do
context 'when shortcut for Issue is given' do
let(:params) do
{ issue_references: [issue.to_reference] }
end
it 'returns error' do
is_expected.to eq(message: 'No Issue found for given params', status: :error, http_status: 404)
end
subject { assign_issue([issue.to_reference]) }
it 'no relationship is created' do
expect { subject }.not_to change { EpicIssue.count }
end
include_examples 'returns an error'
end
context 'when a full reference is given' do
let(:params) do
{ issue_references: [reference] }
end
it 'creates relationships' do
expect { subject }.to change(EpicIssue, :count).from(0).to(1)
expect(EpicIssue.find_by!(issue_id: issue.id)).to have_attributes(epic: epic)
end
subject { assign_issue([valid_reference]) }
it 'returns success status' do
is_expected.to eq(status: :success)
end
include_examples 'returns success'
end
context 'when an issue links is given' do
let(:params) do
{ issue_references: [IssuesHelper.url_for_issue(issue.iid, issue.project)] }
end
subject { assign_issue([IssuesHelper.url_for_issue(issue.iid, issue.project)]) }
it 'creates relationships' do
expect { subject }.to change(EpicIssue, :count).from(0).to(1)
expect(EpicIssue.find_by!(issue_id: issue.id)).to have_attributes(epic: epic)
end
it 'returns success status' do
is_expected.to eq(status: :success)
end
include_examples 'returns success'
end
end
end
context 'when user does not have permissions to link the issue' do
let(:params) do
{ issue_references: [reference] }
end
it 'returns error' do
is_expected.to eq(message: 'No Issue found for given params', status: :error, http_status: 404)
end
subject { assign_issue([valid_reference]) }
it 'no relationship is created' do
expect { subject }.not_to change { EpicIssue.count }
end
include_examples 'returns an error'
end
context 'when an issue is already assigned to another epic' do
let(:params) do
{ issue_references: [reference] }
end
before do
group.add_developer(user)
create(:epic_issue, epic: epic, issue: issue)
......@@ -104,14 +83,18 @@ describe EpicIssues::CreateService do
let(:another_epic) { create(:epic, group: group) }
subject { described_class.new(another_epic, user, params).execute }
subject do
params = { issue_references: [valid_reference] }
described_class.new(another_epic, user, params).execute
end
it 'does not create a new association' do
expect { subject }.not_to change(EpicIssue, :count).from(1)
end
it 'updates the existing association' do
expect { subject }.to change { EpicIssue.find_by!(issue_id: issue.id).epic }.from(epic).to(another_epic)
expect { subject }.to change { EpicIssue.last.epic }.from(epic).to(another_epic)
end
it 'returns success status' do
......@@ -120,24 +103,16 @@ describe EpicIssues::CreateService do
end
context 'when issue from non group project is given' do
let(:another_issue) { create :issue }
subject { assign_issue([another_issue.to_reference(full: true)]) }
let(:params) do
{ issue_references: [another_issue.to_reference(full: true)] }
end
let(:another_issue) { create :issue }
before do
group.add_developer(user)
another_issue.project.add_developer(user)
end
it 'returns error' do
is_expected.to eq(message: 'No Issue found for given params', status: :error, http_status: 404)
end
it 'no relationship is created' do
expect { subject }.not_to change { EpicIssue.count }
end
include_examples 'returns an error'
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment