Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define anonymous classes for preferable_spec.rb
Browse files Browse the repository at this point in the history
Roddoric committed Nov 30, 2022
1 parent a9c363a commit 686049f
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions core/spec/models/spree/preferences/preferable_spec.rb
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@
require 'rails_helper'

RSpec.describe Spree::Preferences::Preferable, type: :model do
before :each do
class A
let(:config_class_a) do
Class.new do
include Spree::Preferences::Preferable
attr_reader :id

@@ -18,19 +18,21 @@ def preferences
end

def initialize_preference_defaults
@preferences = default_preferences.merge(preferences)
@preferences = default_preferences
end

preference :color, :string, default: 'green'
end
end

class B < A
let(:config_class_b) do
Class.new(config_class_a) do
preference :flavor, :string
end
end

let(:a) { A.new }
let(:b) { B.new }
let(:a) { config_class_a.new }
let(:b) { config_class_b.new }

describe "preference definitions" do
it "parent should not see child definitions" do
@@ -156,7 +158,7 @@ def self.allowed_admin_form_preference_types

context "when a default is a proc" do
before do
A.preference(:context, :string, default: -> { preferred_color })
config_class_a.preference(:context, :string, default: -> { preferred_color })
end

it "calls it from the instance" do
@@ -166,7 +168,7 @@ def self.allowed_admin_form_preference_types

context "converts integer preferences to integer values" do
before do
A.preference :is_integer, :integer
config_class_a.preference :is_integer, :integer
end

it "with strings" do
@@ -185,7 +187,7 @@ def self.allowed_admin_form_preference_types

context "converts decimal preferences to BigDecimal values" do
before do
A.preference :if_decimal, :decimal
config_class_a.preference :if_decimal, :decimal
end

it "returns a BigDecimal" do
@@ -204,7 +206,7 @@ def self.allowed_admin_form_preference_types

context "converts boolean preferences to boolean values" do
before do
A.preference :is_boolean, :boolean, default: true
config_class_a.preference :is_boolean, :boolean, default: true
end

it "with strings" do
@@ -236,7 +238,7 @@ def self.allowed_admin_form_preference_types

context "converts array preferences to array values" do
before do
A.preference :is_array, :array, default: []
config_class_a.preference :is_array, :array, default: []
end

it "with arrays" do
@@ -247,7 +249,7 @@ def self.allowed_admin_form_preference_types

context "converts hash preferences to hash values" do
before do
A.preference :is_hash, :hash, default: {}
config_class_a.preference :is_hash, :hash, default: {}
end

it "with hash" do
@@ -262,18 +264,19 @@ def self.allowed_admin_form_preference_types
end

context "converts any preferences to any values" do
before(:all) do
A.preference :product_ids, :any, default: []
A.preference :product_attributes, :any, default: {}
end

it "with array" do
config_class_a.preference :product_ids, :any, default: []
config_class_a.preference :product_attributes, :any, default: {}

expect(a.preferences[:product_ids]).to eq([])
a.set_preference(:product_ids, [1, 2])
expect(a.preferences[:product_ids]).to eq([1, 2])
end

it "with hash" do
config_class_a.preference :product_ids, :any, default: []
config_class_a.preference :product_attributes, :any, default: {}

expect(a.preferences[:product_attributes]).to eq({})
a.set_preference(:product_attributes, { id: 1, name: 2 })
expect(a.preferences[:product_attributes]).to eq({ id: 1, name: 2 })
@@ -282,7 +285,7 @@ def self.allowed_admin_form_preference_types

context "converts encrypted_string preferences to encrypted values" do
it "with string, encryption key provided as option" do
A.preference :secret, :encrypted_string,
config_class_a.preference :secret, :encrypted_string,
encryption_key: 'VkYp3s6v9y$B?E(H+MbQeThWmZq4t7w!'

a.set_preference(:secret, 'secret_client_id')
@@ -293,15 +296,15 @@ def self.allowed_admin_form_preference_types
it "with string, encryption key provided as env variable" do
expect(ENV).to receive(:[]).with("SOLIDUS_PREFERENCES_MASTER_KEY").and_return("VkYp3s6v9y$B?E(H+MbQeThWmZq4t7w!")

A.preference :secret, :encrypted_string
config_class_a.preference :secret, :encrypted_string

a.set_preference(:secret, 'secret_client_id')
expect(a.get_preference(:secret)).to eq('secret_client_id')
expect(a.preferences[:secret]).not_to eq('secret_client_id')
end

it "with string, encryption key provided as option, set using syntactic sugar method" do
A.preference :secret, :encrypted_string,
config_class_a.preference :secret, :encrypted_string,
encryption_key: 'VkYp3s6v9y$B?E(H+MbQeThWmZq4t7w!'

a.preferred_secret = 'secret_client_id'
@@ -310,11 +313,11 @@ def self.allowed_admin_form_preference_types
end

it "with string, default value" do
A.preference :secret, :encrypted_string,
config_class_a.preference :secret, :encrypted_string,
default: 'my_default_secret',
encryption_key: 'VkYp3s6v9y$B?E(H+MbQeThWmZq4t7w!'

a = A.new
a = config_class_a.new
expect(a.get_preference(:secret)).to eq('my_default_secret')
expect(a.preferences[:secret]).not_to eq('my_default_secret')
end

0 comments on commit 686049f

Please sign in to comment.