message_enricher_test.rb 1.3 KB

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