Skip to content

Commit

Permalink
fix: prefer warning over throwing when <PrismicLink> is given an in…
Browse files Browse the repository at this point in the history
…valid prop in development (#157)

* fix: prefer warning over throwing when `<PrismicLink>` is given an invalid prop in development

* chore(deps): update `@prismicio/helpers`
  • Loading branch information
angeloashmore authored Jun 9, 2022
1 parent fcd7a13 commit 4610658
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 39 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"unit": "nyc --reporter=lcovonly --reporter=text --exclude-after-remap=false ava"
},
"dependencies": {
"@prismicio/helpers": "^2.3.0",
"@prismicio/helpers": "^2.3.1",
"@prismicio/richtext": "^2.0.1"
},
"devDependencies": {
Expand Down
28 changes: 17 additions & 11 deletions src/PrismicLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,30 +138,36 @@ const _PrismicLink = <

if (!__PRODUCTION__) {
if ("field" in props && props.field) {
if (
!("link_type" in props.field) ||
!("url" in props.field || "id" in props.field)
) {
if (!props.field.link_type) {
console.error(
`[PrismicLink] This "field" prop value caused an error to be thrown.\n`,
props.field,
);
throw new Error(
`[PrismicLink] The provided field is missing required properties to properly render a link. The link may not render. For more details, see ${devMsg(
`[PrismicLink] The provided field is missing required properties to properly render a link. The link will not render. For more details, see ${devMsg(
"missing-link-properties",
)}`,
);
} else if (
!(
prismicH.isFilled.link(props.field) &&
("url" in props.field || "id" in props.field)
)
) {
console.warn(
`[PrismicLink] The provided field is missing required properties to properly render a link. The link may not render correctly. For more details, see ${devMsg(
"missing-link-properties",
)}`,
props.field,
);
}
} else if ("document" in props && props.document) {
if (!("url" in props.document || "id" in props.document)) {
console.error(
`[PrismicLink] This "document" prop value caused an error to be thrown.\n`,
props.document,
);
throw new Error(
`[PrismicLink] The provided document is missing required properties to properly render a link. The link may not render. For more details, see ${devMsg(
console.warn(
`[PrismicLink] The provided document is missing required properties to properly render a link. The link may not render correctly. For more details, see ${devMsg(
"missing-link-properties",
)}`,
props.document,
);
}
}
Expand Down
55 changes: 35 additions & 20 deletions test/PrismicLink.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ test("forwards ref to external component", (t) => {
});

test.serial(
"throws error if properties are missing from a given field",
"throws error if `link_type` is missing from a given field",
(t) => {
const field = {} as prismicT.FilledLinkToDocumentField;

Expand All @@ -456,26 +456,41 @@ test.serial(
},
);

test.serial(
"throws error if properties are missing from a given document",
(t) => {
const document = {} as prismicT.PrismicDocument;
test.serial("warns if properties are missing from a given field", (t) => {
const field = { link_type: prismicT.LinkType.Web };

const consoleErrorStub = sinon.stub(console, "error");
const consoleWarnStub = sinon.stub(console, "warn");

t.throws(
() => {
renderJSON(<PrismicLink document={document} />);
},
{ message: /missing-link-properties/ },
);
renderJSON(<PrismicLink field={field} />);

consoleErrorStub.restore();
consoleWarnStub.restore();

t.true(
consoleErrorStub.calledWithMatch(
/this "document" prop value caused an error to be thrown./i,
),
);
},
);
t.true(consoleWarnStub.calledWithMatch("missing-link-properties"));
});

test.serial("does not warn if given field is empty", (t) => {
const field = prismicM.value.link({
seed: t.title,
state: "empty",
});

const consoleWarnStub = sinon.stub(console, "warn");

renderJSON(<PrismicLink field={field} />);

consoleWarnStub.restore();

t.true(consoleWarnStub.calledWithMatch("missing-link-properties"));
});

test.serial("warns if properties are missing from a given document", (t) => {
const document = {} as prismicT.PrismicDocument;

const consoleWarnStub = sinon.stub(console, "warn");

renderJSON(<PrismicLink document={document} />);

consoleWarnStub.restore();

t.true(consoleWarnStub.calledWithMatch("missing-link-properties"));
});

0 comments on commit 4610658

Please sign in to comment.