-
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.
Creating a test functionality that answers of questions removed when …
…a conditional question is removed.
- Loading branch information
John Pinto
committed
Aug 15, 2024
1 parent
e4d6f60
commit 78e372f
Showing
6 changed files
with
227 additions
and
3 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
105 changes: 105 additions & 0 deletions
105
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,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 |
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,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 |
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