require "rails_helper"

feature "Archive and revive 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", "archive-class-WorkflowInstance"] } }

    scenario "Archive workflow instances" 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-revive-button"
      js_click ".workflow-instance-archive-button"

      #page.current_path.should eq "/workflow_instance/scenario1s"
      page.should have_selector "body.archived-mode"
      page.current_path.should eq "/"

      WorkflowInstance.archived.count.should eq 2
      WorkflowInstance.scope_for(user).count.should eq 0

      visit "/workflow_instance/scenario1s"
      page.should have_content "B1"
      page.should have_content "B2"
      select_all_workflow_instances
      js_click ".collection-edit.workflow_instance-actions"
      page.should_not have_selector ".workflow-instance-archive-button"
      js_click ".workflow-instance-revive-button"

      page.current_path.should eq "/workflow_instance/scenario1s"
      page.should_not have_selector "body.archived-mode"

      WorkflowInstance.archived.count.should eq 0
      WorkflowInstance.scope_for(user).count.should eq 2
    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 archive 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-archive-button"
    end
  end
end
