# frozen_string_literal: true
require 'test_helper'

module Admin
  class QuestionControllerTest < ActionDispatch::IntegrationTest
    def setup
      post admin_auth_url, params: { auth:
          { email: 'alan.admin@mailinator.com', password: 'password' } }
    end

    test "should get index" do
      get admin_questions_url
      assert_response :success
      assert assigns :questions
    end

    test "should get new" do
      get admin_new_question_url
      assert_response :success
      assert assigns :question
    end

    test "should fail create" do
      assert_difference("Question.count", 0) do
        post admin_create_question_url, params: { question: { question: 'foo bar baz' } }
      end
      assert :success
      assert_match(/failed/i, session[:flash].values.join)
    end

    test "should fail to create without quiz id" do
      assert_difference("Question.count", 0) do
        post admin_create_question_url, params: { question:
             { question: 'foo bar baz', category: 'ops', input_type: 'text' } }
      end
      assert :success
      assert_match(/failed/i, session[:flash].values.join)
    end

    test "should post create" do
      assert_difference("Question.count", 1) do
        post admin_create_question_url, params: { question:
             { quiz_id: quizzes(:fed).to_i, question: 'foo bar baz', category: 'ops', input_type: 'text' } }
      end
      assert_redirected_to admin_questions_url
    end

    test "should get view" do
      get admin_question_url questions(:fed5).to_i
      assert_response :success
      assert assigns :question
    end

    test "should get edit" do
      get admin_edit_question_url questions(:fed5).to_i
      assert_response :success
      assert assigns :question
    end

    test "should post update quiz" do
      question = questions(:fed9)
      post admin_update_question_url(question.to_i), params: { question:
           { quiz_id: quizzes(:fed).to_i, question: 'foo bar baz', category: 'ops', input_type: 'text' } }
      assert_redirected_to admin_question_path(question.to_i)

      get admin_question_path question.to_i
      assert_select 'p', 'foo bar baz'
    end

    test "should post attachment" do
      question = questions(:fed1)
      post admin_update_question_url(question.to_i), params: { question:
           { quiz_id: quizzes(:fed).to_i, attachment: 'https://dev.perficientdigital.com/logo.png' } }
      assert_redirected_to admin_question_path(question.to_i)

      get admin_question_path question.to_i
      assert_select 'img' do
        assert_select "[src=?]", "https://dev.perficientdigital.com/logo.png"
      end
    end

    test "should fail to update question" do
      question = questions(:fed9)
      post admin_update_question_url(question.to_i), params: { question: { question: nil } }
      assert :success
      assert_match(/failed/i, session[:flash].values.join)
    end

    test "should gracefully fail input_type" do
      get admin_question_option_form_url(input_type: 'fooBarBaz')

      assert :success
      assigns :locals
    end

    test "should return partial for new radio" do
      get admin_question_option_form_url(input_type: 'radio')

      assert :success
      assigns :locals
      assert_select "input[id^=question_multi_choice_]"
    end
  end
end
