require 'rails_helper'

describe Dunlop::UserSettings do
  let(:user) { build :user }
  let(:settings_options) { {storage: {}} }
  subject { described_class.new(user, settings_options[:storage].deep_stringify_keys) }

  describe "#get" do
    it "gets values as multiple arguments with mixed values" do
      settings_options[:storage] = {
        hawk_eye: {analysis: {4 => {45 => [1,2,3]}}}
      }
      subject.get(:hawk_eye, 'analysis', 4, "45").should eq [1,2,3]
    end
  end

  describe "#set" do
    it "sets the proper value into the storage" do
      subject.set(%i[a b 9 d], {a: 1})
      user.settings_storage.should eq(
        "a" => {
          "b" => {
            "9" => {
              "d" => { "a" => 1 }
            }
          }
        }
      )
      user.settings_storage.should eq subject.storage

    end

    it "works with parameters assignment" do
      subject.set :a, 'b', 4, 9
      user.settings_storage.should eq(
        "a" => {
          "b" => {
            "4" => 9
          }
        }
      )
      user.settings_storage.should eq subject.storage
    end

    it "recognizes simple key, value structures" do
      subject.set a: 1, b: 9
      subject.get(:a).should eq 1
      subject.get(:b).should eq 9
    end

    it "does not persist the record" do
      subject.set a: 4
      subject.record.should_not be_persisted
    end
  end

  describe "#set!" do
    it "persists the record" do
      subject.set! a: 4
      subject.get(:a).should eq 4
      subject.record.should be_persisted
      subject.record.reload.settings.get(:a).should eq 4
    end
  end
end
