From 58a350de887325e5aa374ad51df42788434de174 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:21:07 +0200 Subject: [PATCH] refactor --- .../src/__tests__/authors.test.ts | 54 +++++++++++++++++-- .../src/__tests__/authorsMap.test.ts | 12 ++--- .../src/authorsSocials.ts | 6 +++ 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts index a0858a36da87..5723a30090a3 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/authors.test.ts @@ -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 { return fromPartial(partial); @@ -268,6 +272,50 @@ describe('getBlogPostAuthors', () => { ]); }); + 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({ @@ -384,7 +432,7 @@ describe('getBlogPostAuthors', () => { }, }, { - name: 'Slorber', + name: 'Seb', socials: { github: 'https://github.com/slorber', linkedin: 'https://www.linkedin.com/in/sebastienlorber/', @@ -414,7 +462,7 @@ describe('getBlogPostAuthors', () => { page: null, }, { - name: 'Slorber', + name: 'Seb', imageURL: undefined, key: null, socials: { diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/authorsMap.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/authorsMap.test.ts index 606cdbed9fd5..4ff626fca75e 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/authorsMap.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/authorsMap.test.ts @@ -255,7 +255,7 @@ describe('authors socials', () => { }); it('throw socials that are not strings', () => { - const socialnumber: AuthorsMapInput = { + const socialNumber: AuthorsMapInput = { ozaki: { name: 'ozaki', socials: { @@ -265,7 +265,7 @@ describe('authors socials', () => { }, }; - const socialnull: AuthorsMapInput = { + const socialNull: AuthorsMapInput = { ozaki: { name: 'ozaki', socials: { @@ -275,7 +275,7 @@ describe('authors socials', () => { }, }; - const socialnull2: AuthorsMapInput = { + const socialNull2: AuthorsMapInput = { ozaki: { name: 'ozaki', // @ts-expect-error: for tests @@ -284,17 +284,17 @@ describe('authors socials', () => { }; expect(() => - validateAuthorsMap(socialnumber), + validateAuthorsMap(socialNumber), ).toThrowErrorMatchingInlineSnapshot( `""ozaki.socials.twitter" must be a string"`, ); expect(() => - validateAuthorsMap(socialnull), + validateAuthorsMap(socialNull), ).toThrowErrorMatchingInlineSnapshot( `""ozaki.socials.twitter" must be a string"`, ); expect(() => - validateAuthorsMap(socialnull2), + validateAuthorsMap(socialNull2), ).toThrowErrorMatchingInlineSnapshot( `""ozaki.socials" should be an author object containing properties like name, title, and imageURL."`, ); diff --git a/packages/docusaurus-plugin-content-blog/src/authorsSocials.ts b/packages/docusaurus-plugin-content-blog/src/authorsSocials.ts index 8ca12169a3b0..b190e3e09dc2 100644 --- a/packages/docusaurus-plugin-content-blog/src/authorsSocials.ts +++ b/packages/docusaurus-plugin-content-blog/src/authorsSocials.ts @@ -41,6 +41,12 @@ type SocialEntry = [string, string]; function normalizeSocialEntry([platform, value]: SocialEntry): SocialEntry { const normalizer = PredefinedPlatformNormalizers[platform.toLowerCase()]; + if (typeof value !== 'string') { + throw new Error( + `Author socials should be usernames/userIds/handles, or fully qualified HTTP(s) absolute URLs. +Social platform '${platform}' has illegal value '${value}'`, + ); + } const isAbsoluteUrl = value.startsWith('http://') || value.startsWith('https://'); if (isAbsoluteUrl) {