application_handle_info_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ApplicationHandleInfo do
  4. shared_context 'safe block execution' do |attribute:|
  5. # This `around` block is identical to ApplicationHandleInfo.use.
  6. #
  7. # Q: So why don't we just use it here to DRY things up?
  8. # A: Because that's the method we're trying to test, dummy!
  9. #
  10. # Q: Why can't we do `before { ApplicationHandleInfo.current = 'foo' }` instead?
  11. # A: Because that would change `ApplicationHandleInfo.current` for all subsequent specs.
  12. # (RSpec uses database transactions to keep test environments clean,
  13. # but `ApplicationHandleInfo.current` lives outside of the database.)
  14. around do |example|
  15. original = described_class.send(attribute)
  16. described_class.send("#{attribute}=", 'foo')
  17. example.run
  18. described_class.send("#{attribute}=", original)
  19. end
  20. end
  21. describe '.use' do
  22. it 'requires a block' do
  23. expect { described_class.use('foo') }
  24. .to raise_error(ArgumentError)
  25. end
  26. context 'for a given starting ApplicationHandleInfo' do
  27. include_examples 'safe block execution', attribute: :current
  28. it 'runs the block using the given ApplicationHandleInfo' do
  29. described_class.use('bar') do
  30. expect(described_class.current).to eq('bar')
  31. end
  32. end
  33. it 'resets ApplicationHandleInfo to its original value' do
  34. described_class.use('bar') { nil }
  35. expect(described_class.current).to eq('foo')
  36. end
  37. context 'when an error is raised in the given block' do
  38. it 'does not rescue the error, and still resets ApplicationHandleInfo' do
  39. expect { described_class.use('bar') { raise } }
  40. .to raise_error(StandardError)
  41. .and not_change(described_class, :current)
  42. end
  43. end
  44. end
  45. end
  46. describe '.in_context' do
  47. it 'requires a block' do
  48. expect { described_class.use('foo') }
  49. .to raise_error(ArgumentError)
  50. end
  51. context 'for a given starting ApplicationHandleInfo' do
  52. include_examples 'safe block execution', attribute: :context
  53. it 'runs the block using the given ApplicationHandleInfo' do
  54. described_class.in_context('bar') do
  55. expect(described_class.context).to eq('bar')
  56. end
  57. end
  58. it 'resets ApplicationHandleInfo to its original value' do
  59. described_class.in_context('bar') { nil }
  60. expect(described_class.context).to eq('foo')
  61. end
  62. context 'when an error is raised in the given block' do
  63. it 'does not rescue the error, and still resets ApplicationHandleInfo' do
  64. expect { described_class.in_context('bar') { raise } }
  65. .to raise_error(StandardError)
  66. .and not_change(described_class, :context)
  67. end
  68. end
  69. end
  70. end
  71. describe '.context_without_custom_attributes?' do
  72. it 'returns false when set to default context' do
  73. expect(described_class).not_to be_context_without_custom_attributes
  74. end
  75. context 'for a given starting ApplicationHandleInfo' do
  76. include_examples 'safe block execution', attribute: :context
  77. it 'returns true when set to context that does not use custom attributes' do
  78. described_class.context = 'merge'
  79. expect(described_class).to be_context_without_custom_attributes
  80. end
  81. it 'returns true when in .in_context block' do
  82. described_class.in_context(:merge) do
  83. expect(described_class).to be_context_without_custom_attributes
  84. end
  85. end
  86. end
  87. end
  88. end