From c61946c2c9988172e3d6aaa497832304952a4d69 Mon Sep 17 00:00:00 2001 From: Joonas Date: Thu, 11 Apr 2024 16:12:32 +0300 Subject: [PATCH] Add specs for missing map configurations --- .../forms/admin/questionnaires/_form.html.erb | 16 +- .../admin/admin_creates_questionnaire_spec.rb | 167 ++++++++++-------- .../spec/system/admin/map_render_spec.rb | 36 +++- .../spec/system/map_render_spec.rb | 36 +++- .../proposals/admin/proposals/_form.html.erb | 14 +- .../proposals/_edit_form_fields.html.erb | 14 +- .../spec/system/admin/map_render_spec.rb | 75 +++++--- .../spec/system/map_render_spec.rb | 77 +++++--- 8 files changed, 291 insertions(+), 144 deletions(-) diff --git a/decidim-module-forms_locations/app/views/decidim/forms/admin/questionnaires/_form.html.erb b/decidim-module-forms_locations/app/views/decidim/forms/admin/questionnaires/_form.html.erb index 1a8aec0..c40fc9b 100644 --- a/decidim-module-forms_locations/app/views/decidim/forms/admin/questionnaires/_form.html.erb +++ b/decidim-module-forms_locations/app/views/decidim/forms/admin/questionnaires/_form.html.erb @@ -121,14 +121,6 @@ <% end %> -<%= javascript_pack_tag "decidim_forms_admin", defer: false %> - -<% if questionnaire.questions_editable? %> - -<% end %> - <% if Decidim::Map.autocomplete(organization: current_organization).present? %>
<%= cell( @@ -141,3 +133,11 @@ ) %>
<% end %> + +<%= javascript_pack_tag "decidim_forms_admin", defer: false %> + +<% if questionnaire.questions_editable? %> + +<% end %> diff --git a/decidim-module-forms_locations/spec/system/admin/admin_creates_questionnaire_spec.rb b/decidim-module-forms_locations/spec/system/admin/admin_creates_questionnaire_spec.rb index e1951ab..ac1958c 100644 --- a/decidim-module-forms_locations/spec/system/admin/admin_creates_questionnaire_spec.rb +++ b/decidim-module-forms_locations/spec/system/admin/admin_creates_questionnaire_spec.rb @@ -19,96 +19,113 @@ let!(:survey) { create :survey, questionnaire: questionnaire, component: component } let!(:question) { create :questionnaire_question, questionnaire: questionnaire } - before do - utility = Decidim::Map.autocomplete(organization: organization) - allow(Decidim::Map).to receive(:autocomplete).with(organization: organization).and_return(utility) - allow(utility).to receive(:builder_options).and_return( - api_key: "key1234" - ) - - switch_to_host(organization.host) - login_as user, scope: :user - visit questionnaire_edit_path - end + context "when map is provided" do + before do + switch_to_host(organization.host) + login_as user, scope: :user + visit questionnaire_edit_path + end - context "when question type map_locations" do - context "when question added with \"multiple\" -map configuration to survey" do - it "allows creation of survey" do - find(".question--collapse", match: :first).click - check "Mandatory" - select("Map", from: "Type").select_option - expect(page).to have_field("Map config", with: "multiple") - fill_in "Default latitude", with: "11" - fill_in "Default longitude", with: "13" - click_button "Save" - - expect(page).to have_content("Survey successfully saved.") + context "when question type map_locations" do + context "when question added with \"multiple\" -map configuration to survey" do + it "allows creation of survey" do + find(".question--collapse", match: :first).click + check "Mandatory" + select("Map", from: "Type").select_option + expect(page).to have_field("Map config", with: "multiple") + fill_in "Default latitude", with: "11" + fill_in "Default longitude", with: "13" + click_button "Save" + + expect(page).to have_content("Survey successfully saved.") + end end - end - context "when question added with \"single\" -map configuration to survey" do - it "allows creation of survey" do - find(".question--collapse", match: :first).click - check "Mandatory" - select("Map", from: "Type").select_option - select("Single", from: "Map config") - expect(page).to have_field("Map config", with: "single") - fill_in "Default latitude", with: "11" - fill_in "Default longitude", with: "13" - click_button "Save" - - expect(page).to have_content("Survey successfully saved.") + context "when question added with \"single\" -map configuration to survey" do + it "allows creation of survey" do + find(".question--collapse", match: :first).click + check "Mandatory" + select("Map", from: "Type").select_option + select("Single", from: "Map config") + expect(page).to have_field("Map config", with: "single") + fill_in "Default latitude", with: "11" + fill_in "Default longitude", with: "13" + click_button "Save" + + expect(page).to have_content("Survey successfully saved.") + end end end - end - context "when question type select_locations" do - context "when question added with 2 answer options" do - it "allows creation of survey" do - find(".question--collapse", match: :first).click - select("Select locations", from: "Type").select_option - expect(page).to have_content("Answer option") - answer_options = all(".questionnaire-question-location-option") - - answer_options.each do |option| - within(option) do - fill_in find("input", match: :first)["name"], with: "Lauttasaari" - fill_in "Geojson", match: :first, with: '{"type":"Feature","geometry":{"type":"Point","coordinates":[12,5]}}' + context "when question type select_locations" do + context "when question added with 2 answer options" do + it "allows creation of survey" do + find(".question--collapse", match: :first).click + select("Select locations", from: "Type").select_option + scroll_to(:bottom) + expect(page).to have_content("Add answer option") + expect(page).to have_content("Answer option") + answer_options = all(".questionnaire-question-location-option") + + answer_options.each do |option| + within(option) do + fill_in find("input", match: :first)["name"], with: "Lauttasaari" + fill_in "Geojson", match: :first, with: '{"type":"Feature","geometry":{"type":"Point","coordinates":[12,5]}}' + end end - end - click_button "Save" + click_button "Save" - expect(page).to have_content("Survey successfully saved.") - expect(Decidim::Forms::Question.first.answer_options.count).to eq(2) - expect(Decidim::Forms::Question.first.answer_options.first.geojson).to eq( - '{"type":"Feature","geometry":{"type":"Point","coordinates":[12,5]}}' - ) + expect(page).to have_content("Survey successfully saved.") + expect(Decidim::Forms::Question.first.answer_options.count).to eq(2) + expect(Decidim::Forms::Question.first.answer_options.first.geojson).to eq( + '{"type":"Feature","geometry":{"type":"Point","coordinates":[12,5]}}' + ) + end end - end - context "when question added with 3 answer options" do - it "allows creation of survey" do - find(".question--collapse", match: :first).click - select("Select locations", from: "Type").select_option - expect(page).to have_content("Answer option") - click_button "Add answer option" - answer_options = all(".questionnaire-question-location-option") - - answer_options.each do |option| - within(option) do - fill_in find("input", match: :first)["name"], with: "Lauttasaari" - fill_in "Geojson", match: :first, with: '{"type":"Feature","geometry":{"type":"Point","coordinates":[12,5]}}' + context "when question added with 3 answer options" do + it "allows creation of survey" do + find(".question--collapse", match: :first).click + select("Select locations", from: "Type").select_option + expect(page).to have_content("Answer option") + click_button "Add answer option" + answer_options = all(".questionnaire-question-location-option") + + answer_options.each do |option| + within(option) do + fill_in find("input", match: :first)["name"], with: "Lauttasaari" + fill_in "Geojson", match: :first, with: '{"type":"Feature","geometry":{"type":"Point","coordinates":[12,5]}}' + end end + + click_button "Save" + + expect(page).to have_content("Survey successfully saved.") + expect(Decidim::Forms::Question.first.answer_options.count).to eq(3) + expect(Decidim::Forms::Question.first.answer_options.first.geojson).to eq( + '{"type":"Feature","geometry":{"type":"Point","coordinates":[12,5]}}' + ) end + end + end + end - click_button "Save" + context "when map is not provided" do + before do + allow(Decidim::Map).to receive(:autocomplete) - expect(page).to have_content("Survey successfully saved.") - expect(Decidim::Forms::Question.first.answer_options.count).to eq(3) - expect(Decidim::Forms::Question.first.answer_options.first.geojson).to eq( - '{"type":"Feature","geometry":{"type":"Point","coordinates":[12,5]}}' - ) + switch_to_host(organization.host) + login_as user, scope: :user + visit questionnaire_edit_path + end + + context "when selecting a question type" do + it "doesn't allow picking 'Map locations' or 'Select locations'" do + find(".question--collapse", match: :first).click + select_element = find("#questionnaire_questions_#{question.id}_question_type") + expect(select_element).to have_selector("option[value='select_locations'][disabled]") + expect(select_element).to have_selector("option[value='map_locations'][disabled]") end end end diff --git a/decidim-module-meetings_locations/spec/system/admin/map_render_spec.rb b/decidim-module-meetings_locations/spec/system/admin/map_render_spec.rb index e8ecdc3..655ad3d 100644 --- a/decidim-module-meetings_locations/spec/system/admin/map_render_spec.rb +++ b/decidim-module-meetings_locations/spec/system/admin/map_render_spec.rb @@ -10,10 +10,31 @@ before do switch_to_host(organization.host) login_as user, scope: :user + visit decidim.root_path + find("nav.topbar__dropmenu").click + click_link "Admin dashboard" + click_link "Processes" + find(".action-icon--new").click + click_link "Meetings" + click_link "New meeting" end - context "when creating a meeting" do - it "allows adding locations with a map" do + context "when map provided" do + context "when creating a meeting" do + it "allows adding locations with a map" do + expect(page).to have_selector("[data-decidim-map]", visible: :hidden) + find("#meeting_type_of_meeting").find("option[value='in_person']").click + expect(page).to have_selector("[data-decidim-map]") + end + end + end + + context "when map not provided" do + before do + allow(Decidim::Map).to receive(:autocomplete) + + switch_to_host(organization.host) + login_as user, scope: :user visit decidim.root_path find("nav.topbar__dropmenu").click click_link "Admin dashboard" @@ -21,9 +42,14 @@ find(".action-icon--new").click click_link "Meetings" click_link "New meeting" - expect(page).to have_selector("[data-decidim-map]", visible: :hidden) - find("#meeting_type_of_meeting").find("option[value='in_person']").click - expect(page).to have_selector("[data-decidim-map]") + end + + context "when creating a meeting" do + it "renders an address field instead of a map" do + find("#meeting_type_of_meeting").find("option[value='in_person']").click + expect(page).not_to have_selector("[data-decidim-map]") + expect(page).to have_selector("#meeting_address") + end end end end diff --git a/decidim-module-meetings_locations/spec/system/map_render_spec.rb b/decidim-module-meetings_locations/spec/system/map_render_spec.rb index 09987fb..4268470 100644 --- a/decidim-module-meetings_locations/spec/system/map_render_spec.rb +++ b/decidim-module-meetings_locations/spec/system/map_render_spec.rb @@ -10,18 +10,44 @@ before do switch_to_host(organization.host) login_as user, scope: :user + + visit decidim.root_path + click_link "Processes" + find(".card__link").click + click_link "Meetings" + click_link "New meeting" + end + + context "when map provided" do + context "when creating a meeting" do + it "allows adding locations with a map" do + expect(page).to have_selector("[data-decidim-map]", visible: :hidden) + find("#meeting_type_of_meeting").find("option[value='in_person']").click + expect(page).to have_selector("[data-decidim-map]") + end + end end - context "when creating a meeting" do - it "allows adding locations with a map" do + context "when map not provided" do + before do + allow(Decidim::Map).to receive(:autocomplete) + + switch_to_host(organization.host) + login_as user, scope: :user + visit decidim.root_path click_link "Processes" find(".card__link").click click_link "Meetings" click_link "New meeting" - expect(page).to have_selector("[data-decidim-map]", visible: :hidden) - find("#meeting_type_of_meeting").find("option[value='in_person']").click - expect(page).to have_selector("[data-decidim-map]") + end + + context "when creating a meeting" do + it "renders an address field instead of a map" do + find("#meeting_type_of_meeting").find("option[value='in_person']").click + expect(page).not_to have_selector("[data-decidim-map]") + expect(page).to have_selector("#meeting_address") + end end end end diff --git a/decidim-module-proposals_locations/app/views/decidim/proposals/admin/proposals/_form.html.erb b/decidim-module-proposals_locations/app/views/decidim/proposals/admin/proposals/_form.html.erb index 856040d..6f81593 100644 --- a/decidim-module-proposals_locations/app/views/decidim/proposals/admin/proposals/_form.html.erb +++ b/decidim-module-proposals_locations/app/views/decidim/proposals/admin/proposals/_form.html.erb @@ -53,7 +53,19 @@ <% if @form.geocoding_enabled? %>
- <%= cell("decidim/locations/locations", @proposal, form: form, coords: [current_component.settings.default_latitude, current_component.settings.default_longitude], checkbox: true) %> + <% if Decidim::Map.autocomplete(organization: current_organization).present? %> + <%= cell("decidim/locations/locations", + @proposal, + form: form, + coords: [ + current_component.settings.default_latitude, + current_component.settings.default_longitude + ], + checkbox: true + ) %> + <% else %> + <%= form.geocoding_field :address %> + <% end %>
<% end %> diff --git a/decidim-module-proposals_locations/app/views/decidim/proposals/proposals/_edit_form_fields.html.erb b/decidim-module-proposals_locations/app/views/decidim/proposals/proposals/_edit_form_fields.html.erb index fc9928f..5aec1c8 100644 --- a/decidim-module-proposals_locations/app/views/decidim/proposals/proposals/_edit_form_fields.html.erb +++ b/decidim-module-proposals_locations/app/views/decidim/proposals/proposals/_edit_form_fields.html.erb @@ -32,7 +32,19 @@ <% end %> <% if @form.geocoding_enabled? %> - <%= cell("decidim/locations/locations", @proposal, form: form, coords: [current_component.settings.default_latitude, current_component.settings.default_longitude], checkbox: true) %> + <% if Decidim::Map.autocomplete(organization: current_organization).present? %> + <%= cell("decidim/locations/locations", + @proposal, + form: form, + coords: [ + current_component.settings.default_latitude, + current_component.settings.default_longitude + ], + checkbox: true + ) %> + <% else %> + <%= form.check_box :has_address, checked: form_has_address? %> + <% end %> <% end %> <% if @form.categories&.any? %> diff --git a/decidim-module-proposals_locations/spec/system/admin/map_render_spec.rb b/decidim-module-proposals_locations/spec/system/admin/map_render_spec.rb index c6dc2c5..950a437 100644 --- a/decidim-module-proposals_locations/spec/system/admin/map_render_spec.rb +++ b/decidim-module-proposals_locations/spec/system/admin/map_render_spec.rb @@ -7,35 +7,62 @@ let(:organization) { user.organization } let!(:proposal_component) { create(:proposal_component, :with_geocoding_enabled, :with_creation_enabled, :published, organization: organization) } - before do - switch_to_host(organization.host) - login_as user, scope: :user - - visit decidim.root_path - find("nav.topbar__dropmenu").click - click_link "Admin dashboard" - click_link "Processes" - find(".action-icon--new").click - click_link "Proposals" - click_link "New proposal" - end - context "when creating a proposal, geocoding enabled" do - it "allows adding locations with a map" do - expect(page).to have_selector("#proposal_has_location") - expect(page).to have_selector("[data-decidim-map]", visible: :hidden) - find("#proposal_has_location").click - expect(page).to have_selector("[data-decidim-map]") + context "when map provided" do + before do + switch_to_host(organization.host) + login_as user, scope: :user + + visit decidim.root_path + find("nav.topbar__dropmenu").click + click_link "Admin dashboard" + click_link "Processes" + find(".action-icon--new").click + click_link "Proposals" + click_link "New proposal" + end + + context "when creating a proposal, geocoding enabled" do + it "allows adding locations with a map" do + expect(page).to have_selector("#proposal_has_location") + expect(page).to have_selector("[data-decidim-map]", visible: :hidden) + find("#proposal_has_location").click + expect(page).to have_selector("[data-decidim-map]") + end + end + + context "when creating a proposal, geocoding disabled" do + let!(:proposal_component) { create(:proposal_component, :with_creation_enabled, :published, organization: organization) } + + it "doesn't allow adding locations" do + expect(page).not_to have_selector("#proposal_has_location") + expect(page).not_to have_selector("[data-decidim-map]", visible: :hidden) + expect(page).not_to have_selector("[data-decidim-map]") + end end end - context "when creating a proposal, geocoding disabled" do - let!(:proposal_component) { create(:proposal_component, :with_creation_enabled, :published, organization: organization) } + context "when map not provided" do + before do + allow(Decidim::Map).to receive(:autocomplete) + + switch_to_host(organization.host) + login_as user, scope: :user + + visit decidim.root_path + find("nav.topbar__dropmenu").click + click_link "Admin dashboard" + click_link "Processes" + find(".action-icon--new").click + click_link "Proposals" + click_link "New proposal" + end - it "doesn't allow adding locations" do - expect(page).not_to have_selector("#proposal_has_location") - expect(page).not_to have_selector("[data-decidim-map]", visible: :hidden) - expect(page).not_to have_selector("[data-decidim-map]") + context "when creating a proposal, geocoding enabled" do + it "doesn't render map" do + expect(page).not_to have_selector("#proposal_has_location") + expect(page).not_to have_selector("[data-decidim-map]", visible: :all) + end end end end diff --git a/decidim-module-proposals_locations/spec/system/map_render_spec.rb b/decidim-module-proposals_locations/spec/system/map_render_spec.rb index 76b1279..5c1c338 100644 --- a/decidim-module-proposals_locations/spec/system/map_render_spec.rb +++ b/decidim-module-proposals_locations/spec/system/map_render_spec.rb @@ -7,36 +7,63 @@ let(:organization) { user.organization } let!(:proposal_component) { create(:proposal_component, :with_geocoding_enabled, :with_creation_enabled, :published, organization: organization) } - before do - switch_to_host(organization.host) - login_as user, scope: :user - - visit decidim.root_path - click_link "Processes" - find(".card__link").click - click_link "Proposals" - click_link "New proposal" - fill_in "Title", with: "Example proposal for test" - fill_in "Body", with: "Example body text for test" - click_button "Continue" - end + context "when map provided" do + before do + switch_to_host(organization.host) + login_as user, scope: :user + + visit decidim.root_path + click_link "Processes" + find(".card__link").click + click_link "Proposals" + click_link "New proposal" + fill_in "Title", with: "Example proposal for test" + fill_in "Body", with: "Example body text for test" + click_button "Continue" + end - context "when creating a proposal, geocoding enabled" do - it "allows adding locations with a map" do - expect(page).to have_selector("#proposal_has_location") - expect(page).to have_selector("[data-decidim-map]", visible: :hidden) - find("#proposal_has_location").click - expect(page).to have_selector("[data-decidim-map]") + context "when creating a proposal, geocoding enabled" do + it "allows adding locations with a map" do + expect(page).to have_selector("#proposal_has_location") + expect(page).to have_selector("[data-decidim-map]", visible: :hidden) + find("#proposal_has_location").click + expect(page).to have_selector("[data-decidim-map]") + end + end + + context "when creating a proposal, geocoding disabled" do + let!(:proposal_component) { create(:proposal_component, :with_creation_enabled, :published, organization: organization) } + + it "doesn't allow adding locations" do + expect(page).not_to have_selector("#proposal_has_location") + expect(page).not_to have_selector("[data-decidim-map]", visible: :hidden) + expect(page).not_to have_selector("[data-decidim-map]") + end end end - context "when creating a proposal, geocoding disabled" do - let!(:proposal_component) { create(:proposal_component, :with_creation_enabled, :published, organization: organization) } + context "when map not provided" do + before do + allow(Decidim::Map).to receive(:autocomplete) + + switch_to_host(organization.host) + login_as user, scope: :user + + visit decidim.root_path + click_link "Processes" + find(".card__link").click + click_link "Proposals" + click_link "New proposal" + fill_in "Title", with: "Example proposal for test" + fill_in "Body", with: "Example body text for test" + click_button "Continue" + end - it "doesn't allow adding locations" do - expect(page).not_to have_selector("#proposal_has_location") - expect(page).not_to have_selector("[data-decidim-map]", visible: :hidden) - expect(page).not_to have_selector("[data-decidim-map]") + context "when creating a proposal, geocoding enabled" do + it "doesn't render a map" do + expect(page).not_to have_selector("#proposal_has_location") + expect(page).not_to have_selector("[data-decidim-map]", visible: :all) + end end end end