diff --git a/spec/forms/hyrax/forms/workflow_action_form_spec.rb b/spec/forms/hyrax/forms/workflow_action_form_spec.rb index 3eda9f2f16..45af7854b3 100644 --- a/spec/forms/hyrax/forms/workflow_action_form_spec.rb +++ b/spec/forms/hyrax/forms/workflow_action_form_spec.rb @@ -43,30 +43,15 @@ it 'gives false for failure' do expect(form.save).to be false end - - it 'will not add a comment' do - expect { form.save }.not_to change { Sipity::Comment.count } - end - - it 'will not send the #deliver_on_action_taken message to Hyrax::Workflow::NotificationService' do - expect(Hyrax::Workflow::NotificationService) - .not_to receive(:deliver_on_action_taken) - - form.save - end - - it 'will not send the #handle_action_taken message to Hyrax::Workflow::ActionTakenService' do - expect(Hyrax::Workflow::ActionTakenService) - .not_to receive(:handle_action_taken) - - form.save - end end end context 'if the given user can perform the given action' do before do - allow(described_class).to receive(:workflow_action_for).with('an_action', scope: sipity_entity.workflow).and_return(an_action) + allow(described_class) + .to receive(:workflow_action_for) + .with('an_action', scope: sipity_entity.workflow) + .and_return(an_action) expect(Hyrax::Workflow::PermissionQuery) .to receive(:authorized_for_processing?) @@ -86,57 +71,16 @@ expect(form.save).to be true end - context 'and the action has a resulting_workflow_state_id' do - it 'will update the state of the given work and index it' do - expect(work).to receive(:update_index) - - expect { form.save } - .to change { sipity_entity.reload.workflow_state_id } - .from(2) - .to(an_action.resulting_workflow_state_id) - end - end - - context 'and the action does not have a resulting_workflow_state_id' do - let(:an_action) do - instance_double(Sipity::WorkflowAction, - resulting_workflow_state_id: nil, - notifiable_contexts: [], - triggered_methods: Sipity::Method.none) - end - - it 'will not update the state of the given work' do - expect { form.save } - .not_to change { sipity_entity.reload.workflow_state_id } - end - end - - it 'will create the given comment for the entity' do - expect { form.save } - .to change { Sipity::Comment.count } - .by(1) - end - - it 'will send the #deliver_on_action_taken message to Hyrax::Workflow::NotificationService' do - expect(Hyrax::Workflow::NotificationService) - .to receive(:deliver_on_action_taken) - .with(entity: sipity_entity, - comment: kind_of(Sipity::Comment), - action: an_action, - user: user) - - form.save - end - - it 'will send the #handle_action_taken message to Hyrax::Workflow::ActionTakenService' do - expect(Hyrax::Workflow::ActionTakenService) - .to receive(:handle_action_taken) - .with(target: work, - comment: kind_of(Sipity::Comment), - action: an_action, - user: user) - - form.save + it 'enqueues WorkflowActionJob' do + expect(WorkflowActionJob).to( + receive(:perform_later).with( + comment: 'a_comment', + name: 'an_action', + user: user, + work_id: work.id.to_s, + workflow: sipity_entity.workflow + ) + ) end end end diff --git a/spec/services/hyrax/workflow/workflow_action_service_spec.rb b/spec/services/hyrax/workflow/workflow_action_service_spec.rb new file mode 100644 index 0000000000..f2ee49960e --- /dev/null +++ b/spec/services/hyrax/workflow/workflow_action_service_spec.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true +require "spec_helper" + +RSpec.describe Hyrax::Workflow::WorkflowActionService, :clean_repo do + let(:user) { FactoryBot.create(:user) } + let(:work) { FactoryBot.valkyrie_create(:hyrax_work) } + + let(:sipity_entity) do + FactoryBot + .create(:sipity_entity, + proxy_for_global_id: work.to_global_id.to_s, + workflow_state_id: 2) + end + + let(:an_action) do + instance_double(Sipity::WorkflowAction, + resulting_workflow_state_id: 3, + notifiable_contexts: [], + triggered_methods: Sipity::Method.none) + end + + describe '.run' do + subject do + described_class.run( + subject: ::Hyrax::WorkflowActionInfo.new(work, user), + action: an_action, + comment: 'a_comment' + ) + end + + context 'the action has a resulting_workflow_state_id' do + it 'will update the state of the given work and index it' do + expect(work).to receive(:update_index) + + expect(sipity_entity.reload.workflow_state_id) + .to change + .from(2) + .to(an_action.resulting_workflow_state_id) + end + end + + context 'and the action does not have a resulting_workflow_state_id' do + let(:an_action) do + instance_double(Sipity::WorkflowAction, + resulting_workflow_state_id: nil, + notifiable_contexts: [], + triggered_methods: Sipity::Method.none) + end + + it 'will not update the state of the given work' do + expect(sipity_entity.reload.workflow_state_id).not_to change + end + end + + it 'will create the given comment for the entity' do + expect(Sipity::Comment.count).to change.by(1) + end + + it 'will send the #deliver_on_action_taken message to Hyrax::Workflow::NotificationService' do + expect(Hyrax::Workflow::NotificationService) + .to receive(:deliver_on_action_taken) + .with(entity: sipity_entity, + comment: kind_of(Sipity::Comment), + action: an_action, + user: user) + end + + it 'will send the #handle_action_taken message to Hyrax::Workflow::ActionTakenService' do + expect(Hyrax::Workflow::ActionTakenService) + .to receive(:handle_action_taken) + .with(target: work, + comment: kind_of(Sipity::Comment), + action: an_action, + user: user) + end + end +end