shop_test.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class ShopTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. fake("shop")
  7. @shop = ShopifyAPI::Shop.current
  8. end
  9. def test_current_should_return_current_shop
  10. assert(@shop.is_a?(ShopifyAPI::Shop))
  11. assert_equal("Apple Computers", @shop.name)
  12. assert_equal("apple.myshopify.com", @shop.myshopify_domain)
  13. assert_equal(690933842, @shop.id)
  14. assert_equal("2007-12-31T19:00:00-05:00", @shop.created_at)
  15. assert_nil(@shop.tax_shipping)
  16. end
  17. def test_current_with_options_should_return_current_shop
  18. fake(
  19. "shop.json?fields%5B%5D=name&fields%5B%5D=myshopify_domain",
  20. extension: false,
  21. method: :get,
  22. status: 201,
  23. body: load_fixture('shop')
  24. )
  25. @shop = ShopifyAPI::Shop.current(params: { fields: [:name, :myshopify_domain] })
  26. assert(@shop.is_a?(ShopifyAPI::Shop))
  27. assert_equal("Apple Computers", @shop.name)
  28. assert_equal("apple.myshopify.com", @shop.myshopify_domain)
  29. end
  30. def test_get_all_metafields_for_shop
  31. fake("metafields")
  32. metafields = @shop.metafields
  33. assert_equal(3, metafields.length)
  34. assert(metafields.all? { |m| m.is_a?(ShopifyAPI::Metafield) })
  35. end
  36. def test_get_2_metafields_for_shop
  37. body = ActiveSupport::JSON.decode(load_fixture('metafields'))
  38. body['metafields'].slice!(2, 1)
  39. fake('metafields.json?limit=2', body: body.to_json, extension: false)
  40. metafields = @shop.metafields(limit: 2)
  41. assert_equal(2, metafields.length)
  42. assert(metafields.all? { |m| m.is_a?(ShopifyAPI::Metafield) })
  43. end
  44. def test_add_metafield
  45. fake("metafields", method: :post, status: 201, body: load_fixture('metafield'))
  46. field = @shop.add_metafield(
  47. ShopifyAPI::Metafield.new(namespace: "contact", key: "email", value: "123@example.com", value_type: "string")
  48. )
  49. assert_equal(
  50. ActiveSupport::JSON.decode(
  51. '{"metafield":{"namespace":"contact","key":"email","value":"123@example.com","value_type":"string"}}'
  52. ),
  53. ActiveSupport::JSON.decode(WebMock.last_request.body)
  54. )
  55. assert(!field.new_record?)
  56. assert_equal("contact", field.namespace)
  57. assert_equal("email", field.key)
  58. assert_equal("123@example.com", field.value)
  59. end
  60. def test_events
  61. fake("events")
  62. events = @shop.events
  63. assert_equal(3, events.length)
  64. assert(events.all? { |m| m.is_a?(ShopifyAPI::Event) })
  65. end
  66. end