Skip to content

Commit

Permalink
added tests for state
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsparkes committed Jan 20, 2025
1 parent 4616d3a commit 32c656e
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions spec/models/tube_rack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@
end

describe '#state' do
let(:class_with_state) do
Class.new do
attr_accessor :state

def initialize(state)
@state = state
end
end
end

let(:tube_rack) { create(:tube_rack) }

context 'when there are no transfer requests' do
Expand All @@ -66,30 +76,42 @@
end

context 'when all transfer requests have the same state' do
let(:tube_state) { 'pending' }
let(:transfer_requests) do
[class_with_state.new(tube_state), class_with_state.new(tube_state), class_with_state.new(tube_state)]
end

it 'returns the single state' do
allow(tube_rack).to receive(:transfer_requests_as_target).and_return([double(state: 'pending')])
allow(tube_rack).to receive(:transfer_requests_as_target).and_return(transfer_requests)
expect(tube_rack.state).to eq('pending')
end
end

context 'when there are multiple states including states to filter out' do
let(:tube1_state) { 'pending' }
let(:tube2_state) { 'cancelled' }
let(:tube3_state) { 'failed' }
let(:transfer_requests) do
[class_with_state.new(tube1_state), class_with_state.new(tube2_state), class_with_state.new(tube3_state)]
end

it 'returns the remaining state after filtering' do
allow(tube_rack).to receive(:transfer_requests_as_target).and_return([
double(state: 'pending'),
double(state: 'cancelled'),
double(state: 'failed')
])
allow(tube_rack).to receive(:transfer_requests_as_target).and_return(transfer_requests)

expect(tube_rack.state).to eq('pending')
end
end

context 'when there are multiple states and filtering still results in multiple states' do
let(:tube1_state) { 'pending' }
let(:tube2_state) { 'started' }
let(:tube3_state) { 'failed' }
let(:transfer_requests) do
[class_with_state.new(tube1_state), class_with_state.new(tube2_state), class_with_state.new(tube3_state)]
end

it 'returns STATE_MIXED' do
allow(tube_rack).to receive(:transfer_requests_as_target).and_return([
double(state: 'pending'),
double(state: 'started'),
double(state: 'failed')
])
allow(tube_rack).to receive(:transfer_requests_as_target).and_return(transfer_requests)
expect(tube_rack.state).to eq(TubeRack::STATE_MIXED)
end
end
Expand Down

0 comments on commit 32c656e

Please sign in to comment.