Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -1,19 +1,80 @@
import decodeOpaqueIdForNamespace from "@reactioncommerce/api-utils/decodeOpaqueIdForNamespace.js";
import encodeOpaqueId from "@reactioncommerce/api-utils/encodeOpaqueId.js";
import importAsString from "@reactioncommerce/api-utils/importAsString.js";
import Factory from "/tests/util/factory.js";
import TestApp from "/tests/util/TestApp.js";
import { internalShopId, shopName } from "../../../../mocks/mockShop";
import { internalTagIds } from "../../../../mocks/mockTags";
import { mockCatalogItems, mockExternalCatalogProductNodes } from "../../../../mocks/mockCatalogItems";
import { opaqueCatalogItemIds } from "../../../../mocks/mockCatalogProducts";

const CatalogItemProductFullQuery = importAsString("./CatalogItemProductFullQuery.graphql");

jest.setTimeout(300000);
const decodeCatalogProductOpaqueId = decodeOpaqueIdForNamespace("reaction/catalogProduct");
const decodeCatalogItemOpaqueId = decodeOpaqueIdForNamespace("reaction/catalogItem");
const decodeCatalogProductVariantOpaqueId = decodeOpaqueIdForNamespace("reaction/catalogProductVariant");

const internalShopId = "123";
const shopName = "Test Shop";

const mockTag = Factory.Tag.makeOne({
shopId: internalShopId,
slug: "2"
});

const mockCatalogItem = mockCatalogItems[0];
const mockCatalogItem = Factory.Catalog.makeOne({
_id: "400",
product: () =>
Factory.CatalogProduct.makeOne({
_id: "500",
productId: "500",
isDeleted: false,
isVisible: true,
tagIds: [mockTag._id],
shopId: internalShopId,
pricing: {
USD: {
compareAtPrice: 6.0,
displayPrice: "2.99 - 4.99",
maxPrice: 4.99,
minPrice: 2.99,
price: null
}
},
media: [
{
priority: 1,
productId: "500",
variantId: null,
URLs: {
thumbnail: "/thumbnail",
small: "/small",
medium: "/medium",
large: "/large",
original: "/original"
}
}
],
socialMetadata: [
{ service: "twitter", message: "twitterMessage" },
{ service: "facebook", message: "facebookMessage" },
{ service: "googleplus", message: "googlePlusMessage" },
{ service: "pinterest", message: "pinterestMessage" }
],
variants: Factory.CatalogProductVariant.makeMany(1, {
options: null,
shopId: internalShopId,
pricing: {
USD: {
compareAtPrice: 6.0,
displayPrice: "2.99 - 4.99",
maxPrice: 4.99,
minPrice: 2.99,
price: null
}
}
})
}),
shopId: internalShopId
});

const expectedItemsResponse = {
catalogItemProduct: mockExternalCatalogProductNodes[0]
};
jest.setTimeout(300000);

let testApp;
let query;
Expand All @@ -22,13 +83,13 @@ beforeAll(async () => {
await testApp.start();
query = testApp.query(CatalogItemProductFullQuery);
await testApp.insertPrimaryShop({ _id: internalShopId, name: shopName });
await Promise.all(internalTagIds.map((_id) => testApp.collections.Tags.insertOne({ _id, shopId: internalShopId, slug: `slug${_id}` })));
await testApp.collections.Tags.insertOne(mockTag);
await testApp.collections.Catalog.insertOne(mockCatalogItem);
});

afterAll(async () => {
await testApp.collections.Shops.deleteOne({ _id: internalShopId });
await testApp.collections.Tags.deleteMany({ _id: { $in: internalTagIds } });
await testApp.collections.Tags.deleteOne(mockTag);
await testApp.collections.Catalog.deleteOne({ _id: mockCatalogItem._id });
await testApp.stop();
});
Expand All @@ -42,17 +103,61 @@ test("get a catalog product by slug", async () => {
return;
}

expect(result).toEqual(expectedItemsResponse);
expect(decodeCatalogItemOpaqueId(result.catalogItemProduct._id)).toEqual(mockCatalogItem._id);
expect(decodeCatalogProductOpaqueId(result.catalogItemProduct.product._id)).toEqual(mockCatalogItem.product._id);
expect(decodeCatalogProductVariantOpaqueId(result.catalogItemProduct.product.variants[0]._id))
.toEqual(mockCatalogItem.product.variants[0]._id);
expect(result.catalogItemProduct.product.socialMetadata).toEqual([
{ service: "twitter", message: "twitterMessage" },
{ service: "facebook", message: "facebookMessage" },
{ service: "googleplus", message: "googlePlusMessage" },
{ service: "pinterest", message: "pinterestMessage" }
]);
expect(result.catalogItemProduct.product.media).toEqual([
{
priority: 1,
productId: result.catalogItemProduct.product.productId,
variantId: null,
URLs: {
thumbnail: "https://shop.fake.site/thumbnail",
small: "https://shop.fake.site/small",
medium: "https://shop.fake.site/medium",
large: "https://shop.fake.site/large",
original: "https://shop.fake.site/original"
}
}
]);
});

test("get a catalog product by ID", async () => {
let result;
try {
result = await query({ slugOrId: opaqueCatalogItemIds[0] });
result = await query({ slugOrId: encodeOpaqueId("reaction/catalogItem", mockCatalogItem._id) });
} catch (error) {
expect(error).toBeUndefined();
return;
}

expect(result).toEqual(expectedItemsResponse);
expect(decodeCatalogItemOpaqueId(result.catalogItemProduct._id)).toEqual(mockCatalogItem._id);
expect(decodeCatalogProductOpaqueId(result.catalogItemProduct.product._id)).toEqual(mockCatalogItem.product._id);
expect(decodeCatalogProductVariantOpaqueId(result.catalogItemProduct.product.variants[0]._id))
.toEqual(mockCatalogItem.product.variants[0]._id);
expect(result.catalogItemProduct.product.socialMetadata).toEqual([
{ service: "twitter", message: "twitterMessage" },
{ service: "facebook", message: "facebookMessage" },
{ service: "googleplus", message: "googlePlusMessage" },
{ service: "pinterest", message: "pinterestMessage" }
]);
expect(result.catalogItemProduct.product.media).toMatchObject([
{
productId: result.catalogItemProduct.product.productId,
URLs: {
thumbnail: "https://shop.fake.site/thumbnail",
small: "https://shop.fake.site/small",
medium: "https://shop.fake.site/medium",
large: "https://shop.fake.site/large",
original: "https://shop.fake.site/original"
}
}
]);
});