-
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 12, 2024
1 parent
56759df
commit 0bfac45
Showing
11 changed files
with
605 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
337 changes: 337 additions & 0 deletions
337
spec/controllers/answers_controller_with_conditional_questions_spec.rb
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
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,145 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'Question::Conditions questions', type: :feature do | ||
before do | ||
@user = create(:user) | ||
@template = create(:template, :default, :published) | ||
@plan = create(:plan, :creator, template: @template) | ||
@phase = create(:phase, template: @template) | ||
@section = create(:section, phase: @phase) | ||
|
||
# Different types of questions (than can have conditional options) | ||
@checkbox_question = create(:question, :checkbox, section: @section, options: 5) | ||
@radiobutton_question = create(:question, :radiobuttons, section: @section, options: 5) | ||
@dropdown_question = create(:question, :dropdown, section: @section, options: 5) | ||
|
||
create(:role, :creator, :editor, :commenter, user: @user, plan: @plan) | ||
sign_in(@user) | ||
end | ||
|
||
# NOTE: Condition is only implemented for checkboxes, radio buttons and dropdowns. In these cases, currently | ||
# the option_list only takes one option in the UI. | ||
# As functionality for more than option per condition does not yet exist in code. | ||
# So all Conditions are created with option_list with a single option id. | ||
|
||
scenario 'User answers multiple options for a checkbox with conditional options', :js do | ||
chk_condition1 = create(:condition, question: @checkbox_question, | ||
option_list: [@checkbox_question.question_options[2].id], | ||
action_type: 'remove', | ||
remove_data: [create(:question, :dropdown, section: @section, options: 3).id, | ||
create(:question, :date, section: @section).id, | ||
create(:question, :textfield, section: @section).id]) | ||
|
||
chk_condition2 = create(:condition, question: @checkbox_question, | ||
option_list: [@checkbox_question.question_options[1].id], | ||
action_type: 'remove', | ||
remove_data: [create(:question, :checkbox, section: @section, options: 3).id, | ||
create(:question, :date, section: @section).id, | ||
create(:question, :textfield, section: @section).id]) | ||
|
||
chk_condition3 = create(:condition, question: @checkbox_question, | ||
option_list: [@checkbox_question.question_options[3].id], | ||
action_type: 'remove', | ||
remove_data: [create(:question, :radiobuttons, section: @section, options: 3).id, | ||
create(:question, :rda_metadata, section: @section).id, | ||
create(:question, :textarea, | ||
section: @section).id]) | ||
|
||
visit overview_plan_path(@plan) | ||
|
||
click_link 'Write plan' | ||
|
||
find("#section-panel-#{@section.id}").click | ||
|
||
save_screenshot | ||
expect(page).to have_text('(0 / 12)') | ||
|
||
within("#answer-form-#{@checkbox_question.id}") do | ||
check @checkbox_question.question_options[2].text | ||
click_button 'Save' | ||
end | ||
|
||
save_screenshot | ||
expect(page).to have_text('Answered just now') | ||
expect(page).not_to have_selector("#answer-form-#{chk_condition1.remove_data[0]}") | ||
expect(page).to have_text('(1 / 9)') | ||
|
||
within("#answer-form-#{@checkbox_question.id}") do | ||
check @checkbox_question.question_options[1].text | ||
click_button 'Save' | ||
end | ||
|
||
save_screenshot | ||
expect(page).to have_text('Answered just now') | ||
expect(page).not_to have_selector("#answer-form-#{chk_condition2.remove_data[2]}") | ||
expect(page).to have_text('(1 / 6)') | ||
|
||
# within("#answer-form-#{question2.id}") do | ||
# select q2_options.last.text | ||
# click_button 'Save' | ||
# end | ||
|
||
# expect(page).to have_text('Answered just now') | ||
# expect(page).not_to have_selector("#answer-form-#{question5.id}") | ||
# # As one question has been removed, the total of questions is 3 | ||
# expect(page).to have_text('(2 / 5)') | ||
end | ||
|
||
# scenario 'Conditions with multiple options selected', :js do | ||
# question = create(:question, :checkbox, section: @section, options: 4) | ||
# dependent_question = create(:question, :textfield, section: @section) | ||
|
||
# options = question.question_options | ||
|
||
# create(:condition, question: question, | ||
# option_list: [options[0].id, options[1].id], | ||
# action_type: 'remove', | ||
# remove_data: [dependent_question.id]) | ||
|
||
# visit edit_plan_path(@plan) | ||
|
||
# find("#section-panel-#{@section.id}").click | ||
|
||
# within("#answer-form-#{question.id}") do | ||
# check options[0].text | ||
# check options[2].text | ||
# click_button 'Save' | ||
# end | ||
|
||
# expect(page).to have_selector("#answer-form-#{dependent_question.id}") | ||
|
||
# within("#answer-form-#{question.id}") do | ||
# check options[1].text | ||
# click_button 'Save' | ||
# end | ||
|
||
# expect(page).not_to have_selector("#answer-form-#{dependent_question.id}") | ||
# end | ||
|
||
# scenario 'User answers question', :js do | ||
# # Setup | ||
# visit overview_plan_path(@plan) | ||
|
||
# # Action | ||
# click_link 'Write plan' | ||
|
||
# # Expectations | ||
# expect(current_path).to eql(edit_plan_path(@plan)) | ||
# expect(page).to have_text('(4 / 5)') | ||
|
||
# # # Action | ||
# find("#section-panel-#{@section.id}").click | ||
# # # Fill in the answer form... | ||
# within("#answer-form-#{@questions[0].id}") do | ||
# check @questions[0].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 |
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