about summary refs log tree commit diff
path: root/spec/support/examples/lib/settings/scoped_settings.rb
blob: e7b51d90b837b4948932c6e4cbc4435052eccbfb (plain) (blame)
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
# frozen_string_literal: true

shared_examples 'ScopedSettings' do
  describe '[]' do
    it 'inherits default settings' do
      expect(Setting.boost_modal).to eq true
      expect(Setting.interactions['must_be_follower']).to eq false

      settings = create!

      expect(settings['boost_modal']).to eq true
      expect(settings['interactions']['must_be_follower']).to eq false
    end
  end

  describe 'all_as_records' do
    # expecting [] and []= works

    it 'returns records merged with default values except hashes' do
      expect(Setting.boost_modal).to eq true
      expect(Setting.delete_modal).to eq true

      settings = create!
      settings['boost_modal'] = false

      records = settings.all_as_records

      expect(records['boost_modal'].value).to eq false
      expect(records['delete_modal'].value).to eq true
    end
  end

  describe 'missing methods' do
    # expecting [] and []= works.

    it 'reads settings' do
      expect(Setting.boost_modal).to eq true
      settings = create!
      expect(settings.boost_modal).to eq true
    end

    it 'updates settings' do
      settings = fabricate
      settings.boost_modal = false
      expect(settings['boost_modal']).to eq false
    end
  end

  it 'can update settings with [] and can read with []=' do
    settings = fabricate

    settings['boost_modal'] = false
    settings['interactions'] = settings['interactions'].merge('must_be_follower' => true)

    Setting.save!

    expect(settings['boost_modal']).to eq false
    expect(settings['interactions']['must_be_follower']).to eq true

    Rails.cache.clear

    expect(settings['boost_modal']).to eq false
    expect(settings['interactions']['must_be_follower']).to eq true
  end

  xit 'does not mutate defaults via the cache' do
    fabricate['interactions']['must_be_follower'] = true
    # TODO
    # This mutates the global settings default such that future
    # instances will inherit the incorrect starting values

    expect(fabricate.settings['interactions']['must_be_follower']).to eq false
  end
end