Skip to content

Commit

Permalink
Remove duplication for add questions specs examples (#11559)
Browse files Browse the repository at this point in the history
* Remove duplication for add questions specs examples

* Fix typo

Suggested on code view

Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro>

* Add missing `it` block with example

---------

Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro>
  • Loading branch information
andreslucena and alecslupu committed Sep 5, 2023
1 parent f1ed6d6 commit 5db45c0
Showing 1 changed file with 40 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@
require "spec_helper"

shared_examples_for "add questions" do
shared_examples_for "updating the max choices selector according to the configured options" do
it "updates them" do
expect(page).not_to have_select("Maximum number of choices")

select "Multiple option", from: "Type"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2))

click_button "Add answer option"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2 3))

click_button "Add answer option"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2 3 4))

within(".questionnaire-question-answer-option:last-of-type") { click_button "Remove" }
expect(page).to have_select("Maximum number of choices", options: %w(Any 2 3))

within(".questionnaire-question-answer-option:last-of-type") { click_button "Remove" }
expect(page).to have_select("Maximum number of choices", options: %w(Any 2))

click_button "Add question"
expand_all_questions

within(".questionnaire-question:last-of-type") do
select multiple_option_string, from: "Type"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2))

select single_option_string, from: "Type"
expect(page).not_to have_select("Maximum number of choices")
end
end
end

it "adds a few questions and separators to the questionnaire" do
fields_body = ["This is the first question", "This is the second question", "This is the first title and description"]

Expand Down Expand Up @@ -369,6 +401,9 @@
end

context "when adding a multiple option question" do
let(:multiple_option_string) { "Multiple option" }
let(:single_option_string) { "Single option" }

before do
visit questionnaire_edit_path

Expand Down Expand Up @@ -399,38 +434,13 @@
expect(page).to have_selector("input[type=checkbox][id$=_free_text]")
end

it "updates the max choices selector according to the configured options" do
expect(page).not_to have_select("Maximum number of choices")

select "Multiple option", from: "Type"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2))

click_button "Add answer option"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2 3))

click_button "Add answer option"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2 3 4))

within(".questionnaire-question-answer-option:last-of-type") { click_button "Remove" }
expect(page).to have_select("Maximum number of choices", options: %w(Any 2 3))

within(".questionnaire-question-answer-option:last-of-type") { click_button "Remove" }
expect(page).to have_select("Maximum number of choices", options: %w(Any 2))

click_button "Add question"
expand_all_questions

within(".questionnaire-question:last-of-type") do
select "Multiple option", from: "Type"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2))

select "Single option", from: "Type"
expect(page).not_to have_select("Maximum number of choices")
end
end
it_behaves_like "updating the max choices selector according to the configured options"
end

context "when adding a matrix question" do
let(:multiple_option_string) { "Matrix (Multiple option)" }
let(:single_option_string) { "Matrix (Single option)" }

before do
visit questionnaire_edit_path

Expand Down Expand Up @@ -461,34 +471,6 @@
expect(page).to have_selector("input[type=checkbox][id$=_free_text]")
end

it "updates the max choices selector according to the configured options" do
expect(page).not_to have_select("Maximum number of choices")

select "Matrix (Multiple option)", from: "Type"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2))

click_button "Add answer option"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2 3))

click_button "Add answer option"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2 3 4))

within(".questionnaire-question-answer-option:last-of-type") { click_button "Remove" }
expect(page).to have_select("Maximum number of choices", options: %w(Any 2 3))

within(".questionnaire-question-answer-option:last-of-type") { click_button "Remove" }
expect(page).to have_select("Maximum number of choices", options: %w(Any 2))

click_button "Add question"
expand_all_questions

within(".questionnaire-question:last-of-type") do
select "Matrix (Multiple option)", from: "Type"
expect(page).to have_select("Maximum number of choices", options: %w(Any 2))

select "Matrix (Single option)", from: "Type"
expect(page).not_to have_select("Maximum number of choices")
end
end
it_behaves_like "updating the max choices selector according to the configured options"
end
end

0 comments on commit 5db45c0

Please sign in to comment.