Sha256: d98fa0524297e5a071c2db8688b695de3127cdf2d7411d4dca924af50be37ba9

Contents?: true

Size: 1.68 KB

Versions: 43

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require 'rails_helper'
require 'spree/preferences/statically_configurable'

module Spree
  RSpec.describe Preferences::StaticallyConfigurable do
    let(:superklass) do
      # Same interface activerecord's serialization would provide
      Class.new do
        attr_accessor :preferences
        def initialize
          @preferences = { color: 'blue' }
        end

        def [](key)
          return @preferences if key == :preferences
        end
      end
    end
    let(:klass) do
      Class.new(superklass) do
        include Preferences::Preferable
        include Preferences::StaticallyConfigurable

        preference :color, :string

        attr_accessor :preference_source
      end
    end

    subject do
      klass.new.tap do |o|
        o.preference_source = preference_source
      end
    end

    before do
      Spree::Config.static_model_preferences.add(klass, 'credentials', color: 'red')
    end

    describe "available_preference_sources" do
      it "should contain the defined preference sets" do
        expect(klass.available_preference_sources).to eq ['credentials']
      end
    end

    context "with no preference_source" do
      let(:preference_source) { nil }
      it "uses the persisted preference" do
        expect(subject.preferred_color).to eq "blue"
      end
    end

    context "with preference_source set" do
      let(:preference_source) { 'credentials' }
      it "uses the statically configured preference" do
        expect(subject.preferred_color).to eq "red"
      end

      it "ignores assignment" do
        subject.preferences = { color: 'orange' }
        expect(subject.preferred_color).to eq "red"
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
solidus_core-2.8.1 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core-2.6.3 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core-2.8.0 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core-2.7.1 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core-2.6.2 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.10 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.9 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.8 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.7 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.6 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.5 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.4 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.3 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.1 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core_devise_token_auth-2.8.0.alpha.0 spec/models/spree/preferences/statically_configurable_spec.rb
solidus_core-2.7.0 spec/models/spree/preferences/statically_configurable_spec.rb