From 4fb981d9fff968fed047481858275a9f437f90ee Mon Sep 17 00:00:00 2001 From: Kepler Sticka-Jones Date: Tue, 20 Aug 2019 14:27:10 -0600 Subject: [PATCH] fix: test function returns false if value is undefined --- src/index.test.tsx | 6 ++++++ src/index.tsx | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/index.test.tsx b/src/index.test.tsx index 50ee89f..008f17d 100644 --- a/src/index.test.tsx +++ b/src/index.test.tsx @@ -33,6 +33,12 @@ describe("Serializer test function", () => { expect(shouldSerialize).toEqual(false); }); + + it("returns false if value is undefined", () => { + const shouldSerialize = serializer.test(undefined); + + expect(shouldSerialize).toEqual(false); + }); }); describe("Serialization function", () => { diff --git a/src/index.tsx b/src/index.tsx index 9b18c4e..a75f978 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -2,13 +2,13 @@ import * as React from "react"; const JSONLDSerializer: jest.SnapshotSerializerPlugin = { test(val) { - return ( + return Boolean( val && - val.$$typeof === Symbol.for("react.element") && - val.type === "script" && - val.props.dangerouslySetInnerHTML && - val.props.type && - val.props.type === "application/ld+json" + val.$$typeof === Symbol.for("react.element") && + val.type === "script" && + val.props.dangerouslySetInnerHTML && + val.props.type && + val.props.type === "application/ld+json" ); },