diff --git a/spec/factories/my_modules.rb b/spec/factories/my_modules.rb
index dca7990f4..d002ed7db 100644
--- a/spec/factories/my_modules.rb
+++ b/spec/factories/my_modules.rb
@@ -3,8 +3,8 @@
FactoryBot.define do
factory :my_module do
sequence(:name) { |n| "Task-#{n}" }
- x { Faker::Number.between(from: 1, to: 100) }
sequence(:y) { |n| n }
+ sequence(:x) { |n| n }
workflow_order { MyModule.where(experiment_id: experiment.id).count + 1 }
experiment
my_module_group { create :my_module_group, experiment: experiment }
diff --git a/spec/models/experiment_spec.rb b/spec/models/experiment_spec.rb
index 4552dcac6..c68ca534d 100644
--- a/spec/models/experiment_spec.rb
+++ b/spec/models/experiment_spec.rb
@@ -92,12 +92,8 @@ describe Experiment, type: :model do
let(:user) { experiment.created_by }
context 'when creating tasks' do
- let(:to_add) do
- [{ id: 'n0', name: 'new task name', x: 50, y: 50 }]
- end
- let(:function_call) do
- experiment.update_canvas([], to_add, [], [], [], [], [], [], user)
- end
+ let(:to_add) { [{ id: 'n0', name: 'new task name', x: 50, y: 50 }] }
+ let(:function_call) { experiment.update_canvas([], to_add, [], {}, [], [], [], {}, user) }
it 'calls create activity for creating tasks' do
expect(Activities::CreateActivityService)
@@ -112,6 +108,57 @@ describe Experiment, type: :model do
it 'adds activity in DB' do
expect { function_call }.to(change { Activity.all.count })
end
+
+ context 'when moving existing one and creating new one on the same position' do
+ let(:first_task) { experiment.my_modules.first }
+ let(:to_add) { [{ id: 'n0', name: 'new task name', x: 0, y: 0 }] }
+ let(:function_call) { experiment.update_canvas([], to_add, [], {}, [], [], [], positions, user) }
+ let(:positions) do
+ Hash[first_task.id.to_s,
+ { x: first_task.x + 1, y: first_task.y + 1 }, 'n0', { x: first_task.x, y: first_task.y }]
+ end
+
+ before do
+ first_task.update(x: 0, y: 0)
+ end
+
+ it 'returns true' do
+ expect(function_call).to be_truthy
+ end
+ end
+
+ context 'when creating new one on position of "toBeArchived" task' do
+ let(:first_task) { experiment.my_modules.first }
+ let(:to_add) { [{ id: 'n0', name: 'new task name', x: 0, y: 0 }] }
+ let(:positions) { Hash['n0', { x: first_task.x, y: first_task.y }] }
+ let(:to_archive) { [first_task.id] }
+ let(:function_call) { experiment.update_canvas(to_archive, to_add, [], {}, [], [], [], positions, user) }
+
+ before do
+ first_task.update(x: 0, y: 0)
+ end
+
+ it 'returns true' do
+ expect(function_call).to be_truthy
+ end
+ end
+
+ context 'when creating new one on position of "toBeMoved" task' do
+ let(:first_task) { experiment.my_modules.first }
+ let(:to_add) { [{ id: 'n0', name: 'new task name', x: 0, y: 0 }] }
+ let(:positions) { Hash['n0', { x: first_task.x, y: first_task.y }] }
+ let(:to_move) { Hash[first_task.id, second_experiment.id] }
+ let(:second_experiment) { create :experiment, project: experiment.project }
+ let(:function_call) { experiment.update_canvas([], to_add, [], to_move, [], [], [], positions, user) }
+
+ before do
+ first_task.update(x: 0, y: 0)
+ end
+
+ it 'returns true' do
+ expect(function_call).to be_truthy
+ end
+ end
end
context 'when cloning tasks' do
@@ -131,7 +178,7 @@ describe Experiment, type: :model do
.map.with_index { |t, i| { 'n' + i.to_s => t.id } }.reduce({}, :merge)
end
let(:function_call) do
- experiment.update_canvas([], to_add, [], [], [], to_clone, [], [], user)
+ experiment.update_canvas([], to_add, [], {}, [], to_clone, [], {}, user)
end
it 'calls create activity for cloning tasks' do
@@ -159,7 +206,7 @@ describe Experiment, type: :model do
end
let(:function_call) do
- experiment.update_canvas([], [], to_rename, [], [], [], [], [], user)
+ experiment.update_canvas([], [], to_rename, {}, [], [], [], {}, user)
end
it 'calls create activity for renaming my_moudles' do
@@ -180,7 +227,7 @@ describe Experiment, type: :model do
let(:to_archive) { experiment.my_modules.pluck(:id) }
let(:function_call) do
- experiment.update_canvas(to_archive, [], [], [], [], [], [], [], user)
+ experiment.update_canvas(to_archive, [], [], {}, [], [], [], {}, user)
end
it 'calls create activity for archiving tasks' do
@@ -205,7 +252,7 @@ describe Experiment, type: :model do
.map { |t| { t.id => new_experiment.id } }.reduce({}, :merge)
end
let(:function_call) do
- experiment.update_canvas([], [], [], to_move, [], [], [], [], user)
+ experiment.update_canvas([], [], [], to_move, [], [], [], {}, user)
end
it 'calls create activity for moving tasks to another experiment' do
@@ -221,5 +268,21 @@ describe Experiment, type: :model do
expect { function_call }.to change { Activity.all.count }.by(3)
end
end
+
+ context 'when moving new task (not saved to DB yet)' do
+ let(:first_task) { experiment.my_modules.first }
+ let(:second_experiment) { create :experiment, project: experiment.project }
+ let(:to_add) { [{ id: 'n0', name: 'new task name', x: 0, y: 0 }] }
+ let(:to_move) { Hash['n0', second_experiment.id] }
+ let(:function_call) { experiment.update_canvas([], to_add, [], to_move, [], [], [], {}, user) }
+
+ it 'returns true' do
+ expect(function_call).to be_truthy
+ end
+
+ it 'assigns task to new experiment' do
+ expect { function_call }.to change { second_experiment.my_modules.count }.by(1)
+ end
+ end
end
end