Sha256: d811638ef50e6cd552208555f4aa46f0d88649da535540fbb7db3d8f3fe57516

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

require 'spec_helper'

describe UserPreferences::HasPreferences do
  let(:user) { User.create }

  it 'should mix in preference methods' do
    expect(user).to respond_to(:saved_preferences)
    expect(user).to respond_to(:preferences)
  end

  describe '#preferences' do
    it 'should return an API instance' do
      expect(user.preferences(:food)).to be_kind_of(UserPreferences::API)
    end
  end

  describe '.with_preference' do
    before(:each) { User.destroy_all }

    it 'only returns users with the matching preference' do
      user_1, user_2 = 2.times.map { User.create }
      user_1.preferences(:food).set(wine: 'white')
      user_2.preferences(:food).set(wine: 'red')
      expect(User.with_preference(:food, :wine, 'white')).to eq([user_1])

      user_2.preferences(:food).set(wine: 'white')
      expect(User.with_preference(:food, :wine, 'white')).to eq([user_1, user_2])
    end

    it 'returns a chainable active record relation' do
      user.preferences(:food).set(wine: 'white')
      expect(User.with_preference(:food, :wine, 'white')).to be_kind_of(ActiveRecord::Relation)
      expect(User.with_preference(:food, :wine, 'white').where('1=1')).to eq([user])
    end

    context 'the desired preference matches the default value' do
      it 'includes users who have not explicitely overriden the preference' do
        user.preferences(:food).set(wine: 'red') # the default value
        user_2 = User.create
        expect(User.with_preference(:food, :wine, 'red')).to eq([user, user_2])
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
user_preferences-1.0.2 spec/user_preferences/has_preferences_spec.rb
user_preferences-1.0.1 spec/user_preferences/has_preferences_spec.rb
user_preferences-1.0.0 spec/user_preferences/has_preferences_spec.rb
user_preferences-0.0.3 spec/user_preferences/has_preferences_spec.rb
user_preferences-0.0.2 spec/user_preferences/has_preferences_spec.rb
user_preferences-0.0.1 spec/user_preferences/has_preferences_spec.rb