Commit 3baaeb75 authored by Alex Kalderimis's avatar Alex Kalderimis

Add stricter tests that we preserve order

By testing this, we can remove the tests on private members.
parent bca169a3
This diff is collapsed.
...@@ -4,7 +4,7 @@ require 'spec_helper' ...@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe RelativePositioning do RSpec.describe RelativePositioning do
let_it_be(:default_user) { create_default(:user) } let_it_be(:default_user) { create_default(:user) }
let_it_be(:project) { create(:project) } let_it_be(:project, reload: true) { create(:project) }
def create_issue(pos) def create_issue(pos)
create(:issue, project: project, relative_position: pos) create(:issue, project: project, relative_position: pos)
...@@ -22,24 +22,29 @@ RSpec.describe RelativePositioning do ...@@ -22,24 +22,29 @@ RSpec.describe RelativePositioning do
describe '#move_to_end' do describe '#move_to_end' do
shared_examples 'able to place a new item at the end' do shared_examples 'able to place a new item at the end' do
it 'can place any new item' do it 'can place any new item' do
existing_issues = project.issues.to_a
new_item = create_issue(nil) new_item = create_issue(nil)
subject.move_to_end(new_item) subject.move_to_end(new_item)
new_item.save! new_item.save!
expect(new_item.relative_position).to eq(project.issues.maximum(:relative_position)) expect(new_item.relative_position).to eq(project.issues.maximum(:relative_position))
expect(project.issues.reorder(:relative_position).pluck(:id)).to eq(existing_issues.map(&:id) + [new_item.id])
end end
end end
shared_examples 'able to move existing items to the end' do shared_examples 'able to move existing items to the end' do
it 'can move any existing item' do it 'can move any existing item' do
issue = issues[index] issue = issues[index]
other_issues = issues.reject { |i| i == issue }
subject.move_to_end(issue) subject.move_to_end(issue)
issue.save! issue.save!
project.reset project.reset
expect(project.issues.pluck(:relative_position)).to all(be_between(range.first, range.last)) expect(project.issues.pluck(:relative_position)).to all(be_between(range.first, range.last))
expect(issue.relative_position).to eq(project.issues.maximum(:relative_position)) expect(issue.relative_position).to eq(project.issues.maximum(:relative_position))
expect(project.issues.reorder(:relative_position).pluck(:id)).to eq(other_issues.map(&:id) + [issue.id])
end end
end end
...@@ -98,24 +103,29 @@ RSpec.describe RelativePositioning do ...@@ -98,24 +103,29 @@ RSpec.describe RelativePositioning do
describe '#move_to_start' do describe '#move_to_start' do
shared_examples 'able to place a new item at the start' do shared_examples 'able to place a new item at the start' do
it 'can place any new item' do it 'can place any new item' do
existing_issues = project.issues.to_a
new_item = create_issue(nil) new_item = create_issue(nil)
subject.move_to_start(new_item) subject.move_to_start(new_item)
new_item.save! new_item.save!
expect(new_item.relative_position).to eq(project.issues.minimum(:relative_position)) expect(new_item.relative_position).to eq(project.issues.minimum(:relative_position))
expect(project.issues.reorder(:relative_position).pluck(:id)).to eq([new_item.id] + existing_issues.map(&:id))
end end
end end
shared_examples 'able to move existing items to the start' do shared_examples 'able to move existing items to the start' do
it 'can move any existing item' do it 'can move any existing item' do
issue = issues[index] issue = issues[index]
other_issues = issues.reject { |i| i == issue }
subject.move_to_start(issue) subject.move_to_start(issue)
issue.save! issue.save!
project.reset project.reset
expect(project.issues.pluck(:relative_position)).to all(be_between(range.first, range.last)) expect(project.issues.pluck(:relative_position)).to all(be_between(range.first, range.last))
expect(issue.relative_position).to eq(project.issues.minimum(:relative_position)) expect(issue.relative_position).to eq(project.issues.minimum(:relative_position))
expect(project.issues.reorder(:relative_position).pluck(:id)).to eq([issue.id] + other_issues.map(&:id))
end end
end end
...@@ -173,6 +183,9 @@ RSpec.describe RelativePositioning do ...@@ -173,6 +183,9 @@ RSpec.describe RelativePositioning do
describe '#move' do describe '#move' do
shared_examples 'able to move a new item' do shared_examples 'able to move a new item' do
let(:other_issues) { project.issues.reorder(relative_position: :asc).to_a }
let!(:previous_order) { other_issues.map(&:id) }
it 'can place any new item betwen two others' do it 'can place any new item betwen two others' do
new_item = create_issue(nil) new_item = create_issue(nil)
...@@ -183,6 +196,9 @@ RSpec.describe RelativePositioning do ...@@ -183,6 +196,9 @@ RSpec.describe RelativePositioning do
expect(new_item.relative_position).to be_between(range.first, range.last) expect(new_item.relative_position).to be_between(range.first, range.last)
expect(new_item.relative_position).to be_between(lhs.relative_position, rhs.relative_position) expect(new_item.relative_position).to be_between(lhs.relative_position, rhs.relative_position)
ids = project.issues.reorder(:relative_position).pluck(:id).reject { |id| id == new_item.id }
expect(ids).to eq(previous_order)
end end
it 'can place any new item after another' do it 'can place any new item after another' do
...@@ -194,6 +210,9 @@ RSpec.describe RelativePositioning do ...@@ -194,6 +210,9 @@ RSpec.describe RelativePositioning do
expect(new_item.relative_position).to be_between(range.first, range.last) expect(new_item.relative_position).to be_between(range.first, range.last)
expect(new_item.relative_position).to be > lhs.relative_position expect(new_item.relative_position).to be > lhs.relative_position
ids = project.issues.reorder(:relative_position).pluck(:id).reject { |id| id == new_item.id }
expect(ids).to eq(previous_order)
end end
it 'can place any new item before another' do it 'can place any new item before another' do
...@@ -205,12 +224,17 @@ RSpec.describe RelativePositioning do ...@@ -205,12 +224,17 @@ RSpec.describe RelativePositioning do
expect(new_item.relative_position).to be_between(range.first, range.last) expect(new_item.relative_position).to be_between(range.first, range.last)
expect(new_item.relative_position).to be < rhs.relative_position expect(new_item.relative_position).to be < rhs.relative_position
ids = project.issues.reorder(:relative_position).pluck(:id).reject { |id| id == new_item.id }
expect(ids).to eq(previous_order)
end end
end end
shared_examples 'able to move an existing item' do shared_examples 'able to move an existing item' do
let(:item) { issues[index] } let(:item) { issues[index] }
let(:positions) { project.reset.issues.pluck(:relative_position) } let(:positions) { project.reset.issues.pluck(:relative_position) }
let(:other_issues) { project.issues.reorder(:relative_position).to_a.reject { |i| i == item } }
let!(:previous_order) { other_issues.map(&:id) }
it 'can place any item betwen two others' do it 'can place any item betwen two others' do
subject.move(item, lhs, rhs) subject.move(item, lhs, rhs)
...@@ -221,6 +245,9 @@ RSpec.describe RelativePositioning do ...@@ -221,6 +245,9 @@ RSpec.describe RelativePositioning do
expect(positions).to all(be_between(range.first, range.last)) expect(positions).to all(be_between(range.first, range.last))
expect(positions).to match_array(positions.uniq) expect(positions).to match_array(positions.uniq)
expect(item.relative_position).to be_between(lhs.relative_position, rhs.relative_position) expect(item.relative_position).to be_between(lhs.relative_position, rhs.relative_position)
ids = project.issues.reorder(:relative_position).pluck(:id).reject { |id| id == item.id }
expect(ids).to eq(previous_order)
end end
it 'can place any item after another' do it 'can place any item after another' do
...@@ -238,6 +265,9 @@ RSpec.describe RelativePositioning do ...@@ -238,6 +265,9 @@ RSpec.describe RelativePositioning do
.where(relative_position: (expected_sequence.first.relative_position..expected_sequence.last.relative_position)) .where(relative_position: (expected_sequence.first.relative_position..expected_sequence.last.relative_position))
expect(sequence).to eq(expected_sequence) expect(sequence).to eq(expected_sequence)
ids = project.issues.reorder(:relative_position).pluck(:id).reject { |id| id == item.id }
expect(ids).to eq(previous_order)
end end
it 'can place any item before another' do it 'can place any item before another' do
...@@ -255,6 +285,9 @@ RSpec.describe RelativePositioning do ...@@ -255,6 +285,9 @@ RSpec.describe RelativePositioning do
.where(relative_position: (expected_sequence.first.relative_position..expected_sequence.last.relative_position)) .where(relative_position: (expected_sequence.first.relative_position..expected_sequence.last.relative_position))
expect(sequence).to eq(expected_sequence) expect(sequence).to eq(expected_sequence)
ids = project.issues.reorder(:relative_position).pluck(:id).reject { |id| id == item.id }
expect(ids).to eq(previous_order)
end end
end end
...@@ -303,10 +336,14 @@ RSpec.describe RelativePositioning do ...@@ -303,10 +336,14 @@ RSpec.describe RelativePositioning do
end end
context 'there are a couple of siblings' do context 'there are a couple of siblings' do
where(:pos_a, :pos_b) { range.to_a.product(range.to_a).reject { |x, y| x == y } } where(:pos_movable, :pos_a, :pos_b) do
xs = range.to_a
xs.product(xs).product(xs).map(&:flatten).select { |vals| vals == vals.uniq }
end
with_them do with_them do
let!(:issues) { [range.first, pos_a, pos_b].sort.map { |p| create_issue(p) } } let!(:issues) { ([pos_movable] + [pos_a, pos_b].sort).map { |p| create_issue(p) } }
let(:index) { 0 } let(:index) { 0 }
let(:lhs) { issues[1] } let(:lhs) { issues[1] }
let(:rhs) { issues[2] } let(:rhs) { issues[2] }
......
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