message_enricher_test.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class MessageEnricherTest < Test::Unit::TestCase
  4. def test_enriches_initial_message_when_body_contains_error
  5. response = enriched_response(422, 'InitialMessage', { error: 'My Error' })
  6. assert_equal('InitialMessage (My Error)', response.message)
  7. end
  8. def test_enriches_initial_message_when_body_contains_errors_array
  9. response = enriched_response(422, 'InitialMessage', { errors: ['My Error1', 'My Error2'] })
  10. assert_equal('InitialMessage (My Error1; My Error2)', response.message)
  11. end
  12. def test_enriches_initial_message_when_body_contains_errors_single_value
  13. response = enriched_response(422, 'InitialMessage', { errors: 'My Error1' })
  14. assert_equal('InitialMessage (My Error1)', response.message)
  15. end
  16. def test_returns_initial_message_when_code_is_200
  17. response = enriched_response(200, 'InitialMessage', { result: 'Success' })
  18. assert_equal('InitialMessage', response.message)
  19. end
  20. def test_returns_initial_message_when_body_cant_be_parsed
  21. response = enriched_response(422, 'InitialMessage', 'not a json')
  22. assert_equal('InitialMessage', response.message)
  23. end
  24. private
  25. def enriched_response(code, message, body)
  26. mock_response =
  27. Struct
  28. .new(:code, :message, :body)
  29. .new(code.to_s, message.to_s, body.to_json)
  30. ShopifyAPI::MessageEnricher.new(mock_response)
  31. end
  32. end