Skip to content

Commit

Permalink
Add shared spec for change sets, fixes #330
Browse files Browse the repository at this point in the history
Co-authored-by: Carolyn Cole <cam156@psu.edu>
  • Loading branch information
hackartisan and carolyncole committed Feb 27, 2018
1 parent c6acb16 commit 3e508ac
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/valkyrie/specs/shared_specs.rb
Expand Up @@ -8,3 +8,4 @@
require 'valkyrie/specs/shared_specs/file_characterization_service.rb'
require 'valkyrie/specs/shared_specs/change_set_persister.rb'
require 'valkyrie/specs/shared_specs/file.rb'
require 'valkyrie/specs/shared_specs/change_set.rb'
75 changes: 75 additions & 0 deletions lib/valkyrie/specs/shared_specs/change_set.rb
@@ -0,0 +1,75 @@
# frozen_string_literal: true
RSpec.shared_examples 'a Valkyrie::ChangeSet' do |*_flags|
before do
raise 'adapter must be set with `let(:change_set)`' unless defined? change_set
raise 'change_set must have at least one field' if change_set.fields.empty?
end

subject { change_set }

it { is_expected.to respond_to :append_id }
it { is_expected.to respond_to :fields }
it { is_expected.to respond_to :fields= }
it { is_expected.to respond_to :multiple? }
it { is_expected.to respond_to :prepopulate! }
it { is_expected.to respond_to :required? }
it { is_expected.to respond_to :validate }
it { is_expected.to respond_to :valid? }

it "can set an append_id" do
change_set.append_id = Valkyrie::ID.new("test")
expect(change_set.append_id).to eq Valkyrie::ID.new("test")
expect(change_set[:append_id]).to eq Valkyrie::ID.new("test")
end

describe "#fields" do
it "returns an hash" do
expect(change_set.fields).to be_a Hash
end
end

describe "#fields=" do
it "sets fields" do
change_set.fields = { "totally_a_field" => [] }
expect(change_set.fields).to eq("totally_a_field" => [])
end
end

describe "#multiple?" do
it "returns a boolean" do
expect(change_set.multiple?(change_set.fields.keys.first)).to be_in [true, false]
end
end

describe "#prepopulate!" do
it "doesn't make it look changed" do
expect(change_set).not_to be_changed
change_set.prepopulate!
expect(change_set).not_to be_changed
end
end

describe "#required?" do
it "returns a boolean" do
expect(change_set.required?(change_set.fields.keys.first)).to be_in [true, false]
end
end

describe "#valid?" do
it "returns a boolean" do
expect(change_set.valid?).to be_in [true, false]
end
end

describe "#validate" do
it "returns a change_set" do
expect(change_set.validate(change_set.fields)).to be_in [true, false]
end
end

describe ".validators_on" do
it "the class responds to validators_on" do
expect(described_class).to respond_to(:validators_on)
end
end
end
16 changes: 3 additions & 13 deletions spec/valkyrie/change_set_spec.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true
require 'spec_helper'
require 'valkyrie/specs/shared_specs'

RSpec.describe Valkyrie::ChangeSet do
let(:change_set) { ResourceChangeSet.new(Resource.new) }
before do
class Resource < Valkyrie::Resource
attribute :author, Valkyrie::Types::Set
Expand All @@ -17,19 +19,7 @@ class ResourceChangeSet < Valkyrie::ChangeSet
Object.send(:remove_const, :Resource)
Object.send(:remove_const, :ResourceChangeSet)
end
subject(:change_set) { ResourceChangeSet.new(Resource.new) }

describe ".validators_on" do
it "the class responds to validators_on" do
expect(described_class).to respond_to(:validators_on)
end
end

it "can set an append_id" do
change_set.append_id = Valkyrie::ID.new("test")
expect(change_set.append_id).to eq Valkyrie::ID.new("test")
expect(change_set[:append_id]).to eq Valkyrie::ID.new("test")
end
it_behaves_like "a Valkyrie::ChangeSet"

describe "#multiple?" do
it "is not multiple for tagged items" do
Expand Down

0 comments on commit 3e508ac

Please sign in to comment.