Skip to content

Commit

Permalink
test: ✅ fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spences10 committed Jan 6, 2024
1 parent f01c8b7 commit 929b6f0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 19 deletions.
58 changes: 52 additions & 6 deletions packages/svead/src/lib/components/head.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,71 @@ describe('Head', () => {
document.head.innerHTML = '';
});

it.skip('renders the correct title, description, and author', async () => {
it('renders all required props correctly', async () => {
// Rendering the component with hypothetical required props
render(Head, {
url: 'https://example.com',
title: 'Test Title',
description: 'Test Description',
authorName: 'Test Author',
image: 'https://example.com/test-image.jpg',
paymentPointer: '$example.wallet/test',
});

const titleElement = document.head.querySelector(
'meta[name="title"]',
// Querying elements based on what the component is expected to render
const titleElement = document.head.querySelector('title');
const descriptionElement = document.head.querySelector(
'meta[name="description"]',
);
const authorElement = document.head.querySelector(
'meta[name="author"]',
);
const ogImageElement = document.head.querySelector(
'meta[property="og:image"]',
);
const twitterImageElement = document.head.querySelector(
'meta[name="twitter:image"]',
);
const monetizationElement = document.head.querySelector(
'meta[name="monetization"]',
);

// Assertions for each property based on expected behavior
expect(titleElement?.textContent).toBe('Test Title');
expect(descriptionElement?.getAttribute('content')).toBe(
'Test Description',
);
expect(authorElement?.getAttribute('content')).toBe(
'Test Author',
);
expect(ogImageElement?.getAttribute('content')).toBe(
'https://example.com/test-image.jpg',
);
expect(twitterImageElement?.getAttribute('content')).toBe(
'https://example.com/test-image.jpg',
);
expect(monetizationElement?.getAttribute('content')).toBe(
'$example.wallet/test',
);
});

it('renders the correct title, description, and author', async () => {
render(Head, {
url: 'https://example.com',
title: 'Test Title',
description: 'Test Description',
authorName: 'Test Author',
});

const titleElement = document.head.querySelector('title');
const descriptionElement = document.head.querySelector(
'meta[name="description"]',
);
const authorElement = document.head.querySelector(
'meta[name="author"]',
);

expect(titleElement?.getAttribute('content')).toBe('Test Title');
expect(titleElement?.textContent).toBe('Test Title');
expect(descriptionElement?.getAttribute('content')).toBe(
'Test Description',
);
Expand All @@ -34,7 +80,7 @@ describe('Head', () => {
);
});

it.skip('renders the correct Open Graph and Twitter tags when an image is provided', async () => {
it('renders the correct Open Graph and Twitter tags when an image is provided', async () => {
render(Head, {
url: 'https://example.com',
title: 'Test Title',
Expand All @@ -57,7 +103,7 @@ describe('Head', () => {
);
});

it.skip('renders the monetization tag when a payment pointer is provided', async () => {
it('renders the monetization tag when a payment pointer is provided', async () => {
render(Head, {
url: 'https://example.com',
title: 'Test Title',
Expand Down
36 changes: 23 additions & 13 deletions packages/svead/src/lib/components/schema-org.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ describe('SchemaOrg', () => {
document.head.innerHTML = '';
});

it.skip('renders the correct JSON-LD script with the provided properties', async () => {
render(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',
Expand All @@ -21,7 +21,7 @@ describe('SchemaOrg', () => {
dateModified: '2023-04-05T12:00:00Z',
language: 'en',
mainEntity: {
type: 'Article',
'@type': 'Article',
name: 'Test Title',
url: 'https://example.com',
headline: 'Test Title',
Expand All @@ -30,12 +30,12 @@ describe('SchemaOrg', () => {
datePublished: '2023-04-05T10:00:00Z',
dateModified: '2023-04-05T12:00:00Z',
author: {
type: 'Person',
'@type': 'Person',
name: 'Test Author',
url: 'https://example.com/authors/test-author',
},
publisher: {
type: 'Organization',
'@type': 'Organization',
name: 'https://example.com',
logo: '',
},
Expand All @@ -44,17 +44,20 @@ describe('SchemaOrg', () => {
},
});

const jsonLdScriptElement = document.head.querySelector(
const jsonLdScriptElement = container.querySelector(
'script[type="application/ld+json"]',
);
expect(jsonLdScriptElement).not.toBeNull();

if (!jsonLdScriptElement) {
throw new Error('JSON-LD script element not found');
}
// Clean the innerHTML of the script tag by removing any HTML comments
const cleanedInnerHtml = jsonLdScriptElement!.innerHTML.replace(
/<!--.*?-->/g,
'',
);

const jsonLdContent = JSON.parse(jsonLdScriptElement.innerHTML);
const jsonLdContent = JSON.parse(cleanedInnerHtml);

expect(jsonLdContent['@type']).toBe('Article');
expect(jsonLdContent['@context']).toBe('https://schema.org');
expect(jsonLdContent.name).toBe('Test Title');
expect(jsonLdContent.url).toBe('https://example.com');
expect(jsonLdContent.headline).toBe('Test Title');
Expand All @@ -64,13 +67,20 @@ describe('SchemaOrg', () => {
);
expect(jsonLdContent.datePublished).toBe('2023-04-05T10:00:00Z');
expect(jsonLdContent.dateModified).toBe('2023-04-05T12:00:00Z');
expect(jsonLdContent.inLanguage).toBe('en');
expect(jsonLdContent.author['@type']).toBe('Person');
expect(jsonLdContent.author.name).toBe('Test Author');
expect(jsonLdContent.author.url).toBe(
'https://example.com/authors/test-author',
);
expect(jsonLdContent.publisher['@type']).toBe('Organization');
expect(jsonLdContent.publisher.name).toBe('https://example.com');
expect(jsonLdContent.publisher.logo).toBe('');
expect(jsonLdContent.publisher.name).toBe('');
expect(jsonLdContent.publisher.logo).toBe(
'https://example.com/test-image.jpg',
);
expect(jsonLdContent.mainEntityOfPage['@type']).toBe('WebPage');
expect(jsonLdContent.mainEntityOfPage['@id']).toBe(
'https://example.com',
);
});
});

0 comments on commit 929b6f0

Please sign in to comment.