|
3 | 3 | require "rails_helper" |
4 | 4 |
|
5 | 5 | describe TopicViewSerializer do |
6 | | - fab!(:user) |
7 | 6 | fab!(:topic) |
8 | | - fab!(:post1) { Fabricate(:post, topic: topic).set_detected_locale("en") } |
9 | | - fab!(:post2) { Fabricate(:post, topic: topic).set_detected_locale("es") } |
10 | | - fab!(:post3) { Fabricate(:post, topic: topic).set_detected_locale("ja") } |
11 | 7 |
|
12 | 8 | before do |
13 | 9 | SiteSetting.translator_enabled = true |
14 | 10 | SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" |
15 | 11 | SiteSetting.restrict_translation_by_poster_group = "#{Group::AUTO_GROUPS[:everyone]}" |
16 | 12 | end |
17 | 13 |
|
18 | | - it "preloads translations without N+1 queries" do |
19 | | - topic_view = TopicView.new(topic) |
20 | | - serializer = TopicViewSerializer.new(topic_view, scope: Guardian.new(user), root: false) |
| 14 | + describe "preloading" do |
| 15 | + fab!(:user) |
| 16 | + fab!(:en_post) do |
| 17 | + post = Fabricate(:post, topic: topic) |
| 18 | + post.set_detected_locale("en") |
| 19 | + post |
| 20 | + end |
| 21 | + fab!(:es_post) do |
| 22 | + post = Fabricate(:post, topic: topic) |
| 23 | + post.set_detected_locale("es") |
| 24 | + post |
| 25 | + end |
| 26 | + fab!(:ja_post) do |
| 27 | + post = Fabricate(:post, topic: topic) |
| 28 | + post.set_detected_locale("ja") |
| 29 | + post |
| 30 | + end |
| 31 | + |
| 32 | + it "preloads locale without N+1 queries" do |
| 33 | + topic_view = TopicView.new(topic) |
| 34 | + serializer = TopicViewSerializer.new(topic_view, scope: Guardian.new(user), root: false) |
| 35 | + |
| 36 | + # ensure translation data is included in the JSON |
| 37 | + json = {} |
| 38 | + queries = track_sql_queries { json = serializer.as_json } |
| 39 | + posts_json = json[:post_stream][:posts] |
| 40 | + expect(posts_json.map { |p| p[:can_translate] }).to eq([false, true, true]) |
| 41 | + |
| 42 | + translation_queries = queries.count { |q| q.include?("discourse_translator_post_locales") } |
| 43 | + expect(translation_queries).to eq(1) # would be 3 (posts) if not preloaded |
| 44 | + |
| 45 | + expect(topic_view.posts.first.association(:content_locale)).to be_loaded |
| 46 | + end |
21 | 47 |
|
22 | | - # ensure translation data is included in the JSON |
23 | | - json = {} |
24 | | - queries = track_sql_queries { json = serializer.as_json } |
25 | | - posts_json = json[:post_stream][:posts] |
26 | | - expect(posts_json.map { |p| p[:can_translate] }).to eq([false, true, true]) |
| 48 | + it "preloads translations when experimental_inline_translation is enabled" do |
| 49 | + SiteSetting.experimental_inline_translation = true |
| 50 | + |
| 51 | + en_post.set_translation("es", "Hola") |
| 52 | + es_post.set_translation("en", "Hello") |
| 53 | + |
| 54 | + topic_view = TopicView.new(topic) |
| 55 | + serializer = TopicViewSerializer.new(topic_view, scope: Guardian.new(user), root: false) |
| 56 | + |
| 57 | + topic_view.posts.reload |
27 | 58 |
|
28 | | - translation_queries = queries.count { |q| q.include?("discourse_translator_post_locales") } |
29 | | - expect(translation_queries).to eq(1) # would be 3 (posts) if not preloaded |
| 59 | + queries = |
| 60 | + track_sql_queries do |
| 61 | + json = serializer.as_json |
| 62 | + json[:post_stream][:posts].each { |p| p[:translations] } |
| 63 | + end |
30 | 64 |
|
31 | | - expect(topic_view.posts.first.association(:content_locale)).to be_loaded |
| 65 | + translation_queries = |
| 66 | + queries.count { |q| q.include?("discourse_translator_post_translations") } |
| 67 | + expect(translation_queries).to eq(1) |
| 68 | + expect(topic_view.posts.first.association(:translations)).to be_loaded |
| 69 | + end |
32 | 70 | end |
33 | 71 |
|
34 | 72 | describe "#fancy_title" do |
35 | 73 | fab!(:user) { Fabricate(:user, locale: "ja") } |
36 | | - fab!(:topic) |
37 | 74 |
|
38 | | - let!(:guardian) { Guardian.new(user) } |
39 | 75 | let!(:original_title) { "<h1>FUS ROH DAAHHH</h1>" } |
40 | 76 | let!(:jap_title) { "<h1>フス・ロ・ダ・ア</h1>" } |
41 | 77 |
|
@@ -81,4 +117,39 @@ def serialize_topic(guardian_user: user, params: {}) |
81 | 117 | expect(serialize_topic.fancy_title).to eq("<h1>フス・ロ・ダ・ア</h1>") |
82 | 118 | end |
83 | 119 | end |
| 120 | + |
| 121 | + describe "#is_translated" do |
| 122 | + fab!(:user) |
| 123 | + |
| 124 | + def serialize_topic(guardian_user: user) |
| 125 | + TopicViewSerializer.new(TopicView.new(topic), scope: Guardian.new(guardian_user)) |
| 126 | + end |
| 127 | + |
| 128 | + it "returns false when translator is disabled or experimental inline translation is disabled" do |
| 129 | + SiteSetting.translator_enabled = true |
| 130 | + SiteSetting.experimental_inline_translation = true |
| 131 | + I18n.locale = "ja" |
| 132 | + Fabricate(:post, topic: topic) |
| 133 | + |
| 134 | + expect(serialize_topic.is_translated).to eq(false) |
| 135 | + end |
| 136 | + |
| 137 | + it "returns true when there is translation for the topic" do |
| 138 | + SiteSetting.translator_enabled = true |
| 139 | + SiteSetting.experimental_inline_translation = true |
| 140 | + I18n.locale = "ja" |
| 141 | + topic.set_translation("ja", "こんにちは") |
| 142 | + |
| 143 | + expect(serialize_topic.is_translated).to eq(true) |
| 144 | + end |
| 145 | + |
| 146 | + it "returns true when there is translation for a post in the topic" do |
| 147 | + SiteSetting.translator_enabled = true |
| 148 | + SiteSetting.experimental_inline_translation = true |
| 149 | + I18n.locale = "ja" |
| 150 | + Fabricate(:post, topic: topic).set_translation("ja", "こんにちは") |
| 151 | + |
| 152 | + expect(serialize_topic.is_translated).to eq(true) |
| 153 | + end |
| 154 | + end |
84 | 155 | end |
0 commit comments