2019-03-09 00:01:59 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe StepsController, type: :controller do
|
|
|
|
login_user
|
|
|
|
|
2021-09-16 17:52:18 +08:00
|
|
|
include_context 'reference_project_structure', {
|
|
|
|
step: true
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:24:44 +08:00
|
|
|
let(:protocol_repo) do
|
2023-10-12 22:16:43 +08:00
|
|
|
create :protocol, :in_repository_draft, team: team, added_by: user
|
2019-03-09 00:01:59 +08:00
|
|
|
end
|
2019-03-27 18:24:44 +08:00
|
|
|
let(:step_repo) { create :step, protocol: protocol_repo }
|
2019-03-09 00:01:59 +08:00
|
|
|
|
|
|
|
describe 'POST create' do
|
2019-03-27 18:24:44 +08:00
|
|
|
let(:action) { post :create, params: params, format: :json }
|
|
|
|
|
2019-03-09 00:01:59 +08:00
|
|
|
context 'when in protocol repository' do
|
|
|
|
let(:params) do
|
2019-03-27 18:24:44 +08:00
|
|
|
{ protocol_id: protocol_repo.id,
|
2023-03-07 03:17:45 +08:00
|
|
|
step: { name: 'test', description: 'description' }, position: 1 }
|
2019-03-09 00:01:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity for creating step in protocol repository' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type:
|
|
|
|
:add_step_to_protocol_repository)))
|
2019-03-27 18:24:44 +08:00
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in protocol on task' do
|
|
|
|
let(:params) do
|
2021-09-16 17:52:18 +08:00
|
|
|
{ protocol_id: my_module.protocol.id,
|
2023-03-07 03:17:45 +08:00
|
|
|
step: { name: 'test', description: 'description' }, position: 1 }
|
2019-03-27 18:24:44 +08:00
|
|
|
end
|
2019-03-09 00:01:59 +08:00
|
|
|
|
2019-03-27 18:24:44 +08:00
|
|
|
it 'calls create activity for creating step in protocol on task' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type: :create_step)))
|
2019-03-09 00:01:59 +08:00
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT update' do
|
2019-03-27 18:24:44 +08:00
|
|
|
let(:action) { put :update, params: params, format: :json }
|
|
|
|
|
2019-03-09 00:01:59 +08:00
|
|
|
context 'when in protocol repository' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
2019-03-27 18:24:44 +08:00
|
|
|
id: step_repo.id,
|
|
|
|
protocol_id: protocol_repo.id,
|
2019-03-09 00:01:59 +08:00
|
|
|
step: {
|
|
|
|
name: 'updated name',
|
|
|
|
description: 'updated description'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity for editing step in protocol repository' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type:
|
|
|
|
:edit_step_in_protocol_repository)))
|
2019-03-27 18:24:44 +08:00
|
|
|
action
|
|
|
|
end
|
2019-03-09 00:01:59 +08:00
|
|
|
|
2019-03-27 18:24:44 +08:00
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in protocol on task' do
|
|
|
|
let(:params) do
|
|
|
|
{ id: step.id,
|
|
|
|
step: { name: 'updated name', description: 'updated description' } }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity for editing step in protocol on task' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type: :edit_step)))
|
2019-03-09 00:01:59 +08:00
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE destroy' do
|
2019-03-27 18:24:44 +08:00
|
|
|
let(:action) { delete :destroy, params: params, format: :json }
|
|
|
|
|
2019-03-09 00:01:59 +08:00
|
|
|
context 'when in protocol repository' do
|
2019-03-27 18:24:44 +08:00
|
|
|
let(:params) { { id: step_repo.id } }
|
2019-03-09 00:01:59 +08:00
|
|
|
|
|
|
|
it 'calls create activity for deleting step in protocol repository' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type:
|
|
|
|
:delete_step_in_protocol_repository)))
|
2019-03-27 18:24:44 +08:00
|
|
|
action
|
|
|
|
end
|
2019-03-09 00:01:59 +08:00
|
|
|
|
2019-03-27 18:24:44 +08:00
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in protocol on task' do
|
|
|
|
let(:params) { { id: step.id } }
|
|
|
|
|
|
|
|
it 'calls create activity for deleting step in protocol on task' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type: :destroy_step)))
|
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST toggle_step_state' do
|
|
|
|
let(:action) { post :toggle_step_state, params: params, format: :json }
|
|
|
|
|
|
|
|
context 'when completing step' do
|
|
|
|
let(:step) do
|
2021-09-16 17:52:18 +08:00
|
|
|
create :step, protocol: my_module.protocol, user: user, completed: false
|
2019-03-27 18:24:44 +08:00
|
|
|
end
|
|
|
|
let(:params) { { id: step.id, completed: true } }
|
|
|
|
|
|
|
|
it 'calls create activity for completing step' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type: :complete_step)))
|
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when uncompleting step' do
|
|
|
|
let(:params) { { id: step.id, completed: false } }
|
|
|
|
|
|
|
|
it 'calls create activity for uncompleting step' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type: :uncomplete_step)))
|
2019-03-09 00:01:59 +08:00
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|