From 51c53b6f31d891eaab554251bd9bc1db1aa7bb5f Mon Sep 17 00:00:00 2001 From: taitus Date: Wed, 13 Sep 2017 16:19:40 +0200 Subject: [PATCH] Add missing specs from Maps on New, Edit and Show. --- app/assets/javascripts/map.js.coffee | 2 +- app/helpers/map_locations_helper.rb | 3 +- app/models/map_location.rb | 2 +- app/views/map_locations/_form_fields.html.erb | 5 +- db/seeds.rb | 2 +- spec/factories.rb | 4 +- spec/features/budgets/investments_spec.rb | 8 + spec/features/proposals_spec.rb | 39 ++++ spec/shared/features/documentable.rb | 1 - spec/shared/features/mapeable.rb | 220 ++++++++++++++++++ 10 files changed, 278 insertions(+), 8 deletions(-) create mode 100644 spec/shared/features/mapeable.rb diff --git a/app/assets/javascripts/map.js.coffee b/app/assets/javascripts/map.js.coffee index 53c346c9e..3f28024a7 100644 --- a/app/assets/javascripts/map.js.coffee +++ b/app/assets/javascripts/map.js.coffee @@ -75,4 +75,4 @@ App.Map = if editable $(removeMarkerSelector).on 'click', removeMarker map.on 'zoomend', updateFormfields - map.on 'click', moveOrPlaceMarker \ No newline at end of file + map.on 'click', moveOrPlaceMarker diff --git a/app/helpers/map_locations_helper.rb b/app/helpers/map_locations_helper.rb index 26766e96d..ae45e7c1d 100644 --- a/app/helpers/map_locations_helper.rb +++ b/app/helpers/map_locations_helper.rb @@ -37,6 +37,7 @@ module MapLocationsHelper content_tag :div, class: "text-right" do content_tag :a, id: map_location_remove_marker_link_id(map_location), + href: "#", class: "location-map-remove-marker-button delete" do text end @@ -63,4 +64,4 @@ module MapLocationsHelper } end -end \ No newline at end of file +end diff --git a/app/models/map_location.rb b/app/models/map_location.rb index 6635bf995..14e91d92c 100644 --- a/app/models/map_location.rb +++ b/app/models/map_location.rb @@ -1,7 +1,7 @@ class MapLocation < ActiveRecord::Base belongs_to :proposal - belongs_to :investment + belongs_to :investment, class_name: Budget::Investment def available? latitude.present? && longitude.present? && zoom.present? diff --git a/app/views/map_locations/_form_fields.html.erb b/app/views/map_locations/_form_fields.html.erb index 95e0331ae..39c87c7e4 100644 --- a/app/views/map_locations/_form_fields.html.erb +++ b/app/views/map_locations/_form_fields.html.erb @@ -4,6 +4,9 @@ <%= render_map(map_location, parent_class, editable = true, remove_marker_label) %> <%= form.fields_for :map_location, map_location do |m_l_fields| %> + <%= m_l_fields.hidden_field :id, + value: map_location.id, + id: map_location_input_id(parent_class, 'id') %> <%= m_l_fields.hidden_field :latitude, value: map_location.latitude, id: map_location_input_id(parent_class, 'latitude') %> @@ -13,4 +16,4 @@ <%= m_l_fields.hidden_field :zoom, value: map_location.zoom, id: map_location_input_id(parent_class, 'zoom') %> -<% end %> \ No newline at end of file +<% end %> diff --git a/db/seeds.rb b/db/seeds.rb index a2f25e340..ee444bd91 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -80,7 +80,7 @@ Setting['feature.budgets'] = true Setting['feature.signature_sheets'] = true Setting['feature.legislation'] = true Setting['feature.community'] = true -Setting['feature.map'] = true +Setting['feature.map'] = nil # Spending proposals feature flags Setting['feature.spending_proposal_features.voting_allowed'] = nil diff --git a/spec/factories.rb b/spec/factories.rb index 3fc3d7c36..b0a287aea 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -849,8 +849,8 @@ LOREM_IPSUM proposal end - trait :investment_map_location do - investment + trait :budget_investment_map_location do + association :investment, factory: :budget_investment end end diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index f54b2e3c4..6d487772e 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -485,6 +485,14 @@ feature 'Budget Investments' do "Create Investment", "Budget Investment created successfully." + it_behaves_like "mapeable", + "budget_investment", + "investment", + "new_budget_investment_path", + "", + "budget_investment_path", + { "budget_id": "budget_id" } + context "Destroy" do scenario "Admin cannot destroy budget investments" do diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb index 0c3481fa1..53af5d6ac 100644 --- a/spec/features/proposals_spec.rb +++ b/spec/features/proposals_spec.rb @@ -1328,6 +1328,14 @@ feature 'Proposals' do "Save changes", "Proposal updated successfully" + it_behaves_like "mapeable", + "proposal", + "proposal", + "new_proposal_path", + "edit_proposal_path", + "proposal_path", + { } + scenario 'Erased author' do user = create(:user) proposal = create(:proposal, author: user) @@ -1533,6 +1541,37 @@ feature 'Proposals' do end + scenario 'Add Map on proposal' do + author = create(:user) + login_as(author) + + visit new_proposal_path + fill_in 'proposal_title', with: 'Help refugees' + fill_in 'proposal_question', with: '¿Would you like to give assistance to war refugees?' + fill_in 'proposal_summary', with: 'In summary, what we want is...' + check 'proposal_terms_of_service' + + click_button 'Create proposal' + + expect(page).to have_content 'Proposal created successfully.' + expect(page).to have_content 'Help refugees' + expect(page).not_to have_content 'You can also see more information about improving your campaign' + + click_link 'Not now, go to my proposal' + + expect(page).to have_content 'Help refugees' + expect(page).to have_content '¿Would you like to give assistance to war refugees?' + expect(page).to have_content 'In summary, what we want is...' + expect(page).to have_content 'This is very important because...' + expect(page).to have_content 'http://rescue.org/refugees' + expect(page).to have_content 'http://youtube.com' + expect(page).to have_content author.name + expect(page).to have_content 'Refugees' + expect(page).to have_content 'Solidarity' + expect(page).to have_content I18n.l(Proposal.last.created_at.to_date) + end + + end feature 'Successful proposals' do diff --git a/spec/shared/features/documentable.rb b/spec/shared/features/documentable.rb index 126573348..2bd9d8f86 100644 --- a/spec/shared/features/documentable.rb +++ b/spec/shared/features/documentable.rb @@ -5,7 +5,6 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path, let!(:user) { create(:user) } let!(:arguments) { {} } let!(:documentable) { create(documentable_factory_name, author: user) } - let!(:documentable_dom_name) { documentable_factory_name.parameterize } before do create(:administrator, user: administrator) diff --git a/spec/shared/features/mapeable.rb b/spec/shared/features/mapeable.rb new file mode 100644 index 000000000..1ed69d049 --- /dev/null +++ b/spec/shared/features/mapeable.rb @@ -0,0 +1,220 @@ +shared_examples "mapeable" do |mapeable_factory_name, mapeable_association_name, mapeable_new_path, mapeable_edit_path, mapeable_show_path, mapeable_path_arguments| + + include ActionView::Helpers + + let!(:user) { create(:user, :level_two) } + + before do + Setting['feature.map'] = true + end + + describe "At #{mapeable_new_path}" do + + let!(:arguments) { {} } + let!(:mapeable) { create("#{mapeable_factory_name}".to_sym) } + let!(:map_location) { create(:map_location, "#{mapeable_factory_name}_map_location".to_sym, "#{mapeable_association_name}": mapeable) } + + before { set_arguments(arguments, mapeable, mapeable_path_arguments) } + + scenario "Should not show marker by default on create #{mapeable_factory_name}", :js do + login_as user + visit send(mapeable_new_path, arguments) + + send("fill_in_#{mapeable_factory_name}_form") + + within ".map_location" do + expect(page).not_to have_css(".map-icon") + end + end + + scenario "Should show marker on create #{mapeable_factory_name} when click on map", :js do + login_as user + visit send(mapeable_new_path, arguments) + + send("fill_in_#{mapeable_factory_name}_form") + find("#new_map_location").click + + within ".map_location" do + expect(page).to have_css(".map-icon") + end + end + + scenario "Should create #{mapeable_factory_name} with map", :js do + login_as user + visit send(mapeable_new_path, arguments) + + send("fill_in_#{mapeable_factory_name}_form") + find("#new_map_location").click + send("submit_#{mapeable_factory_name}_form") + + expect(page).to have_css(".map_location") + end + + scenario "Can not display map on #{mapeable_factory_name} when not fill marker on map", :js do + login_as user + visit send(mapeable_new_path, arguments) + + send("fill_in_#{mapeable_factory_name}_form") + expect(page).to have_css ".map_location" + send("submit_#{mapeable_factory_name}_form") + + expect(page).not_to have_css(".map_location") + end + + scenario "Can not display map on #{mapeable_factory_name} when feature.map is disabled", :js do + Setting['feature.map'] = false + login_as user + visit send(mapeable_new_path, arguments) + + send("fill_in_#{mapeable_factory_name}_form") + expect(page).not_to have_css ".map_location" + send("submit_#{mapeable_factory_name}_form") + + expect(page).not_to have_css(".map_location") + end + + end + + describe "At #{mapeable_edit_path}" do + + let!(:mapeable) { create("#{mapeable_factory_name}".to_sym) } + let!(:map_location) { create(:map_location, "#{mapeable_factory_name}_map_location".to_sym, "#{mapeable_association_name}": mapeable) } + + before { skip } unless mapeable_edit_path.present? + + scenario "Should edit map on #{mapeable_factory_name} and contain default values", :js do + login_as mapeable.author + + visit send(mapeable_edit_path, id: mapeable.id) + + expect(page).to have_content "Navigate the map to the location and place the marker." + validate_latitude_longitude(mapeable_factory_name) + end + + scenario "Should edit default values from map on #{mapeable_factory_name} edit page", :js do + login_as mapeable.author + + visit send(mapeable_edit_path, id: mapeable.id) + find(".map_location").click + click_on("Save changes") + mapeable.reload + + expect(page).to have_css(".map_location") + expect(page).not_to have_selector(".map_location[data-marker-latitude='#{map_location.latitude}']") + expect(page).to have_selector(".map_location[data-marker-latitude='#{mapeable.map_location.latitude}']") + end + + scenario "Should edit mapeable on #{mapeable_factory_name} without change map", :js do + login_as mapeable.author + + visit send(mapeable_edit_path, id: mapeable.id) + fill_in "#{mapeable_factory_name}_title", with: "New title" + click_on("Save changes") + mapeable.reload + + expect(page).to have_css(".map_location") + expect(page).to have_selector(".map_location[data-marker-latitude='#{map_location.latitude}']") + expect(page).to have_selector(".map_location[data-marker-latitude='#{mapeable.map_location.latitude}']") + end + + scenario "Can not display map on #{mapeable_factory_name} edit when remove map marker", :js do + login_as mapeable.author + + visit send(mapeable_edit_path, id: mapeable.id) + click_link "Remove map marker" + click_on "Save changes" + + expect(page).not_to have_css(".map_location") + end + + scenario "Can not display map on #{mapeable_factory_name} edit when feature.map is disabled", :js do + Setting['feature.map'] = false + login_as mapeable.author + + visit send(mapeable_edit_path, id: mapeable.id) + fill_in "#{mapeable_factory_name}_title", with: "New title" + click_on("Save changes") + + expect(page).not_to have_css(".map_location") + end + + end + + describe "At #{mapeable_show_path}" do + + let!(:arguments) { {} } + let!(:mapeable) { create("#{mapeable_factory_name}".to_sym) } + let!(:map_location) { create(:map_location, "#{mapeable_factory_name}_map_location".to_sym, "#{mapeable_association_name}": mapeable) } + + before { set_arguments(arguments, mapeable, mapeable_path_arguments) } + + scenario "Should display map on #{mapeable_factory_name} show page", :js do + arguments.merge!("id": mapeable.id) + + visit send(mapeable_show_path, arguments) + + expect(page).to have_css(".map_location") + end + + scenario "Should not display map on #{mapeable_factory_name} show when marker is not defined", :js do + mapeable_without_map = create("#{mapeable_factory_name}".to_sym) + set_arguments(arguments, mapeable_without_map, mapeable_path_arguments) + arguments.merge!("id": mapeable_without_map.id) + + visit send(mapeable_show_path, arguments) + + expect(page).not_to have_css(".map_location") + end + + scenario "Should not display map on #{mapeable_factory_name} show page when feature.map is disable", :js do + Setting['feature.map'] = false + arguments.merge!("id": mapeable.id) + + visit send(mapeable_show_path, arguments) + + expect(page).not_to have_css(".map_location") + end + + end + +end + +def fill_in_proposal_form + fill_in 'proposal_title', with: 'Help refugees' + fill_in 'proposal_question', with: '¿Would you like to give assistance to war refugees?' + fill_in 'proposal_summary', with: 'In summary, what we want is...' +end + +def submit_proposal_form + check :proposal_terms_of_service + click_button 'Create proposal' + + click_link 'Not now, go to my proposal' +end + +def validate_latitude_longitude(mapeable_factory_name) + expect(find("##{mapeable_factory_name}_map_location_attributes_latitude", visible: false).value).to eq "51.48" + expect(find("##{mapeable_factory_name}_map_location_attributes_longitude", visible: false).value).to eq "0.0" + expect(mapeable.map_location.latitude).to eq 51.48 + expect(mapeable.map_location.longitude).to eq 0.0 +end + +def fill_in_budget_investment_form + page.select mapeable.heading.name_scoped_by_group, from: :budget_investment_heading_id + fill_in :budget_investment_title, with: "Budget investment title" + fill_in_ckeditor "budget_investment_description", with: "Budget investment description" + check :budget_investment_terms_of_service +end + +def submit_budget_investment_form + check :budget_investment_terms_of_service + click_button 'Create Investment' +end + +def set_arguments(arguments, mapeable, mapeable_path_arguments) + if mapeable_path_arguments + mapeable_path_arguments.each do |argument_name, path_to_value| + arguments.merge!("#{argument_name}": mapeable.send(path_to_value)) + end + end +end