|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -RSpec.describe Jobs::TranslateTranslatable do |
4 | | - fab!(:post) { Fabricate(:post) } |
5 | | - fab!(:topic) { Fabricate(:topic) } |
| 3 | +describe Jobs::TranslateTranslatable do |
| 4 | + fab!(:post) |
| 5 | + fab!(:topic) |
| 6 | + let!(:job) { Jobs::TranslateTranslatable.new } |
6 | 7 |
|
7 | 8 | before do |
8 | 9 | SiteSetting.translator_enabled = true |
9 | 10 | SiteSetting.translator = "Google" |
10 | 11 | SiteSetting.automatic_translation_target_languages = "es|fr" |
| 12 | + allow(DiscourseTranslator::Google).to receive(:translate) |
11 | 13 | end |
12 | 14 |
|
13 | 15 | describe "#execute" do |
14 | 16 | it "does nothing when translator is disabled" do |
15 | 17 | SiteSetting.translator_enabled = false |
16 | | - expect(DiscourseTranslator::Google).not_to receive(:translate) |
17 | 18 |
|
18 | | - subject.execute(type: "Post", translatable_id: post.id) |
| 19 | + job.execute(type: "Post", translatable_id: post.id) |
| 20 | + |
| 21 | + expect(DiscourseTranslator::Google).not_to have_received(:translate) |
19 | 22 | end |
20 | 23 |
|
21 | 24 | it "does nothing when target languages are empty" do |
22 | 25 | SiteSetting.automatic_translation_target_languages = "" |
23 | | - expect(DiscourseTranslator::Google).not_to receive(:translate) |
24 | 26 |
|
25 | | - subject.execute(type: "Post", translatable_id: post.id) |
| 27 | + job.execute(type: "Post", translatable_id: post.id) |
| 28 | + |
| 29 | + expect(DiscourseTranslator::Google).not_to have_received(:translate) |
26 | 30 | end |
27 | 31 |
|
28 | 32 | it "translates posts to configured target languages" do |
29 | | - expect(DiscourseTranslator::Google).to receive(:translate).with(post, :es) |
30 | | - expect(DiscourseTranslator::Google).to receive(:translate).with(post, :fr) |
31 | | - expect(MessageBus).to receive(:publish).with( |
32 | | - "/topic/#{post.topic_id}", |
33 | | - type: :revised, |
34 | | - id: post.id, |
35 | | - ) |
36 | | - |
37 | | - subject.execute(type: "Post", translatable_id: post.id) |
| 33 | + MessageBus.expects(:publish).with("/topic/#{post.topic.id}", type: :revised, id: post.id).once |
| 34 | + |
| 35 | + job.execute(type: "Post", translatable_id: post.id) |
| 36 | + |
| 37 | + expect(DiscourseTranslator::Google).to have_received(:translate).with(post, :es) |
| 38 | + expect(DiscourseTranslator::Google).to have_received(:translate).with(post, :fr) |
38 | 39 | end |
39 | 40 |
|
40 | 41 | it "translates topics to configured target languages" do |
41 | | - expect(DiscourseTranslator::Google).to receive(:translate).with(topic, :es) |
42 | | - expect(DiscourseTranslator::Google).to receive(:translate).with(topic, :fr) |
43 | | - expect(MessageBus).to receive(:publish).with("/topic/#{topic.id}", type: :revised, id: 1) |
| 42 | + MessageBus.expects(:publish).with("/topic/#{topic.id}", type: :revised, id: 1).once |
| 43 | + |
| 44 | + job.execute(type: "Topic", translatable_id: topic.id) |
44 | 45 |
|
45 | | - subject.execute(type: "Topic", translatable_id: topic.id) |
| 46 | + expect(DiscourseTranslator::Google).to have_received(:translate).with(topic, :es) |
| 47 | + expect(DiscourseTranslator::Google).to have_received(:translate).with(topic, :fr) |
46 | 48 | end |
47 | 49 |
|
48 | 50 | it "does nothing when translatable is not found" do |
49 | | - expect(DiscourseTranslator::Google).not_to receive(:translate) |
50 | | - expect(MessageBus).not_to receive(:publish) |
| 51 | + MessageBus.expects(:publish).never |
| 52 | + |
| 53 | + job.execute(type: "Post", translatable_id: -1) |
51 | 54 |
|
52 | | - subject.execute(type: "Post", translatable_id: -1) |
| 55 | + expect(DiscourseTranslator::Google).not_to have_received(:translate) |
53 | 56 | end |
54 | 57 | end |
55 | 58 | end |
0 commit comments