From 7106c89de0a80b6fa70f4c00c3359e1d851a6739 Mon Sep 17 00:00:00 2001 From: "Fernando G. Vilar" Date: Sun, 27 Nov 2022 19:27:56 +0700 Subject: [PATCH 1/2] feat(tags): :sparkles: adds `JSONLD` tag --- src/index.tsx | 6 ++++++ test/index.spec.tsx | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 6d0be92..5cc475b 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -248,3 +248,9 @@ export const Base: Component> = props => export const Stylesheet: Component< Omit, "rel"> > = props => ; + +export const JSONLD: Component<{ children: Record }> = ({ children }) => + MetaTag("script", { + children: JSON.stringify(children), + type: "application/ld+json" + }); diff --git a/test/index.spec.tsx b/test/index.spec.tsx index 0adf4c0..89fa6c8 100644 --- a/test/index.spec.tsx +++ b/test/index.spec.tsx @@ -1,15 +1,41 @@ /* @jsxImportSource solid-js */ import { createSignal, lazy } from "solid-js"; import { hydrate, render, Show } from "solid-js/web"; -import { MetaProvider, Title, Style, Meta, Link, Base } from "../src"; +import { MetaProvider, Title, Style, Meta, Link, Base, JSONLD } from "../src"; import { hydrationScript, removeScript } from "./hydration_script"; global.queueMicrotask = setImmediate; test("renders into document.head portal", () => { let div = document.createElement("div"); - const snapshot = - 'Test title'; + + const breadcrumbs = { + "@context": "https://schema.org", + "@type": "BreadcrumbList", + itemListElement: [ + { + "@type": "ListItem", + position: 1, + name: "Books", + item: "https://example.com/books" + }, + { + "@type": "ListItem", + position: 2, + name: "Science Fiction", + item: "https://example.com/books/sciencefiction" + }, + { + "@type": "ListItem", + position: 3, + name: "Award Winners" + } + ] + }; + + const snapshotRE = + /Test title<\/title><style>body {}<\/style><style>div {}<\/style><link href="index\.css"><link href="favicon\.ico"><meta charset="utf-8"><base href="\/new_base"><script type="application\/ld\+json">(?<json>.*)<\/script>/; + const dispose = render( () => ( <MetaProvider> @@ -22,12 +48,17 @@ test("renders into document.head portal", () => { <Link href="favicon.ico" /> <Meta charset="utf-8" /> <Base href="/new_base" /> + <JSONLD>{breadcrumbs}</JSONLD> </div> </MetaProvider> ), div ); - expect(document.head.innerHTML).toBe(snapshot); + + const match = document.head.innerHTML.match(snapshotRE); + expect(match).not.toBe(null); + expect(JSON.parse(match?.groups!.json as string)).toStrictEqual(breadcrumbs); + dispose(); }); From 155f510758b6a7303cad6aa72faece16a00c7e86 Mon Sep 17 00:00:00 2001 From: "Fernando G. Vilar" <dev@f42o.me> Date: Mon, 28 Nov 2022 12:30:05 +0700 Subject: [PATCH 2/2] test(tests): :white_check_mark: removes redundant optional chaining --- test/index.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.spec.tsx b/test/index.spec.tsx index 89fa6c8..b23f66e 100644 --- a/test/index.spec.tsx +++ b/test/index.spec.tsx @@ -57,7 +57,7 @@ test("renders into document.head portal", () => { const match = document.head.innerHTML.match(snapshotRE); expect(match).not.toBe(null); - expect(JSON.parse(match?.groups!.json as string)).toStrictEqual(breadcrumbs); + expect(JSON.parse(match!.groups!.json)).toStrictEqual(breadcrumbs); dispose(); });