Skip to content

Commit

Permalink
Creating a test functionality that answers of questions removed when …
Browse files Browse the repository at this point in the history
…a conditional question is removed.
  • Loading branch information
John Pinto committed Aug 1, 2024
1 parent e4d6f60 commit 3d824a8
Show file tree
Hide file tree
Showing 3 changed files with 293 additions and 0 deletions.
272 changes: 272 additions & 0 deletions spec/controllers/answers_controller_conditional_questions_spec.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions spec/factories/answers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
plan
user
question
trait :question_options do
question_options { [create(:question_option), create(:question_option)] }
end
end
end
18 changes: 18 additions & 0 deletions spec/factories/conditions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3d824a8

Please sign in to comment.