spec/undo_spec.rb in mongoid-undo-0.0.1 vs spec/undo_spec.rb in mongoid-undo-0.1.0

- old
+ new

@@ -1,120 +1,153 @@ require File.expand_path('../spec_helper', __FILE__) module Mongoid module Undo describe self do - include ActiveSupport::Testing::Assertions - class Document include Mongoid::Document include Mongoid::Undo field :name, type: String end - subject { Document.create(name: 'Version 1') } + subject { Document.new(name: 'foo') } + let(:name) { subject.name } + let(:action) { subject.send(:_action) } - describe 'undoing create' do - before { subject.undo } + describe 'creating' do + before { subject.save } - it 'deletes' do - subject.persisted?.wont_equal true - subject.deleted_at.wont_be_nil + it 'sets action to :create' do + action.must_equal :create end - describe 'and then redoing' do - it 'redoes' do - subject.redo - - subject.persisted?.must_equal true - subject.deleted_at.must_be_nil + describe 'undoing' do + before { subject.undo } + + + it 'deletes' do + subject.persisted?.wont_equal true end - end - it 'returns proper state' do - 3.times do - subject.undo - subject.action.must_equal :create + it 'keeps :create action' do + action.must_equal :create end - end - end - describe 'undoing update' do - before { subject.update_attributes(name: subject.name.next) } + describe 'redoing' do + before { subject.redo } - it 'creates a new version' do - assert_difference 'subject.version', +1 do - subject.undo - end - - # Ensure the new version is saved to the db - subject.persisted?.must_equal true - subject.name.must_equal 'Version 1' - end + it 'restores' do + subject.persisted?.must_equal true + end - it 'returns proper state' do - 3.times do - subject.undo - subject.action.must_equal :update + it 'keeps :create action' do + action.must_equal :create + end end end - end - describe 'undoing destroy' do - describe 'having one version' do - before { subject.destroy } + describe 'updating' do + before { subject.update_attributes(name: 'bar') } - it 'restores' do - subject.undo - - subject.persisted?.must_equal true - subject.deleted_at.must_be_nil + it 'sets action to :update' do + action.must_equal :update end - it 'returns proper state' do - 3.times do - subject.undo - subject.action.must_equal :destroy + describe 'undoing' do + before { subject.undo } + + + it 'retrieves' do + name.must_equal 'foo' end + + + it 'saves a new version' do + subject.version.must_equal 3 + end + + + it 'keeps :update action' do + action.must_equal :update + end + + + describe 'redoing' do + before { subject.redo } + + + it 'retrieves' do + name.must_equal 'bar' + end + + + it 'saves a new version' do + subject.version.must_equal 4 + end + + + it 'keeps :update action' do + action.must_equal :update + end + end end end - describe 'having multiple versions' do - before do - 3.times { subject.update_attributes(name: subject.name.next) } - subject.destroy + describe 'destroying' do + before { subject.destroy } + + + it 'sets action to :destroy' do + action.must_equal :destroy end - it 'restores' do - assert_difference 'subject.version', 0 do - subject.undo - end - - subject.persisted?.must_equal true - subject.deleted_at.must_be_nil + it 'marks as destroyed' do + subject.persisted?.must_equal false end - it 'returns proper state' do - 3.times do - subject.undo - subject.action.must_equal :destroy + describe 'undoing' do + before { subject.undo } + + + it 'restores' do + subject.persisted?.wont_equal false end + + + it 'keeps :destroy action' do + action.must_equal :destroy + end + + + describe 'redoing' do + before { subject.redo } + + + it 'deletes' do + subject.persisted?.must_equal false + end + + + it 'keeps :destroy action' do + action.must_equal :destroy + end + end end end end @@ -123,50 +156,40 @@ subject.method(:redo).must_equal subject.method(:undo) end end - describe :state do + describe :_action do it 'is a symbol' do - subject.fields['action'].options[:type].must_equal Symbol + subject.fields['_action'].options[:type].must_equal Symbol end it 'is versionless' do - subject.fields['action'].options[:versioned].must_equal false + subject.fields['_action'].options[:versioned].must_equal false end end - describe :retrieve do + describe 'localization' do class Localized include Mongoid::Document include Mongoid::Undo field :language, localize: true end - subject { Localized.new } - - it 'works too with localized fields' do - subject.update_attributes language: 'English' - subject.update_attributes language: 'English Updated' + subject = Localized.create(language: 'English') - assert_difference 'subject.version', +1 do - subject.send(:retrieve) - end + subject.update_attributes(language: 'English Updated') + subject.undo subject.language.must_equal 'English' - assert_difference 'subject.version', +1 do - subject.send(:retrieve) - end + subject.redo subject.language.must_equal 'English Updated' end - - - after { I18n.locale = I18n.default_locale } end end end end