From a50dc06dfc8ed4f1c118852bd985ecb1050f5d29 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 6 Jan 2022 17:38:34 +0000 Subject: [PATCH] Add some unit tests for this --- app/controllers/application_controller.rb | 2 - config/initializers/default_form_builder.rb | 1 + spec/views/form/page_view_spec.rb | 74 +++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 config/initializers/default_form_builder.rb create mode 100644 spec/views/form/page_view_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 21c76efd1..776259fab 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,4 @@ class ApplicationController < ActionController::Base - default_form_builder GOVUKDesignSystemFormBuilder::FormBuilder - def render_not_found render "errors/not_found", status: :not_found end diff --git a/config/initializers/default_form_builder.rb b/config/initializers/default_form_builder.rb new file mode 100644 index 000000000..85458560a --- /dev/null +++ b/config/initializers/default_form_builder.rb @@ -0,0 +1 @@ +Rails.application.config.action_view.default_form_builder = GOVUKDesignSystemFormBuilder::FormBuilder diff --git a/spec/views/form/page_view_spec.rb b/spec/views/form/page_view_spec.rb new file mode 100644 index 000000000..c8c63d9fd --- /dev/null +++ b/spec/views/form/page_view_spec.rb @@ -0,0 +1,74 @@ +require "rails_helper" +require_relative "../../request_helper" + +RSpec.describe "form/page" do + + before do + RequestHelper.stub_http_requests + end + + let(:case_log) { FactoryBot.create(:case_log, :in_progress) } + let(:form) { case_log.form } + let(:subsection) { form.get_subsection("income_and_benefits") } + let(:page) { form.get_page("net_income") } + let(:question) { page.questions.find { |q| q.id == "earnings" } } + + def assign_attributes(object, attrs) + attrs.each_pair do |attr, value| + object.public_send("#{attr}=", value) + end + end + + context "given a question with extra guidance" do + let(:expected_guidance) { /What counts as income?/ } + before do + assign(:case_log, case_log) + assign(:page, page) + assign(:subsection, subsection) + assign_attributes(question, attribs) + render + end + + context "with radio type" do + let(:attribs) { { type: "radio", answer_options: {"1": "A", "2": "B"} } } + it "renders the guidance partial for radio questions" do + expect(rendered).to match(expected_guidance) + end + end + + context "with text type" do + let(:attribs) { { type: "text", answer_options: nil } } + it "renders the guidance partial for text questions" do + expect(rendered).to match(expected_guidance) + end + end + + context "with numeric type" do + let(:attribs) { { type: "numeric", answer_options: nil } } + it "renders the guidance partial for numeric questions" do + expect(rendered).to match(expected_guidance) + end + end + + context "with select type" do + let(:attribs) { { type: "select", answer_options: {"1": "A", "2": "B"} } } + it "renders the guidance partial for select questions" do + expect(rendered).to match(expected_guidance) + end + end + + context "with checkbox type" do + let(:attribs) { { type: "checkbox", answer_options: {"1": "A", "2": "B"} } } + it "renders the guidance partial for checkbox questions" do + expect(rendered).to match(expected_guidance) + end + end + + context "with date type" do + let(:attribs) { { type: "date", answer_options: nil } } + it "renders the guidance partial for date questions" do + expect(rendered).to match(expected_guidance) + end + end + end +end