From ec6a3652279423627bc73574bd27bcc44d0cdd32 Mon Sep 17 00:00:00 2001 From: Julius Pabrinkis Date: Thu, 8 Jun 2017 22:36:03 +0100 Subject: [PATCH] Add test coverage for switching language in darkswarm and admin --- config/environments/test.rb | 1 + spec/features/admin/multilingual_spec.rb | 40 +++++++++++++++++++++ spec/features/consumer/multilingual_spec.rb | 32 +++++++++++++++++ spec/support/request/web_helper.rb | 8 +++++ 4 files changed, 81 insertions(+) create mode 100644 spec/features/admin/multilingual_spec.rb create mode 100644 spec/features/consumer/multilingual_spec.rb diff --git a/config/environments/test.rb b/config/environments/test.rb index 1f725304d0..f0ef05acaa 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -35,6 +35,7 @@ Openfoodnetwork::Application.configure do # Tests assume English text on the site. config.i18n.default_locale = "en" + config.i18n.available_locales = ['en', 'es'] I18n.locale = config.i18n.locale = config.i18n.default_locale # Use SQL instead of Active Record's schema dumper when creating the test database. diff --git a/spec/features/admin/multilingual_spec.rb b/spec/features/admin/multilingual_spec.rb new file mode 100644 index 0000000000..bfad7c39a6 --- /dev/null +++ b/spec/features/admin/multilingual_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +feature 'Multilingual', js: true do + include AuthenticationWorkflow + include WebHelper + + background do + login_to_admin_section + end + + it 'has two locales available' do + expect(Rails.application.config.i18n[:default_locale]).to eq 'en' + expect(Rails.application.config.i18n[:locale]).to eq 'en' + expect(Rails.application.config.i18n[:available_locales]).to eq ['en', 'es'] + end + + it 'can switch language by params' do + expect(get_i18n_locale).to eq 'en' + expect(get_i18n_translation('spree_admin_overview_enterprises_header')).to eq 'My Enterprises' + expect(page).to have_content 'My Enterprises' + + visit spree.admin_path(locale: 'es') + expect(get_i18n_locale).to eq 'es' + expect(get_i18n_translation('spree_admin_overview_enterprises_header')).to eq 'Mis Organizaciones' + expect(page).to have_content 'Mis Organizaciones' + end + + it 'fallbacks to default_locale' do + pending 'current spree core has a bug if not available locale is provided' + # undefined method `delete_if' for "translation missing: it.date.month_names":String + # inside core/app/views/spree/admin/shared/_translations.html.erb + + # I18n-js fallsback to 'en' + visit spree.admin_path(locale: 'it') + expect(get_i18n_locale).to eq 'it' + expect(get_i18n_translation('spree_admin_overview_enterprises_header')).to eq 'My Enterprises' + # This still is italian until we change enforce_available_locales to `true` + expect(page).to have_content 'Le Mie Aziende' + end +end diff --git a/spec/features/consumer/multilingual_spec.rb b/spec/features/consumer/multilingual_spec.rb new file mode 100644 index 0000000000..59858875e2 --- /dev/null +++ b/spec/features/consumer/multilingual_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +feature 'Multilingual', js: true do + include WebHelper + + it 'has two locales available' do + expect(Rails.application.config.i18n[:default_locale]).to eq 'en' + expect(Rails.application.config.i18n[:locale]).to eq 'en' + expect(Rails.application.config.i18n[:available_locales]).to eq ['en', 'es'] + end + + it 'can switch language by params' do + visit root_path + expect(get_i18n_locale).to eq 'en' + expect(get_i18n_translation('label_shops')).to eq 'Shops' + expect(page).to have_content 'Interested in getting on the Open Food Network?' + expect(page).to have_content 'SHOPS' + + visit root_path(locale: 'es') + expect(get_i18n_locale).to eq 'es' + expect(get_i18n_translation('label_shops')).to eq 'Tiendas' + expect(page).to have_content '¿Estás interesada en entrar en Open Food Network?' + expect(page).to have_content 'TIENDAS' + + # I18n-js fallsback to 'en' + visit root_path(locale: 'it') + expect(get_i18n_locale).to eq 'it' + expect(get_i18n_translation('label_shops')).to eq 'Shops' + # This still is italian until we change enforce_available_locales to `true` + expect(page).to have_content 'NEGOZI' + end +end diff --git a/spec/support/request/web_helper.rb b/spec/support/request/web_helper.rb index 27b17b41d7..1bd22b010c 100644 --- a/spec/support/request/web_helper.rb +++ b/spec/support/request/web_helper.rb @@ -113,6 +113,14 @@ module WebHelper DirtyFormDialog.new(page) end + def get_i18n_locale + page.evaluate_script("I18n.locale;") + end + + def get_i18n_translation(key = nil) + page.evaluate_script("I18n.t('#{key}');") + end + # Fetch the content of a script block # eg. script_content with: 'my-script.com' # Returns nil if not found