2017-02-27 15:45:55 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-25 12:28:55 +00:00
|
|
|
describe RelativePositioning do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2017-02-27 15:45:55 +00:00
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:issue1) { create(:issue, project: project) }
|
2017-03-07 11:42:17 +00:00
|
|
|
let(:new_issue) { create(:issue, project: project) }
|
2017-02-27 15:45:55 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
[issue, issue1].each do |issue|
|
|
|
|
issue.move_to_end && issue.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-06 16:08:18 +00:00
|
|
|
describe '#max_relative_position' do
|
2017-02-27 15:45:55 +00:00
|
|
|
it 'returns maximum position' do
|
|
|
|
expect(issue.max_relative_position).to eq issue1.relative_position
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-07 17:57:24 +00:00
|
|
|
describe '#prev_relative_position' do
|
|
|
|
it 'returns previous position if there is an issue above' do
|
|
|
|
expect(issue1.prev_relative_position).to eq issue.relative_position
|
|
|
|
end
|
|
|
|
|
2017-03-10 15:12:31 +00:00
|
|
|
it 'returns nil if there is no issue above' do
|
|
|
|
expect(issue.prev_relative_position).to eq nil
|
2017-03-07 17:57:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#next_relative_position' do
|
|
|
|
it 'returns next position if there is an issue below' do
|
|
|
|
expect(issue.next_relative_position).to eq issue1.relative_position
|
|
|
|
end
|
|
|
|
|
2017-03-10 15:12:31 +00:00
|
|
|
it 'returns nil if there is no issue below' do
|
|
|
|
expect(issue1.next_relative_position).to eq nil
|
2017-03-07 17:57:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#move_before' do
|
|
|
|
it 'moves issue before' do
|
|
|
|
[issue1, issue].each(&:move_to_end)
|
|
|
|
|
|
|
|
issue.move_before(issue1)
|
|
|
|
|
|
|
|
expect(issue.relative_position).to be < issue1.relative_position
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#move_after' do
|
|
|
|
it 'moves issue after' do
|
|
|
|
[issue, issue1].each(&:move_to_end)
|
2017-02-27 15:45:55 +00:00
|
|
|
|
2017-03-07 17:57:24 +00:00
|
|
|
issue.move_after(issue1)
|
2017-02-27 15:45:55 +00:00
|
|
|
|
2017-03-07 17:57:24 +00:00
|
|
|
expect(issue.relative_position).to be > issue1.relative_position
|
2017-02-27 15:45:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#move_to_end' do
|
|
|
|
it 'moves issue to the end' do
|
|
|
|
new_issue.move_to_end
|
|
|
|
|
|
|
|
expect(new_issue.relative_position).to be > issue1.relative_position
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-10 17:04:37 +00:00
|
|
|
describe '#shift_after?' do
|
|
|
|
it 'returns true' do
|
|
|
|
issue.update(relative_position: issue1.relative_position - 1)
|
|
|
|
|
|
|
|
expect(issue.shift_after?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
issue.update(relative_position: issue1.relative_position - 2)
|
|
|
|
|
|
|
|
expect(issue.shift_after?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#shift_before?' do
|
|
|
|
it 'returns true' do
|
|
|
|
issue.update(relative_position: issue1.relative_position + 1)
|
|
|
|
|
|
|
|
expect(issue.shift_before?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
issue.update(relative_position: issue1.relative_position + 2)
|
|
|
|
|
|
|
|
expect(issue.shift_before?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-27 15:45:55 +00:00
|
|
|
describe '#move_between' do
|
|
|
|
it 'positions issue between two other' do
|
|
|
|
new_issue.move_between(issue, issue1)
|
|
|
|
|
|
|
|
expect(new_issue.relative_position).to be > issue.relative_position
|
|
|
|
expect(new_issue.relative_position).to be < issue1.relative_position
|
|
|
|
end
|
|
|
|
|
2017-03-06 16:08:18 +00:00
|
|
|
it 'positions issue between on top' do
|
|
|
|
new_issue.move_between(nil, issue)
|
2017-02-27 15:45:55 +00:00
|
|
|
|
2017-03-06 16:08:18 +00:00
|
|
|
expect(new_issue.relative_position).to be < issue.relative_position
|
2017-02-27 15:45:55 +00:00
|
|
|
end
|
|
|
|
|
2017-03-06 16:08:18 +00:00
|
|
|
it 'positions issue between to end' do
|
|
|
|
new_issue.move_between(issue1, nil)
|
2017-02-27 15:45:55 +00:00
|
|
|
|
2017-03-06 16:08:18 +00:00
|
|
|
expect(new_issue.relative_position).to be > issue1.relative_position
|
2017-02-27 15:45:55 +00:00
|
|
|
end
|
|
|
|
|
2017-03-06 16:08:18 +00:00
|
|
|
it 'positions issues even when after and before positions are the same' do
|
|
|
|
issue1.update relative_position: issue.relative_position
|
2017-02-27 15:45:55 +00:00
|
|
|
|
2017-03-06 16:08:18 +00:00
|
|
|
new_issue.move_between(issue, issue1)
|
2017-02-27 15:45:55 +00:00
|
|
|
|
2017-03-06 16:08:18 +00:00
|
|
|
expect(new_issue.relative_position).to be > issue.relative_position
|
|
|
|
expect(issue.relative_position).to be < issue1.relative_position
|
2017-02-27 15:45:55 +00:00
|
|
|
end
|
2017-03-09 14:51:20 +00:00
|
|
|
|
|
|
|
it 'positions issues between other two if distance is 1' do
|
|
|
|
issue1.update relative_position: issue.relative_position + 1
|
|
|
|
|
|
|
|
new_issue.move_between(issue, issue1)
|
|
|
|
|
|
|
|
expect(new_issue.relative_position).to be > issue.relative_position
|
|
|
|
expect(issue.relative_position).to be < issue1.relative_position
|
|
|
|
end
|
|
|
|
|
2017-03-10 15:12:31 +00:00
|
|
|
it 'positions issue in the middle of other two if distance is big enough' do
|
|
|
|
issue.update relative_position: 6000
|
|
|
|
issue1.update relative_position: 10000
|
2017-03-09 14:51:20 +00:00
|
|
|
|
|
|
|
new_issue.move_between(issue, issue1)
|
|
|
|
|
2017-03-10 15:12:31 +00:00
|
|
|
expect(new_issue.relative_position).to eq(8000)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'positions issue closer to the middle if we are at the very top' do
|
|
|
|
issue1.update relative_position: 6000
|
|
|
|
|
|
|
|
new_issue.move_between(nil, issue1)
|
|
|
|
|
2017-03-10 17:04:37 +00:00
|
|
|
expect(new_issue.relative_position).to eq(6000 - RelativePositioning::IDEAL_DISTANCE)
|
2017-03-10 15:12:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'positions issue closer to the middle if we are at the very bottom' do
|
|
|
|
issue.update relative_position: 6000
|
|
|
|
issue1.update relative_position: nil
|
|
|
|
|
|
|
|
new_issue.move_between(issue, nil)
|
|
|
|
|
2017-03-10 17:04:37 +00:00
|
|
|
expect(new_issue.relative_position).to eq(6000 + RelativePositioning::IDEAL_DISTANCE)
|
2017-03-09 14:51:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'positions issue in the middle of other two if distance is not big enough' do
|
|
|
|
issue.update relative_position: 100
|
|
|
|
issue1.update relative_position: 400
|
|
|
|
|
|
|
|
new_issue.move_between(issue, issue1)
|
|
|
|
|
|
|
|
expect(new_issue.relative_position).to eq(250)
|
|
|
|
end
|
2017-03-10 17:04:37 +00:00
|
|
|
|
|
|
|
it 'positions issue in the middle of other two is there is no place' do
|
|
|
|
issue.update relative_position: 100
|
|
|
|
issue1.update relative_position: 101
|
|
|
|
|
|
|
|
new_issue.move_between(issue, issue1)
|
|
|
|
|
|
|
|
expect(new_issue.relative_position).to be_between(issue.relative_position, issue1.relative_position)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses rebalancing if there is no place' do
|
|
|
|
issue.update relative_position: 100
|
|
|
|
issue1.update relative_position: 101
|
|
|
|
issue2 = create(:issue, relative_position: 102, project: project)
|
|
|
|
new_issue.update relative_position: 103
|
|
|
|
|
|
|
|
new_issue.move_between(issue1, issue2)
|
|
|
|
new_issue.save!
|
|
|
|
|
|
|
|
expect(new_issue.relative_position).to be_between(issue1.relative_position, issue2.relative_position)
|
|
|
|
expect(issue.reload.relative_position).not_to eq(100)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'positions issue right if we pass none-sequential parameters' do
|
|
|
|
issue.update relative_position: 99
|
|
|
|
issue1.update relative_position: 101
|
|
|
|
issue2 = create(:issue, relative_position: 102, project: project)
|
|
|
|
new_issue.update relative_position: 103
|
|
|
|
|
|
|
|
new_issue.move_between(issue, issue2)
|
|
|
|
new_issue.save!
|
|
|
|
|
|
|
|
expect(new_issue.relative_position).to be(100)
|
|
|
|
end
|
2017-02-27 15:45:55 +00:00
|
|
|
end
|
|
|
|
end
|