From e8fbbb8446f6f8c526126d47972c4bb32dce3d82 Mon Sep 17 00:00:00 2001 From: spences10 Date: Sat, 6 Jan 2024 15:21:42 +0000 Subject: [PATCH] refactor: :recycle: update compoent types refactor schema props --- packages/svead/src/lib/components/head.svelte | 2 +- .../src/lib/components/schema-org.svelte | 7 +- .../src/lib/components/schema-org.test.ts | 69 ++++++++++--------- packages/svead/src/lib/types.ts | 3 +- 4 files changed, 41 insertions(+), 40 deletions(-) diff --git a/packages/svead/src/lib/components/head.svelte b/packages/svead/src/lib/components/head.svelte index 2d94d88..a82df59 100644 --- a/packages/svead/src/lib/components/head.svelte +++ b/packages/svead/src/lib/components/head.svelte @@ -22,7 +22,7 @@ export let authorUrl: string = ''; const mainEntity: MainEntity = { - type: contentType, + '@type': contentType, name: title, url, headline: title, diff --git a/packages/svead/src/lib/components/schema-org.svelte b/packages/svead/src/lib/components/schema-org.svelte index d44476b..0fdf6bc 100644 --- a/packages/svead/src/lib/components/schema-org.svelte +++ b/packages/svead/src/lib/components/schema-org.svelte @@ -16,16 +16,15 @@ datePublished, dateModified, language, - mainEntity: { type, headline }, + mainEntity, breadcrumbs, } = schemaOrgProps; const jsonLd: JsonLdMainEntity = { '@context': 'https://schema.org', - '@type': type, - type: type, + '@type': mainEntity['@type'], name: title, - headline: headline, + headline: mainEntity.headline, description: description, url: url, image: image || '', diff --git a/packages/svead/src/lib/components/schema-org.test.ts b/packages/svead/src/lib/components/schema-org.test.ts index a9a85a2..e9c707d 100644 --- a/packages/svead/src/lib/components/schema-org.test.ts +++ b/packages/svead/src/lib/components/schema-org.test.ts @@ -1,7 +1,42 @@ import { render } from '@testing-library/svelte'; import { afterEach, describe, expect, it } from 'vitest'; +import type { SchemaOrgProps } from './schema-org-props.js'; import SchemaOrg from './schema-org.svelte'; +const commonSchemaOrgProps: SchemaOrgProps = { + url: 'https://example.com', + title: 'Test Title', + description: 'Test Description', + authorName: 'Test Author', + authorType: 'Person', + authorUrl: 'https://example.com/authors/test-author', + image: 'https://example.com/test-image.jpg', + datePublished: '2023-04-05T10:00:00Z', + dateModified: '2023-04-05T12:00:00Z', + language: 'en', + mainEntity: { + '@type': 'Article', + name: 'Test Title', + url: 'https://example.com', + headline: 'Test Title', + description: 'Test Description', + image: 'https://example.com/test-image.jpg', + datePublished: '2023-04-05T10:00:00Z', + dateModified: '2023-04-05T12:00:00Z', + author: { + '@type': 'Person', + name: 'Test Author', + url: 'https://example.com/authors/test-author', + }, + publisher: { + '@type': 'Organization', + name: 'https://example.com', + logo: '', + }, + }, + breadcrumbs: [], +}; + describe('SchemaOrg', () => { afterEach(() => { document.head.innerHTML = ''; @@ -9,39 +44,7 @@ describe('SchemaOrg', () => { it('renders the correct JSON-LD script with the provided properties', async () => { const { container } = render(SchemaOrg, { - schemaOrgProps: { - url: 'https://example.com', - title: 'Test Title', - description: 'Test Description', - authorName: 'Test Author', - authorType: 'Person', - authorUrl: 'https://example.com/authors/test-author', - image: 'https://example.com/test-image.jpg', - datePublished: '2023-04-05T10:00:00Z', - dateModified: '2023-04-05T12:00:00Z', - language: 'en', - mainEntity: { - '@type': 'Article', - name: 'Test Title', - url: 'https://example.com', - headline: 'Test Title', - description: 'Test Description', - image: 'https://example.com/test-image.jpg', - datePublished: '2023-04-05T10:00:00Z', - dateModified: '2023-04-05T12:00:00Z', - author: { - '@type': 'Person', - name: 'Test Author', - url: 'https://example.com/authors/test-author', - }, - publisher: { - '@type': 'Organization', - name: 'https://example.com', - logo: '', - }, - }, - breadcrumbs: [], - }, + schemaOrgProps: commonSchemaOrgProps, }); const jsonLdScriptElement = container.querySelector( diff --git a/packages/svead/src/lib/types.ts b/packages/svead/src/lib/types.ts index 780524e..09af040 100644 --- a/packages/svead/src/lib/types.ts +++ b/packages/svead/src/lib/types.ts @@ -14,7 +14,6 @@ interface CommonEntity { export interface AuthorEntity { '@type': AuthorType; - type?: AuthorType; name: string; url: string; } @@ -32,7 +31,7 @@ export interface BreadcrumbItem { } export interface MainEntity extends CommonEntity { - type: MainEntityType; + '@type': MainEntityType; author: AuthorEntity; publisher: PublisherEntity; breadcrumb?: {