diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb index b49458bb1e..0c86a70a8e 100644 --- a/app/controllers/answers_controller.rb +++ b/app/controllers/answers_controller.rb @@ -100,6 +100,8 @@ def create_or_update remove_list_after = remove_list(@plan) + puts 'remove_list_after: ' + p remove_list_after all_question_ids = @plan.questions.pluck(:id) # TBD: Clear all answers for removed questions @@ -111,8 +113,7 @@ def create_or_update Answer.destroy(a.id) end end - puts 'remove_list_after: ' - p remove_list_after + # rubocop pointed out that these variable is not used # all_answers = @plan.answers qn_data = { 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..28340dd0b0 --- /dev/null +++ b/spec/controllers/answers_controller_conditional_questions_spec.rb @@ -0,0 +1,105 @@ +# 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 + + question1 = create(:question, :checkbox, section: @section, options: 5) + question2 = create(:question, :dropdown, section: @section, options: 3) + question3 = create(:question, :radiobuttons, section: @section, options: 3) + question4 = create(:question, :textarea, section: @section) + question5 = create(:question, :textfield, section: @section) + + q1_options = [create(:question_option), create(:question_option), 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)] + + # Add our created question options to the questions + question1.question_options = q1_options + question2.question_options = q2_options + question3.question_options = q3_options + + condition1 = create(:condition, question: question1, + option_list: [q1_options[0].id], + action_type: 'remove', + remove_data: [question2.id, question4.id]) + create(:condition, question: question2, option_list: [q2_options[1].id], remove_data: [question5.id]) + create(:condition, question: question3, option_list: [q2_options[1].id], remove_data: [question4.id]) + + puts condition1.inspect + + @questions = [question1, question2, question3, question4, question5] + @plan = create(:plan, :creator, template: template) + @user = @plan.owner + + puts q1_options.inspect + + ans1 = create(:answer, plan: @plan, question: question1, + question_options: [q1_options[0]], user: @user) + ans2 = create(:answer, plan: @plan, question: question2, + question_options: [q2_options[1]], user: @user) + ans3 = create(:answer, plan: @plan, question: question3, + question_options: [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 'TBD' do + before(:each) do + @question = @questions[0] + puts @question.inspect + puts @questions.inspect + @question_options = @question.question_options + puts @question.id + @ans_text = Faker::Lorem.paragraph + puts @ans_text + @args = { text: @ans_text, question_option_ids: [@question_options[0]], + user_id: @user.id, + question_id: @question.id, plan_id: @plan.id, + lock_version: 0 } + end + it 'succeeds in updating' do + post :create_or_update, params: { answer: @args } + answer = Answer.where(question: @question).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 + puts json.inspect + 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) + puts json[:qn_data] + expect(json[:qn_data][:to_show]).to contain_exactly(@questions[0].id, @questions[2].id, @questions[4].id) + expect(json[:qn_data][:to_hide]).to contain_exactly(@questions[1].id, @questions[3].id) + end + end + end +end diff --git a/spec/factories/answers.rb b/spec/factories/answers.rb index 447c549bb1..f90efea5c2 100644 --- a/spec/factories/answers.rb +++ b/spec/factories/answers.rb @@ -34,5 +34,11 @@ plan user question + trait :question_options do + question_options { [create(:question_option), create(:question_option)] } + end + trait :lock_version do + lock_version { 0 } + end end end diff --git a/spec/factories/conditions.rb b/spec/factories/conditions.rb index afaf5fdea2..813fb366fa 100644 --- a/spec/factories/conditions.rb +++ b/spec/factories/conditions.rb @@ -28,5 +28,17 @@ factory :condition do option_list { nil } remove_data { nil } + action_type { nil } + # the webhook_data is a Json string of form: + # '{"name":"Joe Bloggs","email":"joe.bloggs@example.com","subject":"Large data volume","message":"A message."}' + 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) + }) + end end end diff --git a/spec/features/questions/conditions_questions_spec.rb b/spec/features/questions/conditions_questions_spec.rb new file mode 100644 index 0000000000..cd507e1d28 --- /dev/null +++ b/spec/features/questions/conditions_questions_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Question::Conditions questions', type: :feature do + before do + # @default_template = create(:template, :default, :published) + # @phase = create(:phase, template: @default_template) + # # Create a couple of Sections + # @section = create(:section, phase: @phase) + + # @question = create(:question, :checkbox, section: @section, options: 2) + # @user = create(:user) + # @plan = create(:plan, template: @default_template) + # create(:role, :creator, :editor, :commenter, user: @user, plan: @plan) + # sign_in(@user) + end + + # scenario 'User answers a check box question', :js do + # # Setup + # visit overview_plan_path(@plan) + + # # Action + # click_link 'Write plan' + + # # Expectations + # expect(current_path).to eql(edit_plan_path(@plan)) + # # 4 sections x 3 questions + # expect(page).to have_text('(0 / 1)') + + # # Action + # find("#section-panel-#{@section.id}").click + # # Fill in the answer form... + # within("#answer-form-#{@question.id}") do + # check @question.question_options.first.text + # click_button 'Save' + # end + + # # Expectations + # expect(page).to have_text 'Answered just now' + # expect(page).to have_text '(1 / 1)' + # expect(Answer.where(question_id: @question.id)).to be_any + # end +end diff --git a/spec/models/condition_spec.rb b/spec/models/condition_spec.rb index f5b8d10d45..73bc96e691 100644 --- a/spec/models/condition_spec.rb +++ b/spec/models/condition_spec.rb @@ -3,5 +3,61 @@ require 'rails_helper' RSpec.describe Condition, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + context 'associations' do + it { is_expected.to belong_to :question } + end + + describe '.deep_copy with no options passed in.' do + let!(:question) { build(:question) } + + let!(:condition) do + build(:condition, question: question, option_list: [1, 5], + action_type: 'remove', + remove_data: [7, 8, 9]) + end + + subject { condition.deep_copy } + + it 'creates a new record' do + expect(subject).not_to eql(condition) + end + it 'copies the option_list attribute' do + expect(subject.option_list).to contain_exactly(1, 5) + end + + it 'copies the action_type attribute' do + expect(subject.action_type).to eql('remove') + end + + it 'copies the remove_data attribute' do + expect(subject.remove_data).to contain_exactly(7, 8, 9) + end + end + + describe '.deep_copy with options passed in.' do + let!(:question) { build(:question) } + + let!(:condition) do + build(:condition, question: question, option_list: [1, 5], + action_type: 'remove', + remove_data: [7, 8, 9]) + end + + subject { condition.deep_copy } + + it 'creates a new record' do + expect(subject).not_to eql(condition) + end + it 'copies the option_list attribute' do + expect(subject.option_list).to contain_exactly(1, 5) + end + + it 'copies the action_type attribute' do + expect(subject.action_type).to eql('remove') + end + + it 'copies the remove_data attribute' do + expect(subject.remove_data).to contain_exactly(7, 8, 9) + end + end end