spec/mongoid/persistable/pushable_spec.rb in mongoid-7.1.0 vs spec/mongoid/persistable/pushable_spec.rb in mongoid-7.1.1

- old
+ new

@@ -5,11 +5,11 @@ describe Mongoid::Persistable::Pushable do describe "#add_to_set" do - context "when the document is a root document" do + context "when the document is a top level document" do shared_examples_for "a unique pushable root document" do it "adds single values" do expect(person.aliases).to eq([ 1, 2, 4 ]) @@ -62,9 +62,63 @@ let!(:add) do person.add_to_set(aliases: 4, array: [4, 5], test_array: 1, arrays: [[ 7, 8 ]]) end it_behaves_like "a unique pushable root document" + end + + context 'when the host model is not saved' do + context 'when attribute exists' do + let(:person) do + Person.new(aliases: [2]) + end + + it 'records the change' do + person.add_to_set({aliases: 1}) + + expect(person.aliases).to eq([2, 1]) + end + end + + context 'when attribute does not exist' do + let(:person) do + Person.new + end + + it 'records the change' do + person.add_to_set({aliases: 1}) + + expect(person.aliases).to eq([1]) + end + end + end + + context 'when the host model is loaded from database' do + context 'when attribute exists' do + let(:person) do + Person.create!(aliases: [2]) + person = Person.last + end + + it 'records the change' do + person.add_to_set({aliases: 1}) + + expect(person.aliases).to eq([2, 1]) + end + end + + context 'when attribute does not exist' do + let(:person) do + Person.create! + person = Person.last + end + + it 'records the change' do + person.add_to_set({aliases: 1}) + + expect(person.aliases).to eq([1]) + end + end end end context "when the document is embedded" do