2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-04-10 17:14:34 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe ApplicationHandleInfo do
|
2021-08-20 03:36:30 +00:00
|
|
|
shared_context 'safe block execution' do |attribute:|
|
|
|
|
# This `around` block is identical to ApplicationHandleInfo.use.
|
|
|
|
#
|
|
|
|
# Q: So why don't we just use it here to DRY things up?
|
|
|
|
# A: Because that's the method we're trying to test, dummy!
|
|
|
|
#
|
|
|
|
# Q: Why can't we do `before { ApplicationHandleInfo.current = 'foo' }` instead?
|
|
|
|
# A: Because that would change `ApplicationHandleInfo.current` for all subsequent specs.
|
|
|
|
# (RSpec uses database transactions to keep test environments clean,
|
|
|
|
# but `ApplicationHandleInfo.current` lives outside of the database.)
|
|
|
|
around do |example|
|
|
|
|
original = described_class.send(attribute)
|
|
|
|
described_class.send("#{attribute}=", 'foo')
|
|
|
|
example.run
|
|
|
|
described_class.send("#{attribute}=", original)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-10 17:14:34 +00:00
|
|
|
describe '.use' do
|
|
|
|
it 'requires a block' do
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.use('foo') }
|
2019-04-10 17:14:34 +00:00
|
|
|
.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a given starting ApplicationHandleInfo' do
|
2021-08-20 03:36:30 +00:00
|
|
|
include_examples 'safe block execution', attribute: :current
|
2019-04-10 17:14:34 +00:00
|
|
|
|
|
|
|
it 'runs the block using the given ApplicationHandleInfo' do
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.use('bar') do
|
|
|
|
expect(described_class.current).to eq('bar')
|
2019-04-10 17:14:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets ApplicationHandleInfo to its original value' do
|
2020-11-05 16:31:00 +00:00
|
|
|
described_class.use('bar') { nil }
|
2019-04-10 17:14:34 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(described_class.current).to eq('foo')
|
2019-04-10 17:14:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an error is raised in the given block' do
|
|
|
|
it 'does not rescue the error, and still resets ApplicationHandleInfo' do
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.use('bar') { raise } }
|
2019-04-10 17:14:34 +00:00
|
|
|
.to raise_error(StandardError)
|
2019-07-03 16:12:05 +00:00
|
|
|
.and not_change(described_class, :current)
|
2019-04-10 17:14:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-08-20 03:36:30 +00:00
|
|
|
|
|
|
|
describe '.in_context' do
|
|
|
|
it 'requires a block' do
|
|
|
|
expect { described_class.use('foo') }
|
|
|
|
.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a given starting ApplicationHandleInfo' do
|
|
|
|
include_examples 'safe block execution', attribute: :context
|
|
|
|
|
|
|
|
it 'runs the block using the given ApplicationHandleInfo' do
|
|
|
|
described_class.in_context('bar') do
|
|
|
|
expect(described_class.context).to eq('bar')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets ApplicationHandleInfo to its original value' do
|
|
|
|
described_class.in_context('bar') { nil }
|
|
|
|
|
|
|
|
expect(described_class.context).to eq('foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an error is raised in the given block' do
|
|
|
|
it 'does not rescue the error, and still resets ApplicationHandleInfo' do
|
|
|
|
expect { described_class.in_context('bar') { raise } }
|
|
|
|
.to raise_error(StandardError)
|
|
|
|
.and not_change(described_class, :context)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.context_without_custom_attributes?' do
|
|
|
|
it 'returns false when set to default context' do
|
|
|
|
expect(described_class).not_to be_context_without_custom_attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a given starting ApplicationHandleInfo' do
|
|
|
|
include_examples 'safe block execution', attribute: :context
|
|
|
|
|
|
|
|
it 'returns true when set to context that does not use custom attributes' do
|
|
|
|
described_class.context = 'merge'
|
|
|
|
expect(described_class).to be_context_without_custom_attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when in .in_context block' do
|
|
|
|
described_class.in_context(:merge) do
|
|
|
|
expect(described_class).to be_context_without_custom_attributes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-04-10 17:14:34 +00:00
|
|
|
end
|