Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Don't crash when mediaItem.URLs is null #4982

Merged
merged 1 commit into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const encodeCatalogProductOpaqueId = encodeOpaqueId(namespaces.CatalogPro
* @return {Object} transformed product media item
*/
export function xformProductMedia(mediaItem, context) {
if (!mediaItem) return null;
if (!(mediaItem && mediaItem.URLs)) return null;

const { priority, toGrid, productId, variantId, URLs: { large, medium, original, small, thumbnail } } = mediaItem;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { xformProductMedia } from "./catalogProduct";

const ROOT_URL = "https://example.com";
const context = {
getAbsoluteUrl: (path) => `${ROOT_URL}${path}`
};

test("catalogProduct works in base case", () => {
const expected = {
priority: "unit-test-priority",
toGrid: "unit-test-to-grid",
productId: "unit-test-product-id",
variantId: "unit-test-variant-id",
URLs: {
small: `${ROOT_URL}/small.jpg`,
medium: `${ROOT_URL}/medium.jpg`,
large: `${ROOT_URL}/large.jpg`,
original: `${ROOT_URL}/original.jpg`,
thumbnail: `${ROOT_URL}/thumbnail.jpg`
}
};
const result = xformProductMedia(
{
priority: "unit-test-priority",
toGrid: "unit-test-to-grid",
productId: "unit-test-product-id",
variantId: "unit-test-variant-id",
URLs: {
small: "/small.jpg",
medium: "/medium.jpg",
large: "/large.jpg",
original: "/original.jpg",
thumbnail: "/thumbnail.jpg"
}
},
context
);
expect(result).toMatchObject(expected);
});

test("catalogProduct works in null case", () => {
const result = xformProductMedia(null, context);
expect(result).toBeNull();
});

test("catalogProduct works in {URLs: null} case", () => {
const result = xformProductMedia({ URLs: null }, context);
expect(result).toBeNull();
});