Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(blog): normalize inline authors socials #10424

Merged
merged 10 commits into from
Aug 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

import {fromPartial, type PartialDeep} from '@total-typescript/shoehorn';
import {getBlogPostAuthors, groupBlogPostsByAuthorKey} from '../authors';
import type {AuthorsMap, BlogPost} from '@docusaurus/plugin-content-blog';
import type {
AuthorAttributes,
AuthorsMap,
BlogPost,
} from '@docusaurus/plugin-content-blog';

function post(partial: PartialDeep<BlogPost>): BlogPost {
return fromPartial(partial);
Expand Down Expand Up @@ -268,11 +272,176 @@ describe('getBlogPostAuthors', () => {
]);
});

it('can read authors Author', () => {
it('read different values from socials', () => {
function testSocials(socials: AuthorAttributes['socials'] | undefined) {
return getBlogPostAuthors({
frontMatter: {
authors: {
name: 'Sébastien Lorber',
title: 'maintainer',
socials,
},
},
authorsMap: undefined,
baseUrl: '/',
});
}

// @ts-expect-error test
expect(() => testSocials(null)).not.toThrow();
// @ts-expect-error test
expect(testSocials(null)).toEqual([
{
name: 'Sébastien Lorber',
title: 'maintainer',
imageURL: undefined,
socials: {},
key: null,
page: null,
},
]);
expect(() => () => testSocials(undefined)).not.toThrow();
// @ts-expect-error test
expect(() => testSocials({twitter: undefined}))
.toThrowErrorMatchingInlineSnapshot(`
"Author socials should be usernames/userIds/handles, or fully qualified HTTP(s) absolute URLs.
Social platform 'twitter' has illegal value 'undefined'"
`);
expect(
// @ts-expect-error test
() => testSocials({twitter: null}),
).toThrowErrorMatchingInlineSnapshot(`
"Author socials should be usernames/userIds/handles, or fully qualified HTTP(s) absolute URLs.
Social platform 'twitter' has illegal value 'null'"
`);
});

it('can read empty socials', () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: {
name: 'Sébastien Lorber',
title: 'maintainer',
socials: {},
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
},
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
name: 'Sébastien Lorber',
title: 'maintainer',
imageURL: undefined,
socials: {},
key: null,
page: null,
},
]);
});

it('can normalize full socials from Author', () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: {
name: 'Sébastien Lorber',
title: 'maintainer',
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
},
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
name: 'Sébastien Lorber',
title: 'maintainer',
imageURL: undefined,
key: null,
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
page: null,
},
]);
});

it('can normalize handle socials from Author', () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: {
name: 'Sébastien Lorber',
title: 'maintainer',
socials: {
github: 'slorber',
x: 'sebastienlorber',
linkedin: 'sebastienlorber',
stackoverflow: '82609',
twitter: 'sebastienlorber',
},
},
},
authorsMap: undefined,
baseUrl: '/',
}),
).toEqual([
{
name: 'Sébastien Lorber',
title: 'maintainer',
imageURL: undefined,
key: null,
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
page: null,
},
]);
});

it('can normalize socials from Author[]', () => {
expect(
getBlogPostAuthors({
frontMatter: {
authors: {name: 'Sébastien Lorber', title: 'maintainer'},
authors: [
{
name: 'Sébastien Lorber',
title: 'maintainer',
socials: {
github: 'slorber',
x: 'sebastienlorber',
linkedin: 'sebastienlorber',
stackoverflow: '82609',
twitter: 'sebastienlorber',
},
},
{
name: 'Seb',
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
},
],
},
authorsMap: undefined,
baseUrl: '/',
Expand All @@ -283,6 +452,26 @@ describe('getBlogPostAuthors', () => {
title: 'maintainer',
imageURL: undefined,
key: null,
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
page: null,
},
{
name: 'Seb',
imageURL: undefined,
key: null,
socials: {
github: 'https://github.com/slorber',
linkedin: 'https://www.linkedin.com/in/sebastienlorber/',
stackoverflow: 'https://stackoverflow.com/users/82609',
twitter: 'https://twitter.com/sebastienlorber',
x: 'https://x.com/sebastienlorber',
},
page: null,
},
]);
Expand All @@ -293,8 +482,13 @@ describe('getBlogPostAuthors', () => {
getBlogPostAuthors({
frontMatter: {
authors: [
{name: 'Sébastien Lorber', title: 'maintainer'},
{name: 'Yangshun Tay'},
{
name: 'Sébastien Lorber',
title: 'maintainer',
},
{
name: 'Yangshun Tay',
},
],
},
authorsMap: undefined,
Expand All @@ -306,9 +500,16 @@ describe('getBlogPostAuthors', () => {
title: 'maintainer',
imageURL: undefined,
key: null,
socials: {},
page: null,
},
{
name: 'Yangshun Tay',
imageURL: undefined,
socials: {},
key: null,
page: null,
},
{name: 'Yangshun Tay', imageURL: undefined, key: null, page: null},
]);
});

Expand All @@ -323,7 +524,12 @@ describe('getBlogPostAuthors', () => {
title: 'Yangshun title local override',
extra: 42,
},
{name: 'Alexey'},
{
name: 'Alexey',
socials: {
github: 'lex111',
},
},
],
},
authorsMap: {
Expand Down Expand Up @@ -355,10 +561,19 @@ describe('getBlogPostAuthors', () => {
name: 'Yangshun Tay',
title: 'Yangshun title local override',
extra: 42,
socials: {},
imageURL: undefined,
page: null,
},
{name: 'Alexey', imageURL: undefined, key: null, page: null},
{
name: 'Alexey',
imageURL: undefined,
key: null,
page: null,
socials: {
github: 'https://github.com/lex111',
},
},
]);
});

Expand Down Expand Up @@ -627,6 +842,7 @@ describe('getBlogPostAuthors', () => {
imageURL: './ozaki.png',
key: null,
page: null,
socials: {},
},
]);
expect(() => withoutBaseUrlTest).not.toThrow();
Expand All @@ -635,6 +851,7 @@ describe('getBlogPostAuthors', () => {
imageURL: './ozaki.png',
key: null,
page: null,
socials: {},
},
]);
});
Expand All @@ -654,6 +871,7 @@ describe('getBlogPostAuthors', () => {
imageURL: '/ozaki.png',
key: null,
page: null,
socials: {},
},
]);
});
Expand All @@ -673,6 +891,7 @@ describe('getBlogPostAuthors', () => {
imageURL: '/img/ozaki.png',
key: null,
page: null,
socials: {},
},
]);
});
Expand All @@ -692,6 +911,7 @@ describe('getBlogPostAuthors', () => {
imageURL: '/img/ozaki.png',
key: null,
page: null,
socials: {},
},
]);
});
Expand All @@ -711,6 +931,7 @@ describe('getBlogPostAuthors', () => {
imageURL: '/img/img/ozaki.png',
key: null,
page: null,
socials: {},
},
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ describe('authors socials', () => {
});

it('throw socials that are not strings', () => {
const authorsMap: AuthorsMapInput = {
const socialNumber: AuthorsMapInput = {
ozaki: {
name: 'ozaki',
socials: {
Expand All @@ -318,11 +318,39 @@ describe('authors socials', () => {
},
};

const socialNull: AuthorsMapInput = {
ozaki: {
name: 'ozaki',
socials: {
// @ts-expect-error: for tests
twitter: null,
},
},
};

const socialNull2: AuthorsMapInput = {
ozaki: {
name: 'ozaki',
// @ts-expect-error: for tests
socials: null,
},
};

expect(() =>
validateAuthorsMap(authorsMap),
validateAuthorsMap(socialNumber),
).toThrowErrorMatchingInlineSnapshot(
`""ozaki.socials.twitter" must be a string"`,
);
expect(() =>
validateAuthorsMap(socialNull),
).toThrowErrorMatchingInlineSnapshot(
`""ozaki.socials.twitter" must be a string"`,
);
expect(() =>
validateAuthorsMap(socialNull2),
).toThrowErrorMatchingInlineSnapshot(
`""ozaki.socials" should be an author object containing properties like name, title, and imageURL."`,
);
});

it('throw socials that are objects', () => {
Expand Down
Loading