skill-assessment-app/test/validators/answer_format_validator_test.rb
Mark Moser fc784ffcb1 completes #17 - live-coder plus text input type
Squashed commit of the following:

commit d41cbf66eb2a9ee705ab60bb156eed95881fa193
Author: Mark Moser <mark.moser@perficient.com>
Date:   Thu Aug 4 19:58:20 2016 -0500

    live-coder-text validations

commit 866bfeb863516a8656bc26b10f967d0b9b8d8505
Author: Mark Moser <mark.moser@perficient.com>
Date:   Thu Aug 4 11:57:57 2016 -0500

    getting things green again and rebasing develop

commit 28a23200f291e30c690b71e9bcae3e1a69eb3093
Author: Derek Montgomery <montgomerygraphics@gmail.com>
Date:   Thu Aug 4 10:14:23 2016 -0500

    Progress on live coder text field
2016-08-04 20:01:17 -05:00

207 lines
5.3 KiB
Ruby

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
# input_type LIVE CODER TEXT
test "live_code_text should PASS with populated hash" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = { html: 'this is html', css: '', js: '', text: 'some reasons' }
assert obj.valid?
assert obj.errors.messages.empty?
end
test "live_code_text should PASS with finish later" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = { later: "true" }
assert obj.valid?
assert obj.errors.messages.empty?
end
test "live_code_text should FAIL with nil" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = nil
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
test "live_code_text should FAIL without checking finish later" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = { later: "" }
refute obj.valid?
assert_match(/come back/, obj.errors.messages[:answer][0])
end
test "live_code_text should FAIL without values" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = { later: "", html: "", css: "", js: "", text: "" }
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
test "live_code_text should FAIL without text" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = { later: "", html: "some html", css: "", js: "", text: "" }
refute obj.valid?
assert_match(/provide.*reason/, obj.errors.messages[:answer][0])
end
end