# frozen_string_literal: true
require 'test_helper'

module Admin
  class QuizControllerTest < 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_quizzes_url
      assert_response :success
      assert assigns :quizzes
    end

    test "should get new" do
      get admin_new_quiz_url
      assert_response :success
      assert assigns :quiz
    end

    test "should fail create" do
      assert_difference("Quiz.count", 0) do
        post admin_create_quiz_url, params: { quiz: { dept: nil } }
      end
      assert :success
      assert_match(/failed/i, session[:flash].values.join)
    end

    test "should post create" do
      assert_difference("Quiz.count", 1) do
        post admin_create_quiz_url, params: { quiz:
             { name: 'PDW Mobile team screening', unit: 'PDW', dept: 'MBL' } }
      end
      assert_redirected_to admin_quizzes_url
    end

    test "should get view" do
      quiz = quizzes :fed
      get admin_quiz_url quiz.to_i
      assert_response :success
      assert_select 'div', quiz.dept
    end

    test "should get edit" do
      quiz = quizzes :fed
      get admin_edit_quiz_url quiz.to_i
      assert_response :success
      assert_select "[value=?]", quiz.dept
    end

    test "should post update quiz" do
      quiz = quizzes(:fed)
      post admin_update_quiz_url(quiz.to_i), params: { quiz: { dept: 'new', unit: 'another' } }
      assert_redirected_to admin_quiz_path(quiz.to_i)

      get admin_quiz_path quiz.to_i
      assert_select 'div', 'another'
    end

    test "should fail to update quiz" do
      quiz = quizzes(:fed)
      post admin_update_quiz_url(quiz.to_i), params: { quiz: { dept: nil } }
      assert :success
      assert_match(/failed/i, session[:flash].values.join)
    end
  end
end
