Browse Source
Co-authored-by: baarkerlounger <baarkerlounger@users.noreply.github.com>pull/674/head
16 changed files with 245 additions and 10 deletions
@ -0,0 +1,24 @@
|
||||
class Form::Setup::Pages::Organisation < ::Form::Page |
||||
def initialize(id, hsh, subsection) |
||||
super |
||||
@id = "organisation" |
||||
@header = "" |
||||
@description = "" |
||||
@questions = questions |
||||
@subsection = subsection |
||||
end |
||||
|
||||
def questions |
||||
[ |
||||
Form::Setup::Questions::OwningOrganisationId.new(nil, nil, self), |
||||
] |
||||
end |
||||
|
||||
def routed_to?(_case_log) |
||||
!!form.current_user&.support? |
||||
end |
||||
|
||||
def invalidated?(case_log) |
||||
false |
||||
end |
||||
end |
@ -0,0 +1,30 @@
|
||||
class Form::Setup::Questions::OwningOrganisationId < ::Form::Question |
||||
def initialize(id, hsh, page) |
||||
super |
||||
@id = "owning_organisation_id" |
||||
@check_answer_label = "Owning organisation" |
||||
@header = "Which organisation is the owning organisation for this log?" |
||||
@hint_text = "" |
||||
@type = "select" |
||||
@page = page |
||||
@answer_options = answer_options_values |
||||
end |
||||
|
||||
def answer_options_values |
||||
hsh = { "" => "Select an option" } |
||||
Organisation.all.each_with_object(hsh) do |organisation, hsh| |
||||
hsh[organisation.id] = organisation.name |
||||
hsh |
||||
end |
||||
end |
||||
|
||||
def label_from_value(value) |
||||
return unless value |
||||
|
||||
answer_options[value] |
||||
end |
||||
|
||||
def hidden_in_check_answers |
||||
!form.current_user.support? |
||||
end |
||||
end |
@ -0,0 +1,65 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Form::Setup::Pages::Organisation, type: :model do |
||||
subject(:page) { described_class.new(page_id, page_definition, subsection) } |
||||
|
||||
let(:page_id) { nil } |
||||
let(:page_definition) { nil } |
||||
let(:subsection) { instance_double(Form::Subsection) } |
||||
let(:form) { instance_double(Form) } |
||||
let(:case_log) { instance_double(CaseLog) } |
||||
|
||||
it "has correct subsection" do |
||||
expect(page.subsection).to eq(subsection) |
||||
end |
||||
|
||||
it "has correct questions" do |
||||
expect(page.questions.map(&:id)).to eq(%w[owning_organisation_id]) |
||||
end |
||||
|
||||
it "has the correct id" do |
||||
expect(page.id).to eq("organisation") |
||||
end |
||||
|
||||
it "has the correct header" do |
||||
expect(page.header).to eq("") |
||||
end |
||||
|
||||
it "has the correct description" do |
||||
expect(page.description).to eq("") |
||||
end |
||||
|
||||
it "has the correct depends_on" do |
||||
expect(page.depends_on).to be nil |
||||
end |
||||
|
||||
it "has the correct derived" do |
||||
expect(page.derived).to be nil |
||||
end |
||||
|
||||
context "when the current user is a support user" do |
||||
let(:support_user) { FactoryBot.build(:user, :support) } |
||||
|
||||
before do |
||||
allow(subsection).to receive(:form).and_return(form) |
||||
allow(form).to receive(:current_user).and_return(support_user) |
||||
end |
||||
|
||||
it "is shown" do |
||||
expect(page.routed_to?(case_log)).to be true |
||||
end |
||||
end |
||||
|
||||
context "when the current user is not a support user" do |
||||
let(:user) { FactoryBot.build(:user) } |
||||
|
||||
before do |
||||
allow(subsection).to receive(:form).and_return(form) |
||||
allow(form).to receive(:current_user).and_return(user) |
||||
end |
||||
|
||||
it "is not shown" do |
||||
expect(page.routed_to?(case_log)).to be false |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,76 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Form::Setup::Questions::OwningOrganisationId, type: :model do |
||||
subject(:question) { described_class.new(question_id, question_definition, page) } |
||||
|
||||
let(:question_id) { nil } |
||||
let(:question_definition) { nil } |
||||
let(:page) { instance_double(Form::Page) } |
||||
let(:subsection) { instance_double(Form::Subsection) } |
||||
let(:form) { instance_double(Form) } |
||||
let!(:organisation_1) { FactoryBot.create(:organisation, name: "first test org") } |
||||
let!(:organisation_2) { FactoryBot.create(:organisation, name: "second test org") } |
||||
let(:expected_answer_options) do |
||||
{ |
||||
"" => "Select an option", |
||||
organisation_1.id => organisation_1.name, |
||||
organisation_2.id => organisation_2.name, |
||||
} |
||||
end |
||||
|
||||
it "has correct page" do |
||||
expect(question.page).to eq(page) |
||||
end |
||||
|
||||
it "has the correct id" do |
||||
expect(question.id).to eq("owning_organisation_id") |
||||
end |
||||
|
||||
it "has the correct header" do |
||||
expect(question.header).to eq("Which organisation is the owning organisation for this log?") |
||||
end |
||||
|
||||
it "has the correct check_answer_label" do |
||||
expect(question.check_answer_label).to eq("Owning organisation") |
||||
end |
||||
|
||||
it "has the correct type" do |
||||
expect(question.type).to eq("select") |
||||
end |
||||
|
||||
it "has the correct hint_text" do |
||||
expect(question.hint_text).to eq("") |
||||
end |
||||
|
||||
it "has the correct answer options" do |
||||
expect(question.answer_options).to eq(expected_answer_options) |
||||
end |
||||
|
||||
context "when the current user is support" do |
||||
let(:support_user) { FactoryBot.build(:user, :support) } |
||||
|
||||
before do |
||||
allow(page).to receive(:subsection).and_return(subsection) |
||||
allow(subsection).to receive(:form).and_return(form) |
||||
allow(form).to receive(:current_user).and_return(support_user) |
||||
end |
||||
|
||||
it "is shown in check answers" do |
||||
expect(question.hidden_in_check_answers).to be false |
||||
end |
||||
end |
||||
|
||||
context "when the current user is not support" do |
||||
let(:user) { FactoryBot.build(:user) } |
||||
|
||||
before do |
||||
allow(page).to receive(:subsection).and_return(subsection) |
||||
allow(subsection).to receive(:form).and_return(form) |
||||
allow(form).to receive(:current_user).and_return(user) |
||||
end |
||||
|
||||
it "is not shown in check answers" do |
||||
expect(question.hidden_in_check_answers).to be true |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue