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 }
|
|
|
|
|
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)
|
|
|
|
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)
|
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
|
|
|
|
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
|
|
|
|
it "should support valid a state name" do
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(Argument.aasm.states).to include(:invalid)
|
|
|
|
expect(Argument.aasm.states).to include(:valid)
|
2012-12-03 04:54:15 -05:00
|
|
|
|
|
|
|
argument = Argument.new
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(argument.invalid?).to be_true
|
|
|
|
expect(argument.aasm.current_state).to eq(:invalid)
|
2012-12-03 04:54:15 -05:00
|
|
|
|
|
|
|
argument.valid!
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(argument.valid?).to be_true
|
|
|
|
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
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(AuthMachine.aasm).to respond_to(:from_states_for_state)
|
2013-11-30 15:48:37 -05:00
|
|
|
froms = AuthMachine.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
|
2013-11-30 15:48:37 -05:00
|
|
|
froms = AuthMachine.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
|
|
|
|
end
|