2019-01-21 23:08:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Experiments::CopyExperimentAsTemplateService do
|
2022-06-07 00:21:57 +08:00
|
|
|
let(:user) { create :user }
|
|
|
|
let(:team) { create :team, created_by: user }
|
2019-01-21 23:08:42 +08:00
|
|
|
|
|
|
|
let(:project) do
|
2022-06-07 00:21:57 +08:00
|
|
|
create :project, team: team, created_by: user
|
2019-01-21 23:08:42 +08:00
|
|
|
end
|
|
|
|
let(:new_project) do
|
2022-06-07 00:21:57 +08:00
|
|
|
create :project, team: team, created_by: user
|
2019-01-21 23:08:42 +08:00
|
|
|
end
|
|
|
|
let(:experiment) do
|
2019-03-13 16:06:48 +08:00
|
|
|
create :experiment, :with_tasks, name: 'MyExp', project: project
|
2019-01-21 23:08:42 +08:00
|
|
|
end
|
2022-06-07 00:21:57 +08:00
|
|
|
|
2019-01-21 23:08:42 +08:00
|
|
|
let(:service_call) do
|
2021-10-28 00:17:38 +08:00
|
|
|
Experiments::CopyExperimentAsTemplateService.call(experiment: experiment, project: new_project, user: user)
|
2019-01-21 23:08:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when service call is successful' do
|
|
|
|
it 'adds new experiment to target project' do
|
|
|
|
expect { service_call }.to(change { new_project.experiments.count })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds Activity record' do
|
|
|
|
expect { service_call }.to(change { Activity.all.count })
|
|
|
|
end
|
|
|
|
|
2019-03-05 18:39:57 +08:00
|
|
|
it 'calls create activity service' do
|
|
|
|
expect(Activities::CreateActivityService).to receive(:call)
|
|
|
|
.with(hash_including(activity_type: :clone_experiment))
|
|
|
|
|
|
|
|
service_call
|
|
|
|
end
|
|
|
|
|
2019-01-21 23:08:42 +08:00
|
|
|
it 'copies all tasks to new experiment' do
|
|
|
|
expect(service_call.cloned_experiment.my_modules.count)
|
|
|
|
.to be == experiment.my_modules.count
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'copies all task groups to new experiment' do
|
|
|
|
expect(service_call.cloned_experiment.my_module_groups.count)
|
|
|
|
.to be == experiment.my_module_groups.count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when service call is not successful' do
|
|
|
|
it 'returns an error when can\'t find experiment' do
|
|
|
|
allow(Experiment).to receive(:find).and_return(nil)
|
|
|
|
|
|
|
|
expect(service_call.errors).to have_key(:invalid_arguments)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error when user don\'t have permissions' do
|
|
|
|
expect_any_instance_of(Experiment)
|
|
|
|
.to receive(:projects_with_role_above_user).and_return([])
|
|
|
|
|
|
|
|
expect(service_call.errors).not_to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|