From 3d824a8b8af58aaf2761cd7d3d6bbaf4d86813a7 Mon Sep 17 00:00:00 2001 From: John Pinto Date: Fri, 26 Jul 2024 16:47:21 +0100 Subject: [PATCH] Creating a test functionality that answers of questions removed when a conditional question is removed. --- ...s_controller_conditional_questions_spec.rb | 272 ++++++++++++++++++ spec/factories/answers.rb | 3 + spec/factories/conditions.rb | 18 ++ 3 files changed, 293 insertions(+) create mode 100644 spec/controllers/answers_controller_conditional_questions_spec.rb diff --git a/spec/controllers/answers_controller_conditional_questions_spec.rb b/spec/controllers/answers_controller_conditional_questions_spec.rb new file mode 100644 index 0000000000..0d0021d9e5 --- /dev/null +++ b/spec/controllers/answers_controller_conditional_questions_spec.rb @@ -0,0 +1,272 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AnswersController, type: :controller do + include RolesHelper + + before(:each) do + template = create(:template, phases: 1, sections: 1) + @section = template.sections.first + + q1_options = [create(:question_option), create(:question_option), create(:question_option)] + q2_options = [create(:question_option), create(:question_option), create(:question_option)] + q3_options = [create(:question_option), create(:question_option), create(:question_option)] + + question1 = create(:question, :checkbox, section: @section, options: 3) + question1.question_options = q1_options + question2 = create(:question, :dropdown, section: @section, options: 3) + question2.question_options = q2_options + question3 = create(:question, :radiobuttons, section: @section, options: 3) + question3.question_options = q3_options + question4 = create(:question, :textarea, section: @section) + question5 = create(:question, :textfield, section: @section) + + @questions = [question1, question2, question3, question4, question5] + + create(:condition, question: question1, option_list: [q1_options[0].id], remove_data: [question2.id, question4.id]) + create(:condition, question: question2, option_list: [q2_options[1].id], remove_data: [question5.id]) + + @plan = create(:plan, :creator, template: template) + @user = @plan.owner + + ans1 = create(:answer, plan: @plan, question: question1, + question_options: [q1_options[0], q1_options[1], q1_options[2]], user: @user) + ans2 = create(:answer, plan: @plan, question: question2, + question_options: [q2_options[0], q2_options[1], q2_options[2]], user: @user) + ans3 = create(:answer, plan: @plan, question: question3, + question_options: [q3_options[0], q3_options[1], q3_options[2]], user: @user) + ans4 = create(:answer, plan: @plan, question: question4, text: Faker::Lorem.paragraph, user: @user) + ans5 = create(:answer, plan: @plan, question: question5, text: Faker::Lorem.paragraph, user: @user) + + @answers = [ans1, ans2, ans3, ans4, ans5] + + ActionMailer::Base.deliveries = [] + @controller = described_class.new + sign_in(@user) + end + + after(:each) do + ActionMailer::Base.deliveries = [] + end + + describe 'POST /answers/create_or_update', js: true do + context 'TOBD' do + before(:each) do + @question = @questions[3] + @args = { text: Faker::Lorem.paragraph, user_id: @user.id, + question_id: @question.id, plan_id: @plan.id } + end + + it 'succeeds in updating' do + post :create_or_update, params: { answer: @args } + # answer = Answer.all.last + answer = @answers[3] + expect(answer.present?).to eql(true) + expect(answer.question).to eql(@question) + expect(answer.plan).to eql(@plan) + expect(answer.user).to eql(@user) + + json = JSON.parse(response.body).with_indifferent_access + expect(json[:plan].present?).to eql(true) + expect(json[:plan][:progress]).to eql('') + expect(json[:plan][:id]).to eql(@plan.id) + expect(json[:question].present?).to eql(true) + expect(json[:question][:answer_lock_version]).to eql(answer.lock_version + 1) + expect(json[:question][:answer_status]).to eql('') + expect(json[:question][:form]).to eql('') + expect(json[:question][:id]).to eql(@question.id) + expect(json[:question][:locking]).to eql(nil) + expect(json[:section_data].present?).to eql(true) + expect(json[:qn_data].present?).to eql(true) + # TODO: add validations on content of qn_data and section_data + end + end + + context 'TBD' do + before(:each) do + @question = @questions[0] + @args = { question_option_ids: [@question.question_options[0].id], user_id: @user.id, + question_id: @question.id, plan_id: @plan.id } + end + it 'succeeds in updating' do + post :create_or_update, params: { answer: @args } + # answer = Answer.all.last + answer = @answers[0] + expect(answer.present?).to eql(true) + expect(answer.question).to eql(@question) + expect(answer.plan).to eql(@plan) + expect(answer.user).to eql(@user) + + json = JSON.parse(response.body).with_indifferent_access + expect(json[:plan].present?).to eql(true) + expect(json[:plan][:progress]).to eql('') + expect(json[:plan][:id]).to eql(@plan.id) + expect(json[:question].present?).to eql(true) + expect(json[:question][:answer_lock_version]).to eql(answer.lock_version + 1) + expect(json[:question][:answer_status]).to eql('') + expect(json[:question][:form]).to eql('') + expect(json[:question][:id]).to eql(@question.id) + expect(json[:question][:locking]).to eql(nil) + expect(json[:section_data].present?).to eql(true) + expect(json[:qn_data].present?).to eql(true) + # TODO: add validations on content of qn_data and section_data + end + end + + # it 'succeeds in updating' do + # answer = create(:answer, plan: @plan, question: @question) + # @args[:lock_version] = answer.lock_version + # post :create_or_update, params: { answer: @args } + # answer.reload + # expect(answer.text).to eql(@args[:text]) + # expect(answer.user).to eql(@user) + + # json = JSON.parse(response.body).with_indifferent_access + # expect(json[:plan].present?).to eql(true) + # expect(json[:plan][:progress]).to eql('') + # expect(json[:plan][:id]).to eql(@plan.id) + # expect(json[:question].present?).to eql(true) + # expect(json[:question][:answer_lock_version]).to eql(answer.lock_version) + # expect(json[:question][:answer_status]).to eql('') + # expect(json[:question][:form]).to eql('') + # expect(json[:question][:id]).to eql(@question.id) + # expect(json[:question][:locking]).to eql(nil) + # expect(json[:section_data].present?).to eql(true) + # expect(json[:qn_data].present?).to eql(true) + # end + # it 'fails' do + # Answer.any_instance.stubs(:present?).returns(false) + # post :create_or_update, params: { answer: @args } + # expect(response.body).to eql('') + # end + # end + + # context 'RDA metadata question type' do + # before(:each) do + # @question = create(:question, :rda_metadata, section: @section) + # @args = { text: Faker::Lorem.paragraph, standards: { foo: 'bar' }, + # user_id: @user.id, question_id: @question.id, plan_id: @plan.id } + # end + + # it 'succeeds in creating' do + # post :create_or_update, params: { answer: @args } + # answer = Answer.all.last + # expect(answer.present?).to eql(true) + # expect(answer.question).to eql(@question) + # expect(answer.plan).to eql(@plan) + # expect(answer.user).to eql(@user) + + # json = JSON.parse(response.body).with_indifferent_access + # expect(json[:plan].present?).to eql(true) + # expect(json[:plan][:progress]).to eql('') + # expect(json[:plan][:id]).to eql(@plan.id) + # expect(json[:question].present?).to eql(true) + # expect(json[:question][:answer_lock_version]).to eql(answer.lock_version) + # expect(json[:question][:answer_status]).to eql('') + # expect(json[:question][:form]).to eql('') + # expect(json[:question][:id]).to eql(@question.id) + # expect(json[:question][:locking]).to eql(nil) + # expect(json[:section_data].present?).to eql(true) + # expect(json[:qn_data].present?).to eql(true) + # end + # it 'succeeds in updating' do + # answer = create(:answer, plan: @plan, question: @question) + # @args[:lock_version] = answer.lock_version + # post :create_or_update, params: { answer: @args } + # answer.reload + # json = JSON.parse(answer.text).with_indifferent_access + # expect(json[:standards]).to eql(@args[:standards].with_indifferent_access) + # expect(json[:text]).to eql(@args[:text]) + # expect(answer.user).to eql(@user) + + # json = JSON.parse(response.body).with_indifferent_access + # expect(json[:plan].present?).to eql(true) + # expect(json[:plan][:progress]).to eql('') + # expect(json[:plan][:id]).to eql(@plan.id) + # expect(json[:question].present?).to eql(true) + # expect(json[:question][:answer_lock_version]).to eql(answer.lock_version) + # expect(json[:question][:answer_status]).to eql('') + # expect(json[:question][:form]).to eql('') + # expect(json[:question][:id]).to eql(@question.id) + # expect(json[:question][:locking]).to eql(nil) + # expect(json[:section_data].present?).to eql(true) + # expect(json[:qn_data].present?).to eql(true) + # end + # it 'fails' do + # Answer.any_instance.stubs(:present?).returns(false) + # post :create_or_update, params: { answer: @args } + # expect(response.body).to eql('') + # end + # end + + # context 'question with question_options' do + # before(:each) do + # @question = create(:question, :radiobuttons, section: @section, options: 2) + # @args = { text: Faker::Lorem.paragraph, user_id: @user.id, + # question_id: @question.id, plan_id: @plan.id, + # question_option_ids: [@question.question_options.first.id] } + # end + + # it 'succeeds in creating' do + # post :create_or_update, params: { answer: @args } + # answer = Answer.all.last + # expect(answer.present?).to eql(true) + # expect(answer.question).to eql(@question) + # expect(answer.plan).to eql(@plan) + # expect(answer.user).to eql(@user) + + # json = JSON.parse(response.body).with_indifferent_access + # expect(json[:plan].present?).to eql(true) + # expect(json[:plan][:progress]).to eql('') + # expect(json[:plan][:id]).to eql(@plan.id) + # expect(json[:question].present?).to eql(true) + # expect(json[:question][:answer_lock_version]).to eql(answer.lock_version) + # expect(json[:question][:answer_status]).to eql('') + # expect(json[:question][:form]).to eql('') + # expect(json[:question][:id]).to eql(@question.id) + # expect(json[:question][:locking]).to eql(nil) + # expect(json[:section_data].present?).to eql(true) + # expect(json[:qn_data].present?).to eql(true) + # end + # it 'succeeds in updating' do + # answer = create(:answer, plan: @plan, question: @question) + # @args[:lock_version] = answer.lock_version + # post :create_or_update, params: { answer: @args } + # answer.reload + # expect(answer.text).to eql(@args[:text]) + # expect(answer.question_options.length).to eql(1) + # expect(answer.question_options.first.id).to eql(@args[:question_option_ids].first) + # expect(answer.user).to eql(@user) + + # json = JSON.parse(response.body).with_indifferent_access + # expect(json[:plan].present?).to eql(true) + # expect(json[:plan][:progress]).to eql('') + # expect(json[:plan][:id]).to eql(@plan.id) + # expect(json[:question].present?).to eql(true) + # expect(json[:question][:answer_lock_version]).to eql(answer.lock_version) + # expect(json[:question][:answer_status]).to eql('') + # expect(json[:question][:form]).to eql('') + # expect(json[:question][:id]).to eql(@question.id) + # expect(json[:question][:locking]).to eql(nil) + # expect(json[:section_data].present?).to eql(true) + # expect(json[:qn_data].present?).to eql(true) + # end + # it 'fails' do + # Answer.any_instance.stubs(:present?).returns(false) + # post :create_or_update, params: { answer: @args } + # expect(response.body).to eql('') + # end + # end + + # it 'fails due to Plan not found' do + # @question = create(:question, :textarea, section: @section) + # @args = { text: Faker::Lorem.paragraph, user_id: @user.id, + # question_id: @question.id } + # post :create_or_update, params: { answer: @args } + # json = JSON.parse(response.body).with_indifferent_access + # expect(json[:msg].present?).to eql(true) + # end + # end + end +end diff --git a/spec/factories/answers.rb b/spec/factories/answers.rb index 447c549bb1..01d7a6e64f 100644 --- a/spec/factories/answers.rb +++ b/spec/factories/answers.rb @@ -34,5 +34,8 @@ plan user question + trait :question_options do + question_options { [create(:question_option), create(:question_option)] } + end end end diff --git a/spec/factories/conditions.rb b/spec/factories/conditions.rb index afaf5fdea2..24d8b41b64 100644 --- a/spec/factories/conditions.rb +++ b/spec/factories/conditions.rb @@ -28,5 +28,23 @@ factory :condition do option_list { nil } remove_data { nil } + webhook_data do + # Generates string from hash + JSON.generate({ + name: Faker::Name.name, + email: Faker::Internet.email, + subject: Faker::Lorem.sentence(word_count: 4), + message: Faker::Lorem.paragraph(sentence_count: 2) + }) + + # '{"name":"Joe Bloggs","email":"joe.bloggs@example.com","subject":"Large data volume","message":"A message."}' + end + # do { + # name: Faker::Name.name, + # email: Faker::Internet.email, + # subject: Faker::Lorem.sentence(word_count: 4), + # message: Faker::Lorem.paragraph(sentence_count: 2) + # }.stringify_keys! + # end end end