Spec ScopedSettings (#3559)
This commit is contained in:
		
							parent
							
								
									b0fe58dc69
								
							
						
					
					
						commit
						42844df966
					
				
					 3 changed files with 99 additions and 31 deletions
				
			
		| 
						 | 
				
			
			@ -41,37 +41,6 @@ RSpec.describe User, type: :model do
 | 
			
		|||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'settings' do
 | 
			
		||||
    it 'inherits default settings from default yml' do
 | 
			
		||||
      expect(Setting.boost_modal).to eq false
 | 
			
		||||
      expect(Setting.interactions['must_be_follower']).to eq false
 | 
			
		||||
 | 
			
		||||
      user = User.new
 | 
			
		||||
      expect(user.settings.boost_modal).to eq false
 | 
			
		||||
      expect(user.settings.interactions['must_be_follower']).to eq false
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'can update settings' do
 | 
			
		||||
      user = Fabricate(:user)
 | 
			
		||||
      expect(user.settings['interactions']['must_be_follower']).to eq false
 | 
			
		||||
      user.settings['interactions'] = user.settings['interactions'].merge('must_be_follower' => true)
 | 
			
		||||
      user.reload
 | 
			
		||||
 | 
			
		||||
      expect(user.settings['interactions']['must_be_follower']).to eq true
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    xit 'does not mutate defaults via the cache' do
 | 
			
		||||
      user = Fabricate(:user)
 | 
			
		||||
      user.settings['interactions']['must_be_follower'] = true
 | 
			
		||||
      # TODO
 | 
			
		||||
      # This mutates the global settings default such that future user
 | 
			
		||||
      # instances will inherit the incorrect starting values
 | 
			
		||||
 | 
			
		||||
      other = Fabricate(:user)
 | 
			
		||||
      expect(other.settings['interactions']['must_be_follower']).to eq false
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'scopes' do
 | 
			
		||||
    describe 'recent' do
 | 
			
		||||
      it 'returns an array of recent users ordered by id' do
 | 
			
		||||
| 
						 | 
				
			
			@ -285,4 +254,14 @@ RSpec.describe User, type: :model do
 | 
			
		|||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it_behaves_like 'Settings-extended' do
 | 
			
		||||
    def create!
 | 
			
		||||
      User.create!(account: Fabricate(:account), email: 'foo@mastodon.space', password: 'abcd1234' )
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def fabricate
 | 
			
		||||
      Fabricate(:user)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										74
									
								
								spec/support/examples/lib/settings/scoped_settings.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								spec/support/examples/lib/settings/scoped_settings.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,74 @@
 | 
			
		|||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
shared_examples 'ScopedSettings' do
 | 
			
		||||
  describe '[]' do
 | 
			
		||||
    it 'inherits default settings' do
 | 
			
		||||
      expect(Setting.boost_modal).to eq false
 | 
			
		||||
      expect(Setting.interactions['must_be_follower']).to eq false
 | 
			
		||||
 | 
			
		||||
      settings = create!
 | 
			
		||||
 | 
			
		||||
      expect(settings['boost_modal']).to eq false
 | 
			
		||||
      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 false
 | 
			
		||||
      expect(Setting.delete_modal).to eq true
 | 
			
		||||
 | 
			
		||||
      settings = create!
 | 
			
		||||
      settings['boost_modal'] = true
 | 
			
		||||
 | 
			
		||||
      records = settings.all_as_records
 | 
			
		||||
 | 
			
		||||
      expect(records['boost_modal'].value).to eq true
 | 
			
		||||
      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 false
 | 
			
		||||
      settings = create!
 | 
			
		||||
      expect(settings.boost_modal).to eq false
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'updates settings' do
 | 
			
		||||
      settings = fabricate
 | 
			
		||||
      settings.boost_modal = true
 | 
			
		||||
      expect(settings['boost_modal']).to eq true
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it 'can update settings with [] and can read with []=' do
 | 
			
		||||
    settings = fabricate
 | 
			
		||||
 | 
			
		||||
    settings['boost_modal'] = true
 | 
			
		||||
    settings['interactions'] = settings['interactions'].merge('must_be_follower' => true)
 | 
			
		||||
 | 
			
		||||
    Setting.save!
 | 
			
		||||
 | 
			
		||||
    expect(settings['boost_modal']).to eq true
 | 
			
		||||
    expect(settings['interactions']['must_be_follower']).to eq true
 | 
			
		||||
 | 
			
		||||
    Rails.cache.clear
 | 
			
		||||
 | 
			
		||||
    expect(settings['boost_modal']).to eq true
 | 
			
		||||
    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
 | 
			
		||||
							
								
								
									
										15
									
								
								spec/support/examples/lib/settings/settings_extended.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								spec/support/examples/lib/settings/settings_extended.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,15 @@
 | 
			
		|||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
shared_examples 'Settings-extended' do
 | 
			
		||||
  describe 'settings' do
 | 
			
		||||
    def fabricate
 | 
			
		||||
      super.settings
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def create!
 | 
			
		||||
      super.settings
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it_behaves_like 'ScopedSettings'
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue