akkounts/spec/models/user_preferences_spec.rb
Râu Cao 4e2e13108c
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Update release notes draft
Refactor user preferences, add defaults from file
* Turn prefs into a flat hash structure, since nesting is not worth the
trouble
* Add a custom serializer class for prefs
* Add a config file for defaults and merge set prefs with unset ones
* Use booleans for "true" and "false", and integers where appropriate
2023-04-05 17:02:35 +02:00

42 lines
1.5 KiB
Ruby

require 'rails_helper'
RSpec.describe UserPreferences, type: :model do
let(:default_prefs) { YAML.load_file("#{Rails.root}/config/default_preferences.yml") }
describe ".load" do
it "provides default values when no preferences are stored yet" do
expect(UserPreferences.load(nil)).to eq(default_prefs)
end
it "provides default values for unset preferences" do
prefs = UserPreferences.load("lightning_notify_sats_received: xmpp")
expect(prefs[:lightning_notify_sats_received]).to eq("xmpp")
expect(prefs[:xmpp_exchange_contacts_with_invitees]).to eq(true)
end
end
describe ".process" do
it "turns all keys into strings" do
res = UserPreferences.process({ foo: "bar" })
expect(res[:foo]).to be(nil)
expect(res['foo']).to eq("bar")
end
it "converts value 'true' to boolean" do
res = UserPreferences.process({ lightning_notify_sats_received: "true" })
expect(res['lightning_notify_sats_received']).to be(true)
end
it "converts value 'false' to boolean" do
res = UserPreferences.process({ lightning_notify_sats_received: "false" })
expect(res['lightning_notify_sats_received']).to be(false)
end
it "converts value string with integer into integer" do
res = UserPreferences.process({ lightning_notify_sats_received_threshold: 1000 })
expect(res['lightning_notify_sats_received_threshold']).to be_a(Integer)
expect(res['lightning_notify_sats_received_threshold']).to eq(1000)
end
end
end