-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for bugs in the Conditional Questions functionality:
- In case of conditional question with checkbox answers the removed questions were not being removed from view, nor was the answer to these questions (which persisted in the db. Changes: - Fixed the broken functionality in the method remove_answers_list in app/helpers/conditions_helper.rb. - Removed and destroyed the answers of the removed questions. - Added more tests for Conditional Questions functionality.
- Loading branch information
John Pinto
committed
Sep 11, 2024
1 parent
56759df
commit bad2b87
Showing
11 changed files
with
424 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
spec/controllers/answers_controller_conditional_questions_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
# 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 | ||
|
||
# create(:condition, question: question1, | ||
# 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 | ||
|
||
# Different types of questions (than can have conditions on options) | ||
@checkbox_question = create(:question, :checkbox, section: @section, options: 5) | ||
@dropdown_question = create(:question, :dropdown, section: @section, options: 3) | ||
@radiobuttons_question = create(:question, :radiobuttons, section: @section, options: 3) | ||
# Types of questions that don't have conditions on options | ||
@textarea_question = create(:question, :textarea, section: @section) | ||
@textfield_question = create(:question, :textfield, section: @section) | ||
@rdametadata_question = create(:question, :rda_metadata, section: @section) | ||
|
||
@plan = create(:plan, :creator, template: template) | ||
@user = @plan.owner | ||
|
||
sign_in(@user) | ||
end | ||
|
||
# Note condition option_list only takes one option (currently) in the UI. | ||
# As functionality for more than option per condition does not yet exist in code. | ||
# So tests all use a single option for conditional questions. | ||
describe 'POST /answers/create_or_update (where atleast one question has one or more conditions)' do | ||
context 'with conditional checkbox question' do | ||
it 'handles single option (with condition) in option_list ' do | ||
condition = create(:condition, question: @checkbox_question, | ||
option_list: [@checkbox_question.question_options[2].id], | ||
action_type: 'remove', | ||
remove_data: [create(:question).id, create(:question).id]) | ||
|
||
args = { | ||
question_option_ids: [@checkbox_question.question_options[2].id], | ||
user_id: @user.id, | ||
question_id: @checkbox_question.id, | ||
plan_id: @plan.id, | ||
lock_version: 0 | ||
} | ||
|
||
post :create_or_update, params: { answer: args } | ||
|
||
json = JSON.parse(response.body).with_indifferent_access | ||
expect(json[:qn_data][:to_hide]).to match_array(condition.remove_data) | ||
end | ||
it 'handles single option (without condition) in option_list' do | ||
create(:condition, question: @checkbox_question, | ||
option_list: [@checkbox_question.question_options[2].id], | ||
action_type: 'remove', | ||
remove_data: [create(:question).id, create(:question).id]) | ||
|
||
args = { | ||
question_option_ids: [@checkbox_question.question_options[0].id], | ||
user_id: @user.id, | ||
question_id: @checkbox_question.id, | ||
plan_id: @plan.id, | ||
lock_version: 0 | ||
} | ||
|
||
post :create_or_update, params: { answer: args } | ||
|
||
json = JSON.parse(response.body).with_indifferent_access | ||
expect(json[:qn_data][:to_hide]).to match_array([]) | ||
end | ||
|
||
it 'handles multiple options (some with conditions) in option_list' do | ||
condition1 = create(:condition, question: @checkbox_question, | ||
option_list: [@checkbox_question.question_options[2].id], | ||
action_type: 'remove', | ||
remove_data: [create(:question).id]) | ||
condition2 = create(:condition, question: @checkbox_question, | ||
option_list: [@checkbox_question.question_options[4].id], | ||
action_type: 'remove', | ||
remove_data: [create(:question).id, create(:question).id]) | ||
|
||
args = { | ||
question_option_ids: [@checkbox_question.question_options[2].id, | ||
@checkbox_question.question_options[3].id, | ||
@checkbox_question.question_options[4].id], | ||
user_id: @user.id, | ||
question_id: @checkbox_question.id, | ||
plan_id: @plan.id, | ||
lock_version: 0 | ||
} | ||
|
||
post :create_or_update, params: { answer: args } | ||
|
||
json = JSON.parse(response.body).with_indifferent_access | ||
expect(json[:qn_data][:to_hide]).to match_array(condition1.remove_data + condition2.remove_data) | ||
end | ||
|
||
# it 'handles empty option_list' do | ||
# condition = create(:condition, question: question1, | ||
# option_list: [], | ||
# action_type: 'remove', | ||
# remove_data: [create(:question).id]) | ||
|
||
# args = { text: Faker::Lorem.paragraph, | ||
# question_option_ids: [], | ||
# user_id: @user.id, | ||
# question_id: @checkbox_question1.id, | ||
# plan_id: @plan.id, | ||
# lock_version: 0 } | ||
|
||
# post :create_or_update, params: { answer: args } | ||
|
||
# json = JSON.parse(response.body).with_indifferent_access | ||
# expect(json[:qn_data][:to_hide]).to include(condition.remove_data.first) | ||
# end | ||
# end | ||
end | ||
end | ||
|
||
# describe 'POST /answers/create_or_update', js: true do | ||
# context 'TBD' do | ||
# before(:each) do | ||
# @ans_text = Faker::Lorem.paragraph | ||
# @args = { text: @ans_text, question_option_ids: [@q1_options[0]], | ||
# user_id: @user.id, | ||
# question_id: @checkbox_question1.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: @checkbox_question).last | ||
# expect(answer.present?).to eql(true) | ||
# expect(answer.question).to eql(@checkbox_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(@checkbox_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) | ||
# expect(json[:qn_data][:to_show]).to contain_exactly(@checkbox_questions[0].id, @checkbox_questions[2].id, @checkbox_questions[4].id) | ||
# expect(json[:qn_data][:to_hide]).to contain_exactly(@checkbox_questions[1].id, @checkbox_questions[3].id) | ||
# end | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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":"[email protected]","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 |
Oops, something went wrong.