Skip to content

Commit

Permalink
update specs for background workflow action job
Browse files Browse the repository at this point in the history
  • Loading branch information
dunn committed Nov 13, 2023
1 parent 7e922e6 commit d4397b0
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 70 deletions.
84 changes: 14 additions & 70 deletions spec/forms/hyrax/forms/workflow_action_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand All @@ -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
Expand Down
77 changes: 77 additions & 0 deletions spec/services/hyrax/workflow/workflow_action_service_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d4397b0

Please sign in to comment.