From 612fdb09dddeb24c044e88fb9678c6833be1b393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 28 Aug 2018 17:44:46 +0200 Subject: [PATCH] Share translatable specs --- spec/features/admin/banners_spec.rb | 74 ----------- .../budget_investment_milestones_spec.rb | 5 + spec/features/translations_spec.rb | 113 ---------------- spec/shared/features/translatable.rb | 124 +++++++++++++++++- 4 files changed, 126 insertions(+), 190 deletions(-) delete mode 100644 spec/features/translations_spec.rb diff --git a/spec/features/admin/banners_spec.rb b/spec/features/admin/banners_spec.rb index 2d52bb682..6eaaea486 100644 --- a/spec/features/admin/banners_spec.rb +++ b/spec/features/admin/banners_spec.rb @@ -188,78 +188,4 @@ feature 'Admin banners magement' do visit admin_root_path expect(page).not_to have_content 'Ugly banner' end - - context "Translations" do - - let(:banner) { create(:banner, title_en: "Title in English", - title_es: "Título en Español", - target_url: 'http://url.com', - description_en: "Description in English", - description_es: "Descripción en Español") } - - before do - @edit_banner_url = edit_admin_banner_path(banner) - end - - scenario "Add a translation", :js do - visit @edit_banner_url - - select "Français", from: "translation_locale" - fill_in 'banner_title_fr', with: 'Titre en Français' - fill_in 'banner_description_fr', with: 'Description en Français' - - click_button 'Save changes' - - visit @edit_banner_url - expect(page).to have_field('banner_description_en', with: 'Description in English') - - click_link "Español" - expect(page).to have_field('banner_description_es', with: 'Descripción en Español') - - click_link "Français" - expect(page).to have_field('banner_description_fr', with: 'Description en Français') - end - - scenario "Update a translation", :js do - banner.update_attributes(target_url: 'http://www.url.com', - post_started_at: (Time.current - 4.days), - post_ended_at: (Time.current + 10.days)) - - section = create(:web_section, name: 'debates') - create(:banner_section, web_section: section, banner_id: banner.id) - - visit @edit_banner_url - - click_link "Español" - fill_in 'banner_title_es', with: 'Título correcto en Español' - - click_button 'Save changes' - - visit debates_path - - within('.banner') do - expect(page).to have_content("Description in English") - end - - select('Español', from: 'locale-switcher') - - within('.banner') do - expect(page).to have_content('Título correcto en Español') - end - end - - scenario "Remove a translation", :js do - - visit @edit_banner_url - - click_link "Español" - click_link "Remove language" - - expect(page).not_to have_link "Español" - - click_button "Save changes" - visit @edit_banner_url - expect(page).not_to have_link "Español" - end - end end diff --git a/spec/features/admin/budget_investment_milestones_spec.rb b/spec/features/admin/budget_investment_milestones_spec.rb index a4272432d..3b2777792 100644 --- a/spec/features/admin/budget_investment_milestones_spec.rb +++ b/spec/features/admin/budget_investment_milestones_spec.rb @@ -9,6 +9,11 @@ feature 'Admin budget investment milestones' do @investment = create(:budget_investment) end + it_behaves_like "translatable", + "budget_investment_milestone", + "edit_admin_budget_budget_investment_budget_investment_milestone_path", + %w[description] + context "Index" do scenario 'Displaying milestones' do milestone = create(:budget_investment_milestone, investment: @investment) diff --git a/spec/features/translations_spec.rb b/spec/features/translations_spec.rb deleted file mode 100644 index 12bf585c5..000000000 --- a/spec/features/translations_spec.rb +++ /dev/null @@ -1,113 +0,0 @@ -require 'rails_helper' - -feature "Translations" do - - context "Milestones" do - - let(:investment) { create(:budget_investment) } - let(:milestone) { create(:budget_investment_milestone, - investment: investment, - description_en: "Description in English", - description_es: "Descripción en Español") } - - it_behaves_like "translatable", - "budget_investment_milestone", - "edit_admin_budget_budget_investment_budget_investment_milestone_path", - %w[description] - - background do - admin = create(:administrator) - login_as(admin.user) - end - - before do - @edit_milestone_url = edit_admin_budget_budget_investment_budget_investment_milestone_path(investment.budget, investment, milestone) - end - - scenario "Add a translation", :js do - visit @edit_milestone_url - - select "Français", from: "translation_locale" - fill_in 'budget_investment_milestone_description_fr', with: 'Description en Français' - - click_button 'Update milestone' - expect(page).to have_content "Milestone updated successfully" - - visit @edit_milestone_url - expect(page).to have_field('budget_investment_milestone_description_en', with: 'Description in English') - - click_link "Español" - expect(page).to have_field('budget_investment_milestone_description_es', with: 'Descripción en Español') - - click_link "Français" - expect(page).to have_field('budget_investment_milestone_description_fr', with: 'Description en Français') - end - - scenario "Update a translation", :js do - visit @edit_milestone_url - - click_link "Español" - fill_in 'budget_investment_milestone_description_es', with: 'Descripción correcta en Español' - - click_button 'Update milestone' - expect(page).to have_content "Milestone updated successfully" - - visit budget_investment_path(investment.budget, investment) - - click_link("Milestones (1)") - expect(page).to have_content("Description in English") - - select('Español', from: 'locale-switcher') - click_link("Seguimiento (1)") - - expect(page).to have_content("Descripción correcta en Español") - end - - scenario "Remove a translation", :js do - visit @edit_milestone_url - - click_link "Español" - click_link "Remove language" - - expect(page).not_to have_link "Español" - - click_button "Update milestone" - visit @edit_milestone_url - expect(page).not_to have_link "Español" - end - - scenario 'Change value of a translated field to blank' do - visit @edit_milestone_url - - fill_in 'budget_investment_milestone_description_en', with: '' - - click_button "Update milestone" - expect(page).to have_content "Milestone updated successfully" - - expect(page).to have_content "Milestone updated successfully" - expect(page).not_to have_content "Description in English" - end - - scenario "Add a translation for a locale with non-underscored name", :js do - visit @edit_milestone_url - - select "Português", from: "translation_locale" - fill_in 'budget_investment_milestone_description_pt_br', with: 'Description in pt-BR' - - click_button 'Update milestone' - expect(page).to have_content "Milestone updated successfully" - - visit budget_investment_path(investment.budget, investment) - - click_link("Milestones (1)") - expect(page).to have_content("Description in English") - - select('Português', from: 'locale-switcher') - click_link("Milestones (1)") - - expect(page).to have_content('Description in pt-BR') - end - - end - -end diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb index 5ce91d415..d3fb86477 100644 --- a/spec/shared/features/translatable.rb +++ b/spec/shared/features/translatable.rb @@ -1,5 +1,13 @@ shared_examples "translatable" do |factory_name, path_name, fields| - let(:language_texts) { { es: "en español", en: "in English", fr: "en Français" } } + let(:language_texts) do + { + es: "en español", + en: "in English", + fr: "en Français", + "pt-BR": "Português" + } + end + let(:translatable_class) { build(factory_name).class } let(:attributes) do @@ -12,6 +20,105 @@ shared_examples "translatable" do |factory_name, path_name, fields| let(:path) { send(path_name, *resource_hierarchy_for(translatable)) } before { login_as(create(:administrator).user) } + context "Manage translations" do + before do + if translatable_class.name == "I18nContent" + skip "Translation handling is different for site customizations" + end + end + + scenario "Add a translation", :js do + visit path + + select "Français", from: "translation_locale" + + fields.each do |field| + fill_in field_for(field, :fr), with: text_for(field, :fr) + end + + click_button update_button_text + + visit path + field = fields.sample + + expect(page).to have_field(field_for(field, :en), with: text_for(field, :en)) + + click_link "Español" + expect(page).to have_field(field_for(field, :es), with: text_for(field, :es)) + + click_link "Français" + expect(page).to have_field(field_for(field, :fr), with: text_for(field, :fr)) + end + + scenario "Update a translation", :js do + visit path + + click_link "Español" + field = fields.sample + + fill_in field_for(field, :es), with: "Corrección de #{text_for(field, :es)}" + + click_button update_button_text + + visit path + + expect(page).to have_content(text_for(field, :en)) + + select('Español', from: 'locale-switcher') + + expect(page).to have_content("Corrección de #{text_for(field, :es)}") + end + + scenario "Remove a translation", :js do + visit path + + click_link "Español" + click_link "Remove language" + + expect(page).not_to have_link "Español" + + click_button update_button_text + + visit path + expect(page).not_to have_link "Español" + end + + scenario 'Change value of a translated field to blank' do + possible_blanks = fields.select do |field| + translatable.dup.tap { |duplicate| duplicate.send(:"#{field}=", '') }.valid? + end + + skip("can't have translatable blank fields") if possible_blanks.empty? + + field = possible_blanks.sample + + visit path + expect(page).to have_content text_for(field, :en) + + fill_in field_for(field, :en), with: '' + click_button update_button_text + + visit path + expect(page).not_to have_content text_for(field, :en) + end + + scenario "Add a translation for a locale with non-underscored name", :js do + visit path + field = fields.sample + + select "Português", from: "translation_locale" + fill_in field_for(field, :pt_br), with: text_for(field, :"pt-BR") + + click_button update_button_text + + visit path + + select('Português', from: 'locale-switcher') + + expect(page).to have_content(text_for(field, :"pt-BR")) + end + end + context "Globalize javascript interface" do scenario "Highlight current locale", :js do visit path @@ -35,7 +142,7 @@ shared_examples "translatable" do |factory_name, path_name, fields| scenario "Show selected locale form", :js do visit path - field = fields.last # TODO: not sure about first, last, or sample + field = fields.sample expect(page).to have_field(field_for(field, :en), with: text_for(field, :en)) @@ -53,7 +160,7 @@ shared_examples "translatable" do |factory_name, path_name, fields| click_link "Français" - expect(page).to have_field(field_for(fields.last, :fr)) + expect(page).to have_field(field_for(fields.sample, :fr)) end end end @@ -71,3 +178,14 @@ def field_for(field, locale) "#{translatable_class.model_name.singular}_#{field}_#{locale}" end end + +# FIXME: button texts should be consistent. Right now, buttons don't +# even share the same colour. +def update_button_text + case translatable_class.name + when "Budget::Investment::Milestone" + "Update milestone" + else + "Save changes" + end +end