spec/weeler/i18n/backend/weeler_spec.rb in weeler-2.0.0 vs spec/weeler/i18n/backend/weeler_spec.rb in weeler-2.0.1

- old
+ new

@@ -1,19 +1,16 @@ # encoding: utf-8 require "spec_helper" describe I18n::Backend::Weeler do - describe "#interpolations" do - it "can persist" do translation = I18n::Backend::Weeler::Translation.new(:key => 'foo', :value => 'bar', :locale => :en) translation.interpolations = %w(count name) translation.save expect(translation.valid?).to be(true) end - end before do # isolate each test cases Weeler.i18n_cache.clear @@ -24,13 +21,13 @@ expect(Weeler.i18n_cache).to receive(:clear) I18n.backend.backends[0].reload_cache end it "writes last translations update timestamp to cache" do - Settings.i18n_updated_at = Time.now + Setting.i18n_updated_at = Time.now I18n.backend.backends[0].reload_cache - expect(Weeler.i18n_cache.read('UPDATED_AT')).to eq(Settings.i18n_updated_at) + expect(Weeler.i18n_cache.read('UPDATED_AT')).to eq(Setting.i18n_updated_at) end it "loads all translated data to cache" do I18n::Backend::Weeler::Translation.create(:key => 'weeler.test.value', :value => "testējam", :locale => :lv) I18n::Backend::Weeler::Translation.create(:key => 'weeler.test.value', :value => "testing", :locale => :en) @@ -41,19 +38,48 @@ expect(Weeler.i18n_cache.read(["en", "weeler.test.value"]).value).to eq("testing") end end describe "#available_locales" do + before do + I18n.available_locales = nil + end - it "finds one locale" do - expect(I18n.available_locales.count).to be(2) + context "when no translations and available_locales provided" do + it "returns one :en locale" do + expect(I18n.available_locales.count).to be(1) + expect(I18n.available_locales.first).to eq(:en) + end end + context "when available_locales provided" do + before do + I18n.available_locales = [:en, :es, :lv] + end + + it "returns provided available_locales" do + expect(I18n.available_locales.count).to be(3) + expect(I18n.available_locales).to eq([:en, :es, :lv]) + end + end + + context "when no available_locales provided but have translations" do + before do + I18n::Backend::Weeler::Translation.create(key: 'weeler.test.value', locale: 'en') + I18n::Backend::Weeler::Translation.create(key: 'weeler.test.value', locale: 'es') + I18n::Backend::Weeler::Translation.create(key: 'weeler.test.value_two', locale: 'en') + end + + it "returns locales from translations" do + expect(I18n.available_locales.count).to be(2) + expect(I18n.available_locales).to include(:en) + expect(I18n.available_locales).to include(:es) + end + end end describe "#value" do - it "returns boolean if translation boolean" do true_t = I18n::Backend::Weeler::Translation.create(:key => 'valid', :value => true, :locale => :en) false_t = I18n::Backend::Weeler::Translation.create(:key => 'invalid', :value => false, :locale => :en) expect(true_t.value).to be(true) @@ -62,15 +88,14 @@ it "runs kernel if translation is_proc" do proc = I18n::Backend::Weeler::Translation.create(:key => 'valid', :value => "p 'This ir proc!'", :locale => :en, :is_proc => true) expect { proc.value }.to output("\"This ir proc!\"\n").to_stdout end - end describe "#html?" do - before(:all) do + before do @html_key_translation = I18n::Backend::Weeler::Translation.create(:key => 'methods.body_html', :value => "Super duper", :locale => :en) @html_value_translation = I18n::Backend::Weeler::Translation.create(:key => 'methods.body', :value => "Super <b>duper</b>", :locale => :en) @html_fake_value_translation = I18n::Backend::Weeler::Translation.create(:key => 'methods.body_fake', :value => "Super b", :locale => :en) end @@ -81,22 +106,20 @@ it "is true if value contains html" do expect(@html_value_translation.html?).to be(true) end it "is false if value not contains html" do - expect(@html_fake_value_translation.html?).to be(false) end - end describe "groups" do - - before(:all) do + before do I18n.backend.store_translations(:en, :group2 => { :test2 => 'bar' } ) I18n.backend.store_translations(:en, :group1 => { :test => 'bar' }) I18n.backend.store_translations(:en, :group1 => { :test3 => 'bar' }) + @groups = I18n::Backend::Weeler::Translation.groups end it "returns 2 groups" do expect(@groups.size).to eq(2) @@ -117,11 +140,11 @@ I18n.backend.store_translations(:en, foo: { baz: 'baz' }) I18n.backend.backends[0].reload_cache translations = I18n::Backend::Weeler::Translation.locale(:en).lookup('foo') - expect(translations.map(&:value)).to eq(%w(bar baz)) + expect(translations.map(&:value).sort).to eq(%w(bar baz)) expect(I18n.t(:foo)).to eq("Foo") # no translation end it "store_translations does not allow ambiguous keys (2)" do @@ -141,31 +164,30 @@ expect(I18n.t(:"Pagina's", :locale => :en)).to eq("Pagina's") end end describe "#lookup" do - it "show warning" do warning_message = "[DEPRECATION] Giving a separator to Translation.lookup is deprecated. You can change the internal separator by overwriting FLATTEN_SEPARATOR.\n" expect { I18n::Backend::Weeler::Translation.lookup("foo", "|") }.to output(warning_message).to_stderr end context "cache" do context "differs from settings timestamp" do before do - Settings.i18n_updated_at = Time.now + Setting.i18n_updated_at = Time.now end it "reloads cache" do expect(I18n.backend.backends[0]).to receive(:reload_cache) I18n.t("cancel", scope: "admin.content") end end context "is same as updates timestamp" do before do - Weeler.i18n_cache.write('UPDATED_AT', Settings.i18n_updated_at) + Weeler.i18n_cache.write('UPDATED_AT', Setting.i18n_updated_at) end it "does not reload cache" do expect(I18n.backend.backends[0]).not_to receive(:reload_cache) I18n.t("cancel", scope: "admin.content") @@ -178,53 +200,41 @@ I18n.backend.backends[0].reload_cache expect(I18n.t("title")).to eq("This is weeler") end context "missing translations" do - context "exist in yml" do - context "new translation" do - it "persist it" do I18n.t('hello') expect(I18n::Backend::Weeler::Translation.locale(:en).find_by_key('hello').value).to eq(I18n.t('hello')) end - end context "already stored" do - context "value is empty" do - context "empty as existing" do - before(:all) do - Weeler.empty_translation_acts_like_missing = false - FactoryBot.create(:translation, key: "weeler.test.cms_title", locale: "en", value: "") + context "empty as missing" do + before do + Weeler.empty_translation_acts_like_missing = true + + FactoryBot.create(:translation, key: 'weeler.test.cms_title', locale: 'en', value: '') end - it "saves the fallback backend value" do + it 'saves the fallback backend value' do I18n.t('weeler.test.cms_title') - expect(I18n::Backend::Weeler::Translation.locale(:en).find_by_key('weeler.test.cms_title').value).to eq("Weeler is cool") - end - after(:all) do - Weeler.empty_translation_acts_like_missing = true + expect(I18n::Backend::Weeler::Translation.locale(:en).find_by_key('weeler.test.cms_title').value).to eq('Weeler is cool') end - end - context "empty as missing" do - before(:all) do - FactoryBot.create(:translation, key: "weeler.test.cms_title", locale: "en", value: "") - end - it "saves the fallback backend value" do - I18n.t('weeler.test.cms_title') - expect(I18n::Backend::Weeler::Translation.locale(:en).find_by_key('weeler.test.cms_title').value).to eq("Weeler is cool") + after do + Weeler.empty_translation_acts_like_missing = false end end end + context "value is nil" do - before(:all) do + before do FactoryBot.create(:translation, key: "weeler.test.cms_title", locale: "en", value: nil) end it "saves the fallback backend value" do I18n.t('weeler.test.cms_title') @@ -282,33 +292,8 @@ I18n.t(key, :separator => '|') I18n::Backend::Weeler::Translation.locale(:en).lookup("foo.baz\001zab").first.update!(:value => 'baz!') I18n.backend.backends[0].reload_cache expect(I18n.t(key, :separator => '|')).to eq('baz!') end - end - - context "usage logging" do - - it "translation stores all translation in each locale" do - Settings.log_key_usage = 'true' - expect(I18n.backend.backends[0]).to receive(:log_key_usage) - I18n.t("random.key") - end - - it "doesn't log keys" do - Settings.log_key_usage = 'false' - expect(I18n.backend.backends[0]).not_to receive(:log_key_usage) - I18n.t("random.key") - end - - it "dumps logged keys to database" do - Settings.log_key_usage = 'dump' - expect(I18n.backend.backends[0]).to receive(:dump_key_usage) - I18n.t("random.key") - end - - end - end - end