1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00
aasm/spec/unit/inspection_spec.rb

127 lines
4 KiB
Ruby
Raw Normal View History

2012-12-03 04:54:15 -05:00
require 'spec_helper'
describe 'inspection for common cases' do
it 'should support the new DSL' do
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
expect(Foo.aasm).to respond_to(:initial_state)
expect(Foo.aasm.initial_state).to eq(:open)
2012-12-03 04:54:15 -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
context "instance level inspection" do
let(:foo) { Foo.new }
let(:two) { FooTwo.new }
let(:multi) { MultiTransitioner.new }
it "delivers all states" do
states = foo.aasm.states
expect(states).to include(:open)
expect(states).to include(:closed)
expect(states).to include(:final)
states = foo.aasm.states(:permitted => true)
expect(states).to include(:closed)
expect(states).not_to include(:open)
expect(states).not_to include(:final)
foo.close
expect(foo.aasm.states(:permitted => true)).to be_empty
end
it "delivers all states for subclasses" do
states = two.aasm.states
expect(states).to include(:open)
expect(states).to include(:closed)
expect(states).to include(:final)
expect(states).to include(:foo)
states = two.aasm.states(:permitted => true)
expect(states).to include(:closed)
expect(states).not_to include(:open)
expect(states).not_to include(:final)
two.close
expect(two.aasm.states(:permitted => true)).to be_empty
end
it "delivers all events" do
events = foo.aasm.events
expect(events).to include(:close)
expect(events).to include(:null)
foo.close
expect(foo.aasm.events).to be_empty
end
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
end
2012-12-03 04:54:15 -05:00
it 'should list states in the order they have been defined' do
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 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
argument = ValidStateName.new
expect(argument.invalid?).to be_truthy
expect(argument.aasm.current_state).to eq(:invalid)
2012-12-03 04:54:15 -05:00
argument.valid!
expect(argument.valid?).to be_truthy
expect(argument.aasm.current_state).to eq(:valid)
2012-12-03 04:54:15 -05:00
end
end
describe 'aasm.states_for_select' do
2012-12-03 04:54:15 -05:00
it "should return a select friendly array of states" do
expect(Foo.aasm).to respond_to(:states_for_select)
expect(Foo.aasm.states_for_select).to eq([['Open', 'open'], ['Closed', 'closed'], ['Final', 'final']])
2012-12-03 04:54:15 -05:00
end
end
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
expect(ComplexExample.aasm).to respond_to(:from_states_for_state)
froms = ComplexExample.aasm.from_states_for_state(:active)
[: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
froms = ComplexExample.aasm.from_states_for_state(:active, :transition => :unsuspend)
[:suspended].each {|from| expect(froms).to include(from)}
2012-12-03 04:54:15 -05:00
end
end
describe 'permitted events' do
2013-02-22 04:47:23 -05:00
let(:foo) {Foo.new}
it 'work' do
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