diff --git a/packages/medusa-source-shopify/jest.config.js b/packages/medusa-source-shopify/jest.config.js index 82513aa071..a00e6f5696 100644 --- a/packages/medusa-source-shopify/jest.config.js +++ b/packages/medusa-source-shopify/jest.config.js @@ -1,3 +1,6 @@ module.exports = { testEnvironment: "node", + transform: { + "^.+\\.[jt]s?$": `../../jest-transformer.js`, + }, } diff --git a/packages/medusa-source-shopify/package.json b/packages/medusa-source-shopify/package.json index a49af01646..00aad22b48 100644 --- a/packages/medusa-source-shopify/package.json +++ b/packages/medusa-source-shopify/package.json @@ -45,7 +45,7 @@ "client-sessions": "^0.8.0", "cross-env": "^7.0.3", "eslint": "^7.32.0", - "jest": "^27.2.0" + "jest": "^26.6.3" }, "gitHead": "cd1f5afa5aa8c0b15ea957008ee19f1d695cbd2e" } diff --git a/packages/medusa-source-shopify/src/services/__mocks__/product-collection.js b/packages/medusa-source-shopify/src/services/__mocks__/product-collection.js index 74eec657bb..8e66283420 100644 --- a/packages/medusa-source-shopify/src/services/__mocks__/product-collection.js +++ b/packages/medusa-source-shopify/src/services/__mocks__/product-collection.js @@ -11,4 +11,7 @@ export const ProductCollectionServiceMock = { retrieveByHandle: jest.fn().mockImplementation((handle) => { return Promise.resolve(undefined) }), + addProducts: jest.fn().mockImplementation((id, products) => { + return Promise.resolve() + }), } diff --git a/packages/medusa-source-shopify/src/services/__mocks__/shopify-redis.js b/packages/medusa-source-shopify/src/services/__mocks__/shopify-redis.js index fbe86cbb0b..49aa6967c2 100644 --- a/packages/medusa-source-shopify/src/services/__mocks__/shopify-redis.js +++ b/packages/medusa-source-shopify/src/services/__mocks__/shopify-redis.js @@ -5,4 +5,14 @@ export const ShopifyRedisServiceMock = { shouldIgnore: jest.fn().mockImplementation((_id, _event) => { return false }), + addUniqueValue: jest.fn().mockImplementation((val, type) => { + return Promise.resolve() + }), + getUniqueValue: jest.fn().mockImplementation((val, type) => { + if (val === "uniq") { + return Promise.resolve("uniq") + } else { + return Promise.resolve(undefined) + } + }), } diff --git a/packages/medusa-source-shopify/src/services/__tests__/__snapshots__/shopify-collection.js.snap b/packages/medusa-source-shopify/src/services/__tests__/__snapshots__/shopify-collection.js.snap index 49e012d763..694a0f4856 100644 --- a/packages/medusa-source-shopify/src/services/__tests__/__snapshots__/shopify-collection.js.snap +++ b/packages/medusa-source-shopify/src/services/__tests__/__snapshots__/shopify-collection.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`ShopifyCollectionService create normalizes a collection from Shopify 1`] = ` +exports[`ShopifyCollectionService create normalizes a custom collection from Shopify 1`] = ` Object { "handle": "spring", "metadata": Object { @@ -10,3 +10,14 @@ Object { "title": "Spring", } `; + +exports[`ShopifyCollectionService create normalizes a smart collection from Shopify 1`] = ` +Object { + "handle": "ipods-1", + "metadata": Object { + "sh_body": null, + "sh_id": 1063001322, + }, + "title": "IPods", +} +`; diff --git a/packages/medusa-source-shopify/src/services/__tests__/__snapshots__/shopify-product.js.snap b/packages/medusa-source-shopify/src/services/__tests__/__snapshots__/shopify-product.js.snap index f00c5ca400..f8add2c1cb 100644 --- a/packages/medusa-source-shopify/src/services/__tests__/__snapshots__/shopify-product.js.snap +++ b/packages/medusa-source-shopify/src/services/__tests__/__snapshots__/shopify-product.js.snap @@ -2,7 +2,6 @@ exports[`ShopifyProductService normalizeProduct_ succesfully normalizes a product from Shopify 1`] = ` Object { - "collection_id": null, "description": "

It's the small iPod with one very big idea: Video. Now the world's most popular music player, available in 4GB and 8GB models, lets you enjoy TV shows, movies, video podcasts, and more. The larger, brighter display means amazing picture quality. In six eye-catching colors, iPod nano is stunning all around. And with models starting at just $149, little speaks volumes.

", "external_id": "shopify_ipod", "handle": "ipod-nano", @@ -12,6 +11,9 @@ Object { "https://cdn.shopify.com/s/files/1/0006/9093/3842/products/ipod-nano.png?v=1633120966", ], "is_giftcard": false, + "metadata": Object { + "vendor": "Apple", + }, "options": Array [ Object { "title": "Color", diff --git a/packages/medusa-source-shopify/src/services/__tests__/shopify-collection.js b/packages/medusa-source-shopify/src/services/__tests__/shopify-collection.js index f9ac5320f5..2376c4ca09 100644 --- a/packages/medusa-source-shopify/src/services/__tests__/shopify-collection.js +++ b/packages/medusa-source-shopify/src/services/__tests__/shopify-collection.js @@ -2,7 +2,7 @@ import { MockManager } from "medusa-test-utils" import ShopifyCollectionService from "../shopify-collection" import { ProductCollectionServiceMock } from "../__mocks__/product-collection" import { ShopifyProductServiceMock } from "../__mocks__/shopify-product" -import { shopifyProducts } from "../__mocks__/test-products" +import { medusaProducts } from "../__mocks__/test-products" describe("ShopifyCollectionService", () => { describe("create", () => { @@ -16,7 +16,7 @@ describe("ShopifyCollectionService", () => { jest.clearAllMocks() }) - it("creates a collection with a product", async () => { + it("creates a collection and adds products", async () => { const collects = [ { collection_id: "spring", @@ -36,9 +36,9 @@ describe("ShopifyCollectionService", () => { handle: "spring", }, ] - const products = [shopifyProducts.ipod] + const products = [medusaProducts.ipod] - const results = await shopifyCollectionService.createWithProducts( + const results = await shopifyCollectionService.createCustomCollections( collects, collections, products @@ -48,7 +48,6 @@ describe("ShopifyCollectionService", () => { ProductCollectionServiceMock.retrieveByHandle ).toHaveBeenCalledTimes(1) expect(ProductCollectionServiceMock.create).toHaveBeenCalledTimes(1) - expect(ShopifyProductServiceMock.create).toHaveBeenCalledTimes(1) expect(results).toEqual([ { id: "col_spring", @@ -62,7 +61,7 @@ describe("ShopifyCollectionService", () => { ]) }) - it("normalizes a collection from Shopify", () => { + it("normalizes a custom collection from Shopify", () => { const shopifyCollection = { id: "spring", body_html: "spring collection", @@ -70,9 +69,36 @@ describe("ShopifyCollectionService", () => { handle: "spring", } - const normalized = shopifyCollectionService.normalizeCollection_( - shopifyCollection - ) + const normalized = + shopifyCollectionService.normalizeCustomCollection_(shopifyCollection) + + expect(normalized).toMatchSnapshot() + }) + + it("normalizes a smart collection from Shopify", () => { + const shopifyCollection = { + id: 1063001322, + handle: "ipods-1", + title: "IPods", + updated_at: "2022-03-11T11:00:30-05:00", + body_html: null, + published_at: "2022-03-11T11:00:30-05:00", + sort_order: "best-selling", + template_suffix: null, + disjunctive: false, + rules: [ + { + column: "title", + relation: "starts_with", + condition: "iPod", + }, + ], + published_scope: "web", + admin_graphql_api_id: "gid://shopify/Collection/1063001322", + } + + const normalized = + shopifyCollectionService.normalizeCustomCollection_(shopifyCollection) expect(normalized).toMatchSnapshot() }) diff --git a/packages/medusa-source-shopify/src/services/__tests__/shopify-product.js b/packages/medusa-source-shopify/src/services/__tests__/shopify-product.js index ae1301645a..df02ec4565 100644 --- a/packages/medusa-source-shopify/src/services/__tests__/shopify-product.js +++ b/packages/medusa-source-shopify/src/services/__tests__/shopify-product.js @@ -95,22 +95,22 @@ describe("ShopifyProductService", () => { ], })) - await shopifyProductService.update(data) + await shopifyProductService.update(medusaProducts.ipod, data) expect(ProductVariantServiceMock.update).toHaveBeenCalledTimes(8) expect(ProductServiceMock.update).toHaveBeenCalledTimes(1) - expect(ShopifyClientServiceMock.get).toHaveBeenCalledTimes(1) }) it("updates a product and deletes 2 existing variants", async () => { const data = { ...shopifyProducts.ipod, id: "shopify_deleted" } data.variants = data.variants.slice(1, -1) - - await shopifyProductService.update(data) + await shopifyProductService.update( + { ...medusaProducts.ipod, id: "shopify_deleted" }, + data + ) expect(ProductVariantServiceMock.delete).toHaveBeenCalledTimes(2) expect(ProductServiceMock.update).toHaveBeenCalledTimes(1) - expect(ShopifyClientServiceMock.get).toHaveBeenCalledTimes(1) }) }) }) diff --git a/yarn.lock b/yarn.lock index 52be9f7ac0..4a942841a5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -18694,7 +18694,7 @@ jest@^26.6.3: import-local "^3.0.2" jest-cli "^26.6.3" -jest@^27.0.6, jest@^27.2.0: +jest@^27.0.6: version "27.4.7" resolved "https://registry.yarnpkg.com/jest/-/jest-27.4.7.tgz#87f74b9026a1592f2da05b4d258e57505f28eca4" integrity sha512-8heYvsx7nV/m8m24Vk26Y87g73Ba6ueUd0MWed/NXMhSZIm62U/llVbS0PJe1SHunbyXjJ/BqG1z9bFjGUIvTg==