collection_listing_test.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class CollectionListingTest < Test::Unit::TestCase
  4. def test_get_collection_listings
  5. fake("collection_listings", method: :get, status: 201, body: load_fixture('collection_listings'))
  6. collection_listings = ShopifyAPI::CollectionListing.find(:all)
  7. assert_equal(1, collection_listings.count)
  8. assert_equal(1, collection_listings.first.collection_id)
  9. assert_equal('Home page', collection_listings.first.title)
  10. end
  11. def test_get_collection_listing
  12. fake("collection_listings/1", method: :get, status: 201, body: load_fixture('collection_listing'))
  13. collection_listing = ShopifyAPI::CollectionListing.find(1)
  14. assert_equal(1, collection_listing.collection_id)
  15. assert_equal('Home page', collection_listing.title)
  16. end
  17. def test_get_collection_listing_reload
  18. fake("collection_listings/1", method: :get, status: 201, body: load_fixture('collection_listing'))
  19. collection_listing = ShopifyAPI::CollectionListing.new(collection_id: 1)
  20. collection_listing.reload
  21. assert_equal(1, collection_listing.collection_id)
  22. assert_equal('Home page', collection_listing.title)
  23. end
  24. def test_get_collection_listing_product_ids
  25. fake(
  26. "collection_listings/1/product_ids",
  27. method: :get,
  28. status: 201,
  29. body: load_fixture('collection_listing_product_ids')
  30. )
  31. collection_listing = ShopifyAPI::CollectionListing.new(collection_id: 1)
  32. assert_equal([1, 2], collection_listing.product_ids)
  33. end
  34. def test_get_collection_listing_product_ids_multi_page_with_cursor
  35. version = ShopifyAPI::ApiVersion.find_version('2019-07')
  36. ShopifyAPI::Base.api_version = version.to_s
  37. collection_listing = ShopifyAPI::CollectionListing.new(collection_id: 1)
  38. url = "https://this-is-my-test-shop.myshopify.com/admin/api/2019-07/collection_listings/1/product_ids.json"
  39. next_page_info = "notarealpageinfobutthatsokay"
  40. next_page_url = "#{url}?page_info=#{next_page_info}"
  41. link_header = "<#{next_page_url}>; rel=\"next\""
  42. fake(
  43. "collection_listings/1/product_ids",
  44. method: :get,
  45. status: 201,
  46. url: url,
  47. body: load_fixture('collection_listing_product_ids'),
  48. link: link_header,
  49. )
  50. product_ids = collection_listing.product_ids
  51. assert_equal([1, 2], product_ids)
  52. assert(product_ids.next_page?)
  53. fake(
  54. "collection_listings/1/product_ids",
  55. method: :get,
  56. status: 201,
  57. url: next_page_url,
  58. body: load_fixture('collection_listing_product_ids2'),
  59. link: link_header,
  60. )
  61. next_page = product_ids.fetch_next_page
  62. assert_equal([3, 4], next_page)
  63. end
  64. end