Skip to content

Commit

Permalink
feat: Allow QAPage to set URL for author (#1479)
Browse files Browse the repository at this point in the history
  • Loading branch information
amadeusj5 authored Sep 1, 2024
1 parent 0030c7c commit 1afb6e6
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ If you are using **`pages`** directory then `NextSeo` is **exactly what you need
- [dangerouslySetAllPagesToNoFollow](#dangerouslysetallpagestonofollow)
- [robotsProps](#robotsprops)
- [Twitter](#twitter)
- [facebook](#facebook)
- [Facebook](#facebook)
- [Canonical URL](#canonical-url)
- [Alternate](#alternate)
- [Additional Meta Tags](#additional-meta-tags)
Expand Down Expand Up @@ -2673,14 +2673,18 @@ const Page = () => (
answerCount: 3,
upvoteCount: 26,
dateCreated: '2016-07-23T21:11Z',
author: { name: 'New Baking User' },
author: {
name: 'New Baking User',
url: 'https://example.com/bakinguser',
},
acceptedAnswer: {
text: '1 pound (lb) is equal to 16 ounces (oz).',
dateCreated: '2016-11-02T21:11Z',
upvoteCount: 1337,
url: 'https://example.com/question1#acceptedAnswer',
author: {
name: 'SomeUser',
url: 'https://example.com/someuser',
},
},
suggestedAnswer: [
Expand All @@ -2691,6 +2695,7 @@ const Page = () => (
url: 'https://example.com/question1#suggestedAnswer1',
author: {
name: 'AnotherUser',
url: 'https://example.com/anotheruser',
},
},
{
Expand All @@ -2700,6 +2705,7 @@ const Page = () => (
url: 'https://example.com/question1#suggestedAnswer2',
author: {
name: 'ConfusedUser',
url: 'https://example.com/confuseduser',
},
},
],
Expand Down
4 changes: 4 additions & 0 deletions cypress/e2e/qaPage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('QaPage JSON-LD', () => {
author: {
'@type': 'Person',
name: 'New Baking User',
url: 'https://example.com/user1',
},
acceptedAnswer: {
text: '1 pound (lb) is equal to 16 ounces (oz).',
Expand All @@ -35,6 +36,7 @@ describe('QaPage JSON-LD', () => {
author: {
'@type': 'Person',
name: 'SomeUser',
url: 'https://example.com/SomeUser',
},
'@type': 'Answer',
},
Expand All @@ -46,6 +48,7 @@ describe('QaPage JSON-LD', () => {
author: {
'@type': 'Person',
name: 'AnotherUser',
url: 'https://example.com/AnotherUser',
},
'@type': 'Answer',
upvoteCount: 42,
Expand All @@ -57,6 +60,7 @@ describe('QaPage JSON-LD', () => {
author: {
'@type': 'Person',
name: 'ConfusedUser',
url: 'https://example.com/ConfusedUser',
},
'@type': 'Answer',
upvoteCount: 0,
Expand Down
6 changes: 4 additions & 2 deletions e2e/pages/jsonld/qaPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import { QAPageJsonLd } from '../../..';

function QaPage() {
Expand All @@ -12,14 +11,15 @@ function QaPage() {
answerCount: 3,
upvoteCount: 26,
dateCreated: '2016-07-23T21:11Z',
author: { name: 'New Baking User' },
author: { name: 'New Baking User', url: 'https://example.com/user1' },
acceptedAnswer: {
text: '1 pound (lb) is equal to 16 ounces (oz).',
dateCreated: '2016-11-02T21:11Z',
upvoteCount: 1337,
url: 'https://example.com/question1#acceptedAnswer',
author: {
name: 'SomeUser',
url: 'https://example.com/SomeUser',
},
},
suggestedAnswer: [
Expand All @@ -30,6 +30,7 @@ function QaPage() {
url: 'https://example.com/question1#suggestedAnswer1',
author: {
name: 'AnotherUser',
url: 'https://example.com/AnotherUser',
},
},
{
Expand All @@ -39,6 +40,7 @@ function QaPage() {
url: 'https://example.com/question1#suggestedAnswer2',
author: {
name: 'ConfusedUser',
url: 'https://example.com/ConfusedUser',
},
},
],
Expand Down
74 changes: 74 additions & 0 deletions src/jsonld/__tests__/qaPage.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { render, screen } from '@testing-library/react';
import React from 'react';

import QAPageJsonLd, { QAPageJsonLdProps } from '../qaPage';

describe('QAPage JSON-LD', () => {
it('renders author with given URL', () => {
const props: QAPageJsonLdProps = {
mainEntity: {
'@type': 'Question',
name: 'nameString',
text: 'textString',
author: {
name: 'johndoe',
url: 'https://example.com/author/johndoe',
},
},
scriptId: 'jsonld-qa-page',
};

render(<QAPageJsonLd {...props} />);

const script = screen.getByTestId('jsonld-qa-page');

const expected = {
'@context': 'https://schema.org',
'@type': 'QAPage',
mainEntity: {
'@type': 'Question',
name: 'nameString',
text: 'textString',
author: {
'@type': 'Person',
name: 'johndoe',
url: 'https://example.com/author/johndoe',
},
},
};

expect(script.innerHTML).toEqual(JSON.stringify(expected));
});

it('renders author default JSON with name only', () => {
const props: QAPageJsonLdProps = {
mainEntity: {
'@type': 'Question',
name: 'nameString',
text: 'textString',
author: 'johndoe',
},
scriptId: 'jsonld-qa-page',
};

render(<QAPageJsonLd {...props} />);

const script = screen.getByTestId('jsonld-qa-page');

const expected = {
'@context': 'https://schema.org',
'@type': 'QAPage',
mainEntity: {
'@type': 'Question',
name: 'nameString',
text: 'textString',
author: {
'@type': 'Person',
name: 'johndoe',
},
},
};

expect(script.innerHTML).toEqual(JSON.stringify(expected));
});
});
6 changes: 3 additions & 3 deletions src/jsonld/qaPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ function QAPageJsonLd({
mainEntity: {
...mainEntity,
'@type': 'Question',
author: setAuthor(mainEntity.author?.name),
author: setAuthor(mainEntity.author),
...(mainEntity.acceptedAnswer && {
acceptedAnswer: {
...mainEntity.acceptedAnswer,
'@type': 'Answer',
author: setAuthor(mainEntity.acceptedAnswer?.author?.name),
author: setAuthor(mainEntity.acceptedAnswer?.author),
},
}),
...(mainEntity.suggestedAnswer && {
Expand All @@ -34,7 +34,7 @@ function QAPageJsonLd({
...rest,
'@type': 'Answer',
upvoteCount: upvoteCount || 0,
author: setAuthor(rest.author?.name),
author: setAuthor(rest.author),
}),
),
}),
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type StepDetails = {

export interface Person {
name: string;
url?: string;
}
export interface Answer {
text: string;
Expand Down

0 comments on commit 1afb6e6

Please sign in to comment.