user_settings_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe UserSettings do
  4. subject { described_class.new(json) }
  5. let(:json) { {} }
  6. describe '#[]' do
  7. context 'when setting is not set' do
  8. it 'returns default value' do
  9. expect(subject[:always_send_emails]).to be false
  10. end
  11. end
  12. context 'when setting is set' do
  13. let(:json) { { default_language: 'fr' } }
  14. it 'returns value' do
  15. expect(subject[:default_language]).to eq 'fr'
  16. end
  17. end
  18. context 'when setting was not defined' do
  19. it 'raises error' do
  20. expect { subject[:foo] }.to raise_error UserSettings::KeyError
  21. end
  22. end
  23. end
  24. describe '#[]=' do
  25. context 'when value matches type' do
  26. before do
  27. subject[:always_send_emails] = true
  28. end
  29. it 'updates value' do
  30. expect(subject[:always_send_emails]).to be true
  31. end
  32. end
  33. context 'when value needs to be type-cast' do
  34. before do
  35. subject[:always_send_emails] = '1'
  36. end
  37. it 'updates value with a type-cast' do
  38. expect(subject[:always_send_emails]).to be true
  39. end
  40. end
  41. context 'when the setting has a closed set of values' do
  42. it 'updates the attribute when given a valid value' do
  43. expect { subject[:'web.display_media'] = :show_all }.to change { subject[:'web.display_media'] }.from('default').to('show_all')
  44. end
  45. it 'raises an error when given an invalid value' do
  46. expect { subject[:'web.display_media'] = 'invalid value' }.to raise_error ArgumentError
  47. end
  48. end
  49. end
  50. describe '#update' do
  51. before do
  52. subject.update(always_send_emails: true, default_language: 'fr', default_privacy: nil)
  53. end
  54. it 'updates values' do
  55. expect(subject[:always_send_emails]).to be true
  56. expect(subject[:default_language]).to eq 'fr'
  57. end
  58. it 'does not set values that are nil' do
  59. expect(subject.as_json).to_not include(default_privacy: nil)
  60. end
  61. end
  62. describe '#as_json' do
  63. let(:json) { { default_language: 'fr' } }
  64. it 'returns hash' do
  65. expect(subject.as_json).to eq json
  66. end
  67. end
  68. describe '.keys' do
  69. it 'returns an array' do
  70. expect(described_class.keys).to be_a Array
  71. end
  72. end
  73. describe '.definition_for' do
  74. context 'when key is defined' do
  75. it 'returns a setting' do
  76. expect(described_class.definition_for(:always_send_emails)).to be_a UserSettings::Setting
  77. end
  78. end
  79. context 'when key is not defined' do
  80. it 'returns nil' do
  81. expect(described_class.definition_for(:foo)).to be_nil
  82. end
  83. end
  84. end
  85. describe '.definition_for?' do
  86. context 'when key is defined' do
  87. it 'returns true' do
  88. expect(described_class.definition_for?(:always_send_emails)).to be true
  89. end
  90. end
  91. context 'when key is not defined' do
  92. it 'returns false' do
  93. expect(described_class.definition_for?(:foo)).to be false
  94. end
  95. end
  96. end
  97. end