Skip to content

Commit

Permalink
Add specs, better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dzirtusss committed Dec 30, 2024
1 parent 977b131 commit 313a40d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/react_on_rails/locales/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def generate_translations
val = flatten(translation[key])
translations = translations.deep_merge(key => val)
defaults = defaults.deep_merge(flatten_defaults(val)) if key == default_locale
rescue Psych::Exception => e
raise ReactOnRails::Error, "Error parsing #{f}: #{e.message}"
end
[translations.to_json, defaults.to_json]
end
Expand Down
20 changes: 17 additions & 3 deletions spec/react_on_rails/locales_to_js_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,37 @@ module ReactOnRails
end

describe "with symbols in yaml" do
let(:locale_dir) { File.expand_path("fixtures/i18n/locales_symbols", __dir__) }

before do
ReactOnRails.configure do |config|
config.i18n_yml_dir = File.expand_path("fixtures/i18n/locales_symbols", __dir__)
config.i18n_yml_safe_load_options = { permitted_classes: [Symbol] }
config.i18n_dir = i18n_dir
config.i18n_yml_dir = locale_dir
end
end

after do
ReactOnRails.configure do |config|
config.i18n_dir = nil
config.i18n_yml_dir = nil
config.i18n_yml_safe_load_options = nil
end
end

it "handles locale loading" do
it "handles unsafe locale loading" do
ReactOnRails.configure do |config|
config.i18n_yml_safe_load_options = { permitted_classes: [Symbol] }
end

expect { described_class.new }.not_to raise_error
end

it "raises error with filename when not permitted" do
expect { described_class.new }.to raise_error(
ReactOnRails::Error,
"Error parsing #{locale_dir}/de.yml: Tried to load unspecified class: Symbol"
)
end
end
end
end

0 comments on commit 313a40d

Please sign in to comment.