require "rails_helper"

feature "Reset workflow instances", js: true do
  let(:user_params) { {} }
  let(:user) { create :user, user_params }
  background { i_am_logged_in_as user }

  context "with permissions" do
    let(:user_params) { {role_names: ["read-class-WorkflowInstance", "reset-class-WorkflowInstance"] } }

    scenario "Reset workflow instances" do
      wi = create 'workflow_instance/scenario1', :active, bucket: 'B1'
      wi.workflow_steps.map(&:state).uniq.should match_array ['processing', 'pending']
      visit "/workflow_instance/scenario1s"
      select_all_workflow_instances
      js_click ".collection-edit.workflow_instance-actions"
      js_click ".workflow-instance-reset-button"

      #page.current_path.should eq "/workflow_instance/scenario1s"
      page.current_path.should eq "/workflow_instance/scenario1s" # redirect back
      page.should_not have_content "B1"

      wi_reset = WorkflowInstance.find(wi.id)
      wi_reset.sti_type.should eq "WorkflowInstance::Scenario1" # no scenario reset here
      wi_reset.workflow_steps.map(&:state).uniq.should eq ['pending']
    end
  end

  context "without permissions" do
    let(:user_params) { {role_names: ["read-class-WorkflowInstance", "update-class-WorkflowInstance"] } }

    scenario "business as usual, but should not have reset action" do
      create 'workflow_instance/scenario1', bucket: 'B1'
      create 'workflow_instance/scenario1', bucket: 'B2'
      visit "/workflow_instance/scenario1s"
      select_all_workflow_instances
      js_click ".collection-edit.workflow_instance-actions"
      page.should_not have_selector ".workflow-instance-reset-button"
    end
  end
end
