-
Notifications
You must be signed in to change notification settings - Fork 11
/
backend_spec.rb
137 lines (116 loc) · 5.75 KB
/
backend_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require "rails_helper"
describe Releaf::I18nDatabase::Backend do
let(:translations_store){ Releaf::I18nDatabase::TranslationsStore.new }
describe ".configure_component" do
it "adds new `Releaf::I18nDatabase::Configuration` configuration with enabled missing translation creation" do
allow(Releaf::I18nDatabase::Configuration).to receive(:new)
.with(create_missing_translations: true).and_return("_new")
expect(Releaf.application.config).to receive(:add_configuration).with("_new")
described_class.configure_component
end
end
describe ".initialize_component" do
it "adds itself as i18n backend as primary backend while keeping Rails default simple backend as secondary" do
allow(I18n).to receive(:backend).and_return(:current_backend)
allow(I18n::Backend::Chain).to receive(:new).with(:new_backend, :current_backend).and_return(:x)
allow(described_class).to receive(:new).and_return(:new_backend)
expect(I18n).to receive(:backend=).with(:x)
described_class.initialize_component
end
end
describe "#translations" do
let(:another_translations_store){ Releaf::I18nDatabase::TranslationsStore.new }
context "when translations has been loaded and is not expired" do
it "returns assigned translations hash instance" do
subject.translations_cache = translations_store
allow(translations_store).to receive(:expired?).and_return(false)
expect(Releaf::I18nDatabase::TranslationsStore).to_not receive(:new)
expect(subject.translations).to eq(translations_store)
end
end
context "when translations has been loaded and is expired" do
it "initializes new `TranslationsStore`, cache and return it" do
subject.translations_cache = translations_store
allow(translations_store).to receive(:expired?).and_return(true)
expect(Releaf::I18nDatabase::TranslationsStore).to receive(:new).and_return(another_translations_store)
expect(subject.translations).to eq(another_translations_store)
end
end
context "when translations has not been loaded" do
it "initializes new `TranslationsStore`, cache and return it" do
subject.translations_cache = nil
expect(Releaf::I18nDatabase::TranslationsStore).to receive(:new).and_return(another_translations_store)
expect(subject.translations).to eq(another_translations_store)
end
end
end
describe "#store_translations" do
it "pass given translations to simple translation backend" do
simple_backend = I18n.backend.backends.last
expect(simple_backend).to receive(:store_translations).with(:lv, {a: "x"}, {c: "d"})
subject.store_translations(:lv, {a: "x"}, {c: "d"})
end
end
describe ".translations_updated_at" do
it "returns translations updated_at from cached settings" do
allow(Releaf::Settings).to receive(:[]).with(described_class::UPDATED_AT_KEY).and_return("x")
expect(described_class.translations_updated_at).to eq("x")
end
end
describe ".translations_updated_at=" do
it "stores translations updated_at to cached settings" do
expect(Releaf::Settings).to receive(:[]=).with(described_class::UPDATED_AT_KEY, "xx")
described_class.translations_updated_at = "xx"
end
end
describe "#lookup" do
before do
allow(subject).to receive(:translations).and_return(translations_store)
allow(subject).to receive(:normalize_flat_keys).with(:lv, "some.localization", "_scope_", ":")
.and_return("xx.s.loc")
end
it "flattens key before passing further" do
expect(translations_store).to receive(:missing?).with(:lv, "xx.s.loc")
expect(translations_store).to receive(:lookup).with(:lv, "xx.s.loc", separator: ":", a: "b")
expect(translations_store).to receive(:missing).with(:lv, "xx.s.loc", separator: ":", a: "b")
subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")
end
context "when translation is known as missing" do
it "does not make lookup in translation hash, does not mark it as missing and return nil" do
allow(translations_store).to receive(:missing?).with(:lv, "xx.s.loc").and_return(true)
expect(translations_store).to_not receive(:lookup)
expect(translations_store).to_not receive(:missing)
expect(subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")).to be nil
end
end
context "when translation is not known as missing" do
before do
allow(translations_store).to receive(:missing?).with(:lv, "xx.s.loc").and_return(false)
end
context "when lookup result is not nil" do
before do
allow(translations_store).to receive(:lookup).with(:lv, "xx.s.loc", separator: ":", a: "b").and_return("x")
end
it "returns lookup result" do
expect(subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")).to eq("x")
end
it "does not mark translation as missing" do
expect(translations_store).to_not receive(:missing).with(:lv, "xx.s.loc", separator: ":", a: "b")
subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")
end
end
context "when lookup result is nil" do
before do
allow(translations_store).to receive(:lookup).with(:lv, "xx.s.loc", separator: ":", a: "b").and_return(nil)
end
it "returns nil" do
expect(subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")).to be nil
end
it "marks translation as missing" do
expect(translations_store).to receive(:missing).with(:lv, "xx.s.loc", separator: ":", a: "b")
subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")
end
end
end
end
end