1
0

setting_spec.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe UserSettings::Setting do
  4. subject { described_class.new(name, options) }
  5. let(:name) { :foo }
  6. let(:options) { { default: default, namespace: namespace } }
  7. let(:default) { false }
  8. let(:namespace) { nil }
  9. describe '#default_value' do
  10. context 'when default value is a primitive value' do
  11. it 'returns default value' do
  12. expect(subject.default_value).to eq default
  13. end
  14. end
  15. context 'when default value is a proc' do
  16. let(:default) { -> { 'bar' } }
  17. it 'returns value from proc' do
  18. expect(subject.default_value).to eq 'bar'
  19. end
  20. end
  21. end
  22. describe '#type' do
  23. it 'returns a type' do
  24. expect(subject.type).to be_a ActiveModel::Type::Value
  25. end
  26. context 'when default value is a boolean' do
  27. let(:default) { false }
  28. it 'returns boolean' do
  29. expect(subject.type).to be_a ActiveModel::Type::Boolean
  30. end
  31. end
  32. context 'when default value is a string' do
  33. let(:default) { '' }
  34. it 'returns string' do
  35. expect(subject.type).to be_a ActiveModel::Type::String
  36. end
  37. end
  38. context 'when default value is a lambda returning a boolean' do
  39. let(:default) { -> { false } }
  40. it 'returns boolean' do
  41. expect(subject.type).to be_a ActiveModel::Type::Boolean
  42. end
  43. end
  44. context 'when default value is a lambda returning a string' do
  45. let(:default) { -> { '' } }
  46. it 'returns boolean' do
  47. expect(subject.type).to be_a ActiveModel::Type::String
  48. end
  49. end
  50. end
  51. describe '#type_cast' do
  52. context 'when default value is a boolean' do
  53. let(:default) { false }
  54. it 'returns boolean' do
  55. expect(subject.type_cast('1')).to be true
  56. end
  57. end
  58. context 'when default value is a string' do
  59. let(:default) { '' }
  60. it 'returns string' do
  61. expect(subject.type_cast(1)).to eq '1'
  62. end
  63. end
  64. end
  65. describe '#to_a' do
  66. it 'returns an array' do
  67. expect(subject.to_a).to eq [name, default]
  68. end
  69. end
  70. describe '#key' do
  71. context 'when there is no namespace' do
  72. it 'returns a symbol' do
  73. expect(subject.key).to eq :foo
  74. end
  75. end
  76. context 'when there is a namespace' do
  77. let(:namespace) { :bar }
  78. it 'returns a symbol' do
  79. expect(subject.key).to eq :'bar.foo'
  80. end
  81. end
  82. end
  83. end