introduce check_other and radio_other question types
completes issue #48
This commit is contained in:
41
test/fixtures/answers.yml
vendored
41
test/fixtures/answers.yml
vendored
@ -84,7 +84,9 @@ dawn7:
|
||||
dawn8:
|
||||
candidate: dawn
|
||||
question: fed8
|
||||
answer: option2
|
||||
answer:
|
||||
options:
|
||||
- option2
|
||||
saved: 0
|
||||
submitted: true
|
||||
created_at: <%= DateTime.now() - 38.hours - 38.minutes %>
|
||||
@ -93,7 +95,10 @@ dawn8:
|
||||
dawn9:
|
||||
candidate: dawn
|
||||
question: fed9
|
||||
answer: Grunt
|
||||
answer:
|
||||
other:
|
||||
options:
|
||||
- Grunt
|
||||
saved: 0
|
||||
submitted: true
|
||||
created_at: <%= DateTime.now() - 38.hours - 38.minutes %>
|
||||
@ -174,7 +179,10 @@ peggy7:
|
||||
peggy8:
|
||||
candidate: peggy
|
||||
question: fed8
|
||||
answer: option2
|
||||
answer:
|
||||
other: Some generic user input
|
||||
options:
|
||||
- other
|
||||
saved: 0
|
||||
submitted: true
|
||||
created_at: <%= DateTime.now() - 38.hours - 38.minutes %>
|
||||
@ -183,7 +191,11 @@ peggy8:
|
||||
peggy9:
|
||||
candidate: peggy
|
||||
question: fed9
|
||||
answer: Grunt
|
||||
answer:
|
||||
other: npm
|
||||
options:
|
||||
- Grunt
|
||||
- other
|
||||
saved: 0
|
||||
submitted: true
|
||||
created_at: <%= DateTime.now() - 38.hours - 38.minutes %>
|
||||
@ -265,7 +277,10 @@ richard7:
|
||||
richard8:
|
||||
candidate: richard
|
||||
question: fed8
|
||||
answer: option-4
|
||||
answer:
|
||||
other: Some generic user input
|
||||
options:
|
||||
- other
|
||||
saved: 0
|
||||
submitted: true
|
||||
created_at: <%= DateTime.now() - 36.hours - 36.minutes %>
|
||||
@ -274,7 +289,11 @@ richard8:
|
||||
richard9:
|
||||
candidate: richard
|
||||
question: fed9
|
||||
answer: Grunt
|
||||
answer:
|
||||
other: Brunch
|
||||
options:
|
||||
- Neither
|
||||
- other
|
||||
saved: 0
|
||||
submitted: true
|
||||
created_at: <%= DateTime.now() - 36.hours - 38.minutes %>
|
||||
@ -355,7 +374,10 @@ juan7:
|
||||
juan8:
|
||||
candidate: juan
|
||||
question: fed8
|
||||
answer: option2
|
||||
answer:
|
||||
other: Some generic user input
|
||||
options:
|
||||
- other
|
||||
saved: 0
|
||||
submitted: true
|
||||
created_at: <%= DateTime.now() - 38.hours - 38.minutes %>
|
||||
@ -364,7 +386,10 @@ juan8:
|
||||
juan9:
|
||||
candidate: juan
|
||||
question: fed9
|
||||
answer: Grunt
|
||||
answer:
|
||||
other: Mimosa
|
||||
options:
|
||||
- other
|
||||
saved: 0
|
||||
submitted: true
|
||||
created_at: <%= DateTime.now() - 38.hours - 38.minutes %>
|
||||
|
7
test/fixtures/questions.yml
vendored
7
test/fixtures/questions.yml
vendored
@ -68,7 +68,7 @@ fed7:
|
||||
category: Javascript
|
||||
input_type: live_code
|
||||
input_options:
|
||||
:html: "<p>sample seed html</p>"
|
||||
:html: "<p>Sample seed HTML</p>"
|
||||
:css: "body { color: #644; }"
|
||||
sort: 6
|
||||
active: true
|
||||
@ -77,12 +77,11 @@ fed8:
|
||||
quiz: fed
|
||||
question: Select the HTML from below that would create an input field which restricts the number of characters inside it to 10.
|
||||
category: HTML
|
||||
input_type: radio
|
||||
input_type: radio_other
|
||||
input_options:
|
||||
- option-1
|
||||
- option2
|
||||
- "option 3"
|
||||
- option-4
|
||||
sort: 7
|
||||
active: true
|
||||
|
||||
@ -90,7 +89,7 @@ fed9:
|
||||
quiz: fed
|
||||
question: Grunt or Gulp?
|
||||
category: Javascript
|
||||
input_type: radio
|
||||
input_type: checkbox_other
|
||||
input_options:
|
||||
- Grunt
|
||||
- Gulp
|
||||
|
@ -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