introduce check_other and radio_other question types
completes issue #48
This commit is contained in:
@ -0,0 +1,61 @@
|
||||
require 'test_helper'
|
||||
|
||||
# *_with_other answers expect a hash response:
|
||||
# with_other: { other: 'TEXT-FIELD-VALUE', options: ['selected', 'answer', 'values'] }
|
||||
class AnswerFormatValidatorTest < ActiveSupport::TestCase
|
||||
test "checkbox_other should PASS with populated array" do
|
||||
obj = AnswerValidatable.new('checkbox_other')
|
||||
obj.answer = { other: nil, options: ['some', 'selections', 'not-other'] }
|
||||
|
||||
assert obj.valid?
|
||||
assert obj.errors.messages.empty?
|
||||
end
|
||||
|
||||
test "checkbox_other should PASS with other and value" do
|
||||
obj = AnswerValidatable.new('checkbox_other')
|
||||
obj.answer = { other: 'some random user input', options: ['other', 'another option'] }
|
||||
|
||||
assert obj.valid?
|
||||
assert obj.errors.messages.empty?
|
||||
end
|
||||
|
||||
test "checkbox_other should FAIL with nil" do
|
||||
obj = AnswerValidatable.new('checkbox_other')
|
||||
obj.answer = nil
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
|
||||
test "checkbox_other should FAIL with nil options" do
|
||||
obj = AnswerValidatable.new('checkbox_other')
|
||||
obj.answer = { other: '', options: nil }
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
|
||||
test "checkbox_other should FAIL with empty string" do
|
||||
obj = AnswerValidatable.new('checkbox_other')
|
||||
obj.answer = { other: '', options: [''] }
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
|
||||
test "checkbox_other should FAIL with other selected and no value" do
|
||||
obj = AnswerValidatable.new('checkbox_other')
|
||||
obj.answer = { other: '', options: %w(other some more selections) }
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
|
||||
test "checkbox_other should FAIL with array of empty strings" do
|
||||
obj = AnswerValidatable.new('checkbox_other')
|
||||
obj.answer = { other: 'This is an unselected value', options: ["", "", " "] }
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
end
|
@ -50,16 +50,18 @@ class AnswerFormatValidatorTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
test "live_code should PASS using seed data" do
|
||||
seeded_answer = questions(:fed7).input_options
|
||||
obj = AnswerValidatable.new('live_code', questions(:fed7).id)
|
||||
obj.answer = { text: "no thanks", html: "<p>sample seed html</p>", css: "body { color: #644; }", js: "" }
|
||||
obj.answer = seeded_answer.merge(text: "no thanks", js: "")
|
||||
|
||||
assert obj.valid?
|
||||
assert obj.errors.messages.empty?
|
||||
end
|
||||
|
||||
test "live_code should FAIL with seed data only" do
|
||||
seeded_answer = questions(:fed7).input_options
|
||||
obj = AnswerValidatable.new('live_code', questions(:fed7).id)
|
||||
obj.answer = { text: "", html: "<p>sample seed html</p>", css: "body { color: #644; }", js: "" }
|
||||
obj.answer = seeded_answer.merge(text: "", js: "")
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/write.*code/, obj.errors.messages[:answer][0])
|
||||
|
54
test/validators/answer_format_validator/radio_other_test.rb
Normal file
54
test/validators/answer_format_validator/radio_other_test.rb
Normal file
@ -0,0 +1,54 @@
|
||||
require 'test_helper'
|
||||
|
||||
# *_with_other answers expect a hash answer:
|
||||
# with_other: { other: 'TEXT-FIELD-VALUE', options: ['selected', 'answer', 'values'] }
|
||||
|
||||
class AnswerFormatValidatorTest < ActiveSupport::TestCase
|
||||
test "radio_other should PASS with selection" do
|
||||
obj = AnswerValidatable.new('radio_other')
|
||||
obj.answer = { other: nil, options: ['some-selection-not-other'] }
|
||||
|
||||
assert obj.valid?
|
||||
assert obj.errors.messages.empty?
|
||||
end
|
||||
|
||||
test "radio_other should PASS with other and value" do
|
||||
obj = AnswerValidatable.new('radio_other')
|
||||
obj.answer = { other: 'some random user input', options: ['other'] }
|
||||
|
||||
assert obj.valid?
|
||||
assert obj.errors.messages.empty?
|
||||
end
|
||||
|
||||
test "radio_other should FAIL with nil" do
|
||||
obj = AnswerValidatable.new('radio_other')
|
||||
obj.answer = nil
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
|
||||
test "radio_other should FAIL with nil options" do
|
||||
obj = AnswerValidatable.new('radio_other')
|
||||
obj.answer = { other: '', options: nil }
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
|
||||
test "radio_other should FAIL with empty string" do
|
||||
obj = AnswerValidatable.new('radio_other')
|
||||
obj.answer = { other: '', options: [''] }
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
|
||||
test "radio_other should FAIL with other selected and no value" do
|
||||
obj = AnswerValidatable.new('radio_other')
|
||||
obj.answer = { other: '', options: ['other'] }
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user