2016-09-08 10:25:33 -05:00
|
|
|
# frozen_string_literal: true
|
2016-08-07 09:36:08 -05:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class QuizControllerTest < ActionDispatch::IntegrationTest
|
2016-08-16 17:09:40 -05:00
|
|
|
test "should redirect to saved on save" do
|
2016-09-14 17:05:37 -05:00
|
|
|
auth_candidate candidates(:dawn)
|
2016-08-16 17:09:40 -05:00
|
|
|
qid = questions(:fed5).id
|
2016-09-02 17:51:35 -05:00
|
|
|
post post_answer_path, params: { save: 'Save', answer: { question_id: qid, answer: 'an option' } }
|
2016-08-16 17:09:40 -05:00
|
|
|
|
|
|
|
assert_redirected_to saved_path
|
|
|
|
assert session[:test_id].present?
|
|
|
|
end
|
|
|
|
|
2016-08-07 09:36:08 -05:00
|
|
|
test "should get flash message on bad radio response" do
|
2016-09-14 17:05:37 -05:00
|
|
|
auth_candidate candidates(:dawn)
|
2016-08-16 17:09:40 -05:00
|
|
|
qid = questions(:fed5).id
|
2016-09-02 17:51:35 -05:00
|
|
|
post post_answer_path, params: { answer: { question_id: qid, answer: nil } }
|
2016-08-07 09:36:08 -05:00
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert session[:test_id].present?
|
2016-08-09 23:17:35 -05:00
|
|
|
assert_equal qid, flash[:error]
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
2016-08-07 09:36:08 -05:00
|
|
|
end
|
2016-08-16 17:09:40 -05:00
|
|
|
|
|
|
|
test "should get flash message on bad text response" do
|
2016-09-14 17:05:37 -05:00
|
|
|
auth_candidate candidates(:dawn)
|
2016-08-16 17:09:40 -05:00
|
|
|
qid = questions(:fed4).id
|
2016-09-02 17:51:35 -05:00
|
|
|
post post_answer_path, params: { answer: { question_id: qid, answer: nil } }
|
2016-08-16 17:09:40 -05:00
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert session[:test_id].present?
|
|
|
|
assert_equal qid, flash[:error]
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
2016-08-16 17:09:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should process checkbox" do
|
2016-09-14 17:05:37 -05:00
|
|
|
auth_candidate candidates(:dawn)
|
2016-08-16 17:09:40 -05:00
|
|
|
qid = questions(:fed10).id
|
2016-09-02 17:51:35 -05:00
|
|
|
post post_answer_path, params: { answer: { question_id: qid, answer_array: 'an-option' } }
|
2016-08-16 17:09:40 -05:00
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert session[:test_id].present?
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
2016-08-16 17:09:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should handle XHR update and complete progress' do
|
2016-09-14 17:05:37 -05:00
|
|
|
auth_candidate candidates(:peggy)
|
2016-08-16 17:09:40 -05:00
|
|
|
qid = questions(:fed10).id
|
2016-09-02 17:51:35 -05:00
|
|
|
post post_answer_path, xhr: true, params: { answer: { question_id: qid, answer_array: ['an-option'] } }
|
2016-08-16 17:09:40 -05:00
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_match(/updated successfully/, JSON.parse(@response.body)['message'])
|
|
|
|
assert_equal 100, JSON.parse(@response.body)['progress']
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
2016-08-16 17:09:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should handle XHR fail' do
|
2016-09-14 17:05:37 -05:00
|
|
|
auth_candidate candidates(:peggy)
|
2016-08-16 17:09:40 -05:00
|
|
|
qid = questions(:fed10).id
|
2016-09-02 17:51:35 -05:00
|
|
|
post post_answer_path, xhr: true, params: { answer: { question_id: qid, answer_array: [nil] } }
|
2016-08-16 17:09:40 -05:00
|
|
|
|
|
|
|
assert_response 400
|
|
|
|
assert_match(/select.*answer/i, JSON.parse(@response.body).join)
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
2016-08-16 17:09:40 -05:00
|
|
|
end
|
2016-08-07 09:36:08 -05:00
|
|
|
end
|