From dfa65b4984ae5c311e07b5f407d6abbb6f0b4cc2 Mon Sep 17 00:00:00 2001 From: Kyle MacPherson Date: Wed, 22 Jan 2025 10:34:51 +0000 Subject: [PATCH] Add validation for grade on other qualifications if the jobseeker has finished studying --- .../jobseekers/qualifications/other_form.rb | 1 + .../jobseekers/qualifications/other_form_spec.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/form_models/jobseekers/qualifications/other_form.rb b/app/form_models/jobseekers/qualifications/other_form.rb index 90f3fe741f..950580d206 100644 --- a/app/form_models/jobseekers/qualifications/other_form.rb +++ b/app/form_models/jobseekers/qualifications/other_form.rb @@ -2,4 +2,5 @@ class Jobseekers::Qualifications::OtherForm < Jobseekers::Qualifications::Qualif attr_accessor :subject, :grade validates :finished_studying, :institution, :name, presence: true + validates :grade, presence: true, if: -> { finished_studying == "true" } end diff --git a/spec/form_models/jobseekers/qualifications/other_form_spec.rb b/spec/form_models/jobseekers/qualifications/other_form_spec.rb index f5fedf248f..5423d9113c 100644 --- a/spec/form_models/jobseekers/qualifications/other_form_spec.rb +++ b/spec/form_models/jobseekers/qualifications/other_form_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.describe Jobseekers::Qualifications::OtherForm, type: :model do - subject { described_class.new(params) } + subject(:form) { described_class.new(params) } let(:params) { {} } it { is_expected.to validate_presence_of(:category) } @@ -13,11 +13,19 @@ let(:params) { { "finished_studying" => "false" } } it { is_expected.to validate_presence_of(:finished_studying_details) } + it { is_expected.not_to validate_presence_of(:grade) } end context "when finished studying is true" do let(:params) { { "finished_studying" => "true" } } it { is_expected.to validate_numericality_of(:year).is_less_than_or_equal_to(Time.current.year) } + it { is_expected.to validate_presence_of(:grade) } + + it "raises error without a grade" do + form.grade = nil + expect(form).not_to be_valid + expect(form.errors[:grade]).to include("Enter a grade") + end end end