introduce check_other and radio_other question types

completes issue #48
This commit is contained in:
Mark Moser
2016-08-31 16:59:25 -05:00
parent 229ebf1380
commit df1b101aa2
12 changed files with 276 additions and 14 deletions

View File

@ -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

View File

@ -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])

View 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