# frozen_string_literal: true
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
