Share translatable specs

This commit is contained in:
Javi Martín
2018-08-28 17:44:46 +02:00
parent 726110c91e
commit 612fdb09dd
4 changed files with 126 additions and 190 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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