2012-12-03 04:54:15 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe 'inspection for common cases' do
|
|
|
|
it 'should support the new DSL' do
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(Foo.aasm).to respond_to(:states)
|
|
|
|
expect(Foo.aasm.states).to include(:open)
|
|
|
|
expect(Foo.aasm.states).to include(:closed)
|
2012-12-03 04:54:15 -05:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(Foo.aasm).to respond_to(:initial_state)
|
|
|
|
expect(Foo.aasm.initial_state).to eq(:open)
|
2012-12-03 04:54:15 -05:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(Foo.aasm).to respond_to(:events)
|
|
|
|
expect(Foo.aasm.events).to include(:close)
|
|
|
|
expect(Foo.aasm.events).to include(:null)
|
2012-12-03 04:54:15 -05:00
|
|
|
end
|
|
|
|
|
2013-04-28 10:42:48 -04:00
|
|
|
context "instance level inspection" do
|
2013-04-28 11:46:16 -04:00
|
|
|
let(:foo) { Foo.new }
|
|
|
|
let(:two) { FooTwo.new }
|
2016-01-13 17:02:49 -05:00
|
|
|
let(:multi) { MultiTransitioner.new }
|
2013-04-28 11:46:16 -04:00
|
|
|
|
2013-04-28 10:42:48 -04:00
|
|
|
it "delivers all states" do
|
|
|
|
states = foo.aasm.states
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(states).to include(:open)
|
|
|
|
expect(states).to include(:closed)
|
2014-07-12 09:24:10 -04:00
|
|
|
expect(states).to include(:final)
|
2013-04-28 11:46:16 -04:00
|
|
|
|
2014-10-13 17:24:00 -04:00
|
|
|
states = foo.aasm.states(:permitted => true)
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(states).to include(:closed)
|
|
|
|
expect(states).not_to include(:open)
|
2014-07-12 09:24:10 -04:00
|
|
|
expect(states).not_to include(:final)
|
2013-04-28 11:46:16 -04:00
|
|
|
|
|
|
|
foo.close
|
2014-10-13 17:24:00 -04:00
|
|
|
expect(foo.aasm.states(:permitted => true)).to be_empty
|
2013-04-28 11:46:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "delivers all states for subclasses" do
|
|
|
|
states = two.aasm.states
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(states).to include(:open)
|
|
|
|
expect(states).to include(:closed)
|
2016-01-13 17:02:49 -05:00
|
|
|
expect(states).to include(:final)
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(states).to include(:foo)
|
2013-04-28 11:46:16 -04:00
|
|
|
|
2014-10-13 17:24:00 -04:00
|
|
|
states = two.aasm.states(:permitted => true)
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(states).to include(:closed)
|
|
|
|
expect(states).not_to include(:open)
|
2016-01-13 17:02:49 -05:00
|
|
|
expect(states).not_to include(:final)
|
2013-04-28 11:46:16 -04:00
|
|
|
|
|
|
|
two.close
|
2014-10-13 17:24:00 -04:00
|
|
|
expect(two.aasm.states(:permitted => true)).to be_empty
|
2013-04-28 10:42:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "delivers all events" do
|
|
|
|
events = foo.aasm.events
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(events).to include(:close)
|
|
|
|
expect(events).to include(:null)
|
2013-04-28 10:42:48 -04:00
|
|
|
foo.close
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(foo.aasm.events).to be_empty
|
2013-04-28 10:42:48 -04:00
|
|
|
end
|
2016-01-13 17:02:49 -05:00
|
|
|
|
|
|
|
it "delivers permitted states when multiple transitions are defined" do
|
|
|
|
multi.can_run = false
|
|
|
|
states = multi.aasm.states(:permitted => true)
|
|
|
|
expect(states).to_not include(:running)
|
|
|
|
expect(states).to include(:dancing)
|
|
|
|
|
|
|
|
multi.can_run = true
|
|
|
|
states = multi.aasm.states(:permitted => true)
|
|
|
|
expect(states).to include(:running)
|
|
|
|
expect(states).to_not include(:dancing)
|
|
|
|
end
|
2013-04-28 10:42:48 -04:00
|
|
|
end
|
|
|
|
|
2012-12-03 04:54:15 -05:00
|
|
|
it 'should list states in the order they have been defined' do
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(Conversation.aasm.states).to eq([:needs_attention, :read, :closed, :awaiting_response, :junk])
|
2012-12-03 04:54:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "special cases" do
|
2015-05-15 06:43:36 -04:00
|
|
|
it "should support valid as state name" do
|
|
|
|
expect(ValidStateName.aasm.states).to include(:invalid)
|
|
|
|
expect(ValidStateName.aasm.states).to include(:valid)
|
2012-12-03 04:54:15 -05:00
|
|
|
|
2015-05-15 06:43:36 -04:00
|
|
|
argument = ValidStateName.new
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(argument.invalid?).to be_truthy
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(argument.aasm.current_state).to eq(:invalid)
|
2012-12-03 04:54:15 -05:00
|
|
|
|
|
|
|
argument.valid!
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(argument.valid?).to be_truthy
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(argument.aasm.current_state).to eq(:valid)
|
2012-12-03 04:54:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-30 15:44:00 -05:00
|
|
|
describe 'aasm.states_for_select' do
|
2012-12-03 04:54:15 -05:00
|
|
|
it "should return a select friendly array of states" do
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(Foo.aasm).to respond_to(:states_for_select)
|
2014-07-12 09:24:10 -04:00
|
|
|
expect(Foo.aasm.states_for_select).to eq([['Open', 'open'], ['Closed', 'closed'], ['Final', 'final']])
|
2012-12-03 04:54:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-30 15:48:37 -05:00
|
|
|
describe 'aasm.from_states_for_state' do
|
2012-12-03 04:54:15 -05:00
|
|
|
it "should return all from states for a state" do
|
2015-05-15 06:43:36 -04:00
|
|
|
expect(ComplexExample.aasm).to respond_to(:from_states_for_state)
|
|
|
|
froms = ComplexExample.aasm.from_states_for_state(:active)
|
2014-01-07 13:35:51 -05:00
|
|
|
[:pending, :passive, :suspended].each {|from| expect(froms).to include(from)}
|
2012-12-03 04:54:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return from states for a state for a particular transition only" do
|
2015-05-15 06:43:36 -04:00
|
|
|
froms = ComplexExample.aasm.from_states_for_state(:active, :transition => :unsuspend)
|
2014-01-07 13:35:51 -05:00
|
|
|
[:suspended].each {|from| expect(froms).to include(from)}
|
2012-12-03 04:54:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-13 17:24:00 -04:00
|
|
|
describe 'permitted events' do
|
2013-02-22 04:47:23 -05:00
|
|
|
let(:foo) {Foo.new}
|
|
|
|
|
|
|
|
it 'work' do
|
2014-10-13 17:24:00 -04:00
|
|
|
expect(foo.aasm.events(:permitted => true)).to include(:close)
|
|
|
|
expect(foo.aasm.events(:permitted => true)).not_to include(:null)
|
2012-12-03 04:54:15 -05:00
|
|
|
end
|
2015-10-19 16:01:56 -04:00
|
|
|
|
|
|
|
it 'should not include events in the reject option' do
|
|
|
|
expect(foo.aasm.events(:permitted => true, reject: :close)).not_to include(:close)
|
|
|
|
expect(foo.aasm.events(:permitted => true, reject: [:close])).not_to include(:close)
|
|
|
|
end
|
2012-12-03 04:54:15 -05:00
|
|
|
end
|