In the `i18n_translation` initializer, we're overwriting the `t` helper so calling it uses custom translations if they're available. However, ViewComponent doesn't use the `t` helper but implements its own `t` method. So, when calling the `t` method in a component, we weren't using our implementation of the `t` helper, and so we weren't loading custom translations. Using the `t` helper in components solves the issue. There was a test where we were directly testing a method in a component, and that method uses the `t` helper. This caused an error when running the test: ViewComponent::Base::ViewContextCalledBeforeRenderError: `#helpers` can't be used during initialization, as it depends on the view context that only exists once a ViewComponent is passed to the Rails render pipeline. Using `render_inline` in the test and testing the generated HTML, as recommended in the ViewComponent documentation, solves the issue.
22 lines
456 B
Ruby
22 lines
456 B
Ruby
require "rails_helper"
|
|
|
|
describe ApplicationComponent do
|
|
it "uses custom translations if present" do
|
|
I18nContent.update([{ id: "shared.yes", values: { "value_en" => "Affirmative" }}])
|
|
|
|
component_class = Class.new(ApplicationComponent) do
|
|
def call
|
|
t("shared.yes")
|
|
end
|
|
|
|
def self.name
|
|
""
|
|
end
|
|
end
|
|
|
|
render_inline component_class.new
|
|
|
|
expect(page).to be_rendered with: "<p>Affirmative</p>"
|
|
end
|
|
end
|