spec/integration/integration_spec.rb in mongoid-history-0.7.0 vs spec/integration/integration_spec.rb in mongoid-history-0.8.0
- old
+ new
@@ -120,10 +120,11 @@
end
end
describe 'on destruction' do
it 'should have two history track records in post' do
+ post # This will create history track records for creation
expect do
post.destroy
end.to change(Tracker, :count).by(1)
end
@@ -145,16 +146,18 @@
end
end
describe 'on update non-embedded' do
it 'should create a history track if changed attributes match tracked attributes' do
+ post # This will create history track records for creation
expect do
post.update_attributes(title: 'Another Test')
end.to change(Tracker, :count).by(1)
end
it 'should not create a history track if changed attributes do not match tracked attributes' do
+ post # This will create history track records for creation
expect do
post.update_attributes(rating: 'untracked')
end.to change(Tracker, :count).by(0)
end
@@ -186,12 +189,13 @@
post.update_attributes(title: 'Another Test')
expect(post.history_tracks.first.version).to eq(1)
end
it 'should assign version on post' do
+ expect(post.version).to eq(1) # Created
post.update_attributes(title: 'Another Test')
- expect(post.version).to eq(1)
+ expect(post.version).to eq(2) # Updated
end
it 'should assign scope' do
post.update_attributes(title: 'Another Test')
expect(post.history_tracks.first.scope).to eq('post')
@@ -203,43 +207,43 @@
end
it 'should exclude defined options' do
name = user.name
user.update_attributes(name: 'Aaron2', email: 'aaronsnewemail@randomemail.com')
- expect(user.history_tracks.first.original.keys).to eq(['n'])
- expect(user.history_tracks.first.original['n']).to eq(name)
- expect(user.history_tracks.first.modified.keys).to eq(['n'])
- expect(user.history_tracks.first.modified['n']).to eq(user.name)
+ expect(user.history_tracks.last.original.keys).to eq(['n'])
+ expect(user.history_tracks.last.original['n']).to eq(name)
+ expect(user.history_tracks.last.modified.keys).to eq(['n'])
+ expect(user.history_tracks.last.modified['n']).to eq(user.name)
end
it 'should undo field changes' do
name = user.name
user.update_attributes(name: 'Aaron2', email: 'aaronsnewemail@randomemail.com')
- user.history_tracks.first.undo! nil
+ user.history_tracks.last.undo! nil
expect(user.reload.name).to eq(name)
end
it 'should undo non-existing field changes' do
post = Post.create!(modifier: user, views: 100)
expect(post.reload.title).to be_nil
post.update_attributes(title: 'Aaron2')
expect(post.reload.title).to eq('Aaron2')
- post.history_tracks.first.undo! nil
+ post.history_tracks.last.undo! nil
expect(post.reload.title).to be_nil
end
it 'should track array changes' do
aliases = user.aliases
user.update_attributes(aliases: %w[bob joe])
- expect(user.history_tracks.first.original['aliases']).to eq(aliases)
- expect(user.history_tracks.first.modified['aliases']).to eq(user.aliases)
+ expect(user.history_tracks.last.original['aliases']).to eq(aliases)
+ expect(user.history_tracks.last.modified['aliases']).to eq(user.aliases)
end
it 'should undo array changes' do
aliases = user.aliases
user.update_attributes(aliases: %w[bob joe])
- user.history_tracks.first.undo! nil
+ user.history_tracks.last.undo! nil
expect(user.reload.aliases).to eq(aliases)
end
end
describe '#tracked_changes' do
@@ -257,11 +261,11 @@
it 'consider all fields values as :from' do
expect(subject[:title]).to eq({ from: 'test' }.with_indifferent_access)
end
end
context 'update action' do
- subject { user.history_tracks.first.tracked_changes }
+ subject { user.history_tracks.last.tracked_changes }
before do
user.update_attributes(name: 'Aaron2', email: nil, country: '', city: nil, phone: '867-5309', aliases: ['', 'bill', 'james'])
end
it { is_expected.to be_a HashWithIndifferentAccess }
it 'should track changed field' do
@@ -303,11 +307,11 @@
it 'consider all edits as ;remove' do
expect(subject[:remove]).to eq({ title: 'test' }.with_indifferent_access)
end
end
context 'update action' do
- subject { user.history_tracks.first.tracked_edits }
+ subject { user.history_tracks.last.tracked_edits }
before do
user.update_attributes(name: 'Aaron2', email: nil, country: '', city: nil, phone: '867-5309', aliases: ['', 'bill', 'james'])
end
it { is_expected.to be_a HashWithIndifferentAccess }
it 'should track changed field' do
@@ -346,16 +350,18 @@
end
end
describe 'on update non-embedded twice' do
it 'should assign version on post' do
+ expect(post.version).to eq(1)
post.update_attributes(title: 'Test2')
post.update_attributes(title: 'Test3')
- expect(post.version).to eq(2)
+ expect(post.version).to eq(3)
end
it 'should create a history track if changed attributes match tracked attributes' do
+ post # Created
expect do
post.update_attributes(title: 'Test2')
post.update_attributes(title: 'Test3')
end.to change(Tracker, :count).by(2)
end
@@ -367,19 +373,19 @@
end
it 'should assign modified fields' do
post.update_attributes(title: 'Test2')
post.update_attributes(title: 'Test3')
- expect(post.history_tracks.where(version: 2).first.modified).to eq(
+ expect(post.history_tracks.where(version: 3).first.modified).to eq(
'title' => 'Test3'
)
end
it 'should assign original fields' do
post.update_attributes(title: 'Test2')
post.update_attributes(title: 'Test3')
- expect(post.history_tracks.where(version: 2).first.original).to eq(
+ expect(post.history_tracks.where(version: 3).first.original).to eq(
'title' => 'Test2'
)
end
it 'should assign modifier' do
@@ -502,11 +508,11 @@
end
it 'should be possible to create with redo after undo create embedded from parent' do
comment # initialize
post.comments.create!(title: 'The second one')
- track = post.history_tracks.last
+ track = post.history_tracks[2]
track.undo!(user)
track.redo!(user)
post.reload
expect(post.comments.count).to eq(2)
end
@@ -550,32 +556,32 @@
end
describe 'non-embedded' do
it 'should undo changes' do
post.update_attributes(title: 'Test2')
- post.history_tracks.where(version: 1).last.undo!(user)
+ post.history_tracks.where(version: 2).last.undo!(user)
post.reload
expect(post.title).to eq('Test')
end
it 'should undo destruction' do
post.destroy
- post.history_tracks.where(version: 1).last.undo!(user)
+ post.history_tracks.where(version: 2).last.undo!(user)
expect(Post.find(post.id).title).to eq('Test')
end
it 'should create a new history track after undo' do
comment # initialize
post.update_attributes(title: 'Test2')
post.history_tracks.last.undo!(user)
post.reload
- expect(post.history_tracks.count).to eq(3)
+ expect(post.history_tracks.count).to eq(4)
end
it 'should assign user as the modifier of the newly created history track' do
post.update_attributes(title: 'Test2')
- post.history_tracks.where(version: 1).last.undo!(user)
+ post.history_tracks.where(version: 2).last.undo!(user)
post.reload
expect(post.history_tracks.where(version: 2).last.modifier).to eq(user)
end
it 'should stay the same after undo and redo' do
@@ -588,10 +594,10 @@
expect(post.title).to eq(post2.title)
end
it 'should be destroyed after undo and redo' do
post.destroy
- track = post.history_tracks.where(version: 1).last
+ track = post.history_tracks.where(version: 2).last
track.undo!(user)
track.redo!(user)
expect(Post.where(_id: post.id).first).to be_nil
end
end