-
Notifications
You must be signed in to change notification settings - Fork 110
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 1, 2024
1 parent
e4d6f60
commit 3d824a8
Showing
3 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
272 changes: 272 additions & 0 deletions
272
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,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 |
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