Skip to content

Commit

Permalink
refactor: ♻️ update compoent types
Browse files Browse the repository at this point in the history
refactor schema props
  • Loading branch information
spences10 committed Jan 6, 2024
1 parent e7677c9 commit e8fbbb8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/svead/src/lib/components/head.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
export let authorUrl: string = '';
const mainEntity: MainEntity = {
type: contentType,
'@type': contentType,
name: title,
url,
headline: title,
Expand Down
7 changes: 3 additions & 4 deletions packages/svead/src/lib/components/schema-org.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '',
Expand Down
69 changes: 36 additions & 33 deletions packages/svead/src/lib/components/schema-org.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
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 = '';
});

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(
Expand Down
3 changes: 1 addition & 2 deletions packages/svead/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ interface CommonEntity {

export interface AuthorEntity {
'@type': AuthorType;
type?: AuthorType;
name: string;
url: string;
}
Expand All @@ -32,7 +31,7 @@ export interface BreadcrumbItem {
}

export interface MainEntity extends CommonEntity {
type: MainEntityType;
'@type': MainEntityType;
author: AuthorEntity;
publisher: PublisherEntity;
breadcrumb?: {
Expand Down

0 comments on commit e8fbbb8

Please sign in to comment.