Sha256: c0a0252707f4faf2509d9680ba82cd6294817a88e5154d1a1c16d86a94eeaec2

Contents?: true

Size: 1.88 KB

Versions: 7

Compression:

Stored size: 1.88 KB

Contents

require 'spec_helper'
require 'localeapp/exception_handler'

describe Localeapp::ExceptionHandler, '#call(exception, locale, key, options)' do
  before(:each) do
    Localeapp.configure do |config|
      config.api_key = 'abcdef'
    end
  end

  it "adds the missing translation to the missing translation list" do
    expect(Localeapp.missing_translations).to receive(:add).with(:en, 'foo', nil, { :baz => 'bam' })
    I18n.t('foo', :baz => 'bam')
  end

  it "handles when the key is an array of keys" do
    expect(Localeapp.missing_translations).to receive(:add).with(:en, 'foo', nil, {})
    expect(Localeapp.missing_translations).to receive(:add).with(:en, 'bar', nil, {})
    I18n.t(['foo', 'bar'])
  end

  it "handles when the default is a Symbol that can be resolved" do
    I18n.backend.store_translations(:en, {:default_symbol_test => 'is resolved'})
    expect(Localeapp.missing_translations).not_to receive(:add)
    expect(I18n.t(:foo, :default => :default_symbol_test)).to eq('is resolved')
  end

  it "handles when the default is a Symbol that can't be resolved" do
    expect(Localeapp.missing_translations).to receive(:add).with(:en, 'foo', nil, {:default => :bar})
    I18n.t(:foo, :default => :bar)
  end

  it "escapes html tags from keys to prevent xss attacks" do
    expect(Localeapp.missing_translations).to receive(:add).with(:en, '<script>alert(1);</script>', nil, {})
    expect(I18n.t('<script>alert(1);</script>')).to eq 'en, &lt;script&gt;alert(1);&lt;/script&gt;'
  end

  it "joins locale and keys correctly" do
    expect(I18n.t('foo.bar')).to eq 'en, foo.bar'
    expect(I18n.t(%w{foo.bar foo.baz})).to eq 'en, foo.bar, foo.baz'
  end

  it "handles missing translation exception" do
    expect {
      exception = Localeapp::I18nMissingTranslationException.new(:en, 'foo', {})
      Localeapp::ExceptionHandler.call(exception, :en, 'foo', {})
    }.to_not raise_error
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
localeapp-2.4.0 spec/localeapp/exception_handler_spec.rb
localeapp-2.3.0 spec/localeapp/exception_handler_spec.rb
localeapp-2.2.0 spec/localeapp/exception_handler_spec.rb
localeapp-2.1.1 spec/localeapp/exception_handler_spec.rb
localeapp-2.1.0 spec/localeapp/exception_handler_spec.rb
localeapp-2.0.0 spec/localeapp/exception_handler_spec.rb
localeapp-1.0.2 spec/localeapp/exception_handler_spec.rb