Skip to content

Commit 0e06bab

Browse files
committed
Add further base class tests
1 parent 8c24ecf commit 0e06bab

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

app/services/discourse_translator/base.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def self.translate(topic_or_post)
3333
detected_lang = detect(topic_or_post)
3434

3535
return detected_lang, get_text(topic_or_post) if (detected_lang&.to_s == I18n.locale.to_s)
36+
37+
existing_translation = get_translation(topic_or_post)
38+
return detected_lang, existing_translation if existing_translation.present?
39+
3640
unless translate_supported?(detected_lang, I18n.locale)
3741
raise TranslatorError.new(
3842
I18n.t(
@@ -42,10 +46,7 @@ def self.translate(topic_or_post)
4246
),
4347
)
4448
end
45-
46-
translated_text = get_translation(topic_or_post) || translate!(topic_or_post)
47-
48-
[detected_lang, translated_text]
49+
[detected_lang, translate!(topic_or_post)]
4950
end
5051

5152
# Subclasses must implement this method to translate the text of a post or topic

spec/services/base_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,66 @@ class EmptyTranslator < DiscourseTranslator::Base
9999
)
100100
end
101101
end
102+
103+
describe ".detect" do
104+
fab!(:post)
105+
106+
it "returns nil when text is blank" do
107+
post.cooked = ""
108+
expect(TestTranslator.detect(post)).to be_nil
109+
end
110+
111+
it "returns cached detection if available" do
112+
TestTranslator.save_detected_locale(post) { "en" }
113+
114+
TestTranslator.expects(:detect!).never
115+
expect(TestTranslator.detect(post)).to eq("en")
116+
end
117+
118+
it "performs detection if no cached result" do
119+
TestTranslator.save_detected_locale(post) { nil }
120+
TestTranslator.expects(:detect!).with(post).returns("es")
121+
122+
expect(TestTranslator.detect(post)).to eq("es")
123+
end
124+
end
125+
126+
describe ".translate" do
127+
fab!(:post)
128+
129+
it "returns nil when text is blank" do
130+
post.cooked = ""
131+
expect(TestTranslator.translate(post)).to be_nil
132+
end
133+
134+
it "returns original text when detected language matches current locale" do
135+
TestTranslator.save_detected_locale(post) { I18n.locale.to_s }
136+
post.cooked = "hello"
137+
138+
expect(TestTranslator.translate(post)).to eq(%w[en hello])
139+
end
140+
141+
it "returns cached translation if available" do
142+
TestTranslator.save_detected_locale(post) { "es" }
143+
TestTranslator.save_translation(post) { "hello" }
144+
145+
expect(TestTranslator.translate(post)).to eq(%w[es hello])
146+
end
147+
148+
it "raises error when translation not supported" do
149+
TestTranslator.save_detected_locale(post) { "xx" }
150+
TestTranslator.save_translation(post) { nil }
151+
TestTranslator.expects(:translate_supported?).with("xx", :en).returns(false)
152+
153+
expect { TestTranslator.translate(post) }.to raise_error(DiscourseTranslator::TranslatorError)
154+
end
155+
156+
it "performs translation when needed" do
157+
TestTranslator.save_detected_locale(post) { "es" }
158+
TestTranslator.save_translation(post) { nil }
159+
TestTranslator.expects(:translate!).returns("hello")
160+
161+
expect(TestTranslator.translate(post)).to eq(%w[es hello])
162+
end
163+
end
102164
end

0 commit comments

Comments
 (0)