require 'test_helper' class AnswerValidatable include ActiveModel::Validations attr_accessor :answer attr_accessor :question validates :answer, answer_format: true MockQuestion = Struct.new(:input_type) def initialize input_type @input_type = input_type end def question MockQuestion.new(@input_type) end end class AnswerFormatValidatorTest < ActiveSupport::TestCase # input_type RADIO test "radio should pass with string" do obj = AnswerValidatable.new('radio') obj.answer = 'option-1' assert obj.valid? assert obj.errors.messages.empty? end test "radio should FAIL with nil" do obj = AnswerValidatable.new('radio') obj.answer = nil refute obj.valid? assert_match(/select.*answer/, obj.errors.messages[:answer][0]) end test "radio should FAIL with empty string" do obj = AnswerValidatable.new('radio') obj.answer = '' refute obj.valid? assert_match(/select.*answer/, obj.errors.messages[:answer][0]) end # input_type TEXT test "text should PASS with string" do obj = AnswerValidatable.new('text') obj.answer = "this is a valid answer" assert obj.valid? assert obj.errors.messages.empty? end test "text should FAIL with nil" do obj = AnswerValidatable.new('text') obj.answer = nil refute obj.valid? assert_match(/enter.*answer/, obj.errors.messages[:answer][0]) end test "text should FAIL with empry string" do obj = AnswerValidatable.new('text') obj.answer = " " refute obj.valid? assert_match(/enter.*answer/, obj.errors.messages[:answer][0]) end test "text should FAIL with more than 1000 charactures" do obj = AnswerValidatable.new('text') obj.answer = SecureRandom.urlsafe_base64(1001) refute obj.valid? assert_match(/char.*limit.*1000$/, obj.errors.messages[:answer][0]) end # input_type CHECK BOX test "checkbox should PASS with populated array" do obj = AnswerValidatable.new('checkbox') obj.answer = ["", "", "valid answer"] assert obj.valid? assert obj.errors.messages.empty? end test "checkbox should FAIL with nil" do obj = AnswerValidatable.new('checkbox') obj.answer = nil refute obj.valid? assert_match(/select.*answer/, obj.errors.messages[:answer][0]) end test "checkbox should FAIL with empty string" do obj = AnswerValidatable.new('checkbox') obj.answer = " " refute obj.valid? assert_match(/select.*answer/, obj.errors.messages[:answer][0]) end test "checkbox should FAIL with array of empty strings" do obj = AnswerValidatable.new('checkbox') obj.answer = ["", "", " "] refute obj.valid? assert_match(/select.*answer/, obj.errors.messages[:answer][0]) end # input_type LIVE CODER test "live_code should PASS with populated hash" do obj = AnswerValidatable.new('live_code') obj.answer = { html: 'this is html', css: '', js: '' } assert obj.valid? assert obj.errors.messages.empty? end test "live_code should PASS with finish later" do obj = AnswerValidatable.new('live_code') obj.answer = { later: "true" } assert obj.valid? assert obj.errors.messages.empty? end test "live_code should FAIL with nil" do obj = AnswerValidatable.new('live_code') obj.answer = nil refute obj.valid? assert_match(/write.*code/, obj.errors.messages[:answer][0]) end test "live_code should FAIL without checking finish later" do obj = AnswerValidatable.new('live_code') obj.answer = { "later" => "" } refute obj.valid? assert_match(/come back/, obj.errors.messages[:answer][0]) end test "live_code should FAIL without values" do obj = AnswerValidatable.new('live_code') obj.answer = { "later" => "", "html" => "", "css" => "", "js" => "" } refute obj.valid? assert_match(/write.*code/, obj.errors.messages[:answer][0]) end end